| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
--- |
| 14 |
module: azure_rm_virtualnetwork_info |
| 15 |
|
| 16 |
version_added: "0.0.1" |
| 17 |
|
| 18 |
short_description: Get virtual network facts |
| 19 |
|
| 20 |
description: |
| 21 |
- Get facts for a specific virtual network or all virtual networks within a resource group. |
| 22 |
|
| 23 |
options: |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Only show results for a specific virtual network. |
| 27 |
type: str |
| 28 |
resource_group: |
| 29 |
description: |
| 30 |
- Limit results by resource group. Required when filtering by name. |
| 31 |
type: str |
| 32 |
tags: |
| 33 |
description: |
| 34 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 35 |
type: list |
| 36 |
elements: str |
| 37 |
list_usage: |
| 38 |
description: |
| 39 |
- Whether lists usage stats. I(list_usage=true) to lists usage stats. |
| 40 |
type: bool |
| 41 |
default: False |
| 42 |
|
| 43 |
extends_documentation_fragment: |
| 44 |
- azure.azcollection.azure |
| 45 |
|
| 46 |
author: |
| 47 |
- Chris Houseknecht (@chouseknecht) |
| 48 |
- Matt Davis (@nitzmahone) |
| 49 |
|
| 50 |
''' |
| 51 |
|
| 52 |
EXAMPLES = ''' |
| 53 |
- name: Get facts for one virtual network |
| 54 |
azure_rm_virtualnetwork_info: |
| 55 |
resource_group: myResourceGroup |
| 56 |
name: secgroup001 |
| 57 |
|
| 58 |
- name: Get facts for all virtual networks |
| 59 |
azure_rm_virtualnetwork_info: |
| 60 |
resource_group: myResourceGroup |
| 61 |
|
| 62 |
- name: Get facts by tags |
| 63 |
azure_rm_virtualnetwork_info: |
| 64 |
tags: |
| 65 |
- testing |
| 66 |
''' |
| 67 |
RETURN = ''' |
| 68 |
azure_virtualnetworks: |
| 69 |
description: |
| 70 |
- List of virtual network dicts. |
| 71 |
returned: always |
| 72 |
type: list |
| 73 |
example: [{ |
| 74 |
"etag": 'W/"532ba1be-ae71-40f2-9232-3b1d9cf5e37e"', |
| 75 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/virtualNetworks/vnet2001", |
| 76 |
"location": "eastus2", |
| 77 |
"name": "vnet2001", |
| 78 |
"properties": { |
| 79 |
"addressSpace": { |
| 80 |
"addressPrefixes": [ |
| 81 |
"10.10.0.0/16" |
| 82 |
] |
| 83 |
}, |
| 84 |
"provisioningState": "Succeeded", |
| 85 |
"resourceGuid": "a7ba285f-f7e7-4e17-992a-de4d39f28612", |
| 86 |
"subnets": [] |
| 87 |
}, |
| 88 |
"type": "Microsoft.Network/virtualNetworks" |
| 89 |
}] |
| 90 |
virtualnetworks: |
| 91 |
description: |
| 92 |
- List of virtual network dicts with same format as M(azure.azcollection.azure_rm_virtualnetwork) module parameters. |
| 93 |
returned: always |
| 94 |
type: complex |
| 95 |
contains: |
| 96 |
id: |
| 97 |
description: |
| 98 |
- Resource ID of the virtual network. |
| 99 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/vnet2001 |
| 100 |
returned: always |
| 101 |
type: str |
| 102 |
address_prefixes: |
| 103 |
description: |
| 104 |
- List of IPv4 address ranges where each is formatted using CIDR notation. |
| 105 |
sample: ["10.10.0.0/16"] |
| 106 |
returned: always |
| 107 |
type: list |
| 108 |
dns_servers: |
| 109 |
description: |
| 110 |
- Custom list of DNS servers. |
| 111 |
returned: always |
| 112 |
type: list |
| 113 |
sample: ["www.azure.com"] |
| 114 |
location: |
| 115 |
description: |
| 116 |
- Valid Azure location. |
| 117 |
returned: always |
| 118 |
type: str |
| 119 |
sample: eastus |
| 120 |
tags: |
| 121 |
description: |
| 122 |
- Tags assigned to the resource. Dictionary of string:string pairs. |
| 123 |
returned: always |
| 124 |
type: dict |
| 125 |
sample: { "tag1": "abc" } |
| 126 |
provisioning_state: |
| 127 |
description: |
| 128 |
- Provisioning state of the resource. |
| 129 |
returned: always |
| 130 |
sample: Succeeded |
| 131 |
type: str |
| 132 |
flow_timeout_in_minutes: |
| 133 |
description: |
| 134 |
- The FlowTimeout value (in minutes) for the Virtual Network. |
| 135 |
type: int |
| 136 |
returned: always |
| 137 |
sample: 8 |
| 138 |
name: |
| 139 |
description: |
| 140 |
- Name of the virtual network. |
| 141 |
returned: always |
| 142 |
type: str |
| 143 |
sample: foo |
| 144 |
subnets: |
| 145 |
description: |
| 146 |
- Subnets associated with the virtual network. |
| 147 |
returned: always |
| 148 |
type: list |
| 149 |
contains: |
| 150 |
id: |
| 151 |
description: |
| 152 |
- Resource ID of the subnet. |
| 153 |
returned: always |
| 154 |
type: str |
| 155 |
sample: "/subscriptions/f64d4ee8-be94-457d-ba26-3fa6b6506cef/resourceGroups/v-xisuRG/providers/ |
| 156 |
Microsoft.Network/virtualNetworks/vnetb57dc95232/subnets/vnetb57dc95232" |
| 157 |
name: |
| 158 |
description: |
| 159 |
- Name of the subnet. |
| 160 |
returned: always |
| 161 |
type: str |
| 162 |
sample: vnetb57dc95232 |
| 163 |
provisioning_state: |
| 164 |
description: |
| 165 |
- Provisioning state of the subnet. |
| 166 |
returned: always |
| 167 |
type: str |
| 168 |
sample: Succeeded |
| 169 |
address_prefix: |
| 170 |
description: |
| 171 |
- The address prefix for the subnet. |
| 172 |
returned: always |
| 173 |
type: str |
| 174 |
sample: '10.1.0.0/16' |
| 175 |
address_prefixes: |
| 176 |
description: |
| 177 |
- Both IPv4 and IPv6 address prefixes for the subnet, will return null if only an IPv4 set. |
| 178 |
returned: always |
| 179 |
type: list |
| 180 |
sample: ["10.1.0.0/16", "fdda:e69b:2547:485e::/64"] |
| 181 |
network_security_group: |
| 182 |
description: |
| 183 |
- Existing security group ID with which to associate the subnet. |
| 184 |
returned: always |
| 185 |
type: str |
| 186 |
sample: null |
| 187 |
route_table: |
| 188 |
description: |
| 189 |
- The reference of the RouteTable resource. |
| 190 |
returned: always |
| 191 |
type: str |
| 192 |
sample: null |
| 193 |
service_endpoints: |
| 194 |
description: |
| 195 |
- An array of service endpoints. |
| 196 |
returned: always |
| 197 |
type: list |
| 198 |
sample: [ |
| 199 |
{ |
| 200 |
"locations": [ |
| 201 |
"southeastasia", |
| 202 |
"eastasia" |
| 203 |
], |
| 204 |
"service": "Microsoft.Storage" |
| 205 |
} |
| 206 |
] |
| 207 |
usages: |
| 208 |
description: |
| 209 |
- Lists usage stats. |
| 210 |
type: list |
| 211 |
returned: always |
| 212 |
sample: [ |
| 213 |
{ |
| 214 |
"current_value": 0.0, |
| 215 |
"id": "/subscriptions/xxx/resourceGroups/testRG/providers/Microsoft.Network/virtualNetworks/vnet/subnets/default", |
| 216 |
"limit": 251.0, |
| 217 |
"name": { |
| 218 |
"localized_value": "Subnet size and usage", |
| 219 |
"value": "SubnetSpace" |
| 220 |
} |
| 221 |
} |
| 222 |
] |
| 223 |
''' |
| 224 |
|
| 225 |
try: |
| 226 |
from azure.core.exceptions import ResourceNotFoundError, HttpResponseError |
| 227 |
from azure.mgmt.core.tools import parse_resource_id |
| 228 |
except Exception: |
| 229 |
|
| 230 |
pass |
| 231 |
|
| 232 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 233 |
|
| 234 |
|
| 235 |
AZURE_OBJECT_CLASS = 'VirtualNetwork' |
| 236 |
|
| 237 |
|
| 238 |
class AzureRMNetworkInterfaceInfo(AzureRMModuleBase): |
| 239 |
|
| 240 |
def __init__(self): |
| 241 |
|
| 242 |
self.module_arg_spec = dict( |
| 243 |
name=dict(type='str'), |
| 244 |
resource_group=dict(type='str'), |
| 245 |
tags=dict(type='list', elements='str'), |
| 246 |
list_usage=dict(type='bool', default=False), |
| 247 |
) |
| 248 |
|
| 249 |
self.results = dict( |
| 250 |
changed=False, |
| 251 |
virtualnetworks=[] |
| 252 |
) |
| 253 |
|
| 254 |
self.required_by = {'name': 'resource_group'} |
| 255 |
|
| 256 |
self.name = None |
| 257 |
self.resource_group = None |
| 258 |
self.tags = None |
| 259 |
self.list_usage = None |
| 260 |
|
| 261 |
super(AzureRMNetworkInterfaceInfo, self).__init__(self.module_arg_spec, |
| 262 |
supports_check_mode=True, |
| 263 |
supports_tags=False, |
| 264 |
facts_module=True, |
| 265 |
required_by=self.required_by) |
| 266 |
|
| 267 |
def exec_module(self, **kwargs): |
| 268 |
|
| 269 |
for key in self.module_arg_spec: |
| 270 |
setattr(self, key, kwargs[key]) |
| 271 |
|
| 272 |
if self.name is not None: |
| 273 |
results = self.get_item() |
| 274 |
elif self.resource_group is not None: |
| 275 |
results = self.list_resource_group() |
| 276 |
else: |
| 277 |
results = self.list_items() |
| 278 |
|
| 279 |
self.results['virtualnetworks'] = self.curated(results) |
| 280 |
|
| 281 |
return self.results |
| 282 |
|
| 283 |
def get_item(self): |
| 284 |
self.log('Get properties for {0}'.format(self.name)) |
| 285 |
item = None |
| 286 |
results = [] |
| 287 |
|
| 288 |
try: |
| 289 |
item = self.network_client.virtual_networks.get(resource_group_name=self.resource_group, |
| 290 |
virtual_network_name=self.name) |
| 291 |
except ResourceNotFoundError: |
| 292 |
pass |
| 293 |
|
| 294 |
if item and self.has_tags(item.tags, self.tags): |
| 295 |
results = [item] |
| 296 |
return results |
| 297 |
|
| 298 |
def list_resource_group(self): |
| 299 |
self.log('List items for resource group') |
| 300 |
try: |
| 301 |
response = self.network_client.virtual_networks.list(self.resource_group) |
| 302 |
except ResourceNotFoundError as exc: |
| 303 |
self.fail("Failed to list for resource group {0} - {1}".format(self.resource_group, str(exc))) |
| 304 |
|
| 305 |
results = [] |
| 306 |
for item in response: |
| 307 |
if self.has_tags(item.tags, self.tags): |
| 308 |
results.append(item) |
| 309 |
return results |
| 310 |
|
| 311 |
def list_items(self): |
| 312 |
self.log('List all for items') |
| 313 |
try: |
| 314 |
response = self.network_client.virtual_networks.list_all() |
| 315 |
except ResourceNotFoundError as exc: |
| 316 |
self.fail("Failed to list all items - {0}".format(str(exc))) |
| 317 |
|
| 318 |
results = [] |
| 319 |
for item in response: |
| 320 |
if self.has_tags(item.tags, self.tags): |
| 321 |
results.append(item) |
| 322 |
return results |
| 323 |
|
| 324 |
def serialize(self, raws): |
| 325 |
self.log("Serialize all items") |
| 326 |
return [self.serialize_obj(item, AZURE_OBJECT_CLASS) for item in raws] if raws else [] |
| 327 |
|
| 328 |
def curated(self, raws): |
| 329 |
self.log("Format all items") |
| 330 |
return [self.virtualnetwork_to_dict(x) for x in raws] if raws else [] |
| 331 |
|
| 332 |
def virtualnetwork_to_dict(self, vnet): |
| 333 |
results = dict( |
| 334 |
id=vnet.id, |
| 335 |
name=vnet.name, |
| 336 |
resource_group=parse_resource_id(vnet.id).get('resource_group'), |
| 337 |
location=vnet.location, |
| 338 |
tags=vnet.tags, |
| 339 |
provisioning_state=vnet.provisioning_state, |
| 340 |
flow_timeout_in_minutes=vnet.flow_timeout_in_minutes |
| 341 |
) |
| 342 |
if vnet.dhcp_options and len(vnet.dhcp_options.dns_servers) > 0: |
| 343 |
results['dns_servers'] = [] |
| 344 |
for server in vnet.dhcp_options.dns_servers: |
| 345 |
results['dns_servers'].append(server) |
| 346 |
if vnet.address_space and len(vnet.address_space.address_prefixes) > 0: |
| 347 |
results['address_prefixes'] = [] |
| 348 |
for space in vnet.address_space.address_prefixes: |
| 349 |
results['address_prefixes'].append(space) |
| 350 |
if vnet.subnets and len(vnet.subnets) > 0: |
| 351 |
results['subnets'] = [self.subnet_to_dict(x) for x in vnet.subnets] |
| 352 |
if self.list_usage: |
| 353 |
results['usages'] = self.list_usages(results['resource_group'], results['name']) |
| 354 |
|
| 355 |
return results |
| 356 |
|
| 357 |
def subnet_to_dict(self, subnet): |
| 358 |
result = dict( |
| 359 |
id=subnet.id, |
| 360 |
name=subnet.name, |
| 361 |
provisioning_state=subnet.provisioning_state, |
| 362 |
address_prefix=subnet.address_prefix, |
| 363 |
address_prefixes=subnet.address_prefixes if subnet.address_prefixes else None, |
| 364 |
network_security_group=subnet.network_security_group.id if subnet.network_security_group else None, |
| 365 |
route_table=subnet.route_table.id if subnet.route_table else None |
| 366 |
) |
| 367 |
if subnet.service_endpoints: |
| 368 |
result['service_endpoints'] = [{'service': item.service, 'locations': item.locations} for item in subnet.service_endpoints] |
| 369 |
return result |
| 370 |
|
| 371 |
def list_usages(self, resource_group, name): |
| 372 |
results = [] |
| 373 |
try: |
| 374 |
response = self.network_client.virtual_networks.list_usage(resource_group, name) |
| 375 |
except HttpResponseError as ec: |
| 376 |
self.fail("Faild to list usage status") |
| 377 |
|
| 378 |
for item in response: |
| 379 |
results.append(item.as_dict()) |
| 380 |
return results |
| 381 |
|
| 382 |
|
| 383 |
def main(): |
| 384 |
AzureRMNetworkInterfaceInfo() |
| 385 |
|
| 386 |
|
| 387 |
if __name__ == '__main__': |
| 388 |
main() |