Update address scope to use proxy

Updating address scope module to use proxy layer and new openstacksdk

Change-Id: I4763e22e7ad9174e23aa2030f63f7535006399a0
This commit is contained in:
Arx Cruz 2022-07-04 14:58:19 +02:00 committed by Jakob Meng
parent fb0fb529b7
commit ffa84bd705
3 changed files with 62 additions and 44 deletions

View File

@ -1 +1,8 @@
address_scope_name: "adrdess_scope" address_scope_name: "address_scope"
expected_fields:
- id
- ip_version
- is_shared
- name
- project_id
- tenant_id

View File

@ -7,6 +7,12 @@
ip_version: "4" ip_version: "4"
register: create_address_scope register: create_address_scope
- name: Verify returned values
assert:
that:
- item in create_address_scope.address_scope
loop: "{{ expected_fields }}"
- name: Verify address scope - name: Verify address scope
assert: assert:
that: that:
@ -37,4 +43,4 @@
openstack.cloud.address_scope: openstack.cloud.address_scope:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
name: "{{ address_scope_name }}" name: "{{ address_scope_name }}"
state: absent state: absent

View File

@ -21,27 +21,30 @@ options:
name: name:
description: description:
- Name to be give to the address scope - Name to be give to the address scope
- This option cannot be updated.
required: true required: true
type: str type: str
project: project:
description: description:
- Unique name or ID of the project. - Unique name or ID of the project.
- This option cannot be updated.
type: str type: str
ip_version: ip_version:
description: description:
- The IP version of the subnet 4 or 6 - The IP version of the subnet 4 or 6.
- This option cannot be updated.
default: '4' default: '4'
type: str type: str
choices: ['4', '6'] choices: ['4', '6']
shared: is_shared:
description: description:
- Whether this address scope is shared or not. - Whether this address scope is shared or not.
type: bool type: bool
default: 'no' default: 'no'
aliases: ['shared']
extra_specs: extra_specs:
description: description:
- Dictionary with extra key/value pairs passed to the API - Dictionary with extra key/value pairs passed to the API
required: false
default: {} default: {}
type: dict type: dict
requirements: requirements:
@ -78,29 +81,33 @@ RETURN = '''
address_scope: address_scope:
description: Dictionary describing the address scope. description: Dictionary describing the address scope.
returned: On success when I(state) is 'present' returned: On success when I(state) is 'present'
type: complex type: dict
contains: contains:
id: id:
description: Address Scope ID. description: Address Scope ID.
type: str type: str
sample: "474acfe5-be34-494c-b339-50f06aa143e4" sample: "474acfe5-be34-494c-b339-50f06aa143e4"
name:
description: Address Scope name.
type: str
sample: "my_address_scope"
tenant_id:
description: The tenant ID.
type: str
sample: "861174b82b43463c9edc5202aadc60ef"
ip_version: ip_version:
description: The IP version of the subnet 4 or 6. description: The IP version of the subnet 4 or 6.
type: str type: str
sample: "4" sample: "4"
is_shared: is_shared:
description: Indicates whether this address scope is shared across all tenants. description: Indicates whether this address scope is shared across
all tenants.
type: bool type: bool
sample: false sample: false
name:
description: Address Scope name.
type: str
sample: "my_address_scope"
project_id:
description: The project ID
type: str
sample: "474acfe5-be34-494c-b339-50f06aa143e4"
tenant_id:
description: The tenant ID.
type: str
sample: "861174b82b43463c9edc5202aadc60ef"
''' '''
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
@ -110,23 +117,20 @@ class AddressScopeModule(OpenStackModule):
argument_spec = dict( argument_spec = dict(
state=dict(default='present', choices=['absent', 'present']), state=dict(default='present', choices=['absent', 'present']),
name=dict(required=True), name=dict(required=True),
shared=dict(default=False, type='bool'), is_shared=dict(default=False, type='bool', aliases=['shared']),
ip_version=dict(default='4', choices=['4', '6']), ip_version=dict(default='4', choices=['4', '6']),
project=dict(), project=dict(),
extra_specs=dict(type='dict', default=dict()) extra_specs=dict(type='dict', default=dict())
) )
def _needs_update(self, address_scope, filters=None): def _needs_update(self, address_scope):
"""Decide if the given address_scope needs an update. """Decide if the given address_scope needs an update.
""" """
ip_version = int(self.params['ip_version']) if address_scope['is_shared'] != self.params['is_shared']:
if address_scope['is_shared'] != self.params['shared']:
return True return True
if ip_version and address_scope['ip_version'] != ip_version:
self.fail_json(msg='Cannot update ip_version in existing address scope')
return False return False
def _system_state_change(self, address_scope, filters=None): def _system_state_change(self, address_scope):
"""Check if the system state would be changed.""" """Check if the system state would be changed."""
state = self.params['state'] state = self.params['state']
if state == 'absent' and address_scope: if state == 'absent' and address_scope:
@ -134,27 +138,26 @@ class AddressScopeModule(OpenStackModule):
if state == 'present': if state == 'present':
if not address_scope: if not address_scope:
return True return True
return self._needs_update(address_scope, filters) return self._needs_update(address_scope)
return False return False
def run(self): def run(self):
state = self.params['state'] state = self.params['state']
name = self.params['name'] name = self.params['name']
shared = self.params['shared'] is_shared = self.params['is_shared']
ip_version = self.params['ip_version'] ip_version = self.params['ip_version']
project = self.params['project'] project_name_or_id = self.params['project']
extra_specs = self.params['extra_specs'] extra_specs = self.params['extra_specs']
if project is not None: if project_name_or_id is not None:
proj = self.conn.get_project(project) project_id = self.conn.identity.find_project(
if proj is None: project_name_or_id, ignore_missing=False)['id']
self.fail(msg='Project %s could not be found' % project)
project_id = proj['id']
else: else:
project_id = self.conn.current_project_id project_id = self.conn.session.get_project_id()
address_scope = self.conn.network.find_address_scope(name_or_id=name) address_scope = self.conn.network.find_address_scope(
name_or_id=name, project_id=project_id)
if self.ansible.check_mode: if self.ansible.check_mode:
self.exit_json( self.exit_json(
changed=self._system_state_change(address_scope) changed=self._system_state_change(address_scope)
@ -167,26 +170,28 @@ class AddressScopeModule(OpenStackModule):
kwargs = dict( kwargs = dict(
name=name, name=name,
ip_version=ip_version, ip_version=ip_version,
is_shared=shared, is_shared=is_shared,
tenant_id=project_id) project_id=project_id)
dup_args = set(kwargs.keys()) & set(extra_specs.keys()) dup_args = set(kwargs.keys()) & set(extra_specs.keys())
if dup_args: if dup_args:
raise ValueError('Duplicate key(s) {0} in extra_specs' raise ValueError('Duplicate key(s) {0} in extra_specs'
.format(list(dup_args))) .format(list(dup_args)))
kwargs = dict(kwargs, **extra_specs) kwargs = dict(kwargs, **extra_specs)
address_scope = self.conn.network.create_address_scope(**kwargs) address_scope = \
self.conn.network.create_address_scope(**kwargs)
changed = True changed = True
else:
if self._needs_update(address_scope): elif self._needs_update(address_scope):
address_scope = self.conn.network.update_address_scope(address_scope['id'], is_shared=shared) address_scope = self.conn.network.update_address_scope(
changed = True address_scope['id'], is_shared=is_shared)
else: changed = True
changed = False
self.exit_json(changed=changed, address_scope=address_scope, id=address_scope['id']) self.exit_json(changed=changed,
address_scope=address_scope.to_dict(computed=False))
elif state == 'absent': elif state == 'absent':
if not address_scope: if not address_scope:
self.exit(changed=False) self.exit_json(changed=False)
else: else:
self.conn.network.delete_address_scope(address_scope['id']) self.conn.network.delete_address_scope(address_scope['id'])
self.exit_json(changed=True) self.exit_json(changed=True)