Curate the virtual network facts module (#50311)

pull/50754/head
Yuwei Zhou 6 years ago committed by Yunge Zhu
parent abdcf2a776
commit 07c6ce2dbf

@ -290,34 +290,35 @@ class AzureRMVirtualNetwork(AzureRMModuleBase):
self.log("Create virtual network {0}".format(self.name)) self.log("Create virtual network {0}".format(self.name))
if not self.address_prefixes_cidr: if not self.address_prefixes_cidr:
self.fail('Parameter error: address_prefixes_cidr required when creating a virtual network') self.fail('Parameter error: address_prefixes_cidr required when creating a virtual network')
vnet = self.network_models.VirtualNetwork( vnet_param = self.network_models.VirtualNetwork(
location=self.location, location=self.location,
address_space=self.network_models.AddressSpace( address_space=self.network_models.AddressSpace(
address_prefixes=self.address_prefixes_cidr address_prefixes=self.address_prefixes_cidr
) )
) )
if self.dns_servers: if self.dns_servers:
vnet.dhcp_options = self.network_models.DhcpOptions( vnet_param.dhcp_options = self.network_models.DhcpOptions(
dns_servers=self.dns_servers dns_servers=self.dns_servers
) )
if self.tags: if self.tags:
vnet.tags = self.tags vnet_param.tags = self.tags
self.results['state'] = self.create_or_update_vnet(vnet) self.results['state'] = self.create_or_update_vnet(vnet_param)
else: else:
# update existing virtual network # update existing virtual network
self.log("Update virtual network {0}".format(self.name)) self.log("Update virtual network {0}".format(self.name))
vnet = self.network_models.VirtualNetwork( vnet_param = self.network_models.VirtualNetwork(
location=results['location'], location=results['location'],
address_space=self.network_models.AddressSpace( address_space=self.network_models.AddressSpace(
address_prefixes=results['address_prefixes'] address_prefixes=results['address_prefixes']
), ),
tags=results['tags'] tags=results['tags'],
subnets=vnet.subnets
) )
if results.get('dns_servers'): if results.get('dns_servers'):
vnet.dhcp_options = self.network_models.DhcpOptions( vnet_param.dhcp_options = self.network_models.DhcpOptions(
dns_servers=results['dns_servers'] dns_servers=results['dns_servers']
) )
self.results['state'] = self.create_or_update_vnet(vnet) self.results['state'] = self.create_or_update_vnet(vnet_param)
elif self.state == 'absent': elif self.state == 'absent':
self.delete_virtual_network() self.delete_virtual_network()
self.results['state']['status'] = 'Deleted' self.results['state']['status'] = 'Deleted'

@ -82,6 +82,79 @@ azure_virtualnetworks:
}, },
"type": "Microsoft.Network/virtualNetworks" "type": "Microsoft.Network/virtualNetworks"
}] }]
virtualnetworks:
description: List of virtual network dicts with same format as azure_rm_virtualnetwork module parameters.
returned: always
type: list
contains:
id:
description:
- Resource ID.
sample: /subscriptions/XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX/resourceGroups/Testing/providers/Microsoft.Network/virtualNetworks/vnet2001
type: str
address_prefixes:
description:
- List of IPv4 address ranges where each is formatted using CIDR notation.
sample: ["10.10.0.0/16"]
type: list
dns_servers:
description:
- Custom list of DNS servers.
type: list
sample: ["www.azure.com"]
location:
description:
- Valid azure location.
type: str
sample: eastus
tags:
description:
- Tags assigned to the resource. Dictionary of string:string pairs.
type: dict
sample: { "tag1": "abc" }
provisioning_state:
description:
- Provisioning state of the resource.
sample: Successed
type: str
name:
description:
- name of the virtual network.
type: str
sample: foo
subnets:
description:
- Subnets associate to this virtual network.
type: list
contains:
id:
description:
- Resource ID.
type: str
name:
description:
- Resource Name.
type: str
provisioning_state:
description:
- provision state of the Resource.
type: str
sample: Successed
address_prefix:
description:
- The address prefix for the subnet.
network_security_group:
description:
- Existing security group id with which to associate the subnet.
type: str
route_table:
description:
- The reference of the RouteTable resource.
type: str
service_endpoints:
description:
- An array of service endpoints.
type: list
''' '''
try: try:
@ -108,7 +181,8 @@ class AzureRMNetworkInterfaceFacts(AzureRMModuleBase):
self.results = dict( self.results = dict(
changed=False, changed=False,
ansible_facts=dict(azure_virtualnetworks=[]) ansible_facts=dict(azure_virtualnetworks=[]),
virtualnetworks=[]
) )
self.name = None self.name = None
@ -125,9 +199,14 @@ class AzureRMNetworkInterfaceFacts(AzureRMModuleBase):
setattr(self, key, kwargs[key]) setattr(self, key, kwargs[key])
if self.name is not None: if self.name is not None:
self.results['ansible_facts']['azure_virtualnetworks'] = self.get_item() results = self.get_item()
elif self.resource_group is not None:
results = self.list_resource_group()
else: else:
self.results['ansible_facts']['azure_virtualnetworks'] = self.list_items() results = self.list_items()
self.results['ansible_facts']['azure_virtualnetworks'] = self.serialize(results)
self.results['virtualnetworks'] = self.curated(results)
return self.results return self.results
@ -142,8 +221,7 @@ class AzureRMNetworkInterfaceFacts(AzureRMModuleBase):
pass pass
if item and self.has_tags(item.tags, self.tags): if item and self.has_tags(item.tags, self.tags):
results = [self.serialize_obj(item, AZURE_OBJECT_CLASS)] results = [item]
return results return results
def list_resource_group(self): def list_resource_group(self):
@ -156,7 +234,7 @@ class AzureRMNetworkInterfaceFacts(AzureRMModuleBase):
results = [] results = []
for item in response: for item in response:
if self.has_tags(item.tags, self.tags): if self.has_tags(item.tags, self.tags):
results.append(self.serialize_obj(item, AZURE_OBJECT_CLASS)) results.append(item)
return results return results
def list_items(self): def list_items(self):
@ -169,9 +247,50 @@ class AzureRMNetworkInterfaceFacts(AzureRMModuleBase):
results = [] results = []
for item in response: for item in response:
if self.has_tags(item.tags, self.tags): if self.has_tags(item.tags, self.tags):
results.append(self.serialize_obj(item, AZURE_OBJECT_CLASS)) results.append(item)
return results return results
def serialize(self, raws):
self.log("Serialize all items")
return [self.serialize_obj(item, AZURE_OBJECT_CLASS) for item in raws] if raws else []
def curated(self, raws):
self.log("Format all items")
return [self.virtualnetwork_to_dict(x) for x in raws] if raws else []
def virtualnetwork_to_dict(self, vnet):
results = dict(
id=vnet.id,
name=vnet.name,
location=vnet.location,
tags=vnet.tags,
provisioning_state=vnet.provisioning_state
)
if vnet.dhcp_options and len(vnet.dhcp_options.dns_servers) > 0:
results['dns_servers'] = []
for server in vnet.dhcp_options.dns_servers:
results['dns_servers'].append(server)
if vnet.address_space and len(vnet.address_space.address_prefixes) > 0:
results['address_prefixes'] = []
for space in vnet.address_space.address_prefixes:
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]
return results
def subnet_to_dict(self, subnet):
result = dict(
id=subnet.id,
name=subnet.name,
provisioning_state=subnet.provisioning_state,
address_prefix=subnet.address_prefix,
network_security_group=subnet.network_security_group.id if subnet.network_security_group else None,
route_table=subnet.route_table.id if subnet.route_table else None
)
if subnet.service_endpoints:
result['service_endpoints'] = [{'service': item.service, 'locations': item.locations} for item in subnet.service_endpoints]
return result
def main(): def main():
AzureRMNetworkInterfaceFacts() AzureRMNetworkInterfaceFacts()

