Allow subnets with names formatted like subnet-1234 (#37740)

* Allow subnets with names formatted like `subnet-1234`

* Deduplicate IDs, in case a subnet is specified both by name and ID
This commit is contained in:
Ryan Brown 2018-04-02 17:13:44 -04:00 committed by ansibot
parent d129396274
commit 87bd169ca9

View file

@ -149,6 +149,7 @@ try:
except ImportError: except ImportError:
HAS_BOTO3 = False HAS_BOTO3 = False
import traceback
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ec2 import boto3_conn, ec2_argument_spec, get_aws_connection_info from ansible.module_utils.ec2 import boto3_conn, ec2_argument_spec, get_aws_connection_info
@ -530,19 +531,22 @@ def subnets_to_associate(nacl, client, module):
params = list(module.params.get('subnets')) params = list(module.params.get('subnets'))
if not params: if not params:
return [] return []
if params[0].startswith("subnet-"): all_found = []
if any(x.startswith("subnet-") for x in params):
try: try:
subnets = client.describe_subnets(Filters=[ subnets = client.describe_subnets(Filters=[
{'Name': 'subnet-id', 'Values': params}]) {'Name': 'subnet-id', 'Values': params}])
all_found.extend(subnets.get('Subnets', []))
except botocore.exceptions.ClientError as e: except botocore.exceptions.ClientError as e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e), exception=traceback.format_exc())
else: if len(params) != len(all_found):
try: try:
subnets = client.describe_subnets(Filters=[ subnets = client.describe_subnets(Filters=[
{'Name': 'tag:Name', 'Values': params}]) {'Name': 'tag:Name', 'Values': params}])
all_found.extend(subnets.get('Subnets', []))
except botocore.exceptions.ClientError as e: except botocore.exceptions.ClientError as e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e), exception=traceback.format_exc())
return [s['SubnetId'] for s in subnets['Subnets'] if s['SubnetId']] return list(set(s['SubnetId'] for s in all_found if s.get('SubnetId')))
def main(): def main():