From 0904ddd2fefac023483ddd47daa57932416e74c3 Mon Sep 17 00:00:00 2001 From: Artem Goncharov Date: Thu, 20 May 2021 16:04:40 +0200 Subject: [PATCH] Switch federation_protocol module to OpenStackModule Change-Id: I28a69171df0d4b2df768458f0b5cff72c3937f59 --- .../modules/keystone_federation_protocol.py | 240 ++++++++---------- 1 file changed, 108 insertions(+), 132 deletions(-) diff --git a/plugins/modules/keystone_federation_protocol.py b/plugins/modules/keystone_federation_protocol.py index f125d7f7..5a33d8a3 100644 --- a/plugins/modules/keystone_federation_protocol.py +++ b/plugins/modules/keystone_federation_protocol.py @@ -60,151 +60,127 @@ EXAMPLES = ''' RETURN = ''' ''' -from ansible.module_utils.basic import AnsibleModule -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 +from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule -def normalize_protocol(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(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( +class IdentityFederationProtocolModule(OpenStackModule): + argument_spec = dict( name=dict(required=True, aliases=['id']), state=dict(default='present', choices=['absent', 'present']), idp_id=dict(required=True, aliases=['idp_name']), mapping_id=dict(aliases=['mapping_name']), ) - module_kwargs = openstack_module_kwargs( - ) - module = AnsibleModule( - argument_spec, - supports_check_mode=True, - **module_kwargs + module_kwargs = dict( + supports_check_mode=True ) - name = module.params.get('name') - state = module.params.get('state') - idp = module.params.get('idp_id') - changed = False + def normalize_protocol(self, protocol): + """ + Normalizes the protocol definitions so that the outputs are consistent with the + parameters - sdk, cloud = openstack_cloud_from_module(module, min_version="0.44") - - 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: + - "name" (parameter) == "id" (SDK) + """ if protocol is None: - if module.params.get('mapping_id') is 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) + 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): + """ 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: - (changed, new_protocol) = update_protocol(module, sdk, cloud, protocol) - new_protocol = normalize_protocol(new_protocol) - module.exit_json(changed=changed, protocol=new_protocol) + if protocol is None: + if self.params.get('mapping_id') is None: + 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__':