Refactored keystone_federation_protocol{,_info} modules

Change-Id: I6f9eef184538df4feff49dd6ed659a9b9ac1289f
This commit is contained in:
Jakob Meng
2022-11-08 11:53:30 +01:00
parent 5862d91615
commit 114bc855c0
4 changed files with 224 additions and 283 deletions

View File

@@ -9,6 +9,11 @@ idp_remote_ids:
# Minimal Domain definition # Minimal Domain definition
domain_name: 'test-domain' domain_name: 'test-domain'
expected_fields:
- id
- mapping_id
- name
# Minimal Mapping definition # Minimal Mapping definition
mapping_name_1: 'ansible-test-mapping-1' mapping_name_1: 'ansible-test-mapping-1'
mapping_name_2: 'ansible-test-mapping-2' mapping_name_2: 'ansible-test-mapping-2'

View File

@@ -28,42 +28,24 @@
openstack.cloud.identity_domain: openstack.cloud.identity_domain:
name: '{{ domain_name }}' name: '{{ domain_name }}'
register: create_domain register: create_domain
- assert:
that:
- create_domain is successful
- '"id" in create_domain'
- name: 'Store domain ID as fact'
set_fact:
domain_id: '{{ create_domain.id }}'
- name: 'Create test Identity Provider' - name: 'Create test Identity Provider'
openstack.cloud.federation_idp: openstack.cloud.federation_idp:
state: 'present' state: 'present'
name: '{{ idp_name }}' name: '{{ idp_name }}'
domain_id: '{{ domain_id }}' domain_id: '{{ create_domain.id }}'
register: create_idp
- assert:
that:
- create_idp is successful
- name: 'Create test mapping (1)' - name: 'Create test mapping (1)'
openstack.cloud.federation_mapping: openstack.cloud.federation_mapping:
state: 'present' state: 'present'
name: '{{ mapping_name_1 }}' name: '{{ mapping_name_1 }}'
rules: '{{ mapping_rules_1 }}' rules: '{{ mapping_rules_1 }}'
register: create_mapping
- assert:
that:
- create_mapping is successful
- name: 'Create test mapping (2)' - name: 'Create test mapping (2)'
openstack.cloud.federation_mapping: openstack.cloud.federation_mapping:
state: 'present' state: 'present'
name: '{{ mapping_name_2 }}' name: '{{ mapping_name_2 }}'
rules: '{{ mapping_rules_2 }}' rules: '{{ mapping_rules_2 }}'
register: create_mapping
- assert:
that:
- create_mapping is successful
# We *should* have a blank slate to start with, but we also shouldn't # We *should* have a blank slate to start with, but we also shouldn't
# explode if I(state=absent) and the IDP doesn't exist # explode if I(state=absent) and the IDP doesn't exist
@@ -71,10 +53,6 @@
openstack.cloud.keystone_federation_protocol: openstack.cloud.keystone_federation_protocol:
state: 'absent' state: 'absent'
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
register: delete_protocol
- assert:
that:
- delete_protocol is successful
# ======================================================================== # ========================================================================
# Creation # Creation
@@ -86,19 +64,19 @@
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
mapping_id: '{{ mapping_name_1 }}' mapping_id: '{{ mapping_name_1 }}'
register: create_protocol register: create_protocol
- assert: - assert:
that: that:
- create_protocol is successful
- create_protocol is changed - create_protocol is changed
- name: 'Fetch Protocol info (should be absent)' - name: 'Fetch Protocol info (should be absent)'
openstack.cloud.keystone_federation_protocol_info: openstack.cloud.keystone_federation_protocol_info:
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
register: protocol_info register: protocol_info
ignore_errors: yes
- assert: - assert:
that: that:
- protocol_info is failed - protocol_info.protocols | length == 0
- name: 'Create protocol' - name: 'Create protocol'
openstack.cloud.keystone_federation_protocol: openstack.cloud.keystone_federation_protocol:
@@ -106,21 +84,19 @@
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
mapping_id: '{{ mapping_name_1 }}' mapping_id: '{{ mapping_name_1 }}'
register: create_protocol register: create_protocol
- assert: - assert:
that: that:
- create_protocol is successful
- create_protocol is changed - create_protocol is changed
- '"protocol" in create_protocol' - create_protocol.protocol.id == protocol_name
- '"id" in protocol' - create_protocol.protocol.name == protocol_name
- '"name" in protocol' - create_protocol.protocol.mapping_id == mapping_name_1
- '"idp_id" in protocol'
- '"mapping_id" in protocol' - name: assert return values of keystone_federation_protocol module
- protocol.id == protocol_name assert:
- protocol.name == protocol_name that:
- protocol.idp_id == idp_name # allow new fields to be introduced but prevent fields from being removed
- protocol.mapping_id == mapping_name_1 - expected_fields|difference(create_protocol.protocol.keys())|length == 0
vars:
protocol: '{{ create_protocol.protocol }}'
- name: 'Create protocol (retry - no change) - CHECK MODE' - name: 'Create protocol (retry - no change) - CHECK MODE'
check_mode: yes check_mode: yes
@@ -129,9 +105,9 @@
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
mapping_id: '{{ mapping_name_1 }}' mapping_id: '{{ mapping_name_1 }}'
register: create_protocol register: create_protocol
- assert: - assert:
that: that:
- create_protocol is successful
- create_protocol is not changed - create_protocol is not changed
- name: 'Create protocol (retry - no change)' - name: 'Create protocol (retry - no change)'
@@ -140,21 +116,13 @@
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
mapping_id: '{{ mapping_name_1 }}' mapping_id: '{{ mapping_name_1 }}'
register: create_protocol register: create_protocol
- assert: - assert:
that: that:
- create_protocol is successful
- create_protocol is not changed - create_protocol is not changed
- '"protocol" in create_protocol' - create_protocol.protocol.id == protocol_name
- '"id" in protocol' - create_protocol.protocol.name == protocol_name
- '"name" in protocol' - create_protocol.protocol.mapping_id == mapping_name_1
- '"idp_id" in protocol'
- '"mapping_id" in protocol'
- protocol.id == protocol_name
- protocol.name == protocol_name
- protocol.idp_id == idp_name
- protocol.mapping_id == mapping_name_1
vars:
protocol: '{{ create_protocol.protocol }}'
# ======================================================================== # ========================================================================
# Update # Update
@@ -166,9 +134,9 @@
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
mapping_id: '{{ mapping_name_2 }}' mapping_id: '{{ mapping_name_2 }}'
register: update_protocol register: update_protocol
- assert: - assert:
that: that:
- update_protocol is successful
- update_protocol is changed - update_protocol is changed
- name: 'Update protocol' - name: 'Update protocol'
@@ -177,21 +145,13 @@
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
mapping_id: '{{ mapping_name_2 }}' mapping_id: '{{ mapping_name_2 }}'
register: update_protocol register: update_protocol
- assert: - assert:
that: that:
- update_protocol is successful
- update_protocol is changed - update_protocol is changed
- '"protocol" in update_protocol' - update_protocol.protocol.id == protocol_name
- '"id" in protocol' - update_protocol.protocol.name == protocol_name
- '"name" in protocol' - update_protocol.protocol.mapping_id == mapping_name_2
- '"idp_id" in protocol'
- '"mapping_id" in protocol'
- protocol.id == protocol_name
- protocol.name == protocol_name
- protocol.idp_id == idp_name
- protocol.mapping_id == mapping_name_2
vars:
protocol: '{{ update_protocol.protocol }}'
- name: 'Update protocol (retry - no change) - CHECK MODE' - name: 'Update protocol (retry - no change) - CHECK MODE'
check_mode: yes check_mode: yes
@@ -200,9 +160,9 @@
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
mapping_id: '{{ mapping_name_2 }}' mapping_id: '{{ mapping_name_2 }}'
register: update_protocol register: update_protocol
- assert: - assert:
that: that:
- update_protocol is successful
- update_protocol is not changed - update_protocol is not changed
- name: 'Update protocol (retry - no change)' - name: 'Update protocol (retry - no change)'
@@ -211,21 +171,13 @@
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
mapping_id: '{{ mapping_name_2 }}' mapping_id: '{{ mapping_name_2 }}'
register: update_protocol register: update_protocol
- assert: - assert:
that: that:
- update_protocol is successful
- update_protocol is not changed - update_protocol is not changed
- '"protocol" in update_protocol' - update_protocol.protocol.id == protocol_name
- '"id" in protocol' - update_protocol.protocol.name == protocol_name
- '"name" in protocol' - update_protocol.protocol.mapping_id == mapping_name_2
- '"idp_id" in protocol'
- '"mapping_id" in protocol'
- protocol.id == protocol_name
- protocol.name == protocol_name
- protocol.idp_id == idp_name
- protocol.mapping_id == mapping_name_2
vars:
protocol: '{{ update_protocol.protocol }}'
# ======================================================================== # ========================================================================
# Create second protocol to test openstack.cloud.keystone_federation_protocol_info # Create second protocol to test openstack.cloud.keystone_federation_protocol_info
@@ -238,19 +190,10 @@
register: create_protocol_2 register: create_protocol_2
- assert: - assert:
that: that:
- create_protocol_2 is successful
- create_protocol_2 is changed - create_protocol_2 is changed
- '"protocol" in create_protocol_2' - create_protocol_2.protocol.id == protocol_name_2
- '"id" in protocol' - create_protocol_2.protocol.name == protocol_name_2
- '"name" in protocol' - create_protocol_2.protocol.mapping_id == mapping_name_1
- '"idp_id" in protocol'
- '"mapping_id" in protocol'
- protocol.id == protocol_name_2
- protocol.name == protocol_name_2
- protocol.idp_id == idp_name
- protocol.mapping_id == mapping_name_1
vars:
protocol: '{{ create_protocol_2.protocol }}'
# ======================================================================== # ========================================================================
# Basic tests of openstack.cloud.keystone_federation_protocol_info # Basic tests of openstack.cloud.keystone_federation_protocol_info
@@ -259,49 +202,36 @@
openstack.cloud.keystone_federation_protocol_info: openstack.cloud.keystone_federation_protocol_info:
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
register: protocol_info register: protocol_info
- name: Check info about protocols
assert:
that:
- protocol_info.protocols|length > 0
# allow new fields to be introduced but prevent fields from being removed
- expected_fields|difference(protocol_info.protocols[0].keys())|length == 0
- assert: - assert:
that: that:
- protocol_info is successful - protocol_info.protocols[0].id == protocol_name
- '"protocols" in protocol_info' - protocol_info.protocols[0].name == protocol_name
- protocol_info.protocols | length == 1 - protocol_info.protocols[0].mapping_id == mapping_name_2
- '"id" in protocol'
- '"name" in protocol'
- '"idp_id" in protocol'
- '"mapping_id" in protocol'
- protocol.id == protocol_name
- protocol.name == protocol_name
- protocol.idp_id == idp_name
- protocol.mapping_id == mapping_name_2
vars:
protocol: '{{ protocol_info.protocols[0] }}'
- name: 'Fetch Protocol info (all protocols on our test IDP)' - name: 'Fetch Protocol info (all protocols on our test IDP)'
openstack.cloud.keystone_federation_protocol_info: {} openstack.cloud.keystone_federation_protocol_info: {}
# idp_id defined in defaults at the start # idp_id defined in defaults at the start
register: protocol_info register: protocol_info
- assert: - assert:
that: that:
- protocol_info is successful
- '"protocols" in protocol_info'
# We created the IDP, and we're going to delete it: # We created the IDP, and we're going to delete it:
# we should be able to trust what's attached to it # we should be able to trust what's attached to it
- protocol_info.protocols | length == 2 - protocol_info.protocols | length == 2
- '"id" in protocol_1'
- '"name" in protocol_1'
- '"idp_id" in protocol_1'
- '"mapping_id" in protocol_1'
- '"id" in protocol_2'
- '"name" in protocol_2'
- '"idp_id" in protocol_2'
- '"mapping_id" in protocol_2'
- protocol_name in (protocol_info.protocols | map(attribute='id')) - protocol_name in (protocol_info.protocols | map(attribute='id'))
- protocol_name in (protocol_info.protocols | map(attribute='id')) - protocol_name in (protocol_info.protocols | map(attribute='id'))
- protocol_name_2 in (protocol_info.protocols | map(attribute='name')) - protocol_name_2 in (protocol_info.protocols | map(attribute='name'))
- protocol_name_2 in (protocol_info.protocols | map(attribute='name')) - protocol_name_2 in (protocol_info.protocols | map(attribute='name'))
- mapping_name_1 in (protocol_info.protocols | map(attribute='mapping_id')) - mapping_name_1 in (protocol_info.protocols | map(attribute='mapping_id'))
- mapping_name_2 in (protocol_info.protocols | map(attribute='mapping_id')) - mapping_name_2 in (protocol_info.protocols | map(attribute='mapping_id'))
- protocol_1.idp_id == idp_name
- protocol_2.idp_id == idp_name
vars: vars:
protocol_1: '{{ protocol_info.protocols[0] }}' protocol_1: '{{ protocol_info.protocols[0] }}'
protocol_2: '{{ protocol_info.protocols[1] }}' protocol_2: '{{ protocol_info.protocols[1] }}'
@@ -315,9 +245,9 @@
state: 'absent' state: 'absent'
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
register: update_protocol register: update_protocol
- assert: - assert:
that: that:
- update_protocol is successful
- update_protocol is changed - update_protocol is changed
- name: 'Delete protocol' - name: 'Delete protocol'
@@ -325,9 +255,9 @@
state: 'absent' state: 'absent'
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
register: update_protocol register: update_protocol
- assert: - assert:
that: that:
- update_protocol is successful
- update_protocol is changed - update_protocol is changed
- name: 'Delete protocol (retry - no change) - CHECK MODE' - name: 'Delete protocol (retry - no change) - CHECK MODE'
@@ -336,9 +266,9 @@
state: 'absent' state: 'absent'
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
register: update_protocol register: update_protocol
- assert: - assert:
that: that:
- update_protocol is successful
- update_protocol is not changed - update_protocol is not changed
- name: 'Delete protocol (retry - no change)' - name: 'Delete protocol (retry - no change)'
@@ -346,9 +276,9 @@
state: 'absent' state: 'absent'
name: '{{ protocol_name }}' name: '{{ protocol_name }}'
register: update_protocol register: update_protocol
- assert: - assert:
that: that:
- update_protocol is successful
- update_protocol is not changed - update_protocol is not changed
# ======================================================================== # ========================================================================

