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"
register: create_address_scope
- name: Verify returned values
assert:
that:
- item in create_address_scope.address_scope
loop: "{{ expected_fields }}"
- name: Verify address scope
assert:
that:

View File

@ -21,27 +21,30 @@ options:
name:
description:
- Name to be give to the address scope
- This option cannot be updated.
required: true
type: str
project:
description:
- Unique name or ID of the project.
- This option cannot be updated.
type: str
ip_version:
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'
type: str
choices: ['4', '6']
shared:
is_shared:
description:
- Whether this address scope is shared or not.
type: bool
default: 'no'
aliases: ['shared']
extra_specs:
description:
- Dictionary with extra key/value pairs passed to the API
required: false
default: {}
type: dict
requirements:
@ -78,29 +81,33 @@ RETURN = '''
address_scope:
description: Dictionary describing the address scope.
returned: On success when I(state) is 'present'
type: complex
type: dict
contains:
id:
description: Address Scope ID.
type: str
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:
description: The IP version of the subnet 4 or 6.
type: str
sample: "4"
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
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
@ -110,23 +117,20 @@ class AddressScopeModule(OpenStackModule):
argument_spec = dict(
state=dict(default='present', choices=['absent', 'present']),
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']),
project=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.
"""
ip_version = int(self.params['ip_version'])
if address_scope['is_shared'] != self.params['shared']:
if address_scope['is_shared'] != self.params['is_shared']:
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
def _system_state_change(self, address_scope, filters=None):
def _system_state_change(self, address_scope):
"""Check if the system state would be changed."""
state = self.params['state']
if state == 'absent' and address_scope:
@ -134,27 +138,26 @@ class AddressScopeModule(OpenStackModule):
if state == 'present':
if not address_scope:
return True
return self._needs_update(address_scope, filters)
return self._needs_update(address_scope)
return False
def run(self):
state = self.params['state']
name = self.params['name']
shared = self.params['shared']
is_shared = self.params['is_shared']
ip_version = self.params['ip_version']
project = self.params['project']
project_name_or_id = self.params['project']
extra_specs = self.params['extra_specs']
if project is not None:
proj = self.conn.get_project(project)
if proj is None:
self.fail(msg='Project %s could not be found' % project)
project_id = proj['id']
if project_name_or_id is not None:
project_id = self.conn.identity.find_project(
project_name_or_id, ignore_missing=False)['id']
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:
self.exit_json(
changed=self._system_state_change(address_scope)
@ -167,26 +170,28 @@ class AddressScopeModule(OpenStackModule):
kwargs = dict(
name=name,
ip_version=ip_version,
is_shared=shared,
tenant_id=project_id)
is_shared=is_shared,
project_id=project_id)
dup_args = set(kwargs.keys()) & set(extra_specs.keys())
if dup_args:
raise ValueError('Duplicate key(s) {0} in extra_specs'
.format(list(dup_args)))
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
else:
if self._needs_update(address_scope):
address_scope = self.conn.network.update_address_scope(address_scope['id'], is_shared=shared)
elif self._needs_update(address_scope):
address_scope = self.conn.network.update_address_scope(
address_scope['id'], is_shared=is_shared)
changed = True
else:
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':
if not address_scope:
self.exit(changed=False)
self.exit_json(changed=False)
else:
self.conn.network.delete_address_scope(address_scope['id'])
self.exit_json(changed=True)