Skip to content

Commit

Permalink
Tagging - cleanup docs for ec2_snapshot_copy (ansible-collections#1201)
Browse files Browse the repository at this point in the history
Tagging - ec2_snapshot_copy

SUMMARY

Add the "resource_tags" alias, for consistency with other modules
minor docs clean-up
Use TagSpecification on creation rather than making a separate API call to tag the resource after creation.

Does not add purge_tags, since the module performs a one-shot action rather than managing the resources.
ISSUE TYPE

Docs Pull Request
Feature Pull Request

COMPONENT NAME
ec2_snapshot_copy
ADDITIONAL INFORMATION
Since we don't have a tags-only fragment, I've not switched this over to using a fragment.  If I find more modules with a similar use-case I'll try to find a standard fragment we can use.

Reviewed-by: Alina Buzachis <None>
  • Loading branch information
tremble authored Jun 2, 2022
1 parent 0999933 commit f1ab35b
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions ec2_snapshot_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
---
module: ec2_snapshot_copy
version_added: 1.0.0
short_description: Copies an EC2 snapshot and returns the new Snapshot ID.
short_description: Copies an EC2 snapshot and returns the new Snapshot ID
description:
- Copies an EC2 Snapshot from a source region to a destination region.
- Copies an EC2 Snapshot from a source region to a destination region.
options:
source_region:
description:
Expand All @@ -40,7 +40,7 @@
type: str
wait:
description:
- Wait for the copied Snapshot to be in 'Available' state before returning.
- Wait for the copied Snapshot to be in the C(Available) state before returning.
type: bool
default: 'no'
wait_timeout:
Expand All @@ -50,12 +50,14 @@
type: int
tags:
description:
- A hash/dictionary of tags to add to the new Snapshot; '{"key":"value"}' and '{"key":"value","key":"value"}'
- A dictionary representing the tags to be applied to the newly created resource.
type: dict
author: Deepak Kothandan (@Deepakkothandan) <[email protected]>
aliases: ['resource_tags']
author:
- Deepak Kothandan (@Deepakkothandan) <[email protected]>
extends_documentation_fragment:
- amazon.aws.aws
- amazon.aws.ec2
- amazon.aws.aws
- amazon.aws.ec2
'''

EXAMPLES = '''
Expand Down Expand Up @@ -112,6 +114,7 @@
pass # Handled by AnsibleAWSModule

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_specifications


def copy_snapshot(module, ec2):
Expand All @@ -134,6 +137,9 @@ def copy_snapshot(module, ec2):
if module.params.get('kms_key_id'):
params['KmsKeyId'] = module.params.get('kms_key_id')

if module.params.get('tags'):
params['TagSpecifications'] = boto3_tag_specifications(module.params.get('tags'))

try:
snapshot_id = ec2.copy_snapshot(**params)['SnapshotId']
if module.params.get('wait'):
Expand All @@ -145,11 +151,6 @@ def copy_snapshot(module, ec2):
SnapshotIds=[snapshot_id],
WaiterConfig=dict(Delay=delay, MaxAttempts=max_attempts)
)
if module.params.get('tags'):
ec2.create_tags(
Resources=[snapshot_id],
Tags=[{'Key': k, 'Value': v} for k, v in module.params.get('tags').items()]
)

except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg='An error occurred waiting for the snapshot to become available.')
Expand All @@ -166,7 +167,7 @@ def main():
kms_key_id=dict(type='str', required=False),
wait=dict(type='bool', default=False),
wait_timeout=dict(type='int', default=600),
tags=dict(type='dict'),
tags=dict(type='dict', aliases=['resource_tags']),
)

module = AnsibleAWSModule(argument_spec=argument_spec)
Expand Down

0 comments on commit f1ab35b

Please sign in to comment.