Update catalog service for the new sdk

This patch do the following:

* Update catalog_service to use new openstacksdk
* Add catalog_service role to test catalog_service module

Change-Id: I6778f5e91cb0ead63cede28af0111d7ffbbf3ab1
This commit is contained in:
Arx Cruz 2022-04-25 22:13:06 +02:00 committed by Rafael Castillo
parent d65ea4d560
commit 7c7e61d36b
4 changed files with 134 additions and 28 deletions

View File

@ -65,6 +65,7 @@
tox_extra_args: &skip_broken_ci_roles >- tox_extra_args: &skip_broken_ci_roles >-
address_scope address_scope
auth auth
catalog_service
client_config client_config
dns dns
dns_zone_info dns_zone_info

View File

@ -0,0 +1,94 @@
---
- name: Delete service test
openstack.cloud.catalog_service:
cloud: "{{ cloud }}"
service_type: test
name: test
state: absent
register: service_delete
- name: Assert changed is set to false
assert:
that:
- not service_delete.changed
- name: Create a service for test
openstack.cloud.catalog_service:
cloud: "{{ cloud }}"
name: "test_service"
state: present
service_type: test_type
description: "Test service"
register: service_test
- name: Verify returned values
assert:
that:
- item in service_test.service
loop:
- description
- id
- is_enabled
- links
- name
- type
- name: Check if the service test was created successfully
openstack.cloud.catalog_service:
cloud: "{{ cloud }}"
service_type: test
name: test
register: service_created
- name: Verify returned values
assert:
that:
- item in service_created.service
loop:
- description
- id
- is_enabled
- links
- name
- type
- name: Update service test
openstack.cloud.catalog_service:
cloud: "{{ cloud }}"
service_type: test
description: "A new description"
is_enabled: False
name: test
register: service_test
- name: Check if description and enabled were updated
assert:
that:
- service_test.service.description == "A new description"
- not (service_test.service.is_enabled|bool)
- name: Delete service test
openstack.cloud.catalog_service:
cloud: "{{ cloud }}"
service_type: test
name: test
state: absent
register: service_deleted
- name: Verify if service was deleted
assert:
that:
- service_deleted.changed
- name: Delete service test again
openstack.cloud.catalog_service:
cloud: "{{ cloud }}"
service_type: test
name: test
state: absent
register: service_deleted
- name: Assert changed is set to false
assert:
that:
- not service_deleted.changed

View File

@ -6,6 +6,7 @@
roles: roles:
- { role: address_scope, tags: address_scope } - { role: address_scope, tags: address_scope }
- { role: auth, tags: auth } - { role: auth, tags: auth }
- { role: catalog_service, tags: catalog_service }
- { role: client_config, tags: client_config } - { role: client_config, tags: client_config }
- { role: dns_zone_info, tags: dns_zone_info } - { role: dns_zone_info, tags: dns_zone_info }
- role: object_container - role: object_container

View File

