Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_ipgroup.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2021 Aparna Patil(@techcon65)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 DOCUMENTATION = '''
11 ---
12 module: azure_rm_ipgroup
13
14 version_added: "1.6.0"
15
16 short_description: Create, delete and update IP group
17
18 description:
19 - Creates, deletes, and updates IP group in specified resource group.
20
21 options:
22 resource_group:
23 description:
24 - Name of the resource group.
25 required: true
26 type: str
27 name:
28 description:
29 - The name of the IP group.
30 required: true
31 type: str
32 location:
33 description:
34 - Location for IP group. Defaults to location of resource group if not specified.
35 type: str
36 ip_addresses:
37 description:
38 - The List of IP addresses in IP group.
39 type: list
40 elements: str
41 state:
42 description:
43 - Assert the state of the IP group. Use C(present) to create or update and C(absent) to delete.
44 default: present
45 type: str
46 choices:
47 - absent
48 - present
49
50 extends_documentation_fragment:
51 - azure.azcollection.azure
52 - azure.azcollection.azure_tags
53
54 author:
55 - Aparna Patil (@techcon65)
56 '''
57
58 EXAMPLES = '''
59 - name: Create IP Group
60 azure_rm_ipgroup:
61 resource_group: MyAzureResourceGroup
62 name: myipgroup
63 location: eastus
64 ip_addresses:
65 - 13.64.39.16/32
66 - 40.74.146.80/31
67 - 40.74.147.32/28
68 tags:
69 key1: "value1"
70 state: present
71
72 - name: Update IP Group
73 azure_rm_ipgroup:
74 resource_group: MyAzureResourceGroup
75 name: myipgroup
76 location: eastus
77 ip_addresses:
78 - 10.0.0.0/24
79 tags:
80 key2: "value2"
81
82 - name: Delete IP Group
83 azure_rm_ipgroup:
84 resource_group: MyAzureResourceGroup
85 name: myipgroup
86 state: absent
87 '''
88
89 RETURN = '''
90 state:
91 description:
92 - Current state of the IP group.
93 returned: always
94 type: complex
95 contains:
96 id:
97 description:
98 - The IP group ID.
99 returned: always
100 type: str
101 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyAzureResourceGroup/providers/
102 Microsoft.Network/ipGroups/myipgroup"
103 name:
104 description:
105 - The IP group name.
106 returned: always
107 type: str
108 sample: 'myipgroup'
109 location:
110 description:
111 - The Azure Region where the resource lives.
112 returned: always
113 type: str
114 sample: eastus
115 ip_addresses:
116 description:
117 - The list of IP addresses in IP group.
118 returned: always
119 type: list
120 elements: str
121 sample: [
122 "13.64.39.16/32",
123 "40.74.146.80/31",
124 "40.74.147.32/28"
125 ]
126 provisioning_state:
127 description:
128 - The provisioning state of the resource.
129 returned: always
130 type: str
131 sample: Succeeded
132 firewalls:
133 description:
134 - List of references to Firewall resources that this IpGroups is associated with.
135 returned: always
136 type: list
137 elements: dict
138 sample: [
139 {
140 "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myAzureResourceGroup/providers/
141 Microsoft.Network/azureFirewalls/azurefirewall"
142 }
143 ]
144 tags:
145 description:
146 - Resource tags.
147 returned: always
148 type: list
149 sample: [{"key1": "value1"}]
150 type:
151 description:
152 - The type of resource.
153 returned: always
154 type: str
155 sample: Microsoft.Network/IpGroups
156 etag:
157 description:
158 - The etag of the IP group.
159 returned: always
160 type: str
161 sample: c67388ea-6dab-481b-9387-bd441c0d32f8
162 '''
163
164 from ansible.module_utils.basic import _load_params
165 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase, normalize_location_name
166
167 try:
168 from azure.core.exceptions import ResourceNotFoundError
169 from azure.core.polling import LROPoller
170 except ImportError:
171 # This is handled in azure_rm_common
172 pass
173
174
175 class AzureRMIPGroup(AzureRMModuleBase):
176
177 def __init__(self):
178
179 _load_params()
180 # define user inputs from playbook
181 self.module_arg_spec = dict(
182 resource_group=dict(type='str', required=True),
183 name=dict(type='str', required=True),
184 location=dict(type='str'),
185 ip_addresses=dict(type='list', elements='str'),
186 state=dict(choices=['present', 'absent'], default='present', type='str')
187 )
188
189 self.results = dict(
190 changed=False,
191 state=dict()
192 )
193
194 self.resource_group = None
195 self.name = None
196 self.state = None
197 self.location = None
198 self.ip_addresses = None
199 self.tags = None
200
201 super(AzureRMIPGroup, self).__init__(self.module_arg_spec,
202 supports_check_mode=True)
203
204 def exec_module(self, **kwargs):
205 for key in list(self.module_arg_spec.keys()) + ['tags']:
206 setattr(self, key, kwargs[key])
207
208 changed = False
209 results = dict()
210 ip_group_old = None
211 ip_group_new = None
212
213 # retrieve resource group to make sure it exists
214 resource_group = self.get_resource_group(self.resource_group)
215 if not self.location:
216 # Set default location
217 self.location = resource_group.location
218
219 self.location = normalize_location_name(self.location)
220
221 try:
222 self.log('Fetching IP group {0}'.format(self.name))
223 ip_group_old = self.network_client.ip_groups.get(self.resource_group, self.name)
224 # serialize object into a dictionary
225 results = self.ipgroup_to_dict(ip_group_old)
226 if self.state == 'present':
227 changed = False
228 update_tags, results['tags'] = self.update_tags(results['tags'])
229 if update_tags:
230 changed = True
231 self.tags = results['tags']
232 update_ip_address = self.ip_addresses_changed(self.ip_addresses, results['ip_addresses'])
233 if update_ip_address:
234 changed = True
235 results['ip_addresses'] = self.ip_addresses
236 elif self.state == 'absent':
237 changed = True
238
239 except ResourceNotFoundError:
240 if self.state == 'present':
241 changed = True
242 else:
243 changed = False
244
245 self.results['changed'] = changed
246 self.results['state'] = results
247
248 if self.check_mode:
249 return self.results
250
251 if changed:
252 if self.state == 'present':
253 # create or update ip group
254 ip_group_new = \
255 self.network_models.IpGroup(location=self.location,
256 ip_addresses=self.ip_addresses)
257 if self.tags:
258 ip_group_new.tags = self.tags
259 self.results['state'] = self.create_or_update_ipgroup(ip_group_new)
260
261 elif self.state == 'absent':
262 # delete ip group
263 self.delete_ipgroup()
264 self.results['state'] = 'Deleted'
265
266 return self.results
267
268 def create_or_update_ipgroup(self, ip_group):
269 try:
270 # create ip group
271 response = self.network_client.ip_groups.begin_create_or_update(resource_group_name=self.resource_group,
272 ip_groups_name=self.name,
273 parameters=ip_group)
274 if isinstance(response, LROPoller):
275 response = self.get_poller_result(response)
276 except Exception as exc:
277 self.fail("Error creating or updating IP group {0} - {1}".format(self.name, str(exc)))
278 return self.ipgroup_to_dict(response)
279
280 def delete_ipgroup(self):
281 try:
282 # delete ip group
283 response = self.network_client.ip_groups.begin_delete(resource_group_name=self.resource_group,
284 ip_groups_name=self.name)
285 if isinstance(response, LROPoller):
286 response = self.get_poller_result(response)
287 except Exception as exc:
288 self.fail("Error deleting IP group {0} - {1}".format(self.name, str(exc)))
289 return response
290
291 def ip_addresses_changed(self, input_records, ip_group_records):
292 # comparing IP addresses list
293
294 input_set = set(input_records)
295 ip_group_set = set(ip_group_records)
296
297 changed = input_set != ip_group_set
298
299 return changed
300
301 def ipgroup_to_dict(self, ipgroup):
302 result = ipgroup.as_dict()
303 result['tags'] = ipgroup.tags
304 return result
305
306
307 def main():
308 AzureRMIPGroup()
309
310
311 if __name__ == '__main__':
312 main()