OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2011 Mitch Garnaat http://garnaat.org/ |
| 2 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved |
| 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: |
| 11 # |
| 12 # The above copyright notice and this permission notice shall be included |
| 13 # in all copies or substantial portions of the Software. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 # IN THE SOFTWARE. |
| 22 |
| 23 """ |
| 24 Represents an EC2 Elastic Network Interface |
| 25 """ |
| 26 from boto.ec2.ec2object import TaggedEC2Object |
| 27 from boto.resultset import ResultSet |
| 28 from boto.ec2.group import Group |
| 29 |
| 30 |
| 31 class Attachment(object): |
| 32 """ |
| 33 :ivar id: The ID of the attachment. |
| 34 :ivar instance_id: The ID of the instance. |
| 35 :ivar device_index: The index of this device. |
| 36 :ivar status: The status of the device. |
| 37 :ivar attach_time: The time the device was attached. |
| 38 :ivar delete_on_termination: Whether the device will be deleted |
| 39 when the instance is terminated. |
| 40 """ |
| 41 |
| 42 def __init__(self): |
| 43 self.id = None |
| 44 self.instance_id = None |
| 45 self.instance_owner_id = None |
| 46 self.device_index = 0 |
| 47 self.status = None |
| 48 self.attach_time = None |
| 49 self.delete_on_termination = False |
| 50 |
| 51 def __repr__(self): |
| 52 return 'Attachment:%s' % self.id |
| 53 |
| 54 def startElement(self, name, attrs, connection): |
| 55 return None |
| 56 |
| 57 def endElement(self, name, value, connection): |
| 58 if name == 'attachmentId': |
| 59 self.id = value |
| 60 elif name == 'instanceId': |
| 61 self.instance_id = value |
| 62 elif name == 'instanceOwnerId': |
| 63 self.instance_owner_id = value |
| 64 elif name == 'status': |
| 65 self.status = value |
| 66 elif name == 'attachTime': |
| 67 self.attach_time = value |
| 68 elif name == 'deleteOnTermination': |
| 69 if value.lower() == 'true': |
| 70 self.delete_on_termination = True |
| 71 else: |
| 72 self.delete_on_termination = False |
| 73 else: |
| 74 setattr(self, name, value) |
| 75 |
| 76 |
| 77 class NetworkInterface(TaggedEC2Object): |
| 78 """ |
| 79 An Elastic Network Interface. |
| 80 |
| 81 :ivar id: The ID of the ENI. |
| 82 :ivar subnet_id: The ID of the VPC subnet. |
| 83 :ivar vpc_id: The ID of the VPC. |
| 84 :ivar description: The description. |
| 85 :ivar owner_id: The ID of the owner of the ENI. |
| 86 :ivar requester_managed: |
| 87 :ivar status: The interface's status (available|in-use). |
| 88 :ivar mac_address: The MAC address of the interface. |
| 89 :ivar private_ip_address: The IP address of the interface within |
| 90 the subnet. |
| 91 :ivar source_dest_check: Flag to indicate whether to validate |
| 92 network traffic to or from this network interface. |
| 93 :ivar groups: List of security groups associated with the interface. |
| 94 :ivar attachment: The attachment object. |
| 95 :ivar private_ip_addresses: A list of PrivateIPAddress objects. |
| 96 """ |
| 97 |
| 98 def __init__(self, connection=None): |
| 99 TaggedEC2Object.__init__(self, connection) |
| 100 self.id = None |
| 101 self.subnet_id = None |
| 102 self.vpc_id = None |
| 103 self.availability_zone = None |
| 104 self.description = None |
| 105 self.owner_id = None |
| 106 self.requester_managed = False |
| 107 self.status = None |
| 108 self.mac_address = None |
| 109 self.private_ip_address = None |
| 110 self.source_dest_check = None |
| 111 self.groups = [] |
| 112 self.attachment = None |
| 113 self.private_ip_addresses = [] |
| 114 |
| 115 def __repr__(self): |
| 116 return 'NetworkInterface:%s' % self.id |
| 117 |
| 118 def startElement(self, name, attrs, connection): |
| 119 retval = TaggedEC2Object.startElement(self, name, attrs, connection) |
| 120 if retval is not None: |
| 121 return retval |
| 122 if name == 'groupSet': |
| 123 self.groups = ResultSet([('item', Group)]) |
| 124 return self.groups |
| 125 elif name == 'attachment': |
| 126 self.attachment = Attachment() |
| 127 return self.attachment |
| 128 elif name == 'privateIpAddressesSet': |
| 129 self.private_ip_addresses = ResultSet([('item', PrivateIPAddress)]) |
| 130 return self.private_ip_addresses |
| 131 else: |
| 132 return None |
| 133 |
| 134 def endElement(self, name, value, connection): |
| 135 if name == 'networkInterfaceId': |
| 136 self.id = value |
| 137 elif name == 'subnetId': |
| 138 self.subnet_id = value |
| 139 elif name == 'vpcId': |
| 140 self.vpc_id = value |
| 141 elif name == 'availabilityZone': |
| 142 self.availability_zone = value |
| 143 elif name == 'description': |
| 144 self.description = value |
| 145 elif name == 'ownerId': |
| 146 self.owner_id = value |
| 147 elif name == 'requesterManaged': |
| 148 if value.lower() == 'true': |
| 149 self.requester_managed = True |
| 150 else: |
| 151 self.requester_managed = False |
| 152 elif name == 'status': |
| 153 self.status = value |
| 154 elif name == 'macAddress': |
| 155 self.mac_address = value |
| 156 elif name == 'privateIpAddress': |
| 157 self.private_ip_address = value |
| 158 elif name == 'sourceDestCheck': |
| 159 if value.lower() == 'true': |
| 160 self.source_dest_check = True |
| 161 else: |
| 162 self.source_dest_check = False |
| 163 else: |
| 164 setattr(self, name, value) |
| 165 |
| 166 def delete(self): |
| 167 return self.connection.delete_network_interface(self.id) |
| 168 |
| 169 |
| 170 class PrivateIPAddress(object): |
| 171 def __init__(self, connection=None, private_ip_address=None, |
| 172 primary=None): |
| 173 self.connection = connection |
| 174 self.private_ip_address = private_ip_address |
| 175 self.primary = primary |
| 176 |
| 177 def startElement(self, name, attrs, connection): |
| 178 pass |
| 179 |
| 180 def endElement(self, name, value, connection): |
| 181 if name == 'privateIpAddress': |
| 182 self.private_ip_address = value |
| 183 elif name == 'primary': |
| 184 self.primary = True if value.lower() == 'true' else False |
| 185 |
| 186 def __repr__(self): |
| 187 return "PrivateIPAddress(%s, primary=%s)" % (self.private_ip_address, |
| 188 self.primary) |
| 189 |
| 190 |
| 191 class NetworkInterfaceCollection(list): |
| 192 def __init__(self, *interfaces): |
| 193 self.extend(interfaces) |
| 194 |
| 195 def build_list_params(self, params, prefix=''): |
| 196 for i, spec in enumerate(self, 1): |
| 197 full_prefix = '%sNetworkInterface.%s.' % (prefix, i) |
| 198 if spec.network_interface_id is not None: |
| 199 params[full_prefix + 'NetworkInterfaceId'] = \ |
| 200 str(spec.network_interface_id) |
| 201 if spec.device_index is not None: |
| 202 params[full_prefix + 'DeviceIndex'] = \ |
| 203 str(spec.device_index) |
| 204 if spec.subnet_id is not None: |
| 205 params[full_prefix + 'SubnetId'] = str(spec.subnet_id) |
| 206 if spec.description is not None: |
| 207 params[full_prefix + 'Description'] = str(spec.description) |
| 208 if spec.delete_on_termination is not None: |
| 209 params[full_prefix + 'DeleteOnTermination'] = \ |
| 210 'true' if spec.delete_on_termination else 'false' |
| 211 if spec.secondary_private_ip_address_count is not None: |
| 212 params[full_prefix + 'SecondaryPrivateIpAddressCount'] = \ |
| 213 str(spec.secondary_private_ip_address_count) |
| 214 if spec.private_ip_address is not None: |
| 215 params[full_prefix + 'PrivateIpAddress'] = \ |
| 216 str(spec.private_ip_address) |
| 217 if spec.groups is not None: |
| 218 for j, group_id in enumerate(spec.groups, 1): |
| 219 query_param_key = '%sSecurityGroupId.%s' % (full_prefix, j) |
| 220 params[query_param_key] = str(group_id) |
| 221 if spec.private_ip_addresses is not None: |
| 222 for k, ip_addr in enumerate(spec.private_ip_addresses, 1): |
| 223 query_param_key_prefix = ( |
| 224 '%sPrivateIpAddresses.%s' % (full_prefix, k)) |
| 225 params[query_param_key_prefix + '.PrivateIpAddress'] = \ |
| 226 str(ip_addr.private_ip_address) |
| 227 if ip_addr.primary is not None: |
| 228 params[query_param_key_prefix + '.Primary'] = \ |
| 229 'true' if ip_addr.primary else 'false' |
| 230 |
| 231 |
| 232 class NetworkInterfaceSpecification(object): |
| 233 def __init__(self, network_interface_id=None, device_index=None, |
| 234 subnet_id=None, description=None, private_ip_address=None, |
| 235 groups=None, delete_on_termination=None, |
| 236 private_ip_addresses=None, |
| 237 secondary_private_ip_address_count=None): |
| 238 self.network_interface_id = network_interface_id |
| 239 self.device_index = device_index |
| 240 self.subnet_id = subnet_id |
| 241 self.description = description |
| 242 self.private_ip_address = private_ip_address |
| 243 self.groups = groups |
| 244 self.delete_on_termination = delete_on_termination |
| 245 self.private_ip_addresses = private_ip_addresses |
| 246 self.secondary_private_ip_address_count = \ |
| 247 secondary_private_ip_address_count |
OLD | NEW |