mirror of
https://opendev.org/openstack/ansible-collections-openstack.git
synced 2026-05-08 06:13:13 +00:00
Merge "Switch federation_protocol module to OpenStackModule"
This commit is contained in:
@@ -60,151 +60,127 @@ EXAMPLES = '''
|
|||||||
RETURN = '''
|
RETURN = '''
|
||||||
'''
|
'''
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
|
||||||
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import openstack_full_argument_spec
|
|
||||||
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import openstack_module_kwargs
|
|
||||||
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import openstack_cloud_from_module
|
|
||||||
|
|
||||||
|
|
||||||
def normalize_protocol(protocol):
|
class IdentityFederationProtocolModule(OpenStackModule):
|
||||||
"""
|
argument_spec = dict(
|
||||||
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(module, sdk, cloud, protocol):
|
|
||||||
"""
|
|
||||||
Delete an existing Protocol
|
|
||||||
|
|
||||||
returns: the "Changed" state
|
|
||||||
"""
|
|
||||||
|
|
||||||
if protocol is None:
|
|
||||||
return False
|
|
||||||
|
|
||||||
if module.check_mode:
|
|
||||||
return True
|
|
||||||
|
|
||||||
try:
|
|
||||||
cloud.identity.delete_federation_protocol(None, protocol)
|
|
||||||
except sdk.exceptions.OpenStackCloudException as ex:
|
|
||||||
module.fail_json(msg='Failed to delete protocol: {0}'.format(str(ex)))
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def create_protocol(module, sdk, cloud, name):
|
|
||||||
"""
|
|
||||||
Create a new Protocol
|
|
||||||
|
|
||||||
returns: the "Changed" state and the new protocol
|
|
||||||
"""
|
|
||||||
|
|
||||||
if module.check_mode:
|
|
||||||
return True, None
|
|
||||||
|
|
||||||
idp_name = module.params.get('idp_id')
|
|
||||||
mapping_id = module.params.get('mapping_id')
|
|
||||||
|
|
||||||
attributes = {
|
|
||||||
'idp_id': idp_name,
|
|
||||||
'mapping_id': mapping_id,
|
|
||||||
}
|
|
||||||
|
|
||||||
try:
|
|
||||||
protocol = cloud.identity.create_federation_protocol(id=name, **attributes)
|
|
||||||
except sdk.exceptions.OpenStackCloudException as ex:
|
|
||||||
module.fail_json(msg='Failed to create protocol: {0}'.format(str(ex)))
|
|
||||||
return (True, protocol)
|
|
||||||
|
|
||||||
|
|
||||||
def update_protocol(module, sdk, cloud, protocol):
|
|
||||||
"""
|
|
||||||
Update an existing Protocol
|
|
||||||
|
|
||||||
returns: the "Changed" state and the new protocol
|
|
||||||
"""
|
|
||||||
|
|
||||||
mapping_id = module.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 module.check_mode:
|
|
||||||
return True, None
|
|
||||||
|
|
||||||
try:
|
|
||||||
new_protocol = cloud.identity.update_federation_protocol(None, protocol, **attributes)
|
|
||||||
except sdk.exceptions.OpenStackCloudException as ex:
|
|
||||||
module.fail_json(msg='Failed to update protocol: {0}'.format(str(ex)))
|
|
||||||
return (True, new_protocol)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
""" Module entry point """
|
|
||||||
|
|
||||||
argument_spec = openstack_full_argument_spec(
|
|
||||||
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_id=dict(required=True, aliases=['idp_name']),
|
||||||
mapping_id=dict(aliases=['mapping_name']),
|
mapping_id=dict(aliases=['mapping_name']),
|
||||||
)
|
)
|
||||||
module_kwargs = openstack_module_kwargs(
|
module_kwargs = dict(
|
||||||
)
|
supports_check_mode=True
|
||||||
module = AnsibleModule(
|
|
||||||
argument_spec,
|
|
||||||
supports_check_mode=True,
|
|
||||||
**module_kwargs
|
|
||||||
)
|
)
|
||||||
|
|
||||||
name = module.params.get('name')
|
def normalize_protocol(self, protocol):
|
||||||
state = module.params.get('state')
|
"""
|
||||||
idp = module.params.get('idp_id')
|
Normalizes the protocol definitions so that the outputs are consistent with the
|
||||||
changed = False
|
parameters
|
||||||
|
|
||||||
sdk, cloud = openstack_cloud_from_module(module, min_version="0.44")
|
- "name" (parameter) == "id" (SDK)
|
||||||
|
"""
|
||||||
try:
|
|
||||||
protocol = cloud.identity.get_federation_protocol(idp, name)
|
|
||||||
except sdk.exceptions.ResourceNotFound:
|
|
||||||
protocol = None
|
|
||||||
except sdk.exceptions.OpenStackCloudException as ex:
|
|
||||||
module.fail_json(msg='Failed to get protocol: {0}'.format(str(ex)))
|
|
||||||
|
|
||||||
if state == 'absent':
|
|
||||||
if protocol is not None:
|
|
||||||
changed = delete_protocol(module, sdk, cloud, protocol)
|
|
||||||
module.exit_json(changed=changed)
|
|
||||||
|
|
||||||
# state == 'present'
|
|
||||||
else:
|
|
||||||
if protocol is None:
|
if protocol is None:
|
||||||
if module.params.get('mapping_id') is None:
|
return None
|
||||||
module.fail_json(msg='A mapping_id must be passed when creating'
|
|
||||||
' a protocol')
|
|
||||||
(changed, protocol) = create_protocol(module, sdk, cloud, name)
|
|
||||||
protocol = normalize_protocol(protocol)
|
|
||||||
module.exit_json(changed=changed, protocol=protocol)
|
|
||||||
|
|
||||||
|
_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):
|
||||||
|
""" Module entry point """
|
||||||
|
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)
|
||||||
|
|
||||||
|
if state == 'absent':
|
||||||
|
if protocol is not None:
|
||||||
|
changed = self.delete_protocol(protocol)
|
||||||
|
self.exit_json(changed=changed)
|
||||||
|
|
||||||
|
# state == 'present'
|
||||||
else:
|
else:
|
||||||
(changed, new_protocol) = update_protocol(module, sdk, cloud, protocol)
|
if protocol is None:
|
||||||
new_protocol = normalize_protocol(new_protocol)
|
if self.params.get('mapping_id') is None:
|
||||||
module.exit_json(changed=changed, protocol=new_protocol)
|
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():
|
||||||
|
module = IdentityFederationProtocolModule()
|
||||||
|
module()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user