[cloud] Add ec2_vpc_dhcp_options_facts check mode (#23106)

* Support check mode in ec2_vpc_dhcp_options_facts

As a facts module, ec2_vpc_dhcp_options_facts supports check mode
by default

* ec2_vpc_dhcp_options_facts tidy up

Use named method imports, move imports to top of code
Use shared code to handle filters and tags
Use snake case for parameter names while retaining backward compatibility
pull/23156/head
Will Thames 8 years ago committed by Ryan Brown
parent 071903b868
commit 9229d53143

@ -34,12 +34,13 @@ options:
See U(http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeRouteTables.html) for possible filters. See U(http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeRouteTables.html) for possible filters.
required: false required: false
default: null default: null
DhcpOptionsIds: dhcp_options_ids:
description: description:
- Get details of specific DHCP Option ID - Get details of specific DHCP Option ID
- Provide this value as a list - Provide this value as a list
required: false required: false
default: None default: None
aliases: ['DhcpOptionsIds']
extends_documentation_fragment: extends_documentation_fragment:
- aws - aws
- ec2 - ec2
@ -83,76 +84,63 @@ changed:
returned: always returned: always
''' '''
import json import traceback
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ec2 import ec2_argument_spec, boto3_conn, HAS_BOTO3
from ansible.module_utils.ec2 import ansible_dict_to_boto3_filter_list, get_aws_connection_info
from ansible.module_utils.ec2 import camel_dict_to_snake_dict, boto3_tag_list_to_ansible_dict
try: try:
import botocore import botocore
import boto3
HAS_BOTO3 = True
except ImportError: except ImportError:
HAS_BOTO3 = False pass # caught by imported HAS_BOTO3
def get_dhcp_options_info(dhcp_option): def get_dhcp_options_info(dhcp_option):
dhcp_option_info = {'DhcpOptionsId': dhcp_option['DhcpOptionsId'], dhcp_option_info = {'DhcpOptionsId': dhcp_option['DhcpOptionsId'],
'DhcpConfigurations': dhcp_option['DhcpConfigurations'], 'DhcpConfigurations': dhcp_option['DhcpConfigurations'],
'Tags': dhcp_option['Tags'] 'Tags': boto3_tag_list_to_ansible_dict(dhcp_option['Tags'])}
}
return dhcp_option_info return dhcp_option_info
def list_dhcp_options(client, module): def list_dhcp_options(client, module):
dryrun = module.params.get("DryRun") params = dict(Filters=ansible_dict_to_boto3_filter_list(module.params.get('filters')))
all_dhcp_options_array = []
params = dict() if module.params.get("dry_run"):
params['DryRun'] = True
if module.params.get('filters'):
params['Filters'] = [] if module.params.get("dhcp_options_ids"):
for key, value in module.params.get('filters').items(): params['DhcpOptionsIds'] = module.params.get("dhcp_options_ids")
temp_dict = dict()
temp_dict['Name'] = key
if isinstance(value, basestring):
temp_dict['Values'] = [value]
else:
temp_dict['Values'] = value
params['Filters'].append(temp_dict)
if module.params.get("DryRun"):
params['DryRun'] = module.params.get("DryRun")
if module.params.get("DhcpOptionsIds"):
params['DhcpOptionsIds'] = module.params.get("DhcpOptionsIds")
try: try:
all_dhcp_options = client.describe_dhcp_options(**params) all_dhcp_options = client.describe_dhcp_options(**params)
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(),
**camel_dict_to_snake_dict(e.response))
for dhcp_option in all_dhcp_options['DhcpOptions']:
all_dhcp_options_array.append(get_dhcp_options_info(dhcp_option))
snaked_dhcp_options_array = [] results = [camel_dict_to_snake_dict(get_dhcp_options_info(option))
for dhcp_option in all_dhcp_options_array: for option in all_dhcp_options['DhcpOptions']]
snaked_dhcp_options_array.append(camel_dict_to_snake_dict(dhcp_option))
module.exit_json(dhcp_options=snaked_dhcp_options_array) module.exit_json(dhcp_options=results)
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = ec2_argument_spec()
argument_spec.update( argument_spec.update(
dict( dict(
filters = dict(type='dict', default=None, ), filters=dict(type='dict', default={}),
DryRun = dict(type='bool', default=False), dry_run=dict(type='bool', default=False, aliases=['DryRun']),
DhcpOptionsIds = dict(type='list', default=None) dhcp_options_ids=dict(type='list', aliases=['DhcpOptionIds'])
) )
) )
module = AnsibleModule(argument_spec=argument_spec) module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)
# Validate Requirements # Validate Requirements
if not HAS_BOTO3: if not HAS_BOTO3:
module.fail_json(msg='json and botocore/boto3 is required.') module.fail_json(msg='boto3 and botocore are required.')
try: try:
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
@ -165,9 +153,6 @@ def main():
module.exit_json(result=results) module.exit_json(result=results)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -174,7 +174,6 @@ lib/ansible/modules/cloud/amazon/ec2_tag.py
lib/ansible/modules/cloud/amazon/ec2_vol.py lib/ansible/modules/cloud/amazon/ec2_vol.py
lib/ansible/modules/cloud/amazon/ec2_vol_facts.py lib/ansible/modules/cloud/amazon/ec2_vol_facts.py
lib/ansible/modules/cloud/amazon/ec2_vpc_dhcp_options.py lib/ansible/modules/cloud/amazon/ec2_vpc_dhcp_options.py
lib/ansible/modules/cloud/amazon/ec2_vpc_dhcp_options_facts.py
lib/ansible/modules/cloud/amazon/ec2_vpc_igw.py lib/ansible/modules/cloud/amazon/ec2_vpc_igw.py
lib/ansible/modules/cloud/amazon/ec2_vpc_nacl.py lib/ansible/modules/cloud/amazon/ec2_vpc_nacl.py
lib/ansible/modules/cloud/amazon/ec2_vpc_nacl_facts.py lib/ansible/modules/cloud/amazon/ec2_vpc_nacl_facts.py

Loading…
Cancel
Save