Skip to content

Commit

Permalink
Tagging - cleanup docs for ec2_snapshot_copy (#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>
tremble authored Jun 2, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 9ca5540 commit a538562
Showing 2 changed files with 17 additions and 13 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/1201-tagging-ec2_snapshot_copy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- ec2_snapshot_copy - ``resource_tags`` has been added as an alias for the ``tags`` parameter (https://github.com/ansible-collections/community.aws/pull/1201).
- ec2_snapshot_copy - updated to pass tags as part of the copy API call rather than tagging the snapshot after creation (https://github.com/ansible-collections/community.aws/pull/1201).
27 changes: 14 additions & 13 deletions plugins/modules/ec2_snapshot_copy.py
Original file line number Diff line number Diff line change
@@ -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:
@@ -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:
@@ -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) <deepak.kdy@gmail.com>
aliases: ['resource_tags']
author:
- Deepak Kothandan (@Deepakkothandan) <deepak.kdy@gmail.com>
extends_documentation_fragment:
- amazon.aws.aws
- amazon.aws.ec2
- amazon.aws.aws
- amazon.aws.ec2
'''

EXAMPLES = '''
@@ -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):
@@ -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'):
@@ -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.')
@@ -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)

0 comments on commit a538562

Please sign in to comment.