Debian Codegraph @

ansible_collections/amazon/aws/plugins/modules/rds_cluster_snapshot.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2014 Ansible Project
5 # Copyright (c) 2021 Alina Buzachis (@alinabuzachis)
6 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 DOCUMENTATION = r"""
9 ---
10 module: rds_cluster_snapshot
11 version_added: 5.0.0
12 short_description: Manage Amazon RDS snapshots of DB clusters
13 description:
14 - Create, modify and delete RDS snapshots of DB clusters.
15 - This module was originally added to C(community.aws) in release 4.0.0.
16 options:
17 state:
18 description:
19 - Specify the desired state of the snapshot.
20 default: present
21 choices: [ 'present', 'absent']
22 type: str
23 db_cluster_snapshot_identifier:
24 description:
25 - The identifier of the DB cluster snapshot.
26 required: true
27 aliases:
28 - snapshot_id
29 - id
30 - snapshot_name
31 type: str
32 db_cluster_identifier:
33 description:
34 - The identifier of the DB cluster to create a snapshot for.
35 - Required when O(state=present).
36 aliases:
37 - cluster_id
38 - cluster_name
39 type: str
40 source_db_cluster_snapshot_identifier:
41 description:
42 - The identifier of the DB cluster snapshot to copy.
43 - If the source snapshot is in the same AWS region as the copy, specify the snapshot's identifier.
44 - If the source snapshot is in a different AWS region as the copy, specify the snapshot's ARN.
45 aliases:
46 - source_id
47 - source_snapshot_id
48 type: str
49 source_region:
50 description:
51 - The region that contains the snapshot to be copied.
52 type: str
53 copy_tags:
54 description:
55 - Whether to copy all tags from O(source_db_cluster_snapshot_identifier) to O(db_cluster_snapshot_identifier).
56 type: bool
57 default: false
58 wait:
59 description:
60 - Whether or not to wait for snapshot creation or deletion.
61 type: bool
62 default: false
63 wait_timeout:
64 description:
65 - How long before wait gives up, in seconds.
66 default: 300
67 type: int
68 notes:
69 - Retrieve the information about a specific DB cluster or list the DB cluster snapshots for a specific DB cluster
70 can de done using M(community.aws.rds_snapshot_info).
71 author:
72 - Alina Buzachis (@alinabuzachis)
73 extends_documentation_fragment:
74 - amazon.aws.common.modules
75 - amazon.aws.region.modules
76 - amazon.aws.tags
77 - amazon.aws.boto3
78 """
79
80 EXAMPLES = r"""
81 - name: Create a DB cluster snapshot
82 amazon.aws.rds_cluster_snapshot:
83 db_cluster_identifier: "{{ cluster_id }}"
84 db_cluster_snapshot_identifier: new-cluster-snapshot
85
86 - name: Delete a DB cluster snapshot
87 amazon.aws.rds_cluster_snapshot:
88 db_cluster_snapshot_identifier: new-cluster-snapshot
89 state: absent
90
91 - name: Copy snapshot from a different region and copy its tags
92 amazon.aws.rds_cluster_snapshot:
93 id: new-database-snapshot-copy
94 region: us-east-1
95 source_id: "{{ snapshot.db_snapshot_arn }}"
96 source_region: us-east-2
97 copy_tags: true
98 """
99
100 RETURN = r"""
101 availability_zone:
102 description: Availability zone of the database from which the snapshot was created.
103 returned: always
104 type: str
105 sample: "us-west-2a"
106 allocated_storage:
107 description: The allocated storage size of the DB cluster snapshot in gibibytes (GiB).
108 returned: always
109 type: int
110 sample: 0
111 cluster_create_time:
112 description: The time when the DB cluster was created, in Universal Coordinated Time (UTC).
113 returned: always
114 type: str
115 sample: "2024-07-01T05:27:53.478000+00:00"
116 db_cluster_snapshot_identifier:
117 description: Specifies the identifier for the DB cluster snapshot.
118 returned: always
119 type: str
120 sample: "ansible-test-16638696-test-snapshot"
121 db_cluster_identifier:
122 description: Specifies the DB cluster identifier of the DB cluster that this DB cluster snapshot was created from.
123 returned: always
124 type: str
125 sample: "ansible-test-16638696"
126 snapshot_create_time:
127 description: Provides the time when the snapshot was taken, in Universal Coordinated Time (UTC).
128 returned: always
129 type: str
130 sample: "2019-06-15T10:46:23.776000+00:00"
131 engine:
132 description: Specifies the name of the database engine for this DB cluster snapshot.
133 returned: always
134 type: str
135 sample: "aurora"
136 engine_mode:
137 description: Provides the engine mode of the database engine for this DB cluster snapshot.
138 returned: always
139 type: str
140 sample: "5.6.mysql_aurora.1.22.5"
141 status:
142 description: Specifies the status of this DB cluster snapshot.
143 returned: always
144 type: str
145 sample: available
146 port:
147 description: Port on which the database is listening.
148 returned: always
149 type: int
150 sample: 3306
151 vpc_id:
152 description: ID of the VPC in which the DB lives.
153 returned: always
154 type: str
155 sample: vpc-09ff232e222710ae0
156 master_username:
157 description: Provides the master username for this DB cluster snapshot.
158 returned: always
159 type: str
160 sample: "test"
161 engine_version:
162 description: Version of the cluster from which the snapshot was created.
163 returned: always
164 type: str
165 sample: "5.6.mysql_aurora.1.22.5"
166 license_model:
167 description: Provides the license model information for this DB cluster snapshot.
168 returned: always
169 type: str
170 sample: "general-public-license"
171 snapshot_type:
172 description: How the snapshot was created (always manual for this module!).
173 returned: always
174 type: str
175 sample: "manual"
176 percent_progress:
177 description: Specifies the percentage of the estimated data that has been transferred.
178 returned: always
179 type: int
180 sample: 100
181 storage_encrypted:
182 description: Specifies whether the DB cluster snapshot is encrypted.
183 returned: always
184 type: bool
185 sample: false
186 kms_key_id:
187 description: The Amazon Web Services KMS key identifier is the key ARN, key ID, alias ARN, or alias name for the KMS key.
188 returned: always
189 type: str
190 db_cluster_snapshot_arn:
191 description: Amazon Resource Name for the snapshot.
192 returned: always
193 type: str
194 sample: "arn:aws:rds:us-west-2:123456789012:snapshot:ansible-test-16638696-test-snapshot"
195 source_db_cluster_snapshot_arn:
196 description: If the DB cluster snapshot was copied from a source DB cluster snapshot, the ARN for the source DB cluster snapshot, otherwise, null.
197 returned: always
198 type: str
199 sample: null
200 iam_database_authentication_enabled:
201 description: Whether IAM database authentication is enabled.
202 returned: always
203 type: bool
204 sample: false
205 tag_list:
206 description: A list of tags.
207 returned: always
208 type: list
209 sample: [
210 {
211 "key": "tag_one",
212 "value": "ansible-test-123456789012-rds-cluster-snapshot-b One"
213 }]
214 tags:
215 description: Tags applied to the snapshot.
216 returned: always
217 type: dict
218 sample: {
219 "Tag Two": "two ansible-test-123456789012-rds-cluster-snapshot-b"
220 }
221 """
222
223 from typing import Any
224 from typing import Dict
225
226 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
227
228 from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule
229 from ansible_collections.amazon.aws.plugins.module_utils.rds import AnsibleRDSError
230 from ansible_collections.amazon.aws.plugins.module_utils.rds import arg_spec_to_rds_params
231 from ansible_collections.amazon.aws.plugins.module_utils.rds import call_method
232 from ansible_collections.amazon.aws.plugins.module_utils.rds import ensure_tags
233 from ansible_collections.amazon.aws.plugins.module_utils.rds import format_rds_client_method_parameters
234 from ansible_collections.amazon.aws.plugins.module_utils.rds import get_snapshot
235
236
237 def ensure_snapshot_absent(client, module: AnsibleAWSModule) -> None:
238 snapshot_id = module.params.get("db_cluster_snapshot_identifier")
239 changed = False
240
241 try:
242 snapshot = get_snapshot(client, snapshot_id, "cluster")
243 except AnsibleRDSError as e:
244 module.fail_json_aws(e, msg=f"Failed to get snapshot: {snapshot_id}")
245 if snapshot and snapshot["Status"] != "deleting":
246 snapshot, changed = call_method(
247 client, module, "delete_db_cluster_snapshot", {"DBClusterSnapshotIdentifier": snapshot_id}
248 )
249
250 module.exit_json(changed=changed)
251
252
253 def copy_snapshot(client, module: AnsibleAWSModule, params: Dict[str, Any]) -> bool:
254 changed = False
255 snapshot_id = module.params.get("db_cluster_snapshot_identifier")
256
257 try:
258 snapshot = get_snapshot(client, snapshot_id, "cluster")
259 except AnsibleRDSError as e:
260 module.fail_json_aws(e, msg=f"Failed to get snapshot: {snapshot_id}")
261 if not snapshot:
262 params["TargetDBClusterSnapshotIdentifier"] = snapshot_id
263 method_params = format_rds_client_method_parameters(
264 client, module, params, "copy_db_cluster_snapshot", format_tags=True
265 )
266 _result, changed = call_method(client, module, "copy_db_cluster_snapshot", method_params)
267
268 return changed
269
270
271 def ensure_snapshot_present(client, module: AnsibleAWSModule, params: Dict[str, Any]) -> None:
272 source_id = module.params.get("source_db_cluster_snapshot_identifier")
273 snapshot_id = module.params.get("db_cluster_snapshot_identifier")
274 changed = False
275
276 try:
277 snapshot = get_snapshot(client, snapshot_id, "cluster")
278 except AnsibleRDSError as e:
279 module.fail_json_aws(e, msg=f"Failed to get snapshot: {snapshot_id}")
280
281 # Copy snapshot
282 if source_id:
283 changed |= copy_snapshot(client, module, params)
284
285 # Create snapshot
286 elif not snapshot:
287 changed |= create_snapshot(client, module, params)
288
289 # Snapshot exists and we're not creating a copy - modify exising snapshot
290 else:
291 changed |= modify_snapshot(client, module)
292
293 try:
294 snapshot = get_snapshot(client, snapshot_id, "cluster")
295 except AnsibleRDSError as e:
296 module.fail_json_aws(e, msg=f"Failed to get snapshot: {snapshot_id}")
297
298 module.exit_json(changed=changed, **camel_dict_to_snake_dict(snapshot, ignore_list=["Tags"]))
299
300
301 def create_snapshot(client, module: AnsibleAWSModule, params: Dict[str, Any]) -> bool:
302 method_params = format_rds_client_method_parameters(
303 client, module, params, "create_db_cluster_snapshot", format_tags=True
304 )
305 _snapshot, changed = call_method(client, module, "create_db_cluster_snapshot", method_params)
306
307 return changed
308
309
310 def modify_snapshot(client, module: AnsibleAWSModule) -> bool:
311 # TODO - add other modifications aside from purely tags
312 changed = False
313 snapshot_id = module.params.get("db_cluster_snapshot_identifier")
314
315 try:
316 snapshot = get_snapshot(client, snapshot_id, "cluster")
317 except AnsibleRDSError as e:
318 module.fail_json_aws(e, msg=f"Failed to get snapshot: {snapshot_id}")
319
320 if module.params.get("tags"):
321 changed |= ensure_tags(
322 client,
323 module,
324 snapshot["DBClusterSnapshotArn"],
325 snapshot["Tags"],
326 module.params["tags"],
327 module.params["purge_tags"],
328 )
329
330 return changed
331
332
333 def main():
334 argument_spec = dict(
335 state=dict(type="str", choices=["present", "absent"], default="present"),
336 db_cluster_snapshot_identifier=dict(type="str", aliases=["id", "snapshot_id", "snapshot_name"], required=True),
337 db_cluster_identifier=dict(type="str", aliases=["cluster_id", "cluster_name"]),
338 source_db_cluster_snapshot_identifier=dict(type="str", aliases=["source_id", "source_snapshot_id"]),
339 wait=dict(type="bool", default=False),
340 wait_timeout=dict(type="int", default=300),
341 tags=dict(type="dict", aliases=["resource_tags"]),
342 purge_tags=dict(type="bool", default=True),
343 copy_tags=dict(type="bool", default=False),
344 source_region=dict(type="str"),
345 )
346
347 module = AnsibleAWSModule(
348 argument_spec=argument_spec,
349 supports_check_mode=True,
350 )
351 client = module.client("rds")
352 state = module.params.get("state")
353
354 if state == "absent":
355 ensure_snapshot_absent(client, module)
356 elif state == "present":
357 params = arg_spec_to_rds_params(dict((k, module.params[k]) for k in module.params if k in argument_spec))
358 ensure_snapshot_present(client, module, params)
359
360
361 if __name__ == "__main__":
362 main()