Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright 2014 Jens Carl, Hothead Games Inc.
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_subnet_group
10 version_added: 1.0.0
11 short_description: manage Redshift cluster subnet groups
12 description:
13 - Create, modifies, and deletes Redshift cluster subnet groups.
14 options:
15 state:
16 description:
17 - Specifies whether the subnet group should be present or absent.
18 default: 'present'
19 choices: ['present', 'absent' ]
20 type: str
21 name:
22 description:
23 - Cluster subnet group name.
24 required: true
25 aliases: ['group_name']
26 type: str
27 description:
28 description:
29 - Cluster subnet group description.
30 aliases: ['group_description']
31 type: str
32 subnets:
33 description:
34 - List of subnet IDs that make up the cluster subnet group.
35 - At least one subnet must be provided when creating a cluster subnet group.
36 aliases: ['group_subnets']
37 type: list
38 elements: str
39 extends_documentation_fragment:
40 - amazon.aws.common.modules
41 - amazon.aws.region.modules
42 - amazon.aws.boto3
43 author:
44 - "Jens Carl (@j-carl), Hothead Games Inc."
45 """
46
47 EXAMPLES = r"""
48 - name: Create a Redshift subnet group
49 community.aws.redshift_subnet_group:
50 state: present
51 group_name: redshift-subnet
52 group_description: Redshift subnet
53 group_subnets:
54 - 'subnet-aaaaa'
55 - 'subnet-bbbbb'
56
57 - name: Remove subnet group
58 community.aws.redshift_subnet_group:
59 state: absent
60 group_name: redshift-subnet
61 """
62
63 RETURN = r"""
64 cluster_subnet_group:
65 description: A dictionary containing information about the Redshift subnet group.
66 returned: success
67 type: dict
68 contains:
69 name:
70 description: Name of the Redshift subnet group.
71 returned: when the cache subnet group exists
72 type: str
73 sample: "redshift_subnet_group_name"
74 vpc_id:
75 description: Id of the VPC where the subnet is located.
76 returned: when the cache subnet group exists
77 type: str
78 sample: "vpc-aabb1122"
79 description:
80 description: The description of the cache subnet group.
81 returned: when the cache subnet group exists
82 type: str
83 sample: Redshift subnet
84 subnet_ids:
85 description: The IDs of the subnets beloging to the Redshift subnet group.
86 returned: when the cache subnet group exists
87 type: list
88 elements: str
89 sample:
90 - subnet-aaaaaaaa
91 - subnet-bbbbbbbb
92 """
93
94 try:
95 import botocore
96 except ImportError:
97 pass # Handled by AnsibleAWSModule
98
99 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
100
101 from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code
102 from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry
103 from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
104
105 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
106
107
108 def get_subnet_group(name):
109 try:
110 groups = client.describe_cluster_subnet_groups(
111 aws_retry=True,
112 ClusterSubnetGroupName=name,
113 )["ClusterSubnetGroups"]
114 except is_boto3_error_code("ClusterSubnetGroupNotFoundFault"):
115 return None
116 except (
117 botocore.exceptions.ClientError,
118 botocore.exceptions.BotoCoreError,
119 ) as e: # pylint: disable=duplicate-except
120 module.fail_json_aws(e, msg="Failed to describe subnet group")
121
122 if not groups:
123 return None
124
125 if len(groups) > 1:
126 module.fail_aws(
127 msg="Found multiple matches for subnet group",
128 cluster_subnet_groups=camel_dict_to_snake_dict(groups),
129 )
130
131 # No support for managing tags yet, but make sure that we don't need to
132 # change the return value structure after it's been available in a release.
133 tags = boto3_tag_list_to_ansible_dict(groups[0]["Tags"])
134
135 subnet_group = camel_dict_to_snake_dict(groups[0])
136
137 subnet_group["tags"] = tags
138 subnet_group["name"] = subnet_group["cluster_subnet_group_name"]
139
140 subnet_ids = list(s["subnet_identifier"] for s in subnet_group["subnets"])
141 subnet_group["subnet_ids"] = subnet_ids
142
143 return subnet_group
144
145
146 def create_subnet_group(name, description, subnets):
147 if not subnets:
148 module.fail_json(msg="At least one subnet must be provided when creating a subnet group")
149
150 if module.check_mode:
151 return True
152
153 try:
154 if not description:
155 description = name
156 client.create_cluster_subnet_group(
157 aws_retry=True,
158 ClusterSubnetGroupName=name,
159 Description=description,
160 SubnetIds=subnets,
161 )
162 return True
163 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
164 module.fail_json_aws(e, msg="Failed to create subnet group")
165
166
167 def update_subnet_group(subnet_group, name, description, subnets):
168 update_params = dict()
169 if description and subnet_group["description"] != description:
170 update_params["Description"] = description
171 if subnets:
172 old_subnets = set(subnet_group["subnet_ids"])
173 new_subnets = set(subnets)
174 if old_subnets != new_subnets:
175 update_params["SubnetIds"] = list(subnets)
176
177 if not update_params:
178 return False
179
180 if module.check_mode:
181 return True
182
183 # Description is optional, SubnetIds is not
184 if "SubnetIds" not in update_params:
185 update_params["SubnetIds"] = subnet_group["subnet_ids"]
186
187 try:
188 client.modify_cluster_subnet_group(
189 aws_retry=True,
190 ClusterSubnetGroupName=name,
191 **update_params,
192 )
193 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
194 module.fail_json_aws(e, msg="Failed to update subnet group")
195
196 return True
197
198
199 def delete_subnet_group(name):
200 if module.check_mode:
201 return True
202
203 try:
204 client.delete_cluster_subnet_group(
205 aws_retry=True,
206 ClusterSubnetGroupName=name,
207 )
208 return True
209 except is_boto3_error_code("ClusterSubnetGroupNotFoundFault"):
210 # AWS is "eventually consistent", cope with the race conditions where
211 # deletion hadn't completed when we ran describe
212 return False
213 except (
214 botocore.exceptions.ClientError,
215 botocore.exceptions.BotoCoreError,
216 ) as e: # pylint: disable=duplicate-except
217 module.fail_json_aws(e, msg="Failed to delete subnet group")
218
219
220 def main():
221 argument_spec = dict(
222 state=dict(default="present", choices=["present", "absent"]),
223 name=dict(required=True, aliases=["group_name"]),
224 description=dict(required=False, aliases=["group_description"]),
225 subnets=dict(required=False, aliases=["group_subnets"], type="list", elements="str"),
226 )
227
228 global module
229 global client
230
231 module = AnsibleAWSModule(
232 argument_spec=argument_spec,
233 supports_check_mode=True,
234 )
235
236 state = module.params.get("state")
237 name = module.params.get("name")
238 description = module.params.get("description")
239 subnets = module.params.get("subnets")
240
241 client = module.client("redshift", retry_decorator=AWSRetry.jittered_backoff())
242
243 subnet_group = get_subnet_group(name)
244 changed = False
245
246 if state == "present":
247 if not subnet_group:
248 result = create_subnet_group(name, description, subnets)
249 changed |= result
250 else:
251 result = update_subnet_group(subnet_group, name, description, subnets)
252 changed |= result
253 subnet_group = get_subnet_group(name)
254 else:
255 if subnet_group:
256 result = delete_subnet_group(name)
257 changed |= result
258 subnet_group = None
259
260 compat_results = dict()
261 if subnet_group:
262 compat_results["group"] = dict(
263 name=subnet_group["name"],
264 vpc_id=subnet_group["vpc_id"],
265 )
266
267 module.exit_json(
268 changed=changed,
269 cluster_subnet_group=subnet_group,
270 **compat_results,
271 )
272
273
274 if __name__ == "__main__":
275 main()