OLD | NEW |
1 # Copyright (C) 2009 Robert Collins <robertc@robertcollins.net> | 1 # Copyright (C) 2009 Robert Collins <robertc@robertcollins.net> |
2 # Copyright (C) 2009 Canonical Ltd | 2 # Copyright (C) 2009 Canonical Ltd |
3 # Copyright (C) 2009 Duncan McGreggor <oubiwann@adytum.us> | 3 # Copyright (C) 2009 Duncan McGreggor <oubiwann@adytum.us> |
4 # Licenced under the txaws licence available at /LICENSE in the txaws source. | 4 # Licenced under the txaws licence available at /LICENSE in the txaws source. |
5 | 5 |
6 """EC2 client support.""" | 6 """EC2 client support.""" |
7 | 7 |
8 from datetime import datetime | 8 from datetime import datetime |
9 from urllib import quote | 9 from urllib import quote |
10 from base64 import b64encode | 10 from base64 import b64encode |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 group_names = {} | 103 group_names = {} |
104 if names: | 104 if names: |
105 group_names = dict([("GroupName.%d" % (i + 1), name) | 105 group_names = dict([("GroupName.%d" % (i + 1), name) |
106 for i, name in enumerate(names)]) | 106 for i, name in enumerate(names)]) |
107 query = self.query_factory( | 107 query = self.query_factory( |
108 action="DescribeSecurityGroups", creds=self.creds, | 108 action="DescribeSecurityGroups", creds=self.creds, |
109 endpoint=self.endpoint, other_params=group_names) | 109 endpoint=self.endpoint, other_params=group_names) |
110 d = query.submit() | 110 d = query.submit() |
111 return d.addCallback(self.parser.describe_security_groups) | 111 return d.addCallback(self.parser.describe_security_groups) |
112 | 112 |
113 def create_security_group(self, name, description): | 113 def create_security_group(self, name, description, vpc_id=None): |
114 """Create security group. | 114 """Create security group. |
115 | 115 |
116 @param name: Name of the new security group. | 116 @param name: Name of the new security group. |
117 @param description: Description of the new security group. | 117 @param description: Description of the new security group. |
| 118 @param vpc_id: ID of the VPC to which the security group will belong. |
118 @return: A C{Deferred} that will fire with a truth value for the | 119 @return: A C{Deferred} that will fire with a truth value for the |
119 success of the operation. | 120 success of the operation. |
120 """ | 121 """ |
121 parameters = {"GroupName": name, "GroupDescription": description} | 122 parameters = {"GroupName": name, "GroupDescription": description} |
| 123 if vpc_id: |
| 124 parameters["VpcId"] = vpc_id |
122 query = self.query_factory( | 125 query = self.query_factory( |
123 action="CreateSecurityGroup", creds=self.creds, | 126 action="CreateSecurityGroup", creds=self.creds, |
124 endpoint=self.endpoint, other_params=parameters) | 127 endpoint=self.endpoint, other_params=parameters) |
125 d = query.submit() | 128 d = query.submit() |
126 return d.addCallback(self.parser.truth_return) | 129 return d.addCallback(self.parser.create_security_group) |
127 | 130 |
128 def delete_security_group(self, name): | 131 def delete_security_group(self, name=None, id=None): |
129 """ | 132 """ |
130 @param name: Name of the new security group. | 133 @param name: Name of the security group. |
| 134 @param id: Id of the security group. |
131 @return: A C{Deferred} that will fire with a truth value for the | 135 @return: A C{Deferred} that will fire with a truth value for the |
132 success of the operation. | 136 success of the operation. |
133 """ | 137 """ |
134 parameter = {"GroupName": name} | 138 if name: |
| 139 parameter = {"GroupName": name} |
| 140 elif id: |
| 141 parameter = {"GroupId": id} |
| 142 else: |
| 143 raise ValueError("You must provide either the security group name or
id") |
135 query = self.query_factory( | 144 query = self.query_factory( |
136 action="DeleteSecurityGroup", creds=self.creds, | 145 action="DeleteSecurityGroup", creds=self.creds, |
137 endpoint=self.endpoint, other_params=parameter) | 146 endpoint=self.endpoint, other_params=parameter) |
138 d = query.submit() | 147 d = query.submit() |
139 return d.addCallback(self.parser.truth_return) | 148 return d.addCallback(self.parser.truth_return) |
140 | 149 |
141 def authorize_security_group( | 150 def authorize_security_group( |
142 self, group_name, source_group_name="", source_group_owner_id="", | 151 self, group_name, source_group_name="", source_group_owner_id="", |
143 ip_protocol="", from_port="", to_port="", cidr_ip=""): | 152 ip_protocol="", from_port="", to_port="", cidr_ip=""): |
144 """ | 153 """ |
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 def describe_security_groups(self, xml_bytes): | 672 def describe_security_groups(self, xml_bytes): |
664 """Parse the XML returned by the C{DescribeSecurityGroups} function. | 673 """Parse the XML returned by the C{DescribeSecurityGroups} function. |
665 | 674 |
666 @param xml_bytes: XML bytes with a C{DescribeSecurityGroupsResponse} | 675 @param xml_bytes: XML bytes with a C{DescribeSecurityGroupsResponse} |
667 root element. | 676 root element. |
668 @return: A list of L{SecurityGroup} instances. | 677 @return: A list of L{SecurityGroup} instances. |
669 """ | 678 """ |
670 root = XML(xml_bytes) | 679 root = XML(xml_bytes) |
671 result = [] | 680 result = [] |
672 for group_info in root.findall("securityGroupInfo/item"): | 681 for group_info in root.findall("securityGroupInfo/item"): |
| 682 id = group_info.findtext("groupId") |
673 name = group_info.findtext("groupName") | 683 name = group_info.findtext("groupName") |
674 description = group_info.findtext("groupDescription") | 684 description = group_info.findtext("groupDescription") |
675 owner_id = group_info.findtext("ownerId") | 685 owner_id = group_info.findtext("ownerId") |
676 allowed_groups = [] | 686 allowed_groups = [] |
677 allowed_ips = [] | 687 allowed_ips = [] |
678 ip_permissions = group_info.find("ipPermissions") | 688 ip_permissions = group_info.find("ipPermissions") |
679 if ip_permissions is None: | 689 if ip_permissions is None: |
680 ip_permissions = () | 690 ip_permissions = () |
681 for ip_permission in ip_permissions: | 691 for ip_permission in ip_permissions: |
682 | 692 |
(...skipping 19 matching lines...) Expand all Loading... |
702 for ip_ranges in ip_permission.findall("ipRanges/item") or (): | 712 for ip_ranges in ip_permission.findall("ipRanges/item") or (): |
703 cidr_ip = ip_ranges.findtext("cidrIp") | 713 cidr_ip = ip_ranges.findtext("cidrIp") |
704 allowed_ips.append( | 714 allowed_ips.append( |
705 model.IPPermission( | 715 model.IPPermission( |
706 ip_protocol, from_port, to_port, cidr_ip)) | 716 ip_protocol, from_port, to_port, cidr_ip)) |
707 | 717 |
708 allowed_groups = [model.UserIDGroupPair(user_id, group_name) | 718 allowed_groups = [model.UserIDGroupPair(user_id, group_name) |
709 for user_id, group_name in allowed_groups] | 719 for user_id, group_name in allowed_groups] |
710 | 720 |
711 security_group = model.SecurityGroup( | 721 security_group = model.SecurityGroup( |
712 name, description, owner_id=owner_id, | 722 id, name, description, owner_id=owner_id, |
713 groups=allowed_groups, ips=allowed_ips) | 723 groups=allowed_groups, ips=allowed_ips) |
714 result.append(security_group) | 724 result.append(security_group) |
715 return result | 725 return result |
716 | 726 |
| 727 def create_security_group(self, xml_bytes): |
| 728 root = XML(xml_bytes) |
| 729 return root.findtext("groupId") |
| 730 |
717 def truth_return(self, xml_bytes): | 731 def truth_return(self, xml_bytes): |
718 """Parse the XML for a truth value. | 732 """Parse the XML for a truth value. |
719 | 733 |
720 @param xml_bytes: XML bytes. | 734 @param xml_bytes: XML bytes. |
721 @return: True if the node contains "return" otherwise False. | 735 @return: True if the node contains "return" otherwise False. |
722 """ | 736 """ |
723 root = XML(xml_bytes) | 737 root = XML(xml_bytes) |
724 return root.findtext("return") == "true" | 738 return root.findtext("return") == "true" |
725 | 739 |
726 def describe_volumes(self, xml_bytes): | 740 def describe_volumes(self, xml_bytes): |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1066 See the AWS dev reference page 186 (2009-11-30 version). | 1080 See the AWS dev reference page 186 (2009-11-30 version). |
1067 @return: a_string encoded. | 1081 @return: a_string encoded. |
1068 """ | 1082 """ |
1069 if isinstance(string, unicode): | 1083 if isinstance(string, unicode): |
1070 string = string.encode("utf-8") | 1084 string = string.encode("utf-8") |
1071 return quote(string, safe="~") | 1085 return quote(string, safe="~") |
1072 | 1086 |
1073 def sorted_params(self): | 1087 def sorted_params(self): |
1074 """Return the query parameters sorted appropriately for signing.""" | 1088 """Return the query parameters sorted appropriately for signing.""" |
1075 return sorted(self.params.items()) | 1089 return sorted(self.params.items()) |
OLD | NEW |