1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

azure subnet's properties should not be changed if parameter set to None (route table, nsg...) (#54019)

This commit is contained in:
Yuwei Zhou 2019-03-22 14:27:12 +08:00 committed by Zim Kalinowski
parent aa32164d15
commit d73db7f060
2 changed files with 30 additions and 27 deletions

View file

@ -36,7 +36,6 @@ options:
description: description:
- CIDR defining the IPv4 address space of the subnet. Must be valid within the context of the - CIDR defining the IPv4 address space of the subnet. Must be valid within the context of the
virtual network. virtual network.
required: true
aliases: aliases:
- address_prefix - address_prefix
security_group: security_group:
@ -205,10 +204,6 @@ class AzureRMSubnet(AzureRMModuleBase):
) )
) )
required_if = [
('state', 'present', ['address_prefix_cidr'])
]
self.results = dict( self.results = dict(
changed=False, changed=False,
state=dict() state=dict()
@ -224,8 +219,7 @@ class AzureRMSubnet(AzureRMModuleBase):
self.service_endpoints = None self.service_endpoints = None
super(AzureRMSubnet, self).__init__(self.module_arg_spec, super(AzureRMSubnet, self).__init__(self.module_arg_spec,
supports_check_mode=True, supports_check_mode=True)
required_if=required_if)
def exec_module(self, **kwargs): def exec_module(self, **kwargs):
@ -235,9 +229,10 @@ class AzureRMSubnet(AzureRMModuleBase):
for key in self.module_arg_spec: for key in self.module_arg_spec:
setattr(self, key, kwargs[key]) setattr(self, key, kwargs[key])
if self.state == 'present' and not CIDR_PATTERN.match(self.address_prefix_cidr): if self.address_prefix_cidr and not CIDR_PATTERN.match(self.address_prefix_cidr):
self.fail("Invalid address_prefix_cidr value {0}".format(self.address_prefix_cidr)) self.fail("Invalid address_prefix_cidr value {0}".format(self.address_prefix_cidr))
nsg = dict()
if self.security_group: if self.security_group:
nsg = self.parse_nsg() nsg = self.parse_nsg()
@ -262,19 +257,17 @@ class AzureRMSubnet(AzureRMModuleBase):
results = subnet_to_dict(subnet) results = subnet_to_dict(subnet)
if self.state == 'present': if self.state == 'present':
if self.address_prefix_cidr: if self.address_prefix_cidr and results['address_prefix'] != self.address_prefix_cidr:
if results['address_prefix'] != self.address_prefix_cidr: self.log("CHANGED: subnet {0} address_prefix_cidr".format(self.name))
self.log("CHANGED: subnet {0} address_prefix_cidr".format(self.name)) changed = True
changed = True results['address_prefix'] = self.address_prefix_cidr
results['address_prefix'] = self.address_prefix_cidr
if nsg: if self.security_group is not None and results['network_security_group'].get('id') != nsg.get('id'):
if results['network_security_group'].get('id') != nsg.get('id'): self.log("CHANGED: subnet {0} network security group".format(self.name))
self.log("CHANGED: subnet {0} network security group".format(self.name)) changed = True
changed = True results['network_security_group']['id'] = nsg.get('id')
results['network_security_group']['id'] = nsg.get('id') results['network_security_group']['name'] = nsg.get('name')
results['network_security_group']['name'] = nsg.get('name') if self.route_table is not None and self.route_table != results['route_table'].get('id'):
if self.route_table != results['route_table'].get('id'):
changed = True changed = True
results['route_table']['id'] = self.route_table results['route_table']['id'] = self.route_table
self.log("CHANGED: subnet {0} route_table to {1}".format(self.name, route_table.get('name'))) self.log("CHANGED: subnet {0} route_table to {1}".format(self.name, route_table.get('name')))
@ -310,6 +303,8 @@ class AzureRMSubnet(AzureRMModuleBase):
if self.state == 'present' and changed: if self.state == 'present' and changed:
if not subnet: if not subnet:
# create new subnet # create new subnet
if not self.address_prefix_cidr:
self.fail('address_prefix_cidr is not set')
self.log('Creating subnet {0}'.format(self.name)) self.log('Creating subnet {0}'.format(self.name))
subnet = self.network_models.Subnet( subnet = self.network_models.Subnet(
address_prefix=self.address_prefix_cidr address_prefix=self.address_prefix_cidr
@ -324,13 +319,13 @@ class AzureRMSubnet(AzureRMModuleBase):
subnet = self.network_models.Subnet( subnet = self.network_models.Subnet(
address_prefix=results['address_prefix'] address_prefix=results['address_prefix']
) )
if results['network_security_group'].get('id'): if results['network_security_group'].get('id') is not None:
subnet.network_security_group = self.network_models.NetworkSecurityGroup(id=results['network_security_group'].get('id')) subnet.network_security_group = self.network_models.NetworkSecurityGroup(id=results['network_security_group'].get('id'))
if self.route_table: if results['route_table'].get('id') is not None:
subnet.route_table = self.network_models.RouteTable(id=self.route_table) subnet.route_table = self.network_models.RouteTable(id=results['route_table'].get('id'))
if self.service_endpoints: if results.get('service_endpoints') is not None:
subnet.service_endpoints = self.service_endpoints subnet.service_endpoints = results['service_endpoints']
self.results['state'] = self.create_or_update_subnet(subnet) self.results['state'] = self.create_or_update_subnet(subnet)
elif self.state == 'absent' and changed: elif self.state == 'absent' and changed:

View file

@ -48,6 +48,16 @@
- assert: - assert:
that: output.changed that: output.changed
- name: Add the subnet back (idempontent)
azure_rm_subnet:
name: foobar
virtual_network_name: My_Virtual_Network
resource_group: "{{ resource_group }}"
register: output
- assert:
that: not output.changed
- name: Create network security group - name: Create network security group
azure_rm_securitygroup: azure_rm_securitygroup:
name: secgroupfoo name: secgroupfoo
@ -77,7 +87,6 @@
virtual_network_name: My_Virtual_Network virtual_network_name: My_Virtual_Network
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
address_prefix_cidr: "10.1.0.0/16" address_prefix_cidr: "10.1.0.0/16"
security_group: secgroupfoo
service_endpoints: service_endpoints:
- service: Microsoft.Sql - service: Microsoft.Sql
locations: locations:
@ -123,7 +132,6 @@
virtual_network_name: My_Virtual_Network virtual_network_name: My_Virtual_Network
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
address_prefix_cidr: "10.1.0.0/16" address_prefix_cidr: "10.1.0.0/16"
route_table: "{{ route_table.id }}"
security_group: "{{ nsg.state.id }}" security_group: "{{ nsg.state.id }}"
tags: tags:
testing: testing testing: testing