Skip to content

Commit

Permalink
azure_rm_virualnetwork_info - List usage of the subnets within a vi…
Browse files Browse the repository at this point in the history
…rtual network (#1673)

* List usage of the subnets within a virtual network

* Modify exception code

* small change
  • Loading branch information
Fred-sun authored Aug 15, 2024
1 parent 8985b7a commit 048407c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 40 additions & 1 deletion plugins/modules/azure_rm_virtualnetwork_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
type: list
elements: str
list_usage:
description:
- Whether lists usage stats. I(list_usage=true) to lists usage stats.
type: bool
default: False
extends_documentation_fragment:
- azure.azcollection.azure
Expand Down Expand Up @@ -199,10 +204,27 @@
"service": "Microsoft.Storage"
}
]
usages:
description:
- Lists usage stats.
type: list
returned: always
sample: [
{
"current_value": 0.0,
"id": "/subscriptions/xxx/resourceGroups/testRG/providers/Microsoft.Network/virtualNetworks/vnet/subnets/default",
"limit": 251.0,
"name": {
"localized_value": "Subnet size and usage",
"value": "SubnetSpace"
}
}
]
'''

try:
from azure.core.exceptions import ResourceNotFoundError
from azure.core.exceptions import ResourceNotFoundError, HttpResponseError
from azure.mgmt.core.tools import parse_resource_id
except Exception:
# This is handled in azure_rm_common
pass
Expand All @@ -221,6 +243,7 @@ def __init__(self):
name=dict(type='str'),
resource_group=dict(type='str'),
tags=dict(type='list', elements='str'),
list_usage=dict(type='bool', default=False),
)

self.results = dict(
Expand All @@ -233,6 +256,7 @@ def __init__(self):
self.name = None
self.resource_group = None
self.tags = None
self.list_usage = None

super(AzureRMNetworkInterfaceInfo, self).__init__(self.module_arg_spec,
supports_check_mode=True,
Expand Down Expand Up @@ -316,6 +340,7 @@ def virtualnetwork_to_dict(self, vnet):
results = dict(
id=vnet.id,
name=vnet.name,
resource_group=parse_resource_id(vnet.id).get('resource_group'),
location=vnet.location,
tags=vnet.tags,
provisioning_state=vnet.provisioning_state,
Expand All @@ -331,6 +356,9 @@ def virtualnetwork_to_dict(self, vnet):
results['address_prefixes'].append(space)
if vnet.subnets and len(vnet.subnets) > 0:
results['subnets'] = [self.subnet_to_dict(x) for x in vnet.subnets]
if self.list_usage:
results['usages'] = self.list_usages(results['resource_group'], results['name'])

return results

def subnet_to_dict(self, subnet):
Expand All @@ -347,6 +375,17 @@ def subnet_to_dict(self, subnet):
result['service_endpoints'] = [{'service': item.service, 'locations': item.locations} for item in subnet.service_endpoints]
return result

def list_usages(self, resource_group, name):
results = []
try:
response = self.network_client.virtual_networks.list_usage(resource_group, name)
except HttpResponseError as ec:
self.fail("Faild to list usage status")

for item in response:
results.append(item.as_dict())
return results


def main():
AzureRMNetworkInterfaceInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
azure_rm_virtualnetwork_info:
resource_group: "{{ resource_group }}"
name: "{{ vnetname }}"
list_usage: true
tags:
- testing
register: facts
Expand All @@ -69,6 +70,7 @@
- "facts.virtualnetworks[0].address_prefixes | length == 3"
- "facts.virtualnetworks[0].flow_timeout_in_minutes == 8"
- "facts.virtualnetworks[0].subnets | length == 1"
- "facts.virtualnetworks[0].usages | length == 1"

- name: Gather facts by resource group, tags
azure_rm_virtualnetwork_info:
Expand Down

0 comments on commit 048407c

Please sign in to comment.