| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: elasticache_snapshot |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manage cache snapshots in Amazon ElastiCache |
| 12 |
description: |
| 13 |
- Manage cache snapshots in Amazon ElastiCache. |
| 14 |
- Returns information about the specified snapshot. |
| 15 |
author: |
| 16 |
- "Sloane Hertel (@s-hertel)" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- The name of the snapshot we want to create, copy, delete. |
| 21 |
required: true |
| 22 |
type: str |
| 23 |
state: |
| 24 |
description: |
| 25 |
- Actions that will create, destroy, or copy a snapshot. |
| 26 |
required: true |
| 27 |
choices: ['present', 'absent', 'copy'] |
| 28 |
type: str |
| 29 |
replication_id: |
| 30 |
description: |
| 31 |
- The name of the existing replication group to make the snapshot. |
| 32 |
type: str |
| 33 |
cluster_id: |
| 34 |
description: |
| 35 |
- The name of an existing cache cluster in the replication group to make the snapshot. |
| 36 |
type: str |
| 37 |
target: |
| 38 |
description: |
| 39 |
- The name of a snapshot copy. |
| 40 |
type: str |
| 41 |
bucket: |
| 42 |
description: |
| 43 |
- The s3 bucket to which the snapshot is exported. |
| 44 |
type: str |
| 45 |
extends_documentation_fragment: |
| 46 |
- amazon.aws.common.modules |
| 47 |
- amazon.aws.region.modules |
| 48 |
- amazon.aws.boto3 |
| 49 |
""" |
| 50 |
|
| 51 |
EXAMPLES = r""" |
| 52 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 53 |
|
| 54 |
- name: 'Create a snapshot' |
| 55 |
community.aws.elasticache_snapshot: |
| 56 |
name: 'test-snapshot' |
| 57 |
state: 'present' |
| 58 |
cluster_id: '{{ cluster }}' |
| 59 |
replication_id: '{{ replication }}' |
| 60 |
""" |
| 61 |
|
| 62 |
RETURN = r""" |
| 63 |
response_metadata: |
| 64 |
description: response metadata about the snapshot |
| 65 |
returned: always |
| 66 |
type: dict |
| 67 |
sample: |
| 68 |
http_headers: |
| 69 |
content-length: 1490 |
| 70 |
content-type: text/xml |
| 71 |
date: 'Tue, 07 Feb 2017 16:43:04 GMT' |
| 72 |
x-amzn-requestid: 7f436dea-ed54-11e6-a04c-ab2372a1f14d |
| 73 |
http_status_code: 200 |
| 74 |
request_id: 7f436dea-ed54-11e6-a04c-ab2372a1f14d |
| 75 |
retry_attempts: 0 |
| 76 |
snapshot: |
| 77 |
description: snapshot data |
| 78 |
returned: always |
| 79 |
type: dict |
| 80 |
sample: |
| 81 |
auto_minor_version_upgrade: true |
| 82 |
cache_cluster_create_time: '2017-02-01T17:43:58.261000+00:00' |
| 83 |
cache_cluster_id: test-please-delete |
| 84 |
cache_node_type: cache.m1.small |
| 85 |
cache_parameter_group_name: default.redis3.2 |
| 86 |
cache_subnet_group_name: default |
| 87 |
engine: redis |
| 88 |
engine_version: 3.2.4 |
| 89 |
node_snapshots: |
| 90 |
cache_node_create_time: '2017-02-01T17:43:58.261000+00:00' |
| 91 |
cache_node_id: 0001 |
| 92 |
cache_size: |
| 93 |
num_cache_nodes: 1 |
| 94 |
port: 11211 |
| 95 |
preferred_availability_zone: us-east-1d |
| 96 |
preferred_maintenance_window: wed:03:00-wed:04:00 |
| 97 |
snapshot_name: deletesnapshot |
| 98 |
snapshot_retention_limit: 0 |
| 99 |
snapshot_source: manual |
| 100 |
snapshot_status: creating |
| 101 |
snapshot_window: 10:00-11:00 |
| 102 |
vpc_id: vpc-c248fda4 |
| 103 |
changed: |
| 104 |
description: if a snapshot has been created, deleted, or copied |
| 105 |
returned: always |
| 106 |
type: bool |
| 107 |
sample: |
| 108 |
changed: true |
| 109 |
""" |
| 110 |
|
| 111 |
try: |
| 112 |
import botocore |
| 113 |
except ImportError: |
| 114 |
pass # Handled by AnsibleAWSModule |
| 115 |
|
| 116 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 117 |
|
| 118 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 119 |
|
| 120 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 121 |
|
| 122 |
|
| 123 |
def create(module, connection, replication_id, cluster_id, name): |
| 124 |
"""Create an ElastiCache backup.""" |
| 125 |
try: |
| 126 |
response = connection.create_snapshot( |
| 127 |
ReplicationGroupId=replication_id, CacheClusterId=cluster_id, SnapshotName=name |
| 128 |
) |
| 129 |
changed = True |
| 130 |
except is_boto3_error_code("SnapshotAlreadyExistsFault"): |
| 131 |
response = {} |
| 132 |
changed = False |
| 133 |
except botocore.exceptions.ClientError as e: # pylint: disable=duplicate-except |
| 134 |
module.fail_json_aws(e, msg="Unable to create the snapshot.") |
| 135 |
return response, changed |
| 136 |
|
| 137 |
|
| 138 |
def copy(module, connection, name, target, bucket): |
| 139 |
"""Copy an ElastiCache backup.""" |
| 140 |
try: |
| 141 |
response = connection.copy_snapshot(SourceSnapshotName=name, TargetSnapshotName=target, TargetBucket=bucket) |
| 142 |
changed = True |
| 143 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 144 |
module.fail_json_aws(e, msg="Unable to copy the snapshot.") |
| 145 |
return response, changed |
| 146 |
|
| 147 |
|
| 148 |
def delete(module, connection, name): |
| 149 |
"""Delete an ElastiCache backup.""" |
| 150 |
try: |
| 151 |
response = connection.delete_snapshot(SnapshotName=name) |
| 152 |
changed = True |
| 153 |
except is_boto3_error_code("SnapshotNotFoundFault"): |
| 154 |
response = {} |
| 155 |
changed = False |
| 156 |
except is_boto3_error_code("InvalidSnapshotState"): # pylint: disable=duplicate-except |
| 157 |
module.fail_json( |
| 158 |
msg=( |
| 159 |
"Error: InvalidSnapshotState. The snapshot is not in an available state or failed state to allow" |
| 160 |
" deletion.You may need to wait a few minutes." |
| 161 |
) |
| 162 |
) |
| 163 |
except botocore.exceptions.ClientError as e: # pylint: disable=duplicate-except |
| 164 |
module.fail_json_aws(e, msg="Unable to delete the snapshot.") |
| 165 |
return response, changed |
| 166 |
|
| 167 |
|
| 168 |
def main(): |
| 169 |
argument_spec = dict( |
| 170 |
name=dict(required=True, type="str"), |
| 171 |
state=dict(required=True, type="str", choices=["present", "absent", "copy"]), |
| 172 |
replication_id=dict(type="str"), |
| 173 |
cluster_id=dict(type="str"), |
| 174 |
target=dict(type="str"), |
| 175 |
bucket=dict(type="str"), |
| 176 |
) |
| 177 |
|
| 178 |
module = AnsibleAWSModule(argument_spec=argument_spec) |
| 179 |
|
| 180 |
name = module.params.get("name") |
| 181 |
state = module.params.get("state") |
| 182 |
replication_id = module.params.get("replication_id") |
| 183 |
cluster_id = module.params.get("cluster_id") |
| 184 |
target = module.params.get("target") |
| 185 |
bucket = module.params.get("bucket") |
| 186 |
|
| 187 |
try: |
| 188 |
connection = module.client("elasticache") |
| 189 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 190 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 191 |
|
| 192 |
changed = False |
| 193 |
response = {} |
| 194 |
|
| 195 |
if state == "present": |
| 196 |
if not all((replication_id, cluster_id)): |
| 197 |
module.fail_json(msg="The state 'present' requires options: 'replication_id' and 'cluster_id'") |
| 198 |
response, changed = create(module, connection, replication_id, cluster_id, name) |
| 199 |
elif state == "absent": |
| 200 |
response, changed = delete(module, connection, name) |
| 201 |
elif state == "copy": |
| 202 |
if not all((target, bucket)): |
| 203 |
module.fail_json(msg="The state 'copy' requires options: 'target' and 'bucket'.") |
| 204 |
response, changed = copy(module, connection, name, target, bucket) |
| 205 |
|
| 206 |
facts_result = dict(changed=changed, **camel_dict_to_snake_dict(response)) |
| 207 |
|
| 208 |
module.exit_json(**facts_result) |
| 209 |
|
| 210 |
|
| 211 |
if __name__ == "__main__": |
| 212 |
main() |