mirror of
https://opendev.org/openstack/ansible-collections-openstack.git
synced 2026-03-26 21:43:02 +00:00
Refactored federation_idp{,_info} modules
Change-Id: Icbff6c799a9c33f1104633f7d9521f02228217a5
This commit is contained in:
@@ -4,54 +4,54 @@
|
||||
# Copyright: Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
DOCUMENTATION = r'''
|
||||
module: federation_idp
|
||||
short_description: manage a federation Identity Provider
|
||||
short_description: Manage an identity provider in a OpenStack cloud
|
||||
author: OpenStack Ansible SIG
|
||||
description:
|
||||
- Manage a federation Identity Provider.
|
||||
- Create, update or delete an identity provider of the OpenStack
|
||||
identity (Keystone) service.
|
||||
options:
|
||||
name:
|
||||
description:
|
||||
- The name of the Identity Provider.
|
||||
type: str
|
||||
required: true
|
||||
aliases: ['id']
|
||||
state:
|
||||
description:
|
||||
- Whether the Identity Provider should be C(present) or C(absent).
|
||||
choices: ['present', 'absent']
|
||||
default: present
|
||||
type: str
|
||||
description:
|
||||
description:
|
||||
- The description of the Identity Provider.
|
||||
- The description of the identity provider.
|
||||
type: str
|
||||
domain_id:
|
||||
description:
|
||||
- The ID of a domain that is associated with the Identity Provider.
|
||||
Federated users that authenticate with the Identity Provider will be
|
||||
- The ID of a domain that is associated with the identity provider.
|
||||
- Federated users that authenticate with the identity provider will be
|
||||
created under the domain specified.
|
||||
- Required when creating a new Identity Provider.
|
||||
- Required when creating a new identity provider.
|
||||
type: str
|
||||
enabled:
|
||||
id:
|
||||
description:
|
||||
- Whether the Identity Provider is enabled or not.
|
||||
- Will default to C(true) when creating a new Identity Provider.
|
||||
- The ID (and name) of the identity provider.
|
||||
type: str
|
||||
required: true
|
||||
aliases: ['name']
|
||||
is_enabled:
|
||||
description:
|
||||
- Whether the identity provider is enabled or not.
|
||||
- Will default to C(false) when creating a new identity provider.
|
||||
type: bool
|
||||
aliases: ['is_enabled']
|
||||
aliases: ['enabled']
|
||||
remote_ids:
|
||||
description:
|
||||
- "List of the unique Identity Provider's remote IDs."
|
||||
- Will default to an empty list when creating a new Identity Provider.
|
||||
- "List of the unique identity provider's remote IDs."
|
||||
- Will default to an empty list when creating a new identity provider.
|
||||
type: list
|
||||
elements: str
|
||||
state:
|
||||
description:
|
||||
- Whether the identity provider should be C(present) or C(absent).
|
||||
choices: ['present', 'absent']
|
||||
default: present
|
||||
type: str
|
||||
extends_documentation_fragment:
|
||||
- openstack.cloud.openstack
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
EXAMPLES = r'''
|
||||
- name: Create an identity provider
|
||||
openstack.cloud.federation_idp:
|
||||
cloud: example_cloud
|
||||
@@ -59,7 +59,7 @@ EXAMPLES = '''
|
||||
domain_id: 0123456789abcdef0123456789abcdef
|
||||
description: 'My example IDP'
|
||||
remote_ids:
|
||||
- 'https://auth.example.com/auth/realms/ExampleRealm'
|
||||
- 'https://auth.example.com/auth/realms/ExampleRealm'
|
||||
|
||||
- name: Delete an identity provider
|
||||
openstack.cloud.federation_idp:
|
||||
@@ -68,160 +68,86 @@ EXAMPLES = '''
|
||||
state: absent
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
RETURN = r'''
|
||||
identity_provider:
|
||||
description: Dictionary describing the identity providers
|
||||
returned: On success when I(state) is 'present'
|
||||
type: dict
|
||||
elements: dict
|
||||
contains:
|
||||
description:
|
||||
description: Identity provider description
|
||||
type: str
|
||||
sample: "demodescription"
|
||||
domain_id:
|
||||
description: Domain to which the identity provider belongs
|
||||
type: str
|
||||
sample: "default"
|
||||
id:
|
||||
description: Identity provider ID
|
||||
type: str
|
||||
sample: "test-idp"
|
||||
is_enabled:
|
||||
description: Indicates whether the identity provider is enabled
|
||||
type: bool
|
||||
name:
|
||||
description: Name of the identity provider, equals its ID.
|
||||
type: str
|
||||
sample: "test-idp"
|
||||
remote_ids:
|
||||
description: Remote IDs associated with the identity provider
|
||||
type: list
|
||||
description: Dictionary describing the identity providers
|
||||
returned: On success when I(state) is C(present).
|
||||
type: dict
|
||||
contains:
|
||||
description:
|
||||
description: Identity provider description
|
||||
type: str
|
||||
sample: "demodescription"
|
||||
domain_id:
|
||||
description: Domain to which the identity provider belongs
|
||||
type: str
|
||||
sample: "default"
|
||||
id:
|
||||
description: Identity provider ID
|
||||
type: str
|
||||
sample: "test-idp"
|
||||
is_enabled:
|
||||
description: Indicates whether the identity provider is enabled
|
||||
type: bool
|
||||
name:
|
||||
description: Name of the identity provider, equals its ID.
|
||||
type: str
|
||||
sample: "test-idp"
|
||||
remote_ids:
|
||||
description: Remote IDs associated with the identity provider
|
||||
type: list
|
||||
'''
|
||||
|
||||
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
|
||||
from ansible_collections.openstack.cloud.plugins.module_utils.resource import StateMachine
|
||||
|
||||
|
||||
class IdentityFederationIdpModule(OpenStackModule):
|
||||
class IdentityProviderModule(OpenStackModule):
|
||||
argument_spec = dict(
|
||||
name=dict(required=True, aliases=['id']),
|
||||
state=dict(default='present', choices=['absent', 'present']),
|
||||
description=dict(),
|
||||
domain_id=dict(),
|
||||
enabled=dict(type='bool', aliases=['is_enabled']),
|
||||
id=dict(required=True, aliases=['name']),
|
||||
is_enabled=dict(type='bool', aliases=['enabled']),
|
||||
remote_ids=dict(type='list', elements='str'),
|
||||
state=dict(default='present', choices=['absent', 'present']),
|
||||
)
|
||||
module_kwargs = dict(
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
def delete_identity_provider(self, idp):
|
||||
"""
|
||||
Delete an existing Identity Provider
|
||||
|
||||
returns: the "Changed" state
|
||||
"""
|
||||
if idp is None:
|
||||
return False
|
||||
|
||||
if self.ansible.check_mode:
|
||||
return True
|
||||
|
||||
self.conn.identity.delete_identity_provider(idp)
|
||||
return True
|
||||
|
||||
def create_identity_provider(self, name):
|
||||
"""
|
||||
Create a new Identity Provider
|
||||
|
||||
returns: the "Changed" state and the new identity provider
|
||||
"""
|
||||
|
||||
if self.ansible.check_mode:
|
||||
return True, None
|
||||
|
||||
description = self.params.get('description')
|
||||
enabled = self.params.get('enabled')
|
||||
domain_id = self.params.get('domain_id')
|
||||
remote_ids = self.params.get('remote_ids')
|
||||
|
||||
if enabled is None:
|
||||
enabled = True
|
||||
if remote_ids is None:
|
||||
remote_ids = []
|
||||
|
||||
attributes = {
|
||||
'domain_id': domain_id,
|
||||
'enabled': enabled,
|
||||
'remote_ids': remote_ids,
|
||||
}
|
||||
if description is not None:
|
||||
attributes['description'] = description
|
||||
|
||||
idp = self.conn.identity.create_identity_provider(id=name, **attributes)
|
||||
return (True, idp.to_dict(computed=False))
|
||||
|
||||
def update_identity_provider(self, idp):
|
||||
"""
|
||||
Update an existing Identity Provider
|
||||
|
||||
returns: the "Changed" state and the new identity provider
|
||||
"""
|
||||
|
||||
description = self.params.get('description')
|
||||
enabled = self.params.get('enabled')
|
||||
domain_id = self.params.get('domain_id')
|
||||
remote_ids = self.params.get('remote_ids')
|
||||
|
||||
attributes = {}
|
||||
|
||||
if (description is not None) and (description != idp.description):
|
||||
attributes['description'] = description
|
||||
if (enabled is not None) and (enabled != idp.is_enabled):
|
||||
attributes['enabled'] = enabled
|
||||
if (domain_id is not None) and (domain_id != idp.domain_id):
|
||||
attributes['domain_id'] = domain_id
|
||||
if (remote_ids is not None) and (remote_ids != idp.remote_ids):
|
||||
attributes['remote_ids'] = remote_ids
|
||||
|
||||
if not attributes:
|
||||
return False, idp.to_dict(computed=False)
|
||||
|
||||
if self.ansible.check_mode:
|
||||
return True, None
|
||||
|
||||
new_idp = self.conn.identity.update_identity_provider(idp, **attributes)
|
||||
return (True, new_idp.to_dict(computed=False))
|
||||
|
||||
def run(self):
|
||||
""" Module entry point """
|
||||
sm = StateMachine(connection=self.conn,
|
||||
service_name='identity',
|
||||
type_name='identity_provider',
|
||||
sdk=self.sdk)
|
||||
|
||||
name = self.params.get('name')
|
||||
state = self.params.get('state')
|
||||
changed = False
|
||||
kwargs = dict((k, self.params[k])
|
||||
for k in ['state', 'timeout']
|
||||
if self.params[k] is not None)
|
||||
|
||||
idp = self.conn.identity.find_identity_provider(name)
|
||||
kwargs['attributes'] = \
|
||||
dict((k, self.params[k])
|
||||
for k in ['description', 'domain_id', 'id', 'is_enabled',
|
||||
'remote_ids']
|
||||
if self.params[k] is not None)
|
||||
|
||||
if state == 'absent':
|
||||
if idp is not None:
|
||||
changed = self.delete_identity_provider(idp)
|
||||
self.exit_json(changed=changed)
|
||||
identity_provider, is_changed = \
|
||||
sm(check_mode=self.ansible.check_mode,
|
||||
updateable_attributes=None,
|
||||
non_updateable_attributes=['domain_id'],
|
||||
wait=False,
|
||||
**kwargs)
|
||||
|
||||
# state == 'present'
|
||||
if identity_provider is None:
|
||||
self.exit_json(changed=is_changed)
|
||||
else:
|
||||
if idp is None:
|
||||
if self.params.get('domain_id') is None:
|
||||
self.fail_json(msg='A domain_id must be passed when creating'
|
||||
' an identity provider')
|
||||
(changed, idp) = self.create_identity_provider(name)
|
||||
self.exit_json(changed=changed, identity_provider=idp)
|
||||
|
||||
(changed, new_idp) = self.update_identity_provider(idp)
|
||||
self.exit_json(changed=changed, identity_provider=new_idp)
|
||||
self.exit_json(
|
||||
changed=is_changed,
|
||||
identity_provider=identity_provider.to_dict(computed=False))
|
||||
|
||||
|
||||
def main():
|
||||
module = IdentityFederationIdpModule()
|
||||
module = IdentityProviderModule()
|
||||
module()
|
||||
|
||||
|
||||
|
||||
@@ -4,25 +4,23 @@
|
||||
# Copyright: Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
DOCUMENTATION = r'''
|
||||
module: federation_idp_info
|
||||
short_description: Get the information about the available federation identity
|
||||
providers
|
||||
short_description: Fetch OpenStack federation identity providers
|
||||
author: OpenStack Ansible SIG
|
||||
description:
|
||||
- Fetch available federation identity providers.
|
||||
- Fetch OpenStack federation identity providers.
|
||||
options:
|
||||
name:
|
||||
id:
|
||||
description:
|
||||
- The name of the identity provider to fetch.
|
||||
- The ID (and name) of the identity provider to fetch.
|
||||
type: str
|
||||
aliases: ['id']
|
||||
aliases: ['name']
|
||||
extends_documentation_fragment:
|
||||
- openstack.cloud.openstack
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
EXAMPLES = r'''
|
||||
- name: Fetch a specific identity provider
|
||||
openstack.cloud.federation_idp_info:
|
||||
cloud: example_cloud
|
||||
@@ -33,35 +31,35 @@ EXAMPLES = '''
|
||||
cloud: example_cloud
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
RETURN = r'''
|
||||
identity_providers:
|
||||
description: Dictionary describing the identity providers
|
||||
returned: success
|
||||
type: list
|
||||
elements: dict
|
||||
contains:
|
||||
description:
|
||||
description: Identity provider description
|
||||
type: str
|
||||
sample: "demodescription"
|
||||
domain_id:
|
||||
description: Domain to which the identity provider belongs
|
||||
type: str
|
||||
sample: "default"
|
||||
id:
|
||||
description: Identity provider ID
|
||||
type: str
|
||||
sample: "test-idp"
|
||||
is_enabled:
|
||||
description: Indicates wether the identity provider is enabled
|
||||
type: bool
|
||||
name:
|
||||
description: Name of the identity provider, equals its ID.
|
||||
type: str
|
||||
sample: "test-idp"
|
||||
remote_ids:
|
||||
description: Remote IDs associated with the identity provider
|
||||
type: list
|
||||
description: Dictionary describing the identity providers
|
||||
returned: always
|
||||
type: list
|
||||
elements: dict
|
||||
contains:
|
||||
description:
|
||||
description: Identity provider description
|
||||
type: str
|
||||
sample: "demodescription"
|
||||
domain_id:
|
||||
description: Domain to which the identity provider belongs
|
||||
type: str
|
||||
sample: "default"
|
||||
id:
|
||||
description: Identity provider ID
|
||||
type: str
|
||||
sample: "test-idp"
|
||||
is_enabled:
|
||||
description: Indicates whether the identity provider is enabled
|
||||
type: bool
|
||||
name:
|
||||
description: Name of the identity provider, equals its ID.
|
||||
type: str
|
||||
sample: "test-idp"
|
||||
remote_ids:
|
||||
description: Remote IDs associated with the identity provider
|
||||
type: list
|
||||
'''
|
||||
|
||||
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
|
||||
@@ -69,24 +67,21 @@ from ansible_collections.openstack.cloud.plugins.module_utils.openstack import O
|
||||
|
||||
class IdentityFederationIdpInfoModule(OpenStackModule):
|
||||
argument_spec = dict(
|
||||
name=dict(aliases=['id']),
|
||||
id=dict(aliases=['name']),
|
||||
)
|
||||
module_kwargs = dict(
|
||||
supports_check_mode=True
|
||||
)
|
||||
|
||||
def run(self):
|
||||
""" Module entry point """
|
||||
|
||||
name = self.params['name']
|
||||
|
||||
query = {}
|
||||
if name:
|
||||
query["id"] = name
|
||||
|
||||
idps = self.conn.identity.identity_providers(**query)
|
||||
idps = [idp.to_dict(computed=False) for idp in idps]
|
||||
self.exit_json(changed=False, identity_providers=idps)
|
||||
kwargs = dict((k, self.params[k])
|
||||
for k in ['id']
|
||||
if self.params[k] is not None)
|
||||
identity_providers = self.conn.identity.identity_providers(**kwargs)
|
||||
self.exit_json(
|
||||
changed=False,
|
||||
identity_providers=[i.to_dict(computed=False)
|
||||
for i in identity_providers])
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
Reference in New Issue
Block a user