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