View File

@@ -4,62 +4,85 @@
# Copyright: Ansible Project # Copyright: Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = ''' DOCUMENTATION = r'''
--- ---
module: keystone_federation_protocol module: keystone_federation_protocol
short_description: manage a federation Protocol short_description: Manage a Keystone federation protocol
author: OpenStack Ansible SIG author: OpenStack Ansible SIG
description: description:
- Manage a federation Protocol. - Manage a Keystone federation protocol.
options: options:
name: name:
description: description:
- The name of the Protocol. - ID or name of the federation protocol.
- This attribute cannot be updated.
type: str type: str
required: true required: true
aliases: ['id'] aliases: ['id']
idp:
description:
- ID or name of the identity provider this protocol is associated with.
- This attribute cannot be updated.
aliases: ['idp_id', 'idp_name']
required: true
type: str
mapping:
description:
- ID or name of the mapping to use for this protocol.
- Required when creating a new protocol.
type: str
aliases: ['mapping_id', 'mapping_name']
state: state:
description: description:
- Whether the protocol should be C(present) or C(absent). - Whether the protocol should be C(present) or C(absent).
choices: ['present', 'absent'] choices: ['present', 'absent']
default: present default: present
type: str type: str
idp_id: notes:
description: - Name equals the ID of a federation protocol.
- The name of the Identity Provider this Protocol is associated with. - Name equals the ID of an identity provider.
aliases: ['idp_name'] - Name equals the ID of a mapping.
required: true
type: str
mapping_id:
description:
- The name of the Mapping to use for this Protocol.'
- Required when creating a new Protocol.
type: str
aliases: ['mapping_name']
requirements: requirements:
- "python >= 3.6" - "python >= 3.6"
- "openstacksdk >= 0.44" - "openstacksdk"
extends_documentation_fragment: extends_documentation_fragment:
- openstack.cloud.openstack - openstack.cloud.openstack
''' '''
EXAMPLES = ''' EXAMPLES = r'''
- name: Create a protocol - name: Create a protocol
openstack.cloud.keystone_federation_protocol: openstack.cloud.keystone_federation_protocol:
cloud: example_cloud cloud: example_cloud
name: example_protocol name: example_protocol
idp_id: example_idp idp: example_idp
mapping_id: example_mapping mapping: example_mapping
- name: Delete a protocol - name: Delete a protocol
openstack.cloud.keystone_federation_protocol: openstack.cloud.keystone_federation_protocol:
cloud: example_cloud cloud: example_cloud
name: example_protocol name: example_protocol
idp_id: example_idp idp: example_idp
state: absent state: absent
''' '''
RETURN = ''' RETURN = r'''
protocol:
description: Dictionary describing the federation protocol.
returned: always
type: dict
contains:
id:
description: ID of the federation protocol.
returned: success
type: str
mapping_id:
description: The definition of the federation protocol.
returned: success
type: str
name:
description: Name of the protocol. Equal to C(id).
returned: success
type: str
''' '''
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
@@ -69,115 +92,91 @@ class IdentityFederationProtocolModule(OpenStackModule):
argument_spec = dict( argument_spec = dict(
name=dict(required=True, aliases=['id']), name=dict(required=True, aliases=['id']),
state=dict(default='present', choices=['absent', 'present']), state=dict(default='present', choices=['absent', 'present']),
idp_id=dict(required=True, aliases=['idp_name']), idp=dict(required=True, aliases=['idp_id', 'idp_name']),
mapping_id=dict(aliases=['mapping_name']), mapping=dict(aliases=['mapping_id', 'mapping_name']),
) )
module_kwargs = dict( module_kwargs = dict(
required_if=[
('state', 'present', ('mapping',)),
],
supports_check_mode=True supports_check_mode=True
) )
def normalize_protocol(self, protocol):
"""
Normalizes the protocol definitions so that the outputs are consistent with the
parameters
- "name" (parameter) == "id" (SDK)
"""
if protocol is None:
return None
_protocol = protocol.to_dict()
_protocol['name'] = protocol['id']
# As of 0.44 SDK doesn't copy the URI parameters over, so let's add them
_protocol['idp_id'] = protocol['idp_id']
return _protocol
def delete_protocol(self, protocol):
"""
Delete an existing Protocol
returns: the "Changed" state
"""
if protocol is None:
return False
if self.ansible.check_mode:
return True
self.conn.identity.delete_federation_protocol(None, protocol)
return True
def create_protocol(self, name):
"""
Create a new Protocol
returns: the "Changed" state and the new protocol
"""
if self.ansible.check_mode:
return True, None
idp_name = self.params.get('idp_id')
mapping_id = self.params.get('mapping_id')
attributes = {
'idp_id': idp_name,
'mapping_id': mapping_id,
}
protocol = self.conn.identity.create_federation_protocol(id=name, **attributes)
return (True, protocol)
def update_protocol(self, protocol):
"""
Update an existing Protocol
returns: the "Changed" state and the new protocol
"""
mapping_id = self.params.get('mapping_id')
attributes = {}
if (mapping_id is not None) and (mapping_id != protocol.mapping_id):
attributes['mapping_id'] = mapping_id
if not attributes:
return False, protocol
if self.ansible.check_mode:
return True, None
new_protocol = self.conn.identity.update_federation_protocol(None, protocol, **attributes)
return (True, new_protocol)
def run(self): def run(self):
""" Module entry point """ state = self.params['state']
name = self.params.get('name')
state = self.params.get('state')
idp = self.params.get('idp_id')
changed = False
protocol = self.conn.identity.find_federation_protocol(idp, name) id = self.params['name']
idp_id = self.params['idp']
protocol = self.conn.identity.find_federation_protocol(idp_id, id)
if state == 'absent': if self.ansible.check_mode:
if protocol is not None: self.exit_json(changed=self._will_change(state, protocol))
changed = self.delete_protocol(protocol)
self.exit_json(changed=changed)
# state == 'present' if state == 'present' and not protocol:
# Create protocol
protocol = self._create()
self.exit_json(changed=True,
protocol=protocol.to_dict(computed=False))
elif state == 'present' and protocol:
# Update protocol
update = self._build_update(protocol)
if update:
protocol = self._update(protocol, update)
self.exit_json(changed=bool(update),
protocol=protocol.to_dict(computed=False))
elif state == 'absent' and protocol:
# Delete protocol
self._delete(protocol)
self.exit_json(changed=True)
elif state == 'absent' and not protocol:
# Do nothing
self.exit_json(changed=False)
def _build_update(self, protocol):
update = {}
attributes = dict(
(k, self.params[p])
for (p, k) in {'mapping': 'mapping_id'}.items()
if p in self.params and self.params[p] is not None
and self.params[p] != protocol[k])
if attributes:
update['attributes'] = attributes
return update
def _create(self):
return self.conn.identity.create_federation_protocol(
id=self.params['name'],
idp_id=self.params['idp'],
mapping_id=self.params['mapping'])
def _delete(self, protocol):
self.conn.identity.delete_federation_protocol(None, protocol)
def _update(self, protocol, update):
attributes = update.get('attributes')
if attributes:
protocol = self.conn.identity.update_federation_protocol(
protocol.idp_id, protocol.id, **attributes)
return protocol
def _will_change(self, state, protocol):
if state == 'present' and not protocol:
return True
elif state == 'present' and protocol:
return bool(self._build_update(protocol))
elif state == 'absent' and protocol:
return True
else: else:
if protocol is None: # state == 'absent' and not protocol:
if self.params.get('mapping_id') is None: return False
self.fail_json(
msg='A mapping_id must be passed when creating'
' a protocol')
(changed, protocol) = self.create_protocol(name)
protocol = self.normalize_protocol(protocol)
self.exit_json(changed=changed, protocol=protocol)
else:
(changed, new_protocol) = self.update_protocol(protocol)
new_protocol = self.normalize_protocol(new_protocol)
self.exit_json(changed=changed, protocol=new_protocol)
def main(): def main():

