Skip to content

Commit

Permalink
external_usergroup: support non-LDAP groups
Browse files Browse the repository at this point in the history
Fixes: theforeman#956
Obsoletes: theforeman#955
  • Loading branch information
evgeni committed Sep 16, 2020
1 parent f12c654 commit 80871b7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
4 changes: 4 additions & 0 deletions changelogs/fragments/956-external_usergroup-non-ldap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bugfixes:
- external_usergroup - support non-LDAP external groups (https://github.com/theforeman/foreman-ansible-modules/issues/956)
minor_changes:
- external_usergroup - rename the ``auth_source_ldap`` parameter to ``auth_source`` (``auth_source_ldap`` is still supported via an alias)
13 changes: 5 additions & 8 deletions plugins/module_utils/foreman_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,8 @@ def find_resource(self, resource, search, params=None, failsafe=False, thin=None
else:
error_msg = "no"
self.fail_json(msg="Found {0} results while searching for {1} with {2}".format(error_msg, resource, search))
if result:
if thin:
result = {'id': result['id']}
else:
result = self.show_resource(resource, result['id'], params=params)
if result and not thin:
result = self.show_resource(resource, result['id'], params=params)
return result

def find_resource_by(self, resource, search_field, value, **kwargs):
Expand Down Expand Up @@ -1164,7 +1161,7 @@ def _foreman_spec_helper(spec):
foreman_type = value.get('type')
flat_name = value.get('flat_name')

if foreman_type == 'entity':
if foreman_type in ['entity', 'invisible-entity']:
if not flat_name:
flat_name = '{0}_id'.format(key)
foreman_value['resource_type'] = HAS_APYPIE and inflector.pluralize(key)
Expand Down Expand Up @@ -1194,7 +1191,7 @@ def _foreman_spec_helper(spec):

foreman_spec[key] = foreman_value

if foreman_type != 'invisible':
if foreman_type not in ['invisible', 'invisible-entity']:
argument_spec[key] = argument_value

return foreman_spec, argument_spec
Expand All @@ -1210,7 +1207,7 @@ def _flatten_entity(entity, foreman_spec):
spec = foreman_spec[key]
flat_name = spec.get('flat_name', key)
property_type = spec.get('type', 'str')
if property_type == 'entity':
if property_type in ['entity', 'invisible-entity']:
result[flat_name] = value['id']
elif property_type == 'entity_list':
result[flat_name] = sorted(val['id'] for val in value)
Expand Down
14 changes: 12 additions & 2 deletions plugins/modules/external_usergroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@
- Name of the linked usergroup
required: true
type: str
auth_source_ldap:
auth_source:
description:
- Name of the authentication source to be used for this group
required: true
type: str
aliases:
- auth_source_ldap
extends_documentation_fragment:
- theforeman.foreman.foreman
- theforeman.foreman.foreman.entity_state
Expand Down Expand Up @@ -82,7 +84,9 @@ def main():
foreman_spec=dict(
name=dict(required=True),
usergroup=dict(required=True),
auth_source_ldap=dict(required=True, type='entity', flat_name='auth_source_id', resource_type='auth_sources'),
auth_source=dict(required=True, aliases=['auth_source_ldap'], type='entity', flat_name='auth_source_id', resource_type='auth_sources'),
auth_source_ldap=dict(type='invisible-entity', flat_name='auth_source_id'),
auth_source_external=dict(type='invisible-entity', flat_name='auth_source_id'),
),
)

Expand All @@ -98,6 +102,12 @@ def main():

module.set_entity('entity', entity)
module.auto_lookup_entities()
if module.foreman_params['auth_source']['type'] == 'AuthSourceExternal':
module.foreman_params['auth_source_external'] = module.foreman_params.pop('auth_source')
elif module.foreman_params['auth_source']['type'] == 'AuthSourceLdap':
module.foreman_params['auth_source_ldap'] = module.foreman_params.pop('auth_source')
else:
module.fail_json(msg="Unsupported authentication source type: {0}".format(module.foreman_params['auth_source']['type']))
module.ensure_entity('external_usergroups', module.foreman_params, entity, params)


Expand Down

0 comments on commit 80871b7

Please sign in to comment.