Skip to content

Commit

Permalink
Merge branch 'fix-508' of git://github.com/garnaat/aws-cli into garna…
Browse files Browse the repository at this point in the history
…at-fix-508
  • Loading branch information
garnaat committed Dec 5, 2013
2 parents 95245ae + 6bba10d commit 8dc7b76
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
35 changes: 35 additions & 0 deletions awscli/customizations/ec2protocolarg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""
This customization allows the user to specify the values "tcp", "udp",
or "icmp" as values for the --protocol parameter. The actual Protocol
parameter of the operation accepts only integer protocol numbers.
"""

def _fix_args(operation, endpoint, params, **kwargs):
if 'protocol' in params:
if params['protocol'] == 'tcp':
params['protocol'] = '6'
elif params['protocol'] == 'udp':
params['protocol'] = '17'
elif params['protocol'] == 'icmp':
params['protocol'] = '1'
elif params['protocol'] == 'all':
params['protocol'] = '-1'


def register_protocol_args(cli):
('before-parameter-build.ec2.RunInstances', _fix_args),
cli.register('before-parameter-build.ec2.CreateNetworkAclEntry',
_fix_args)

2 changes: 2 additions & 0 deletions awscli/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from awscli.customizations.configure import register_configure_cmd
from awscli.customizations.cloudtrail import initialize as cloudtrail_init
from awscli.customizations.toplevelbool import register_bool_params
from awscli.customizations.ec2protocolarg import register_protocol_args


def awscli_initialize(event_handlers):
Expand Down Expand Up @@ -81,3 +82,4 @@ def awscli_initialize(event_handlers):
register_configure_cmd(event_handlers)
cloudtrail_init(event_handlers)
register_bool_params(event_handlers)
register_protocol_args(event_handlers)
120 changes: 120 additions & 0 deletions tests/unit/ec2/test_create_network_acl_entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env python
# Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from tests.unit import BaseAWSCommandParamsTest


class TestCreateNetworkACLEntry(BaseAWSCommandParamsTest):

prefix = 'ec2 create-network-acl-entry'

def test_tcp(self):
cmdline = self.prefix
cmdline += ' --network-acl-id acl-12345678'
cmdline += ' --rule-number 100'
cmdline += ' --protocol tcp'
cmdline += ' --rule-action allow'
cmdline += ' --ingress'
cmdline += ' --port-range From=22,To=22'
cmdline += ' --cidr-block 0.0.0.0/0'
result = {'NetworkAclId': 'acl-12345678',
'RuleNumber': '100',
'Protocol': '6',
'RuleAction': 'allow',
'Egress': 'false',
'CidrBlock': '0.0.0.0/0',
'PortRange.From': '22',
'PortRange.To': '22'
}
self.assert_params_for_cmd(cmdline, result)

def test_udp(self):
cmdline = self.prefix
cmdline += ' --network-acl-id acl-12345678'
cmdline += ' --rule-number 100'
cmdline += ' --protocol udp'
cmdline += ' --rule-action allow'
cmdline += ' --ingress'
cmdline += ' --port-range From=22,To=22'
cmdline += ' --cidr-block 0.0.0.0/0'
result = {'NetworkAclId': 'acl-12345678',
'RuleNumber': '100',
'Protocol': '17',
'RuleAction': 'allow',
'Egress': 'false',
'CidrBlock': '0.0.0.0/0',
'PortRange.From': '22',
'PortRange.To': '22'
}
self.assert_params_for_cmd(cmdline, result)

def test_icmp(self):
cmdline = self.prefix
cmdline += ' --network-acl-id acl-12345678'
cmdline += ' --rule-number 100'
cmdline += ' --protocol icmp'
cmdline += ' --rule-action allow'
cmdline += ' --ingress'
cmdline += ' --port-range From=22,To=22'
cmdline += ' --cidr-block 0.0.0.0/0'
result = {'NetworkAclId': 'acl-12345678',
'RuleNumber': '100',
'Protocol': '1',
'RuleAction': 'allow',
'Egress': 'false',
'CidrBlock': '0.0.0.0/0',
'PortRange.From': '22',
'PortRange.To': '22'
}
self.assert_params_for_cmd(cmdline, result)

def test_all(self):
cmdline = self.prefix
cmdline += ' --network-acl-id acl-12345678'
cmdline += ' --rule-number 100'
cmdline += ' --protocol all'
cmdline += ' --rule-action allow'
cmdline += ' --ingress'
cmdline += ' --port-range From=22,To=22'
cmdline += ' --cidr-block 0.0.0.0/0'
result = {'NetworkAclId': 'acl-12345678',
'RuleNumber': '100',
'Protocol': '-1',
'RuleAction': 'allow',
'Egress': 'false',
'CidrBlock': '0.0.0.0/0',
'PortRange.From': '22',
'PortRange.To': '22'
}
self.assert_params_for_cmd(cmdline, result)

def test_number(self):
cmdline = self.prefix
cmdline += ' --network-acl-id acl-12345678'
cmdline += ' --rule-number 100'
cmdline += ' --protocol 99'
cmdline += ' --rule-action allow'
cmdline += ' --ingress'
cmdline += ' --port-range From=22,To=22'
cmdline += ' --cidr-block 0.0.0.0/0'
result = {'NetworkAclId': 'acl-12345678',
'RuleNumber': '100',
'Protocol': '99',
'RuleAction': 'allow',
'Egress': 'false',
'CidrBlock': '0.0.0.0/0',
'PortRange.From': '22',
'PortRange.To': '22'
}
self.assert_params_for_cmd(cmdline, result)

0 comments on commit 8dc7b76

Please sign in to comment.