Debian Codegraph @

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

raw
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_subnet_group
10 version_added: 1.0.0
11 short_description: manage ElastiCache subnet groups
12 description:
13 - Creates, modifies, and deletes ElastiCache subnet groups.
14 options:
15 state:
16 description:
17 - Specifies whether the subnet should be present or absent.
18 choices: [ 'present' , 'absent' ]
19 default: 'present'
20 type: str
21 name:
22 description:
23 - Database subnet group identifier.
24 - This value is automatically converted to lowercase.
25 required: true
26 type: str
27 description:
28 description:
29 - ElastiCache subnet group description.
30 - When not provided defaults to I(name) on subnet group creation.
31 type: str
32 subnets:
33 description:
34 - List of subnet IDs that make up the ElastiCache subnet group.
35 - At least one subnet must be provided when creating an ElastiCache subnet group.
36 type: list
37 elements: str
38 author:
39 - "Tim Mahoney (@timmahoney)"
40 extends_documentation_fragment:
41 - amazon.aws.common.modules
42 - amazon.aws.region.modules
43 - amazon.aws.boto3
44 """
45
46 EXAMPLES = r"""
47 - name: Add or change a subnet group
48 community.aws.elasticache_subnet_group:
49 state: present
50 name: norwegian-blue
51 description: My Fancy Ex Parrot Subnet Group
52 subnets:
53 - subnet-aaaaaaaa
54 - subnet-bbbbbbbb
55
56 - name: Remove a subnet group
57 community.aws.elasticache_subnet_group:
58 state: absent
59 name: norwegian-blue
60 """
61
62 RETURN = r"""
63 cache_subnet_group:
64 description: Description of the Elasticache Subnet Group.
65 returned: always
66 type: dict
67 contains:
68 arn:
69 description: The Amazon Resource Name (ARN) of the cache subnet group.
70 returned: when the subnet group exists
71 type: str
72 sample: arn:aws:elasticache:us-east-1:123456789012:subnetgroup:norwegian-blue
73 description:
74 description: The description of the cache subnet group.
75 returned: when the cache subnet group exists
76 type: str
77 sample: My Fancy Ex Parrot Subnet Group
78 name:
79 description: The name of the cache subnet group.
80 returned: when the cache subnet group exists
81 type: str
82 sample: norwegian-blue
83 vpc_id:
84 description: The VPC ID of the cache subnet group.
85 returned: when the cache subnet group exists
86 type: str
87 sample: norwegian-blue
88 subnet_ids:
89 description: The IDs of the subnets beloging to the cache subnet group.
90 returned: when the cache subnet group exists
91 type: list
92 elements: str
93 sample:
94 - subnet-aaaaaaaa
95 - subnet-bbbbbbbb
96 """
97
98 try:
99 import botocore
100 except ImportError:
101 pass # Handled by AnsibleAWSModule
102
103 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
104
105 from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code
106 from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry
107
108 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
109
110
111 def get_subnet_group(name):
112 try:
113 groups = client.describe_cache_subnet_groups(
114 aws_retry=True,
115 CacheSubnetGroupName=name,
116 )["CacheSubnetGroups"]
117 except is_boto3_error_code("CacheSubnetGroupNotFoundFault"):
118 return None
119 except (
120 botocore.exceptions.ClientError,
121 botocore.exceptions.BotoCoreError,
122 ) as e: # pylint: disable=duplicate-except
123 module.fail_json_aws(e, msg="Failed to describe subnet group")
124
125 if not groups:
126 return None
127
128 if len(groups) > 1:
129 module.fail_aws(
130 msg="Found multiple matches for subnet group",
131 cache_subnet_groups=camel_dict_to_snake_dict(groups),
132 )
133
134 subnet_group = camel_dict_to_snake_dict(groups[0])
135
136 subnet_group["name"] = subnet_group["cache_subnet_group_name"]
137 subnet_group["description"] = subnet_group["cache_subnet_group_description"]
138
139 subnet_ids = list(s["subnet_identifier"] for s in subnet_group["subnets"])
140 subnet_group["subnet_ids"] = subnet_ids
141
142 return subnet_group
143
144
145 def create_subnet_group(name, description, subnets):
146 if not subnets:
147 module.fail_json(msg="At least one subnet must be provided when creating a subnet group")
148
149 if module.check_mode:
150 return True
151
152 try:
153 if not description:
154 description = name
155 client.create_cache_subnet_group(
156 aws_retry=True,
157 CacheSubnetGroupName=name,
158 CacheSubnetGroupDescription=description,
159 SubnetIds=subnets,
160 )
161 return True
162 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
163 module.fail_json_aws(e, msg="Failed to create subnet group")
164
165
166 def update_subnet_group(subnet_group, name, description, subnets):
167 update_params = dict()
168 if description and subnet_group["description"] != description:
169 update_params["CacheSubnetGroupDescription"] = description
170 if subnets:
171 old_subnets = set(subnet_group["subnet_ids"])
172 new_subnets = set(subnets)
173 if old_subnets != new_subnets:
174 update_params["SubnetIds"] = list(subnets)
175
176 if not update_params:
177 return False
178
179 if module.check_mode:
180 return True
181
182 try:
183 client.modify_cache_subnet_group(
184 aws_retry=True,
185 CacheSubnetGroupName=name,
186 **update_params,
187 )
188 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
189 module.fail_json_aws(e, msg="Failed to update subnet group")
190
191 return True
192
193
194 def delete_subnet_group(name):
195 if module.check_mode:
196 return True
197
198 try:
199 client.delete_cache_subnet_group(
200 aws_retry=True,
201 CacheSubnetGroupName=name,
202 )
203 return True
204 except is_boto3_error_code("CacheSubnetGroupNotFoundFault"):
205 # AWS is "eventually consistent", cope with the race conditions where
206 # deletion hadn't completed when we ran describe
207 return False
208 except (
209 botocore.exceptions.ClientError,
210 botocore.exceptions.BotoCoreError,
211 ) as e: # pylint: disable=duplicate-except
212 module.fail_json_aws(e, msg="Failed to delete subnet group")
213
214
215 def main():
216 argument_spec = dict(
217 state=dict(default="present", choices=["present", "absent"]),
218 name=dict(required=True),
219 description=dict(required=False),
220 subnets=dict(required=False, type="list", elements="str"),
221 )
222
223 global module
224 global client
225
226 module = AnsibleAWSModule(
227 argument_spec=argument_spec,
228 supports_check_mode=True,
229 )
230
231 state = module.params.get("state")
232 name = module.params.get("name").lower()
233 description = module.params.get("description")
234 subnets = module.params.get("subnets")
235
236 client = module.client("elasticache", retry_decorator=AWSRetry.jittered_backoff())
237
238 subnet_group = get_subnet_group(name)
239 changed = False
240
241 if state == "present":
242 if not subnet_group:
243 result = create_subnet_group(name, description, subnets)
244 changed |= result
245 else:
246 result = update_subnet_group(subnet_group, name, description, subnets)
247 changed |= result
248 subnet_group = get_subnet_group(name)
249 else:
250 if subnet_group:
251 result = delete_subnet_group(name)
252 changed |= result
253 subnet_group = None
254
255 module.exit_json(changed=changed, cache_subnet_group=subnet_group)
256
257
258 if __name__ == "__main__":
259 main()