Debian Codegraph @

ansible_collections/community/aws/plugins/modules/redshift_cross_region_snapshots.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, JR Kerkstra <[email protected]>
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: redshift_cross_region_snapshots
10 version_added: 1.0.0
11 short_description: Manage Redshift Cross Region Snapshots
12 description:
13 - Manage Redshift Cross Region Snapshots. Supports KMS-Encrypted Snapshots.
14 - For more information, see U(https://docs.aws.amazon.com/redshift/latest/mgmt/working-with-snapshots.html#cross-region-snapshot-copy)
15 author:
16 - JR Kerkstra (@captainkerk)
17 options:
18 cluster_name:
19 description:
20 - The name of the cluster to configure cross-region snapshots for.
21 required: true
22 aliases: [ "cluster" ]
23 type: str
24 state:
25 description:
26 - Create or remove the cross-region snapshot configuration.
27 choices: [ "present", "absent" ]
28 default: present
29 type: str
30 region:
31 description:
32 - "The cluster's region."
33 required: true
34 aliases: [ "source" ]
35 type: str
36 destination_region:
37 description:
38 - The region to copy snapshots to.
39 required: true
40 aliases: [ "destination" ]
41 type: str
42 snapshot_copy_grant:
43 description:
44 - A grant for Amazon Redshift to use a master key in the I(destination_region).
45 - See U(http://boto3.readthedocs.io/en/latest/reference/services/redshift.html#Redshift.Client.create_snapshot_copy_grant)
46 aliases: [ "copy_grant" ]
47 type: str
48 snapshot_retention_period:
49 description:
50 - The number of days to keep cross-region snapshots for.
51 required: true
52 aliases: [ "retention_period" ]
53 type: int
54 extends_documentation_fragment:
55 - amazon.aws.region.modules
56 - amazon.aws.common.modules
57 - amazon.aws.boto3
58 """
59
60 EXAMPLES = r"""
61 - name: configure cross-region snapshot on cluster `johniscool`
62 community.aws.redshift_cross_region_snapshots:
63 cluster_name: johniscool
64 state: present
65 region: us-east-1
66 destination_region: us-west-2
67 retention_period: 1
68
69 - name: configure cross-region snapshot on kms-encrypted cluster
70 community.aws.redshift_cross_region_snapshots:
71 cluster_name: whatever
72 state: present
73 region: us-east-1
74 destination: us-west-2
75 copy_grant: 'my-grant-in-destination'
76 retention_period: 10
77
78 - name: disable cross-region snapshots, necessary before most cluster modifications (rename, resize)
79 community.aws.redshift_cross_region_snapshots:
80 cluster_name: whatever
81 state: absent
82 region: us-east-1
83 destination_region: us-west-2
84 """
85
86 RETURN = r""" # """
87
88 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
89
90
91 class SnapshotController(object):
92 def __init__(self, client, cluster_name):
93 self.client = client
94 self.cluster_name = cluster_name
95
96 def get_cluster_snapshot_copy_status(self):
97 response = self.client.describe_clusters(ClusterIdentifier=self.cluster_name)
98 return response["Clusters"][0].get("ClusterSnapshotCopyStatus")
99
100 def enable_snapshot_copy(self, destination_region, grant_name, retention_period):
101 if grant_name:
102 self.client.enable_snapshot_copy(
103 ClusterIdentifier=self.cluster_name,
104 DestinationRegion=destination_region,
105 RetentionPeriod=retention_period,
106 SnapshotCopyGrantName=grant_name,
107 )
108 else:
109 self.client.enable_snapshot_copy(
110 ClusterIdentifier=self.cluster_name,
111 DestinationRegion=destination_region,
112 RetentionPeriod=retention_period,
113 )
114
115 def disable_snapshot_copy(self):
116 self.client.disable_snapshot_copy(ClusterIdentifier=self.cluster_name)
117
118 def modify_snapshot_copy_retention_period(self, retention_period):
119 self.client.modify_snapshot_copy_retention_period(
120 ClusterIdentifier=self.cluster_name, RetentionPeriod=retention_period
121 )
122
123
124 def requesting_unsupported_modifications(actual, requested):
125 if (
126 actual["SnapshotCopyGrantName"] != requested["snapshot_copy_grant"]
127 or actual["DestinationRegion"] != requested["destination_region"]
128 ):
129 return True
130 return False
131
132
133 def needs_update(actual, requested):
134 if actual["RetentionPeriod"] != requested["snapshot_retention_period"]:
135 return True
136 return False
137
138
139 def run_module():
140 argument_spec = dict(
141 cluster_name=dict(type="str", required=True, aliases=["cluster"]),
142 state=dict(type="str", choices=["present", "absent"], default="present"),
143 region=dict(type="str", required=True, aliases=["source"]),
144 destination_region=dict(type="str", required=True, aliases=["destination"]),
145 snapshot_copy_grant=dict(type="str", aliases=["copy_grant"]),
146 snapshot_retention_period=dict(type="int", required=True, aliases=["retention_period"]),
147 )
148
149 module = AnsibleAWSModule(
150 argument_spec=argument_spec,
151 supports_check_mode=True,
152 )
153
154 result = dict(
155 changed=False,
156 message="",
157 )
158 connection = module.client("redshift")
159
160 snapshot_controller = SnapshotController(client=connection, cluster_name=module.params.get("cluster_name"))
161
162 current_config = snapshot_controller.get_cluster_snapshot_copy_status()
163 if current_config is not None:
164 if module.params.get("state") == "present":
165 if requesting_unsupported_modifications(current_config, module.params):
166 message = (
167 "Cannot modify destination_region or grant_name. Please disable cross-region snapshots, and re-run."
168 )
169 module.fail_json(msg=message, **result)
170 if needs_update(current_config, module.params):
171 result["changed"] = True
172 if not module.check_mode:
173 snapshot_controller.modify_snapshot_copy_retention_period(
174 module.params.get("snapshot_retention_period")
175 )
176 else:
177 result["changed"] = True
178 if not module.check_mode:
179 snapshot_controller.disable_snapshot_copy()
180 else:
181 if module.params.get("state") == "present":
182 result["changed"] = True
183 if not module.check_mode:
184 snapshot_controller.enable_snapshot_copy(
185 module.params.get("destination_region"),
186 module.params.get("snapshot_copy_grant"),
187 module.params.get("snapshot_retention_period"),
188 )
189 module.exit_json(**result)
190
191
192 def main():
193 run_module()
194
195
196 if __name__ == "__main__":
197 main()