Merge "Switch federation_mapping module to OpenStackModule"

This commit is contained in:
Zuul
2021-06-17 11:59:37 +00:00
committed by Gerrit Code Review

View File

@@ -78,13 +78,24 @@ 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_mapping(mapping): class IdentityFederationMappingModule(OpenStackModule):
argument_spec = dict(
name=dict(required=True, aliases=['id']),
state=dict(default='present', choices=['absent', 'present']),
rules=dict(type='list', elements='dict', options=dict(
local=dict(required=True, type='list', elements='dict'),
remote=dict(required=True, type='list', elements='dict')
)),
)
module_kwargs = dict(
required_if=[('state', 'present', ['rules'])],
supports_check_mode=True
)
def normalize_mapping(self, mapping):
""" """
Normalizes the mapping definitions so that the outputs are consistent with Normalizes the mapping definitions so that the outputs are consistent with
the parameters the parameters
@@ -98,27 +109,22 @@ def normalize_mapping(mapping):
_mapping['name'] = mapping['id'] _mapping['name'] = mapping['id']
return _mapping return _mapping
def create_mapping(self, name):
def create_mapping(module, sdk, cloud, name):
""" """
Attempt to create a Mapping Attempt to create a Mapping
returns: A tuple containing the "Changed" state and the created mapping returns: A tuple containing the "Changed" state and the created mapping
""" """
if module.check_mode: if self.ansible.check_mode:
return (True, None) return (True, None)
rules = module.params.get('rules') rules = self.params.get('rules')
try: mapping = self.conn.identity.create_mapping(id=name, rules=rules)
mapping = cloud.identity.create_mapping(id=name, rules=rules)
except sdk.exceptions.OpenStackCloudException as ex:
module.fail_json(msg='Failed to create mapping: {0}'.format(str(ex)))
return (True, mapping) return (True, mapping)
def delete_mapping(self, mapping):
def delete_mapping(module, sdk, cloud, mapping):
""" """
Attempt to delete a Mapping Attempt to delete a Mapping
@@ -127,17 +133,13 @@ def delete_mapping(module, sdk, cloud, mapping):
if mapping is None: if mapping is None:
return False return False
if module.check_mode: if self.ansible.check_mode:
return True return True
try: self.conn.identity.delete_mapping(mapping)
cloud.identity.delete_mapping(mapping)
except sdk.exceptions.OpenStackCloudException as ex:
module.fail_json(msg='Failed to delete mapping: {0}'.format(str(ex)))
return True return True
def update_mapping(self, mapping):
def update_mapping(module, sdk, cloud, mapping):
""" """
Attempt to delete a Mapping Attempt to delete a Mapping
@@ -145,73 +147,50 @@ def update_mapping(module, sdk, cloud, mapping):
""" """
current_rules = mapping.rules current_rules = mapping.rules
new_rules = module.params.get('rules') new_rules = self.params.get('rules')
# Nothing to do # Nothing to do
if current_rules == new_rules: if current_rules == new_rules:
return (False, mapping) return (False, mapping)
if module.check_mode: if self.ansible.check_mode:
return (True, None) return (True, None)
try: new_mapping = self.conn.identity.update_mapping(mapping, rules=new_rules)
new_mapping = cloud.identity.update_mapping(mapping, rules=new_rules)
except sdk.exceptions.OpenStackCloudException as ex:
module.fail_json(msg='Failed to update mapping: {0}'.format(str(ex)))
return (True, new_mapping) return (True, new_mapping)
def run(self):
def main():
""" Module entry point """ """ Module entry point """
argument_spec = openstack_full_argument_spec( name = self.params.get('name')
name=dict(required=True, aliases=['id']), state = self.params.get('state')
state=dict(default='present', choices=['absent', 'present']),
rules=dict(type='list', elements='dict', options=dict(
local=dict(required=True, type='list', elements='dict'),
remote=dict(required=True, type='list', elements='dict')
)),
)
module_kwargs = openstack_module_kwargs(
required_if=[('state', 'present', ['rules'])]
)
module = AnsibleModule(
argument_spec,
supports_check_mode=True,
**module_kwargs
)
name = module.params.get('name')
state = module.params.get('state')
changed = False changed = False
sdk, cloud = openstack_cloud_from_module(module, min_version="0.44") mapping = self.conn.identity.find_mapping(name)
try:
mapping = cloud.identity.get_mapping(name)
except sdk.exceptions.ResourceNotFound:
mapping = None
except sdk.exceptions.OpenStackCloudException as ex:
module.fail_json(msg='Failed to fetch mapping: {0}'.format(str(ex)))
if state == 'absent': if state == 'absent':
if mapping is not None: if mapping is not None:
changed = delete_mapping(module, sdk, cloud, mapping) changed = self.delete_mapping(mapping)
module.exit_json(changed=changed) self.exit_json(changed=changed)
# state == 'present' # state == 'present'
else: else:
if len(module.params.get('rules')) < 1: if len(self.params.get('rules')) < 1:
module.fail_json(msg='At least one rule must be passed') self.fail_json(msg='At least one rule must be passed')
if mapping is None: if mapping is None:
(changed, mapping) = create_mapping(module, sdk, cloud, name) (changed, mapping) = self.create_mapping(name)
mapping = normalize_mapping(mapping) mapping = self.normalize_mapping(mapping)
module.exit_json(changed=changed, mapping=mapping) self.exit_json(changed=changed, mapping=mapping)
else: else:
(changed, new_mapping) = update_mapping(module, sdk, cloud, mapping) (changed, new_mapping) = self.update_mapping(mapping)
new_mapping = normalize_mapping(new_mapping) new_mapping = self.normalize_mapping(new_mapping)
module.exit_json(mapping=new_mapping, changed=changed) self.exit_json(mapping=new_mapping, changed=changed)
def main():
module = IdentityFederationMappingModule()
module()
if __name__ == '__main__': if __name__ == '__main__':