From 8a9a9b84ddf246bdcbbae5c21457d738712a035e Mon Sep 17 00:00:00 2001 From: Prasad Katti Date: Tue, 24 Oct 2017 13:01:33 -0700 Subject: [PATCH] [ec2-ami] Use required_if with state parameter (#32009) * [ec2-ami] Use required_if with state parameter * Update failure msg on delete ami with no image_id task in integration test --- lib/ansible/modules/cloud/amazon/ec2_ami.py | 11 +++++------ test/integration/targets/ec2_ami/tasks/main.yml | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2_ami.py b/lib/ansible/modules/cloud/amazon/ec2_ami.py index fcfe6c94aa..540628358e 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_ami.py +++ b/lib/ansible/modules/cloud/amazon/ec2_ami.py @@ -616,7 +616,11 @@ def main(): launch_permissions=dict(type='dict') ) ) - module = AnsibleModule(argument_spec=argument_spec) + module = AnsibleModule( + argument_spec=argument_spec, + required_if=[('state', 'present', ('name',)), + ('state', 'absent', ('image_id',))] + ) if not HAS_BOTO: module.fail_json(msg='boto required for this module') @@ -627,9 +631,6 @@ def main(): module.fail_json(msg="Error while connecting to aws: %s" % str(e)) if module.params.get('state') == 'absent': - if not module.params.get('image_id'): - module.fail_json(msg='image_id needs to be an ami image to registered/delete') - deregister_image(module, ec2) elif module.params.get('state') == 'present': @@ -640,8 +641,6 @@ def main(): # Changed is always set to true when provisioning new AMI if not module.params.get('instance_id') and not module.params.get('device_mapping'): module.fail_json(msg='instance_id or device_mapping (register from ebs snapshot) parameter is required for new image') - if not module.params.get('name'): - module.fail_json(msg='name parameter is required for new image') create_image(module, ec2) diff --git a/test/integration/targets/ec2_ami/tasks/main.yml b/test/integration/targets/ec2_ami/tasks/main.yml index 8e201fbc5f..68b8e2b3bd 100644 --- a/test/integration/targets/ec2_ami/tasks/main.yml +++ b/test/integration/targets/ec2_ami/tasks/main.yml @@ -158,7 +158,7 @@ assert: that: - "result.failed" - - "result.msg == 'image_id needs to be an ami image to registered/delete'" + - "result.msg == 'state is absent but the following are missing: image_id'" # ============================================================