add boto3_tag_list_to_ansible_dict to ec2_vpc_peering_facts.py, and parameter checking to ec2_vpc_peer.py (#52307)

* add boto3_tag_list_to_ansible_dict to ec2_vpc_peering_facts.py

* Add parameter checking to ec2_vpc_peer and give helpful error message

* Fixed modules --> module typo in ec2_vpc_peer

* Added required_if logic and fixed incorrect boolean check for absent peering connection

* Changed error message to one of the following is

* Added changelog fragments for ec2_vpc_peer changes

* Changed changelog fragment as per request
pull/53114/head
Brandon Bui 6 years ago committed by Will Thames
parent 861446b2a6
commit 1aae196cfa

@ -0,0 +1,2 @@
minor_changes:
- Added parameter checking before the module attempts to do an action to give helpful error message

@ -0,0 +1,2 @@
minor_changes:
- Changed output of tags dictionary in results to standard Ansible format

@ -403,11 +403,20 @@ def main():
state=dict(default='present', choices=['present', 'absent', 'accept', 'reject'])
)
)
module = AnsibleModule(argument_spec=argument_spec)
required_if = [
('state', 'present', ['vpc_id', 'peer_vpc_id']),
('state', 'accept', ['peering_id']),
('state', 'reject', ['peering_id'])
]
module = AnsibleModule(argument_spec=argument_spec, required_if=required_if)
if not HAS_BOTO3:
module.fail_json(msg='json, botocore and boto3 are required.')
state = module.params.get('state')
peering_id = module.params.get('peering_id')
vpc_id = module.params.get('vpc_id')
peer_vpc_id = module.params.get('peer_vpc_id')
try:
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
client = boto3_conn(module, conn_type='client', resource='ec2',
@ -419,6 +428,9 @@ def main():
(changed, results) = create_peer_connection(client, module)
module.exit_json(changed=changed, peering_id=results)
elif state == 'absent':
if not peering_id and (not vpc_id or not peer_vpc_id):
module.fail_json(msg='state is absent but one of the following is missing: peering_id or [vpc_id, peer_vpc_id]')
remove_peer_connection(client, module)
else:
(changed, results) = accept_reject(state, client, module)

@ -74,7 +74,8 @@ except ImportError:
pass # will be picked up by imported HAS_BOTO3
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ec2 import (ec2_argument_spec, boto3_conn, get_aws_connection_info,
from ansible.module_utils.ec2 import (boto3_tag_list_to_ansible_dict,
ec2_argument_spec, boto3_conn, get_aws_connection_info,
ansible_dict_to_boto3_filter_list, HAS_BOTO3, camel_dict_to_snake_dict)
@ -128,8 +129,13 @@ def main():
except botocore.exceptions.NoCredentialsError as e:
module.fail_json(msg=str(e))
# Turn the boto3 result in to ansible friendly_snaked_names
results = [camel_dict_to_snake_dict(peer) for peer in get_vpc_peers(ec2, module)]
# Turn the boto3 result in to ansible friendly tag dictionary
for peer in results:
peer['tags'] = boto3_tag_list_to_ansible_dict(peer.get('tags', []))
module.exit_json(result=results)

Loading…
Cancel
Save