Only apply necessary changes to subnets

Previously, all subnet properties were updated if any value did not match.
But this behaviour caused errors like e.g. "Current gateway ip 192.168.0.1
already in use by port [ID]. Unable to update." even if that gateway was
not about to be changed.

Task: 40927
Story: 2008172
Change-Id: I049b0dade4c7ea3e1ef24777ae558f650caa136c
This commit is contained in:
Jakob Meng
2020-09-21 14:31:35 +02:00
committed by Shnaidman Sagi (Sergey)
parent a89ec027b0
commit ce421fe370
2 changed files with 19 additions and 22 deletions

View File

@@ -37,6 +37,7 @@
name: "{{ subnet_name }}" name: "{{ subnet_name }}"
state: present state: present
cidr: 192.168.0.0/24 cidr: 192.168.0.0/24
gateway_ip: 192.168.0.1
allocation_pool_start: 192.168.0.2 allocation_pool_start: 192.168.0.2
allocation_pool_end: 192.168.0.8 allocation_pool_end: 192.168.0.8

View File

@@ -221,35 +221,37 @@ class SubnetModule(OpenStackModule):
no_gateway_ip = self.params['no_gateway_ip'] no_gateway_ip = self.params['no_gateway_ip']
dns = self.params['dns_nameservers'] dns = self.params['dns_nameservers']
host_routes = self.params['host_routes'] host_routes = self.params['host_routes']
curr_pool = dict(start=pool_start, end=pool_end) if pool_start and pool_end:
pool = dict(start=pool_start, end=pool_end)
else:
pool = None
changes = dict()
if subnet['enable_dhcp'] != enable_dhcp: if subnet['enable_dhcp'] != enable_dhcp:
return True changes['enable_dhcp'] = enable_dhcp
if subnet_name and subnet['name'] != subnet_name: if subnet_name and subnet['name'] != subnet_name:
return True changes['subnet_name'] = subnet_name
if not subnet['allocation_pools'] and pool_start and pool_end: if pool and (not subnet['allocation_pools'] or subnet['allocation_pools'] != [pool]):
return True changes['allocation_pools'] = [pool]
if subnet['allocation_pools'] != [curr_pool]:
return True
if gateway_ip and subnet['gateway_ip'] != gateway_ip: if gateway_ip and subnet['gateway_ip'] != gateway_ip:
return True changes['gateway_ip'] = gateway_ip
if dns and sorted(subnet['dns_nameservers']) != sorted(dns): if dns and sorted(subnet['dns_nameservers']) != sorted(dns):
return True changes['dns_nameservers'] = dns
if host_routes: if host_routes:
curr_hr = sorted(subnet['host_routes'], key=lambda t: t.keys()) curr_hr = sorted(subnet['host_routes'], key=lambda t: t.keys())
new_hr = sorted(host_routes, key=lambda t: t.keys()) new_hr = sorted(host_routes, key=lambda t: t.keys())
if curr_hr != new_hr: if curr_hr != new_hr:
return True changes['host_routes'] = host_routes
if no_gateway_ip and subnet['gateway_ip']: if no_gateway_ip and subnet['gateway_ip']:
return True changes['disable_gateway_ip'] = no_gateway_ip
return False return changes
def _system_state_change(self, subnet, filters=None): def _system_state_change(self, subnet, filters=None):
state = self.params['state'] state = self.params['state']
if state == 'present': if state == 'present':
if not subnet: if not subnet:
return True return True
return self._needs_update(subnet, filters) return bool(self._needs_update(subnet, filters))
if state == 'absent' and subnet: if state == 'absent' and subnet:
return True return True
return False return False
@@ -334,15 +336,9 @@ class SubnetModule(OpenStackModule):
subnet = self.conn.create_subnet(network_name, **kwargs) subnet = self.conn.create_subnet(network_name, **kwargs)
changed = True changed = True
else: else:
if self._needs_update(subnet, filters): changes = self._needs_update(subnet, filters)
subnet = self.conn.update_subnet(subnet['id'], if changes:
subnet_name=subnet_name, subnet = self.conn.update_subnet(subnet['id'], **changes)
enable_dhcp=enable_dhcp,
gateway_ip=gateway_ip,
disable_gateway_ip=no_gateway_ip,
dns_nameservers=dns,
allocation_pools=pool,
host_routes=host_routes)
changed = True changed = True
else: else:
changed = False changed = False