Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024 xuzhang3 (@xuzhang3), Fred-sun (@Fred-sun)
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
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_virtualnetworkgatewaynatrule
14
15 version_added: "2.4.0"
16
17 short_description: Gets or list the specified local network gateway in a resource group
18
19 description:
20 - Gets or list the specified local network gateway in a resource group.
21
22 options:
23 resource_group:
24 description:
25 - The local network gateway's resource group.
26 type: str
27 required: true
28 virtual_network_gateway_name:
29 description:
30 - The name of the local network gateway.
31 type: str
32 required: true
33 name:
34 description:
35 - he name of the resource that is unique within a resource group.
36 type: str
37 required: true
38 type_properties_type:
39 description:
40 - The type of NAT rule for VPN NAT.
41 type: str
42 choices:
43 - Dynamic
44 - Static
45 mode:
46 description:
47 - The Source NAT direction of a VPN NAT.
48 type: str
49 choices:
50 - EgressSnat
51 - IngressSnat
52 ip_configuration_id:
53 description:
54 - The IP Configuration ID this NAT rule applies to.
55 type: str
56 external_mappings:
57 description:
58 - The private IP address external mapping for NAT.
59 type: list
60 elements: str
61 internal_mappings:
62 description:
63 - The private IP address internal mapping for NAT.
64 type: list
65 elements: str
66 state:
67 description:
68 - Use C(present) to create or update the virtual network gateway nat rule.
69 - Use C(absent) to delete the nat rule.
70 type: str
71 default: present
72 choices:
73 - absent
74 - present
75 extends_documentation_fragment:
76 - azure.azcollection.azure
77 - azure.azcollection.azure_tags
78
79 author:
80 - xuzhang3 (@xuzhang3)
81 - Fred Sun (@Fred-sun)
82 '''
83
84 EXAMPLES = '''
85 - name: Create a virtual netowrk nat rule
86 azure_rm_virtualnetworkgatewaynatrule:
87 resource_group: "{{ resource_group }}"
88 virtual_network_gateway_name: "{{ vngname }}"
89 name: "{{ natrulename }}"
90 type_properties_type: Dynamic
91 ip_configuration_id: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Network/virtualNetworkGateways/testRG/ipConfigurations/ipconfig"
92 mode: EgressSnat
93 internal_mappings:
94 - 10.1.0.0/24
95 external_mappings:
96 - 192.168.1.0/24
97
98 - name: Delete the virtual netowrk nat rule
99 azure_rm_virtualnetworkgatewaynatrule:
100 resource_group: "{{ resource_group }}"
101 virtual_network_gateway_name: "{{ vngname }}"
102 name: "{{ natrulename }}"
103 state: absent
104 '''
105
106 RETURN = '''
107 state:
108 description:
109 - Gets the nat rules for a particular virtual network gateway
110 returned: always
111 type: complex
112 contains:
113 id:
114 description:
115 - The resource ID.
116 type: str
117 returned: always
118 sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Network/virtualNetworkGateways/vng01/natRules/natrule"
119 internal_mappings:
120 description:
121 - The private IP address internal mapping for NAT.
122 type: list
123 returned: always
124 sample: ["10.1.0.0/24"]
125 external_mappings:
126 description:
127 - The private IP address external mapping for NAT.
128 type: list
129 returned: always
130 sample: ["192.168.1.0/24"]
131 ip_configuration_id:
132 description:
133 - he IP Configuration ID this NAT rule applies to.
134 type: str
135 returned: always
136 sample: "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualNetworkGateways/gateway1/ipConfigurations/default"
137 type_properties_type:
138 description:
139 - The type of NAT rule for VPN NAT.
140 type: str
141 returned: always
142 sample: Static
143 mode:
144 description:
145 - The Source NAT direction of a VPN NAT.
146 type: str
147 returned: always
148 sample: EgressSnat
149 name:
150 description:
151 - The resource name.
152 type: str
153 returned: always
154 sample: natrule_name
155 resource_group:
156 description:
157 - The resource group name.
158 type: str
159 returned: always
160 sample: testRG
161 etag:
162 description:
163 - A unique read-only string that changes whenever the resource is updated.
164 type: str
165 returned: always
166 sample: b5a32693-2e75-49e0-9137-ded19db658d6
167 provisioning_state:
168 description:
169 - The provisioning state of the nat rule resource.
170 type: str
171 returned: always
172 sample: Succeeded
173 type:
174 description:
175 - The resource type.
176 type: str
177 returned: always
178 sample: Microsoft.Network/virtualNetworkGateways/natRules
179 '''
180
181 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
182
183 try:
184 from azure.core.exceptions import HttpResponseError
185 from azure.core.polling import LROPoller
186 except Exception:
187 # handled in azure_rm_common
188 pass
189
190
191 class AzureRMVirtualNetworkNatGateway(AzureRMModuleBase):
192 """Utility class to get Azure Kubernetes Service Credentials facts"""
193
194 def __init__(self):
195
196 self.module_arg_spec = dict(
197 name=dict(type='str', required=True),
198 resource_group=dict(type='str', required=True),
199 virtual_network_gateway_name=dict(type='str', required=True),
200 type_properties_type=dict(type='str', choices=['Dynamic', 'Static']),
201 mode=dict(type='str', choices=['EgressSnat', 'IngressSnat']),
202 ip_configuration_id=dict(type='str'),
203 external_mappings=dict(type='list', elements='str'),
204 internal_mappings=dict(type='list', elements='str'),
205 state=dict(type='str', default='present', choices=['present', 'absent'])
206 )
207
208 self.type_properties_type = None
209 self.mode = None
210 self.ip_configuration_id = None
211 self.external_mappings = None
212 self.internal_mappings = None
213
214 self.results = dict(
215 changed=False,
216 state=[],
217 )
218 required_if = [('type_properties_type', 'Dynamic', ['ip_configuration_id'])]
219
220 super(AzureRMVirtualNetworkNatGateway, self).__init__(derived_arg_spec=self.module_arg_spec,
221 supports_check_mode=True,
222 required_if=required_if,
223 supports_tags=True,
224 facts_module=False)
225
226 def exec_module(self, **kwargs):
227
228 for key in list(self.module_arg_spec):
229 setattr(self, key, kwargs[key])
230
231 old_response = self.get_nat_rule()
232 changed = False
233 response = None
234
235 if self.state == 'present':
236 if old_response is not None:
237 if self.type_properties_type is not None and self.type_properties_type != old_response['type_properties_type']:
238 self.fail("NAT type_properties_type cannot be changed.")
239 else:
240 self.type_properties_type = old_response['type_properties_type']
241 if self.mode is not None and self.mode != old_response['mode']:
242 self.fail("NAT mode cannot be changed.")
243 else:
244 self.mode = old_response['mode']
245 if self.ip_configuration_id is not None and self.ip_configuration_id != old_response['ip_configuration_id']:
246 changed = True
247 else:
248 self.ip_configuration_id = old_response['ip_configuration_id']
249 if self.internal_mappings is not None and old_response['internal_mappings'] != self.internal_mappings:
250 changed = True
251 else:
252 self.internal_mappings = old_response['internal_mappings']
253
254 if self.external_mappings is not None and self.external_mappings != old_response['external_mappings']:
255 changed = True
256 else:
257 self.external_mappings = old_response['external_mappings']
258 else:
259 changed = True
260
261 internal_mappings = None
262 external_mappings = None
263 if self.internal_mappings is not None:
264 internal_mappings = [self.network_models.VpnNatRuleMapping(address_space=item) for item in self.internal_mappings]
265 if self.external_mappings is not None:
266 external_mappings = [self.network_models.VpnNatRuleMapping(address_space=item) for item in self.external_mappings]
267
268 natrule_resource = self.network_models.VirtualNetworkGatewayNatRule(name=self.name,
269 type_properties_type=self.type_properties_type,
270 mode=self.mode,
271 ip_configuration_id=self.ip_configuration_id,
272 internal_mappings=internal_mappings,
273 external_mappings=external_mappings)
274 if changed:
275 if not self.check_mode:
276 response = self.create_or_update_local_network_gateway(natrule_resource)
277 else:
278 if not self.check_mode:
279 if old_response is not None:
280 self.delete_local_network_gateway()
281 changed = True
282 response = None
283 else:
284 changed = True
285
286 if response is None:
287 response = old_response
288 self.results['state'] = response
289 self.results['changed'] = changed
290 return self.results
291
292 def get_nat_rule(self):
293 """Gets the specified nat rule"""
294 response = None
295 try:
296 response = self.network_client.virtual_network_gateway_nat_rules.get(self.resource_group, self.virtual_network_gateway_name, self.name)
297 except HttpResponseError as ec:
298 self.log("Gets the specified local network gateway in a resource group Failed, Exception as {0}".format(ec))
299 return None
300 return self.format_response(response)
301
302 def create_or_update_local_network_gateway(self, body):
303 """Create or Update local network gateway"""
304 response = None
305 try:
306 response = self.network_client.virtual_network_gateway_nat_rules.begin_create_or_update(self.resource_group,
307 self.virtual_network_gateway_name, self.name, body)
308 if isinstance(response, LROPoller):
309 response = self.get_poller_result(response)
310 except HttpResponseError as ec:
311 self.fail("Create or Updated a local network gateway in a resource group Failed, Exception as {0}".format(ec))
312
313 return self.format_response(response)
314
315 def delete_local_network_gateway(self):
316 """Deletes the specified local network gateway"""
317 try:
318 self.network_client.virtual_network_gateway_nat_rules.begin_delete(self.resource_group, self.virtual_network_gateway_name, self.name)
319 except HttpResponseError as ec:
320 self.fail("Deletes the specified nat rule, Exception as {0}".format(ec))
321 return None
322
323 def format_response(self, item):
324 result = dict(
325 resource_group=self.resource_group,
326 id=item.id,
327 name=item.name,
328 type=item.type,
329 etag=item.etag,
330 provisioning_state=item.provisioning_state,
331 type_properties_type=item.type_properties_type,
332 mode=item.mode,
333 internal_mappings=list(),
334 external_mappings=list(),
335 ip_configuration_id=item.ip_configuration_id
336 )
337
338 if item.internal_mappings is not None:
339 for value in item.internal_mappings:
340 result['internal_mappings'].append(value.address_space)
341 if item.external_mappings is not None:
342 for value in item.external_mappings:
343 result['external_mappings'].append(value.address_space)
344 return result
345
346
347 def main():
348 """Main module execution code path"""
349
350 AzureRMVirtualNetworkNatGateway()
351
352
353 if __name__ == '__main__':
354 main()