View File

@@ -4,47 +4,67 @@
# Copyright: Ansible Project # Copyright: Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = ''' DOCUMENTATION = r'''
--- ---
module: keystone_federation_protocol_info module: keystone_federation_protocol_info
short_description: get information about federation Protocols short_description: Fetch Keystone federation protocols
author: OpenStack Ansible SIG author: OpenStack Ansible SIG
description: description:
- Get information about federation Protocols. - Fetch Keystone federation protocols.
options: options:
name: name:
description: description:
- The name of the Protocol. - ID or name of the federation protocol.
type: str type: str
aliases: ['id'] aliases: ['id']
idp_id: idp:
description: description:
- The name of the Identity Provider this Protocol is associated with. - ID or name of the identity provider this protocol is associated with.
aliases: ['idp_name'] aliases: ['idp_id', 'idp_name']
required: true required: true
type: str type: str
notes:
- Name equals the ID of a federation protocol.
- Name equals the ID of an identity provider.
requirements: requirements:
- "python >= 3.6" - "python >= 3.6"
- "openstacksdk >= 0.44" - "openstacksdk"
extends_documentation_fragment: extends_documentation_fragment:
- openstack.cloud.openstack - openstack.cloud.openstack
''' '''
EXAMPLES = ''' EXAMPLES = r'''
- name: Describe a protocol - name: Fetch all federation protocols attached to an identity provider
openstack.cloud.keystone_federation_protocol_info: openstack.cloud.keystone_federation_protocol_info:
cloud: example_cloud cloud: example_cloud
name: example_protocol idp: example_idp
idp_id: example_idp
mapping_name: example_mapping
- name: Describe all protocols attached to an IDP - name: Fetch federation protocol by name
openstack.cloud.keystone_federation_protocol_info: openstack.cloud.keystone_federation_protocol_info:
cloud: example_cloud cloud: example_cloud
idp_id: example_idp idp: example_idp
name: example_protocol
''' '''
RETURN = ''' RETURN = r'''
protocols:
description: List of federation protocol dictionaries.
returned: always
type: list
elements: dict
contains:
id:
description: ID of the federation protocol.
returned: success
type: str
mapping_id:
description: The definition of the federation protocol.
returned: success
type: str
name:
description: Name of the protocol. Equal to C(id).
returned: success
type: str
''' '''
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
@@ -53,42 +73,29 @@ from ansible_collections.openstack.cloud.plugins.module_utils.openstack import O
class IdentityFederationProtocolInfoModule(OpenStackModule): class IdentityFederationProtocolInfoModule(OpenStackModule):
argument_spec = dict( argument_spec = dict(
name=dict(aliases=['id']), name=dict(aliases=['id']),
idp_id=dict(required=True, aliases=['idp_name']), idp=dict(required=True, aliases=['idp_id', 'idp_name']),
) )
module_kwargs = dict( module_kwargs = dict(
supports_check_mode=True supports_check_mode=True
) )
def normalize_protocol(self, protocol):
"""
Normalizes the protocol definitions so that the outputs are consistent with the
parameters
- "name" (parameter) == "id" (SDK)
"""
if protocol is None:
return None
_protocol = protocol.to_dict()
_protocol['name'] = protocol['id']
# As of 0.44 SDK doesn't copy the URI parameters over, so let's add them
_protocol['idp_id'] = protocol['idp_id']
return _protocol
def run(self): def run(self):
""" Module entry point """ # name is id for federation protocols
id = self.params['name']
name = self.params.get('name') # name is id for identity providers
idp = self.params.get('idp_id') idp_id = self.params['idp']
if name:
protocol = self.conn.identity.get_federation_protocol(idp, name)
protocol = self.normalize_protocol(protocol)
self.exit_json(changed=False, protocols=[protocol])
if id:
protocol = self.conn.identity.find_federation_protocol(idp_id, id)
protocols = [protocol] if protocol else []
else: else:
protocols = list(map(self.normalize_protocol, self.conn.identity.federation_protocols(idp))) protocols = self.conn.identity.federation_protocols(idp_id)
self.exit_json(changed=False, protocols=protocols)
self.exit_json(changed=False,
protocols=[p.to_dict(computed=False)
for p in protocols])
def main(): def main():