From e0139fe9406a3cb63b3f5fd1fb7f499f7b3bc11c Mon Sep 17 00:00:00 2001 From: George Shuklin Date: Wed, 31 Jan 2024 12:10:07 +0200 Subject: [PATCH] fix: subnet module: allow cidr option with subnet_pool Specifying CIDR during creation of subnet from subnet pool is a valid operation. Moreover, in case of use of a subnet pool with multiple subnets, cidr is a mandatory paramter for creating subnet. Following code should be valid: - name: Create subnet openstack.cloud.subnet: name: "subnet_name" network: "some_network" gateway_ip: "192.168.0.1" allocation_pool_start: "192.168.0.2" allocation_pool_end: "192.168.0.254" cidr: "192.168.0.0/24" ip_version: 4 subnet_pool: "192.168.0.0/24" This scenario is added as a subnet-pool.yaml test in the test role. Change-Id: I1163ba34ac3079f76dd0b7477a80a2135985a650 --- ci/roles/subnet/tasks/main.yml | 3 + ci/roles/subnet/tasks/subnet-pool.yaml | 103 +++++++++++++++++++++++++ plugins/modules/subnet.py | 2 +- 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 ci/roles/subnet/tasks/subnet-pool.yaml diff --git a/ci/roles/subnet/tasks/main.yml b/ci/roles/subnet/tasks/main.yml index f002cf95..0ce0cf5f 100644 --- a/ci/roles/subnet/tasks/main.yml +++ b/ci/roles/subnet/tasks/main.yml @@ -150,3 +150,6 @@ - name: Subnet Allocation include_tasks: subnet-allocation.yml + +- name: Subnet Allocations from Subnet Pool + include_tasks: subnet-pool.yaml diff --git a/ci/roles/subnet/tasks/subnet-pool.yaml b/ci/roles/subnet/tasks/subnet-pool.yaml new file mode 100644 index 00000000..94135b9e --- /dev/null +++ b/ci/roles/subnet/tasks/subnet-pool.yaml @@ -0,0 +1,103 @@ +--- +# This test cover case when subnet is constructed +# with few prefixes and neutron API is required +# CIDR parameter to be used together with subnet pool. + +- name: Create network {{ network_name }} + openstack.cloud.network: + cloud: "{{ cloud }}" + name: "{{ network_name }}" + state: present + +- name: Create address_scope + openstack.cloud.address_scope: + cloud: "{{ cloud }}" + name: "{{ address_scope_name }}" + shared: false + ip_version: "4" + register: create_address_scope + +- name: Create subnet pool + openstack.cloud.subnet_pool: + cloud: "{{ cloud }}" + name: "{{ subnet_pool_name }}" + is_shared: false + address_scope: "{{ address_scope_name }}" + prefixes: + - 192.168.0.0/24 + - 192.168.42.0/24 + register: subnet_pool + +- name: Create subnet {{ subnet_name }} on network {{ network_name }} from subnet pool {{ subnet_pool_name }} + openstack.cloud.subnet: + cloud: "{{ cloud }}" + network_name: "{{ network_name }}" + enable_dhcp: "{{ enable_subnet_dhcp }}" + name: "{{ subnet_name }}" + state: present + cidr: 192.168.42.0/24 # we want specific cidr from subnet pool + ip_version: 4 + subnet_pool: "{{ subnet_pool_name }}" + gateway_ip: 192.168.42.1 + allocation_pool_start: 192.168.42.2 + allocation_pool_end: 192.168.42.4 + +- name: Create subnet {{ subnet_name }} on network {{ network_name }} from subnet pool {{ subnet_pool_name }} again + openstack.cloud.subnet: + cloud: "{{ cloud }}" + network_name: "{{ network_name }}" + enable_dhcp: "{{ enable_subnet_dhcp }}" + name: "{{ subnet_name }}" + state: present + cidr: 192.168.42.0/24 + ip_version: 4 + subnet_pool: "{{ subnet_pool_name }}" + gateway_ip: 192.168.42.1 + allocation_pool_start: 192.168.42.2 + allocation_pool_end: 192.168.42.4 + register: idem1 + +- name: Get Subnet Info + openstack.cloud.subnets_info: + cloud: "{{ cloud }}" + name: "{{ subnet_name }}" + register: subnet_result + +- name: Verify Subnet Allocation Pools Exist + assert: + that: + - idem1 is not changed + - subnet_result.subnets is defined + - subnet_result.subnets | length == 1 + - subnet_result.subnets[0].allocation_pools is defined + - subnet_result.subnets[0].allocation_pools | length == 1 + +- name: Verify Subnet Allocation Pools + assert: + that: + - subnet_result.subnets[0].allocation_pools.0.start == '192.168.42.2' + - subnet_result.subnets[0].allocation_pools.0.end == '192.168.42.4' + +- name: Delete subnet {{ subnet_name }} + openstack.cloud.subnet: + cloud: "{{ cloud }}" + name: "{{ subnet_name }}" + state: absent + +- name: Delete created subnet pool + openstack.cloud.subnet_pool: + cloud: "{{ cloud }}" + name: "{{ subnet_pool_name }}" + state: absent + +- name: Delete created address scope + openstack.cloud.address_scope: + cloud: "{{ cloud }}" + name: "{{ address_scope_name }}" + state: absent + +- name: Delete network {{ network_name }} + openstack.cloud.network: + cloud: "{{ cloud }}" + name: "{{ network_name }}" + state: absent diff --git a/plugins/modules/subnet.py b/plugins/modules/subnet.py index d8da4b5d..aa0b8b81 100644 --- a/plugins/modules/subnet.py +++ b/plugins/modules/subnet.py @@ -321,7 +321,7 @@ class SubnetModule(OpenStackModule): ('cidr', 'use_default_subnet_pool', 'subnet_pool'), True), ], mutually_exclusive=[ - ('cidr', 'use_default_subnet_pool', 'subnet_pool') + ('use_default_subnet_pool', 'subnet_pool') ] )