diff options
Diffstat (limited to 'boto/ec2/networkinterface.py')
-rw-r--r-- | boto/ec2/networkinterface.py | 52 |
1 files changed, 46 insertions, 6 deletions
diff --git a/boto/ec2/networkinterface.py b/boto/ec2/networkinterface.py index 5c6088f4..98368050 100644 --- a/boto/ec2/networkinterface.py +++ b/boto/ec2/networkinterface.py @@ -23,6 +23,7 @@ """ Represents an EC2 Elastic Network Interface """ +from boto.exception import BotoClientError from boto.ec2.ec2object import TaggedEC2Object from boto.resultset import ResultSet from boto.ec2.group import Group @@ -59,6 +60,8 @@ class Attachment(object): self.id = value elif name == 'instanceId': self.instance_id = value + elif name == 'deviceIndex': + self.device_index = int(value) elif name == 'instanceOwnerId': self.instance_owner_id = value elif name == 'status': @@ -163,8 +166,11 @@ class NetworkInterface(TaggedEC2Object): else: setattr(self, name, value) - def delete(self): - return self.connection.delete_network_interface(self.id) + def delete(self, dry_run=False): + return self.connection.delete_network_interface( + self.id, + dry_run=dry_run + ) class PrivateIPAddress(object): @@ -194,13 +200,15 @@ class NetworkInterfaceCollection(list): def build_list_params(self, params, prefix=''): for i, spec in enumerate(self): - full_prefix = '%sNetworkInterface.%s.' % (prefix, i+1) + full_prefix = '%sNetworkInterface.%s.' % (prefix, i) if spec.network_interface_id is not None: params[full_prefix + 'NetworkInterfaceId'] = \ str(spec.network_interface_id) if spec.device_index is not None: params[full_prefix + 'DeviceIndex'] = \ str(spec.device_index) + else: + params[full_prefix + 'DeviceIndex'] = 0 if spec.subnet_id is not None: params[full_prefix + 'SubnetId'] = str(spec.subnet_id) if spec.description is not None: @@ -216,25 +224,56 @@ class NetworkInterfaceCollection(list): str(spec.private_ip_address) if spec.groups is not None: for j, group_id in enumerate(spec.groups): - query_param_key = '%sSecurityGroupId.%s' % (full_prefix, j+1) + query_param_key = '%sSecurityGroupId.%s' % (full_prefix, j) params[query_param_key] = str(group_id) if spec.private_ip_addresses is not None: for k, ip_addr in enumerate(spec.private_ip_addresses): query_param_key_prefix = ( - '%sPrivateIpAddresses.%s' % (full_prefix, k+1)) + '%sPrivateIpAddresses.%s' % (full_prefix, k)) params[query_param_key_prefix + '.PrivateIpAddress'] = \ str(ip_addr.private_ip_address) if ip_addr.primary is not None: params[query_param_key_prefix + '.Primary'] = \ 'true' if ip_addr.primary else 'false' + # Associating Public IPs have special logic around them: + # + # * Only assignable on an device_index of ``0`` + # * Only on one interface + # * Only if there are no other interfaces being created + # * Only if it's a new interface (which we can't really guard + # against) + # + # More details on http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-RunInstances.html + if spec.associate_public_ip_address is not None: + if not params[full_prefix + 'DeviceIndex'] in (0, '0'): + raise BotoClientError( + "Only the interface with device index of 0 can " + \ + "be provided when using " + \ + "'associate_public_ip_address'." + ) + + if len(self) > 1: + raise BotoClientError( + "Only one interface can be provided when using " + \ + "'associate_public_ip_address'." + ) + + key = full_prefix + 'AssociatePublicIpAddress' + + if spec.associate_public_ip_address: + params[key] = 'true' + else: + params[key] = 'false' + class NetworkInterfaceSpecification(object): def __init__(self, network_interface_id=None, device_index=None, subnet_id=None, description=None, private_ip_address=None, groups=None, delete_on_termination=None, private_ip_addresses=None, - secondary_private_ip_address_count=None): + secondary_private_ip_address_count=None, + associate_public_ip_address=None): self.network_interface_id = network_interface_id self.device_index = device_index self.subnet_id = subnet_id @@ -245,3 +284,4 @@ class NetworkInterfaceSpecification(object): self.private_ip_addresses = private_ip_addresses self.secondary_private_ip_address_count = \ secondary_private_ip_address_count + self.associate_public_ip_address = associate_public_ip_address |