@ -21,16 +21,18 @@ options:
description: description:
- Description of the service - Description of the service
type: str type: str
enabled: is_enabled:
description: description:
- Is the service enabled - Is the service enabled
type: bool type: bool
default: 'yes' default: 'yes'
service_type: aliases: ['enabled']
type:
description: description:
- The type of service - The type of service
required: true required: true
type: str type: str
aliases: ['service_type']
state: state:
description: description:
- Should the resource be present or absent. - Should the resource be present or absent.
@ -51,21 +53,21 @@ EXAMPLES = '''
cloud: mycloud cloud: mycloud
state: present state: present
name: glance name: glance
service_type: image type: image
description: OpenStack Image Service description: OpenStack Image Service
# Delete a service # Delete a service
- openstack.cloud.catalog_service: - openstack.cloud.catalog_service:
cloud: mycloud cloud: mycloud
state: absent state: absent
name: glance name: glance
service_type: image type: image
''' '''
RETURN = ''' RETURN = '''
service: service:
description: Dictionary describing the service. description: Dictionary describing the service.
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: Service ID. description: Service ID.
@ -75,7 +77,7 @@ service:
description: Service name. description: Service name.
type: str type: str
sample: "glance" sample: "glance"
service_type: type:
description: Service type. description: Service type.
type: str type: str
sample: "image" sample: "image"
@ -83,10 +85,14 @@ service:
description: Service description. description: Service description.
type: str type: str
sample: "OpenStack Image Service" sample: "OpenStack Image Service"
enabled: is_enabled:
description: Service status. description: Service status.
type: bool type: bool
sample: True sample: True
links:
description: Link of the service
type: str
sample: http://10.0.0.1/identity/v3/services/0ae87
id: id:
description: The service ID. description: The service ID.
returned: On success when I(state) is 'present' returned: On success when I(state) is 'present'
@ -100,9 +106,9 @@ from ansible_collections.openstack.cloud.plugins.module_utils.openstack import O
class IdentityCatalogServiceModule(OpenStackModule): class IdentityCatalogServiceModule(OpenStackModule):
argument_spec = dict( argument_spec = dict(
description=dict(default=None), description=dict(default=None),
enabled=dict(default=True, type='bool'), is_enabled=dict(default=True, aliases=['enabled'], type='bool'),
name=dict(required=True), name=dict(required=True),
service_type=dict(required=True), type=dict(required=True, aliases=['service_type']),
state=dict(default='present', choices=['absent', 'present']), state=dict(default='present', choices=['absent', 'present']),
) )
@ -111,10 +117,8 @@ class IdentityCatalogServiceModule(OpenStackModule):
) )
def _needs_update(self, service): def _needs_update(self, service):
if service.enabled != self.params['enabled']: for parameter in ('is_enabled', 'description', 'type'):
return True if service[parameter] != self.params[parameter]:
if service.description is not None and \
service.description != self.params['description']:
return True return True
return False return False
@ -132,46 +136,52 @@ class IdentityCatalogServiceModule(OpenStackModule):
def run(self): def run(self):
description = self.params['description'] description = self.params['description']
enabled = self.params['enabled'] enabled = self.params['is_enabled']
name = self.params['name'] name = self.params['name']
state = self.params['state'] state = self.params['state']
service_type = self.params['service_type'] type = self.params['type']
services = self.conn.search_services( filters = {'name': name, 'type': type}
name_or_id=name, filters=dict(type=service_type))
services = list(self.conn.identity.services(**filters))
service = None
if len(services) > 1: if len(services) > 1:
self.fail_json( self.fail_json(
msg='Service name %s and type %s are not unique' msg='Service name %s and type %s are not unique'
% (name, service_type)) % (name, type))
elif len(services) == 1: elif len(services) == 1:
service = services[0] service = services[0]
else:
service = None
if self.ansible.check_mode: if self.ansible.check_mode:
self.exit_json(changed=self._system_state_change(service)) self.exit_json(changed=self._system_state_change(service))
args = {'name': name, 'enabled': enabled, 'type': type}
if description:
args['description'] = description
if state == 'present': if state == 'present':
if service is None: if service is None:
service = self.conn.create_service( service = self.conn.identity.create_service(**args)
name=name, description=description, type=service_type, enabled=True)
changed = True changed = True
else: else:
if self._needs_update(service): if self._needs_update(service):
service = self.conn.update_service( # The self.conn.update_service calls get_service that
service.id, name=name, type=service_type, enabled=enabled, # checks if the service is duplicated or not. We don't need
description=description) # to do it here because it was already checked above
service = self.conn.identity.update_service(service,
**args)
changed = True changed = True
else: else:
changed = False changed = False
self.exit_json(changed=changed, service=service, id=service.id) service = service.to_dict(computed=False)
self.exit_json(changed=changed, service=service, id=service['id'])
elif state == 'absent': elif state == 'absent':
if service is None: if service is None:
changed = False changed = False
else: else:
self.conn.delete_service(service.id) self.conn.identity.delete_service(service)
changed = True changed = True
self.exit_json(changed=changed) self.exit_json(changed=changed)