Merge "Switch federation_protocol module to OpenStackModule"

This commit is contained in:
Zuul
2021-06-15 09:30:54 +00:00
committed by Gerrit Code Review

View File

@@ -60,13 +60,21 @@ 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(
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 = dict(
supports_check_mode=True
)
def normalize_protocol(self, protocol):
""" """
Normalizes the protocol definitions so that the outputs are consistent with the Normalizes the protocol definitions so that the outputs are consistent with the
parameters parameters
@@ -82,60 +90,48 @@ def normalize_protocol(protocol):
_protocol['idp_id'] = protocol['idp_id'] _protocol['idp_id'] = protocol['idp_id']
return _protocol return _protocol
def delete_protocol(self, protocol):
def delete_protocol(module, sdk, cloud, protocol):
""" """
Delete an existing Protocol Delete an existing Protocol
returns: the "Changed" state returns: the "Changed" state
""" """
if protocol is None: if protocol is None:
return False return False
if module.check_mode: if self.ansible.check_mode:
return True return True
try: self.conn.identity.delete_federation_protocol(None, protocol)
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 return True
def create_protocol(self, name):
def create_protocol(module, sdk, cloud, name):
""" """
Create a new Protocol Create a new Protocol
returns: the "Changed" state and the new protocol returns: the "Changed" state and the new protocol
""" """
if self.ansible.check_mode:
if module.check_mode:
return True, None return True, None
idp_name = module.params.get('idp_id') idp_name = self.params.get('idp_id')
mapping_id = module.params.get('mapping_id') mapping_id = self.params.get('mapping_id')
attributes = { attributes = {
'idp_id': idp_name, 'idp_id': idp_name,
'mapping_id': mapping_id, 'mapping_id': mapping_id,
} }
try: protocol = self.conn.identity.create_federation_protocol(id=name, **attributes)
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) return (True, protocol)
def update_protocol(self, protocol):
def update_protocol(module, sdk, cloud, protocol):
""" """
Update an existing Protocol Update an existing Protocol
returns: the "Changed" state and the new protocol returns: the "Changed" state and the new protocol
""" """
mapping_id = self.params.get('mapping_id')
mapping_id = module.params.get('mapping_id')
attributes = {} attributes = {}
@@ -145,66 +141,46 @@ def update_protocol(module, sdk, cloud, protocol):
if not attributes: if not attributes:
return False, protocol return False, protocol
if module.check_mode: if self.ansible.check_mode:
return True, None return True, None
try: new_protocol = self.conn.identity.update_federation_protocol(None, protocol, **attributes)
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) return (True, new_protocol)
def run(self):
def main():
""" Module entry point """ """ Module entry point """
name = self.params.get('name')
argument_spec = openstack_full_argument_spec( state = self.params.get('state')
name=dict(required=True, aliases=['id']), idp = self.params.get('idp_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
)
name = module.params.get('name')
state = module.params.get('state')
idp = module.params.get('idp_id')
changed = False changed = False
sdk, cloud = openstack_cloud_from_module(module, min_version="0.44") protocol = self.conn.identity.find_federation_protocol(idp, name)
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 state == 'absent':
if protocol is not None: if protocol is not None:
changed = delete_protocol(module, sdk, cloud, protocol) changed = self.delete_protocol(protocol)
module.exit_json(changed=changed) self.exit_json(changed=changed)
# state == 'present' # state == 'present'
else: else:
if protocol is None: if protocol is None:
if module.params.get('mapping_id') is None: if self.params.get('mapping_id') is None:
module.fail_json(msg='A mapping_id must be passed when creating' self.fail_json(
msg='A mapping_id must be passed when creating'
' a protocol') ' a protocol')
(changed, protocol) = create_protocol(module, sdk, cloud, name) (changed, protocol) = self.create_protocol(name)
protocol = normalize_protocol(protocol) protocol = self.normalize_protocol(protocol)
module.exit_json(changed=changed, protocol=protocol) self.exit_json(changed=changed, protocol=protocol)
else: else:
(changed, new_protocol) = update_protocol(module, sdk, cloud, protocol) (changed, new_protocol) = self.update_protocol(protocol)
new_protocol = normalize_protocol(new_protocol) new_protocol = self.normalize_protocol(new_protocol)
module.exit_json(changed=changed, protocol=new_protocol) self.exit_json(changed=changed, protocol=new_protocol)
def main():
module = IdentityFederationProtocolModule()
module()
if __name__ == '__main__': if __name__ == '__main__':