Refactored endpoint module and explained region attribute

Regions have IDs, but do not have names.
Ref.: https://docs.openstack.org/api-ref/identity/v3/#regions

Change-Id: I2512bbde6e96e2ab0f1fef0230295223f46105dd
This commit is contained in:
Jakob Meng
2022-06-23 11:33:40 +02:00
parent c6c1c6a070
commit ad3a3a89f2
2 changed files with 38 additions and 33 deletions

View File

@@ -1,5 +1,5 @@
--- ---
- name: Create a service for compute - name: Create a service endpoint for compute
openstack.cloud.endpoint: openstack.cloud.endpoint:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
service: nova service: nova
@@ -9,7 +9,7 @@
state: present state: present
register: endpoint_test register: endpoint_test
- name: Ensure service was created - name: Ensure service endpoint was created
assert: assert:
that: that:
- endpoint_test.endpoint.id is defined - endpoint_test.endpoint.id is defined
@@ -19,7 +19,7 @@
that: that:
- endpoint_test.endpoint.url == "http://controller:9292" - endpoint_test.endpoint.url == "http://controller:9292"
- name: Create service for compute again - name: Create service endpoint for compute again
openstack.cloud.endpoint: openstack.cloud.endpoint:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
service: nova service: nova
@@ -34,7 +34,7 @@
that: that:
- not endpoint_again.changed - not endpoint_again.changed
- name: Update endpoint url - name: Update service endpoint url
openstack.cloud.endpoint: openstack.cloud.endpoint:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
service: nova service: nova
@@ -44,12 +44,12 @@
state: present state: present
register: endpoint_updated register: endpoint_updated
- name: Ensure endpoint was updated - name: Ensure service endpoint was updated
assert: assert:
that: that:
- endpoint_updated.endpoint.url == "http://controller:9393" - endpoint_updated.endpoint.url == "http://controller:9393"
- name: Delete endpoint - name: Delete service endpoint
openstack.cloud.endpoint: openstack.cloud.endpoint:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
service: nova service: nova
@@ -59,7 +59,7 @@
state: absent state: absent
register: endpoint_deleted register: endpoint_deleted
- name: Ensure endpoint was deleted - name: Ensure service endpoint was deleted
assert: assert:
that: that:
- endpoint_deleted.changed - endpoint_deleted.changed

View File

@@ -10,8 +10,9 @@ short_description: Manage OpenStack Identity service endpoints
author: OpenStack Ansible SIG author: OpenStack Ansible SIG
description: description:
- Create, update, or delete OpenStack Identity service endpoints. If a - Create, update, or delete OpenStack Identity service endpoints. If a
service with the same combination of I(service), I(interface) and I(region) service with the same combination of I(service), I(interface) and
exist, the I(url) and I(state) (C(present) or C(absent)) will be updated. I(region) exist, the I(url), I(enabled) and I(state) (C(present) or
C(absent)) will be updated.
options: options:
service: service:
description: description:
@@ -31,7 +32,8 @@ options:
type: str type: str
region: region:
description: description:
- Region that the service belongs to. Note that I(region_name) is used for authentication. - ID of the region that the service belongs to.
Note that I(region) is used for authentication.
type: str type: str
enabled: enabled:
description: description:
@@ -151,7 +153,9 @@ class IdentityEndpointModule(OpenStackModule):
service_name_or_id = self.params['service'] service_name_or_id = self.params['service']
interface = self.params['endpoint_interface'] interface = self.params['endpoint_interface']
url = self.params['url'] url = self.params['url']
region = self.params['region'] # Regions have IDs but do not have names
# Ref.: https://docs.openstack.org/api-ref/identity/v3/#regions
region_id = self.params['region']
enabled = self.params['enabled'] enabled = self.params['enabled']
state = self.params['state'] state = self.params['state']
@@ -164,45 +168,46 @@ class IdentityEndpointModule(OpenStackModule):
self.fail_json(msg='Service %s does not exist' % service_name_or_id) self.fail_json(msg='Service %s does not exist' % service_name_or_id)
filters = dict(service_id=service.id, interface=interface) filters = dict(service_id=service.id, interface=interface)
if region is not None: if region_id:
filters['region_id'] = region filters['region_id'] = region_id
endpoints = list(self.conn.identity.endpoints(**filters)) endpoints = list(self.conn.identity.endpoints(**filters))
endpoint = None endpoint = None
if len(endpoints) > 1: if len(endpoints) > 1:
self.fail_json(msg='Service %s, interface %s and region %s are ' self.fail_json(msg='Service %s, interface %s and region %s are '
'not unique' % 'not unique' %
(service_name_or_id, interface, region)) (service_name_or_id, interface, region_id))
elif len(endpoints) == 1: elif len(endpoints) == 1:
endpoint = endpoints[0] endpoint = endpoints[0]
if self.ansible.check_mode: if self.ansible.check_mode:
self.exit_json(changed=self._system_state_change(endpoint)) self.exit_json(changed=self._system_state_change(endpoint))
changed = False
if state == 'present': if state == 'present':
if endpoint is None: if not endpoint:
args = {'url': url, 'interface': interface, args = {
'service_id': service.id, 'enabled': enabled, 'url': url,
'region_id': region} 'interface': interface,
endpoint = self.conn.identity.create_endpoint(**args) 'service_id': service.id,
'enabled': enabled,
'region_id': region_id
}
endpoint = self.conn.identity.create_endpoint(**args)
changed = True changed = True
else: elif self._needs_update(endpoint):
if self._needs_update(endpoint):
endpoint = self.conn.identity.update_endpoint( endpoint = self.conn.identity.update_endpoint(
endpoint.id, url=url, enabled=enabled) endpoint.id, url=url, enabled=enabled)
changed = True changed = True
else:
changed = False
self.exit_json(changed=changed, self.exit_json(changed=changed,
endpoint=endpoint.to_dict(computed=False)) endpoint=endpoint.to_dict(computed=False))
elif state == 'absent': elif state == 'absent' and endpoint:
if endpoint is None:
changed = False
else:
self.conn.identity.delete_endpoint(endpoint.id) self.conn.identity.delete_endpoint(endpoint.id)
changed = True changed = True
self.exit_json(changed=changed) self.exit_json(changed=changed)