From 7a9837dfb5e19e92c5d24974c36430d68b147ecb Mon Sep 17 00:00:00 2001 From: Arx Cruz Date: Tue, 26 Apr 2022 14:22:59 +0200 Subject: [PATCH] Backport improvements to endpoint module - Adds endpoint tests - Update docs Change-Id: Ibd647d0c2cd2f90310f381e56088e7e8e93f76fc (cherry picked from commit 452404ee870ab72d496c9bd3d02933a2167963c7) --- ci/roles/endpoint/tasks/main.yml | 65 ++++++++++++++++++++++++++++++++ ci/run-collection.yml | 3 +- plugins/modules/endpoint.py | 47 ++++++++++++++--------- 3 files changed, 97 insertions(+), 18 deletions(-) create mode 100644 ci/roles/endpoint/tasks/main.yml diff --git a/ci/roles/endpoint/tasks/main.yml b/ci/roles/endpoint/tasks/main.yml new file mode 100644 index 00000000..24cfc44e --- /dev/null +++ b/ci/roles/endpoint/tasks/main.yml @@ -0,0 +1,65 @@ +--- +- name: Create a service for compute + openstack.cloud.endpoint: + cloud: "{{ cloud }}" + service: nova + endpoint_interface: internal + url: http://controller:9292 + region: RegionOne + state: present + register: endpoint_test + +- name: Ensure service was created + assert: + that: + - endpoint_test.endpoint.id is defined + +- name: Ensure service have the proper endpoint + assert: + that: + - endpoint_test.endpoint.url == "http://controller:9292" + +- name: Create service for compute again + openstack.cloud.endpoint: + cloud: "{{ cloud }}" + service: nova + endpoint_interface: internal + url: http://controller:9292 + region: RegionOne + state: present + register: endpoint_again + +- name: Ensure changed is false + assert: + that: + - not endpoint_again.changed + +- name: Update endpoint url + openstack.cloud.endpoint: + cloud: "{{ cloud }}" + service: nova + endpoint_interface: internal + url: http://controller:9393 + region: RegionOne + state: present + register: endpoint_updated + +- name: Ensure endpoint was updated + assert: + that: + - endpoint_updated.endpoint.url == "http://controller:9393" + +- name: Delete endpoint + openstack.cloud.endpoint: + cloud: "{{ cloud }}" + service: nova + endpoint_interface: internal + url: http://controller:9393 + region: RegionOne + state: absent + register: endpoint_deleted + +- name: Ensure endpoint was deleted + assert: + that: + - endpoint_deleted.changed diff --git a/ci/run-collection.yml b/ci/run-collection.yml index 2a1a5579..2e26349c 100644 --- a/ci/run-collection.yml +++ b/ci/run-collection.yml @@ -16,8 +16,9 @@ - role: dns tags: dns when: sdk_version is version(0.28, '>=') - - { role: host_aggregate, tags: host_aggregate } + - { role: endpoint, tags: endpoint } - { role: floating_ip_info, tags: floating_ip_info } + - { role: host_aggregate, tags: host_aggregate } - { role: identity_domain_info, tags: identity_domain_info } - { role: identity_group_info, tags: identity_group_info } - { role: identity_user, tags: identity_user } diff --git a/plugins/modules/endpoint.py b/plugins/modules/endpoint.py index a0ca9674..e7864ecf 100644 --- a/plugins/modules/endpoint.py +++ b/plugins/modules/endpoint.py @@ -81,26 +81,34 @@ endpoint: description: Endpoint ID. type: str sample: 3292f020780b4d5baf27ff7e1d224c44 + interface: + description: Endpoint Interface. + type: str + sample: public + enabled: + description: Service status. + type: bool + sample: True + links: + description: Links for the endpoint + type: str + sample: http://controller/identity/v3/endpoints/123 region: - description: Region Name. + description: Same as C(region_id). Deprecated. + type: str + sample: RegionOne + region_id: + description: Region ID. type: str sample: RegionOne service_id: description: Service ID. type: str sample: b91f1318f735494a825a55388ee118f3 - interface: - description: Endpoint Interface. - type: str - sample: public url: description: Service URL. type: str sample: http://controller:9292 - enabled: - description: Service status. - type: bool - sample: True ''' from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule @@ -148,10 +156,11 @@ class IdentityEndpointModule(OpenStackModule): state = self.params['state'] service = self.conn.get_service(service_name_or_id) + if service is None and state == 'absent': self.exit_json(changed=False) - elif service is None and state == 'present': + if service is None and state == 'present': self.fail_json(msg='Service %s does not exist' % service_name_or_id) filters = dict(service_id=service.id, interface=interface) @@ -159,24 +168,27 @@ class IdentityEndpointModule(OpenStackModule): filters['region'] = region endpoints = self.conn.search_endpoints(filters=filters) + endpoint = None if len(endpoints) > 1: self.fail_json(msg='Service %s, interface %s and region %s are ' 'not unique' % (service_name_or_id, interface, region)) elif len(endpoints) == 1: endpoint = endpoints[0] - else: - endpoint = None if self.ansible.check_mode: self.exit_json(changed=self._system_state_change(endpoint)) if state == 'present': if endpoint is None: - result = self.conn.create_endpoint( - service_name_or_id=service, url=url, interface=interface, - region=region, enabled=enabled) - endpoint = result[0] + args = {'url': url, 'interface': interface, + 'service_name_or_id': service.id, 'enabled': enabled, + 'region': region} + endpoints = self.conn.create_endpoint(**args) + # safe because endpoints contains a single item when url is + # given to self.conn.create_endpoint() + endpoint = endpoints[0] + changed = True else: if self._needs_update(endpoint): @@ -185,7 +197,8 @@ class IdentityEndpointModule(OpenStackModule): changed = True else: changed = False - self.exit_json(changed=changed, endpoint=endpoint) + self.exit_json(changed=changed, + endpoint=endpoint) elif state == 'absent': if endpoint is None: