Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2019 Zim Kalinowski, (@zikalino)
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_subnet_info
14 version_added: "0.1.2"
15 short_description: Get Azure Subnet facts
16 description:
17 - Get facts of Azure Subnet.
18
19 options:
20 resource_group:
21 description:
22 - The name of the resource group.
23 required: True
24 type: str
25 virtual_network_name:
26 description:
27 - The name of the virtual network.
28 required: True
29 type: str
30 name:
31 description:
32 - The name of the subnet.
33 type: str
34
35 extends_documentation_fragment:
36 - azure.azcollection.azure
37
38 author:
39 - Zim Kalinowski (@zikalino)
40
41 '''
42
43 EXAMPLES = '''
44 - name: Get facts of specific subnet
45 azure_rm_subnet_info:
46 resource_group: myResourceGroup
47 virtual_network_name: myVirtualNetwork
48 name: mySubnet
49
50 - name: List facts for all subnets in virtual network
51 azure_rm_subnet_info:
52 resource_group: myResourceGroup
53 virtual_network_name: myVirtualNetwork
54 name: mySubnet
55 '''
56
57 RETURN = '''
58 subnets:
59 description:
60 - A list of dictionaries containing facts for subnet.
61 returned: always
62 type: complex
63 contains:
64 id:
65 description:
66 - Subnet resource ID.
67 returned: always
68 type: str
69 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/my
70 VirtualNetwork/subnets/mySubnet"
71 resource_group:
72 description:
73 - Name of resource group.
74 returned: always
75 type: str
76 sample: myResourceGroup
77 virtual_network_name:
78 description:
79 - Name of the containing virtual network.
80 returned: always
81 type: str
82 sample: myVirtualNetwork
83 name:
84 description:
85 - Name of the subnet.
86 returned: always
87 type: str
88 sample: mySubnet
89 address_prefix_cidr:
90 description:
91 - CIDR defining the IPv4 address space of the subnet.
92 returned: always
93 type: str
94 sample: "10.1.0.0/16"
95 address_prefixes_cidr:
96 description:
97 - CIDR defining the IPv4 and IPv6 address space of the subnet.
98 returned: always
99 type: list
100 sample: ["10.2.0.0/24", "fdda:e69b:1587:495e::/64"]
101 route_table:
102 description:
103 - Associated route table ID.
104 returned: always
105 type: str
106 sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/routeTables/myRouteTable
107 private_endpoint_network_policies:
108 description:
109 - C(Enabled) or C(Disabled) apply network policies on private endpoints in the subnet.
110 returned: always
111 type: str
112 sample: Enabled
113 private_link_service_network_policies:
114 description:
115 - C(Enabled) or C(Disabled) apply network policies on private link service in the subnet.
116 returned: always
117 type: str
118 sample: Disabled
119 security_group:
120 description:
121 - Associated security group ID.
122 returned: always
123 type: str
124 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkSecurityGr
125 oups/myNsg"
126 service_endpoints:
127 description:
128 - List of service endpoints.
129 type: list
130 returned: when available
131 contains:
132 service:
133 description:
134 - The type of the endpoint service.
135 returned: always
136 type: str
137 sample: Microsoft.Sql
138 locations:
139 description:
140 - A list of location names.
141 type: list
142 returned: always
143 sample: [ 'eastus', 'westus' ]
144 provisioning_state:
145 description:
146 - Provisioning state.
147 returned: always
148 type: str
149 sample: Succeeded
150 delegations:
151 description:
152 - Associated delegation of subnets
153 returned: always
154 type: list
155 contains:
156 name:
157 description:
158 - name of delegation
159 returned: when delegation is present
160 type: str
161 sample: "delegationname"
162 serviceName:
163 description:
164 - service associated to delegation
165 returned: when delegation is present
166 type: str
167 sample: "Microsoft.ContainerInstance/containerGroups"
168 actions:
169 description:
170 - list of actions associated with service of delegation
171 returned : when delegation is present
172 type: list
173 sample: ["Microsoft.Network/virtualNetworks/subnets/action"]
174 provisioning_state:
175 description:
176 - Provisioning state of delegation.
177 returned: when delegation is present
178 type: str
179 sample: Succeeded
180 provisioning_state:
181 description:
182 - Provisioning state.
183 returned: always
184 type: str
185 sample: Succeeded
186 nat_gateway:
187 description:
188 - ID of the associated NAT Gateway.
189 returned: when available
190 type: str
191 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/natGateways/myGw"
192 sharing_scope:
193 description:
194 - Set this property to Tenant to allow sharing subnet with other subscriptions in your AAD tenant.
195 type: str
196 returned: always
197 sample: None
198 service_endpoint_policies:
199 description:
200 - An array of service endpoint policies.
201 type: list
202 returned: always
203 sample: ["/subscriptions/xxx-xxx/resourceGroups/TestRG/providers/Microsoft.Network/serviceEndpointPolicies/testpolicy"]
204 default_outbound_access:
205 description:
206 - Set this property to false to disable default outbound connectivity for all VMs in the subnet.
207 type: bool
208 returned: always
209 sample: false
210 '''
211
212 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
213
214 try:
215 from azure.core.exceptions import ResourceNotFoundError
216 except ImportError:
217 # This is handled in azure_rm_common
218 pass
219
220
221 class AzureRMSubnetInfo(AzureRMModuleBase):
222 def __init__(self):
223 # define user inputs into argument
224 self.module_arg_spec = dict(
225 resource_group=dict(
226 type='str',
227 required=True
228 ),
229 virtual_network_name=dict(
230 type='str',
231 required=True
232 ),
233 name=dict(
234 type='str'
235 )
236 )
237 # store the results of the module operation
238 self.results = dict(
239 changed=False
240 )
241 self.resource_group = None
242 self.virtual_network_name = None
243 self.name = None
244 super(AzureRMSubnetInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False)
245
246 def exec_module(self, **kwargs):
247
248 for key in self.module_arg_spec:
249 setattr(self, key, kwargs[key])
250
251 if self.name is not None:
252 self.results['subnets'] = self.get()
253 else:
254 self.results['subnets'] = self.list()
255
256 return self.results
257
258 def get(self):
259 response = None
260 results = []
261 try:
262 response = self.network_client.subnets.get(resource_group_name=self.resource_group,
263 virtual_network_name=self.virtual_network_name,
264 subnet_name=self.name)
265 self.log("Response : {0}".format(response))
266 except ResourceNotFoundError as e:
267 self.fail('Could not get facts for Subnet.')
268
269 if response is not None:
270 results.append(self.format_response(response))
271
272 return results
273
274 def list(self):
275 response = None
276 results = []
277 try:
278 response = self.network_client.subnets.list(resource_group_name=self.resource_group,
279 virtual_network_name=self.virtual_network_name)
280 self.log("Response : {0}".format(response))
281 except ResourceNotFoundError as e:
282 self.fail('Could not get facts for Subnet.')
283
284 if response is not None:
285 for item in response:
286 results.append(self.format_response(item))
287
288 return results
289
290 def format_response(self, item):
291 d = item.as_dict()
292 d = {
293 'resource_group': self.resource_group,
294 'virtual_network_name': self.parse_resource_to_dict(d.get('id')).get('name'),
295 'name': d.get('name'),
296 'id': d.get('id'),
297 'address_prefix_cidr': d.get('address_prefix'),
298 'address_prefixes_cidr': d.get('address_prefixes'),
299 'route_table': d.get('route_table', {}).get('id'),
300 'security_group': d.get('network_security_group', {}).get('id'),
301 'provisioning_state': d.get('provisioning_state'),
302 'service_endpoints': d.get('service_endpoints'),
303 'private_endpoint_network_policies': d.get('private_endpoint_network_policies'),
304 'private_link_service_network_policies': d.get('private_link_service_network_policies'),
305 'delegations': d.get('delegations'),
306 'nat_gateway': d.get('nat_gateway', {}).get('id'),
307 'sharing_scope': d.get('sharing_scope'),
308 'default_outbound_access': d.get('default_outbound_access'),
309 'service_endpoint_policies': [item['id'] for item in d['service_endpoint_policies']] if d.get('service_endpoint_policies') else []
310 }
311
312 return d
313
314
315 def main():
316 AzureRMSubnetInfo()
317
318
319 if __name__ == '__main__':
320 main()