Skip to content

Commit

Permalink
If user specifies a --associate-public-ip-address option and provides…
Browse files Browse the repository at this point in the history
… a --subnet-id option, we have to move the subnet ID value into the NetworkInterfaces structure we have created or we get a client error from EC2. Fixes 501.
  • Loading branch information
garnaat committed Nov 22, 2013
1 parent ff3bf85 commit 50d4999
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
26 changes: 22 additions & 4 deletions awscli/customizations/ec2runinstances.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
``--network-interfaces`` complex argument. This just makes two of
the most commonly used features available more easily.
"""
import logging

from awscli.arguments import CustomArgument

LOG = logging.getLogger(__name__)

# --secondary-private-ip-address
SECONDARY_PRIVATE_IP_ADDRESSES_DOCS = (
Expand Down Expand Up @@ -81,16 +84,31 @@ def _check_args(parsed_args, **kwargs):
'not supported.')
raise ValueError(msg)


def _fix_subnet(operation, endpoint, params, **kwargs):
# If the user has supplied a --subnet-id option AND we also
# have inserted an AssociatePublicIpAddress into the network_interfaces
# structure, we need to move the subnetId value down into the
# network_interfaces structure or we will get a client error from EC2.
if 'network_interfaces' in params:
ni = params['network_interfaces']
if 'AssociatePublicIpAddress' in ni[0]:
if 'subnet_id' in params:
ni[0]['SubnetId'] = params['subnet_id']
del params['subnet_id']


EVENTS = [
('building-argument-table.ec2.run-instances', _add_params),
('operation-args-parsed.ec2.run-instances', _check_args),
('building-argument-table.ec2.run-instances', _add_params, None),
('operation-args-parsed.ec2.run-instances', _check_args, None),
('before-parameter-build.ec2.RunInstances', _fix_subnet, 'foobar'),
]


def register_runinstances(event_handler):
# Register all of the events for customizing BundleInstance
for event, handler in EVENTS:
event_handler.register(event, handler)
for event, handler, unique_id in EVENTS:
event_handler.register(event, handler, unique_id)


def _build_network_interfaces(params, key, value):
Expand Down
20 changes: 18 additions & 2 deletions tests/unit/ec2/test_run_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,41 @@ def test_secondary_ip_address_count(self):
self.assert_params_for_cmd(args_list, result)

def test_associate_public_ip_address(self):
args = ' --image-id ami-foobar --count 1 '
args = ' --image-id ami-foobar --count 1 --subnet-id subnet-12345678 '
args += '--associate-public-ip-address'
args_list = (self.prefix + args).split()
result = {
'NetworkInterface.1.DeviceIndex': '0',
'NetworkInterface.1.AssociatePublicIpAddress': 'true',
'NetworkInterface.1.SubnetId': 'subnet-12345678',
'ImageId': 'ami-foobar',
'MaxCount': '1',
'MinCount': '1'
}
self.assert_params_for_cmd(args_list, result)

def test_no_associate_public_ip_address(self):
def test_associate_public_ip_address_2(self):
args = ' --image-id ami-foobar --count 1 '
args += '--associate-public-ip-address --subnet-id subnet-12345678'
args_list = (self.prefix + args).split()
result = {
'NetworkInterface.1.DeviceIndex': '0',
'NetworkInterface.1.AssociatePublicIpAddress': 'true',
'NetworkInterface.1.SubnetId': 'subnet-12345678',
'ImageId': 'ami-foobar',
'MaxCount': '1',
'MinCount': '1'
}
self.assert_params_for_cmd(args_list, result)

def test_no_associate_public_ip_address(self):
args = ' --image-id ami-foobar --count 1 --subnet-id subnet-12345678 '
args += '--no-associate-public-ip-address'
args_list = (self.prefix + args).split()
result = {
'NetworkInterface.1.DeviceIndex': '0',
'NetworkInterface.1.AssociatePublicIpAddress': 'false',
'NetworkInterface.1.SubnetId': 'subnet-12345678',
'ImageId': 'ami-foobar',
'MaxCount': '1',
'MinCount': '1'
Expand Down

0 comments on commit 50d4999

Please sign in to comment.