Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2023 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 DOCUMENTATION = '''
11 ---
12 module: azure_rm_vmssnetworkinterface_info
13
14 version_added: "1.15.0"
15
16 short_description: Get information about network interface in virtul machine scale
17
18 description:
19 - Get information about network interface in virtual machine scale set.
20
21 options:
22 name:
23 description:
24 - The name of the network interface.
25 - If configure I(name), you must set the parameters I(vm_index).
26 type: str
27 vmss_name:
28 description:
29 - The name of the virtual machine scale set.
30 type: str
31 required: True
32 vm_index:
33 description:
34 - The virtual machine index, such as I(vm_index=0).
35 type: str
36 resource_group:
37 description:
38 - Name of the resource group.
39 type: str
40 required: True
41
42 extends_documentation_fragment:
43 - azure.azcollection.azure
44
45 author:
46 - xuzhang3 (@xuzhang3)
47 - Fred-sun (@Fred-sun)
48
49 '''
50
51 EXAMPLES = '''
52 - name: Get information by the network name
53 azure_rm_vmssnetworkinterface_info:
54 resource_group: myResourceGroup
55 name: nic001
56 vmss_name: testVMSS
57 vm_index: 0
58
59 - name: Get all network interface information in virtual machine scale set
60 azure_rm_vmssnetworkinterface_info:
61 resource_group: myResourceGroup
62 vmss_name: testVMSS
63
64 - name: Get all network interface information in the same virtual machine index.
65 azure_rm_vmssnetworkinterface_info:
66 resource_group: myResourceGroup
67 vmss_name: testVMSS
68 vm_index: 1
69 '''
70
71 RETURN = '''
72 vmss_networkinterfaces:
73 description:
74 - List of network interface dicts. Each dict contains parameters can be passed to M(azure.azcollection.azure_rm_vmssnetworkinterface) module.
75 type: complex
76 returned: always
77 contains:
78 id:
79 description:
80 - Id of the network interface.
81 returned: always
82 type: str
83 sample: "/subscriptions/xxx-xxx/resourceGroups/RG/providers/Microsoft.Compute/virtualMachineScaleSets/fredvmss/virtualMachines/1/networkInterfaces/nic01"
84 resource_group:
85 description:
86 - Name of a resource group where the network interface exists.
87 returned: always
88 type: str
89 sample: RG
90 name:
91 description:
92 - Name of the network interface.
93 type: str
94 returned: always
95 sample: nic01
96 location:
97 description:
98 - Azure location.
99 type: str
100 returned: always
101 sample: eastus
102 virtual_network:
103 description:
104 - An existing virtual network with which the network interface will be associated.
105 - It is a dict which contains I(name) and I(resource_group) of the virtual network.
106 type: dict
107 returned: always
108 sample: {"name": "vnet01", "resource_group": "RG"}
109 subnet:
110 description:
111 - Name of an existing subnet within the specified virtual network.
112 type: str
113 returned: always
114 sample: default
115 tags:
116 description:
117 - Tags of the network interface.
118 type: dict
119 returned: always
120 sample: {"key1": "value1"}
121 ip_configurations:
122 description:
123 - List of IP configurations, if contains multiple configurations.
124 type: complex
125 returned: always
126 contains:
127 name:
128 description:
129 - Name of the IP configuration.
130 type: str
131 returned: always
132 sample: defaultIpConfiguration
133 private_ip_address:
134 description:
135 - Private IP address for the IP configuration.
136 type: str
137 returned: always
138 sample: 10.3.0.5
139 private_ip_allocation_method:
140 description:
141 - Private IP allocation method.
142 returned: always
143 type: str
144 sample: Dynamic
145 public_ip_address:
146 description:
147 - Name of the public IP address. None for disable IP address.
148 returned: always
149 type: str
150 sample: null
151 public_ip_allocation_method:
152 description:
153 - Public IP allocation method.
154 returned: always
155 type: str
156 sample: null
157 load_balancer_backend_address_pools:
158 description:
159 - List of existing load-balancer backend address pools associated with the network interface.
160 returned: always
161 type: str
162 sample: null
163 application_gateway_backend_address_pools:
164 description:
165 - List of existing application gateway backend address pools associated with the network interface.
166 returned: always
167 type: str
168 sample: null
169 primary:
170 description:
171 - Whether the IP configuration is the primary one in the list.
172 returned: always
173 type: bool
174 sample: True
175 application_security_groups:
176 description:
177 - List of Application security groups.
178 returned: always
179 type: str
180 sample: /subscriptions/<subsid>/resourceGroups/<rg>/providers/Microsoft.Network/applicationSecurityGroups/myASG
181 enable_accelerated_networking:
182 description:
183 - Specifies whether the network interface should be created with the accelerated networking feature or not.
184 type: bool
185 returned: always
186 sample: True
187 create_with_security_group:
188 description:
189 - Specifies whether a default security group should be be created with the NIC. Only applies when creating a new NIC.
190 type: bool
191 returned: always
192 sample: True
193 security_group:
194 description:
195 - A security group resource ID with which to associate the network interface.
196 type: str
197 returned: always
198 sample: /subscriptions/xxx-xxx/resourceGroups/RG/providers/Microsoft.Network/networkSecurityGroups/nic01
199 enable_ip_forwarding:
200 description:
201 - Whether to enable IP forwarding
202 type: bool
203 returned: always
204 sample: True
205 dns_servers:
206 description:
207 - Which DNS servers should the NIC lookup.
208 - List of IP addresses.
209 type: list
210 returned: always
211 sample: []
212 mac_address:
213 description:
214 - The MAC address of the network interface.
215 type: str
216 returned: always
217 sample: 00-0D-3A-17-EC-36
218 provisioning_state:
219 description:
220 - The provisioning state of the network interface.
221 type: str
222 returned: always
223 sample: Succeeded
224 dns_settings:
225 description:
226 - The DNS settings in network interface.
227 type: complex
228 returned: always
229 contains:
230 dns_servers:
231 description:
232 - List of DNS servers IP addresses.
233 returned: always
234 type: list
235 sample: []
236 applied_dns_servers:
237 description:
238 - If the VM that uses this NIC is part of an Availability Set, then this list will have the union of all DNS servers
239 from all NICs that are part of the Availability Set. This property is what is configured on each of those VMs.
240 returned: always
241 type: list
242 sample: []
243 internal_dns_name_label:
244 description:
245 - Relative DNS name for this NIC used for internal communications between VMs in the same virtual network.
246 returned: always
247 type: str
248 sample: null
249 internal_fqdn:
250 description:
251 - Fully qualified DNS name supporting internal communications between VMs in the same virtual network.
252 returned: always
253 type: str
254 sample: null
255 ''' # NOQA
256 try:
257 from azure.core.exceptions import ResourceNotFoundError
258 except Exception:
259 # This is handled in azure_rm_common
260 pass
261
262 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase, azure_id_to_dict
263
264
265 AZURE_OBJECT_CLASS = 'VMSSNetworkInterface'
266
267
268 def nic_to_dict(nic):
269 ip_configurations = [
270 dict(
271 name=config.name,
272 private_ip_address=config.private_ip_address,
273 private_ip_allocation_method=config.private_ip_allocation_method,
274 primary=config.primary if config.primary else False,
275 load_balancer_backend_address_pools=([item.id for item in config.load_balancer_backend_address_pools]
276 if config.load_balancer_backend_address_pools else None),
277 application_gateway_backend_address_pools=([item.id for item in config.application_gateway_backend_address_pools]
278 if config.application_gateway_backend_address_pools else None),
279 public_ip_address=config.public_ip_address.id if config.public_ip_address else None,
280 public_ip_allocation_method=config.public_ip_address.public_ip_allocation_method if config.public_ip_address else None,
281 application_security_groups=([asg.id for asg in config.application_security_groups]
282 if config.application_security_groups else None)
283 ) for config in nic.ip_configurations
284 ]
285 config = nic.ip_configurations[0] if len(nic.ip_configurations) > 0 else None
286 subnet_dict = azure_id_to_dict(config.subnet.id) if config and config.subnet else None
287 subnet = subnet_dict.get('subnets') if subnet_dict else None
288 virtual_network = dict(
289 resource_group=subnet_dict.get('resourceGroups'),
290 name=subnet_dict.get('virtualNetworks')) if subnet_dict else None
291 return dict(
292 id=nic.id,
293 resource_group=azure_id_to_dict(nic.id).get('resourceGroups'),
294 name=nic.name,
295 subnet=subnet,
296 virtual_network=virtual_network,
297 location=nic.location,
298 tags=nic.tags,
299 security_group=nic.network_security_group.id if nic.network_security_group else None,
300 dns_settings=dict(
301 dns_servers=nic.dns_settings.dns_servers,
302 applied_dns_servers=nic.dns_settings.applied_dns_servers,
303 internal_dns_name_label=nic.dns_settings.internal_dns_name_label,
304 internal_fqdn=nic.dns_settings.internal_fqdn
305 ),
306 ip_configurations=ip_configurations,
307 mac_address=nic.mac_address,
308 enable_ip_forwarding=nic.enable_ip_forwarding,
309 provisioning_state=nic.provisioning_state,
310 enable_accelerated_networking=nic.enable_accelerated_networking,
311 dns_servers=nic.dns_settings.dns_servers,
312 )
313
314
315 class AzureRMVMSSNetworkInterfaceInfo(AzureRMModuleBase):
316
317 def __init__(self):
318
319 self.module_arg_spec = dict(
320 name=dict(type='str'),
321 resource_group=dict(type='str', required=True),
322 vmss_name=dict(type='str', required=True),
323 vm_index=dict(type='str'),
324 )
325
326 self.results = dict(
327 changed=False,
328 )
329
330 self.name = None
331 self.resource_group = None
332 self.vmss_name = None
333 self.vm_index = None
334
335 super(AzureRMVMSSNetworkInterfaceInfo, self).__init__(self.module_arg_spec,
336 supports_check_mode=True,
337 supports_tags=False,
338 facts_module=True
339 )
340
341 def exec_module(self, **kwargs):
342 for key in self.module_arg_spec:
343 setattr(self, key, kwargs[key])
344
345 results = []
346 if self.name is not None:
347 if self.vm_index is not None:
348 results = self.get_item()
349 else:
350 self.fail("Parameter error: vm_index required when filtering by name.")
351 elif self.vm_index is not None:
352 results = self.list_vm_index()
353 else:
354 results = self.list_vmss()
355
356 self.results['vmss_networkinterfaces'] = self.to_dict_list(results)
357 return self.results
358
359 def get_item(self):
360 res = None
361 self.log("Get the specified network interface in a virtual machine scale set.")
362 try:
363 res = self.network_client.network_interfaces.get_virtual_machine_scale_set_network_interface(resource_group_name=self.resource_group,
364 virtual_machine_scale_set_name=self.vmss_name,
365 virtualmachine_index=self.vm_index,
366 network_interface_name=self.name)
367 except ResourceNotFoundError:
368 pass
369
370 return [res] if res is not None else []
371
372 def list_vm_index(self):
373 try:
374 res = self.network_client.network_interfaces.list_virtual_machine_scale_set_vm_network_interfaces(resource_group_name=self.resource_group,
375 virtual_machine_scale_set_name=self.vmss_name,
376 virtualmachine_index=self.vm_index)
377 return list(res)
378 except Exception as exc:
379 self.fail("Error listing by resource group {0} - {1}".format(self.resource_group, str(exc)))
380
381 def list_vmss(self):
382 self.log('List all')
383 try:
384 response = self.network_client.network_interfaces.list_virtual_machine_scale_set_network_interfaces(resource_group_name=self.resource_group,
385 virtual_machine_scale_set_name=self.vmss_name)
386 return list(response)
387 except Exception as exc:
388 self.fail("Error listing all - {0}".format(str(exc)))
389
390 def to_dict_list(self, raws):
391 return [nic_to_dict(item) for item in raws] if raws else []
392
393
394 def main():
395 AzureRMVMSSNetworkInterfaceInfo()
396
397
398 if __name__ == '__main__':
399 main()