From 134a8e9d2346f56a0dae8b5528e5ba386b6ef1ee Mon Sep 17 00:00:00 2001 From: Sagi Shnaidman Date: Tue, 24 Nov 2020 19:35:29 +0200 Subject: [PATCH] Fix subnets update and idempotency Fix subnet idempotency for allocation pools, see the linked story. Return updated subnet information. Remove adding allocation pools that were introduced in Ib8becf5e958f1bc8e5c9fd76f1722536bf1c9f1a in order to add allocation pools, either add new variable or recreate the subnet. Task: 41307 Story: 2008384 Change-Id: Ibe808227de159c6975dc94ef8ad0ab03a9345e17 --- ci/roles/subnet/tasks/subnet-allocation.yml | 25 +++++++++++++++------ plugins/modules/subnet.py | 20 ++++++++--------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/ci/roles/subnet/tasks/subnet-allocation.yml b/ci/roles/subnet/tasks/subnet-allocation.yml index d2fb2686..56a60da5 100644 --- a/ci/roles/subnet/tasks/subnet-allocation.yml +++ b/ci/roles/subnet/tasks/subnet-allocation.yml @@ -17,6 +17,19 @@ allocation_pool_start: 192.168.0.2 allocation_pool_end: 192.168.0.4 +- name: Create subnet {{ subnet_name }} on network {{ network_name }} again + openstack.cloud.subnet: + cloud: "{{ cloud }}" + network_name: "{{ network_name }}" + enable_dhcp: "{{ enable_subnet_dhcp }}" + name: "{{ subnet_name }}" + state: present + cidr: 192.168.0.0/24 + gateway_ip: 192.168.0.1 + allocation_pool_start: 192.168.0.2 + allocation_pool_end: 192.168.0.4 + register: idem1 + - name: Update subnet {{ subnet_name }} allocation pools openstack.cloud.subnet: cloud: "{{ cloud }}" @@ -24,7 +37,7 @@ name: "{{ subnet_name }}" state: present cidr: 192.168.0.0/24 - allocation_pool_start: 192.168.0.5 + allocation_pool_start: 192.168.0.2 allocation_pool_end: 192.168.0.8 - name: Get Subnet Info @@ -36,19 +49,17 @@ - name: Verify Subnet Allocation Pools Exist assert: that: + - idem1 is not changed - subnet_result.openstack_subnets is defined - subnet_result.openstack_subnets | length == 1 - subnet_result.openstack_subnets[0].allocation_pools is defined - - subnet_result.openstack_subnets[0].allocation_pools | length == 2 + - subnet_result.openstack_subnets[0].allocation_pools | length == 1 - name: Verify Subnet Allocation Pools assert: that: - - subnet_result.openstack_subnets[0].allocation_pools | selectattr('start','equalto',item.start) | list | count > 0 - - subnet_result.openstack_subnets[0].allocation_pools | selectattr('end','equalto',item.end) | list | count > 0 - loop: - - {start: '192.168.0.2', end: '192.168.0.4'} - - {start: '192.168.0.5', end: '192.168.0.8'} + - subnet_result.openstack_subnets[0].allocation_pools.0.start == '192.168.0.2' + - subnet_result.openstack_subnets[0].allocation_pools.0.end == '192.168.0.8' - name: Delete subnet {{ subnet_name }} openstack.cloud.subnet: diff --git a/plugins/modules/subnet.py b/plugins/modules/subnet.py index eb45a4ea..73a496a5 100644 --- a/plugins/modules/subnet.py +++ b/plugins/modules/subnet.py @@ -207,7 +207,7 @@ def _needs_update(subnet, module, cloud, filters=None): return True if not subnet['allocation_pools'] and pool_start and pool_end: return True - if subnet['allocation_pools'] and curr_pool not in subnet['allocation_pools']: + if subnet['allocation_pools'] != [curr_pool]: return True if gateway_ip and subnet['gateway_ip'] != gateway_ip: return True @@ -346,16 +346,14 @@ def main(): changed = True else: if _needs_update(subnet, module, cloud, filters): - if subnet['allocation_pools'] and pool is not None: - pool = pool + subnet['allocation_pools'] - cloud.update_subnet(subnet['id'], - subnet_name=subnet_name, - enable_dhcp=enable_dhcp, - gateway_ip=gateway_ip, - disable_gateway_ip=no_gateway_ip, - dns_nameservers=dns, - allocation_pools=pool, - host_routes=host_routes) + subnet = cloud.update_subnet(subnet['id'], + subnet_name=subnet_name, + 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 else: changed = False