@ -41,15 +41,28 @@
- "output.state.tags.delete == 'on-exit'" - "output.state.tags.delete == 'on-exit'"
- "output.state.tags | length == 2" - "output.state.tags | length == 2"
- name: Attach a subnet
azure_rm_subnet:
resource_group: "{{ resource_group }}"
name: "{{ vnetname }}"
virtual_network_name: "{{ vnetname }}"
address_prefix_cidr: "10.1.0.0/24"
- name: Gather facts by name, tags - name: Gather facts by name, tags
azure_rm_virtualnetwork_facts: azure_rm_virtualnetwork_facts:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: "{{ vnetname }}" name: "{{ vnetname }}"
tags: tags:
- testing - testing
register: facts
- assert: - assert:
that: "azure_virtualnetworks | length == 1" that:
- "azure_virtualnetworks | length == 1"
- "facts.virtualnetworks | length == 1"
- "facts.virtualnetworks[0].dns_servers | length == 2"
- "facts.virtualnetworks[0].address_prefixes | length == 2"
- "facts.virtualnetworks[0].subnets | length == 1"
- name: Gather facts by resource group, tags - name: Gather facts by resource group, tags
azure_rm_virtualnetwork_facts: azure_rm_virtualnetwork_facts:
@ -153,9 +166,13 @@
azure_rm_virtualnetwork_facts: azure_rm_virtualnetwork_facts:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: "{{ vnetname }}" name: "{{ vnetname }}"
register: facts
- assert: - assert:
that: "azure_virtualnetworks | length == 1" that:
- azure_virtualnetworks | length == 1
- facts.virtualnetworks | length == 1
- "facts.virtualnetworks[0].subnets | length == 1"
- name: Delete virtual network - name: Delete virtual network
azure_rm_virtualnetwork: azure_rm_virtualnetwork:

Loading…
Cancel
Save