Debian Codegraph @

ansible_collections/theforeman/foreman/plugins/modules/external_usergroup.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2019 Kirill Shirinkin ([email protected])
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import absolute_import, division, print_function
19 __metaclass__ = type
20
21
22 DOCUMENTATION = '''
23 ---
24 module: external_usergroup
25 version_added: 1.0.0
26 short_description: Manage External User Groups
27 description:
28 - Create, update, and delete external user groups
29 author:
30 - "Kirill Shirinkin (@Fodoj)"
31 options:
32 name:
33 description:
34 - Name of the group
35 required: true
36 type: str
37 usergroup:
38 description:
39 - Name of the linked usergroup
40 required: true
41 type: str
42 auth_source:
43 description:
44 - Name of the authentication source to be used for this group
45 required: true
46 type: str
47 aliases:
48 - auth_source_ldap
49 extends_documentation_fragment:
50 - theforeman.foreman.foreman
51 - theforeman.foreman.foreman.entity_state
52 '''
53
54 EXAMPLES = '''
55 - name: Create an external user group
56 theforeman.foreman.external_usergroup:
57 name: test
58 auth_source: "My LDAP server"
59 usergroup: "Internal Usergroup"
60 state: present
61 - name: Link a group from FreeIPA
62 theforeman.foreman.external_usergroup:
63 name: ipa_users
64 auth_source: "External"
65 usergroup: "Internal Usergroup"
66 state: present
67 '''
68
69 RETURN = '''
70 entity:
71 description: Final state of the affected entities grouped by their type.
72 returned: success
73 type: dict
74 contains:
75 external_usergroups:
76 description: List of external usergroups.
77 type: list
78 elements: dict
79 '''
80
81 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanEntityAnsibleModule
82
83
84 class ForemanExternalUsergroupModule(ForemanEntityAnsibleModule):
85 pass
86
87
88 def main():
89 module = ForemanExternalUsergroupModule(
90 foreman_spec=dict(
91 name=dict(required=True),
92 usergroup=dict(required=True, type='entity', ensure=False),
93 auth_source=dict(required=True, aliases=['auth_source_ldap'], type='entity', flat_name='auth_source_id', resource_type='auth_sources'),
94 auth_source_ldap=dict(type='entity', invisible=True, flat_name='auth_source_id'),
95 auth_source_external=dict(type='entity', invisible=True, flat_name='auth_source_id'),
96 ),
97 )
98
99 entity = None
100
101 with module.api_connection():
102 params = module.scope_for('usergroup')
103 # There is no way to find by name via API search, so we need
104 # to iterate over all external user groups of a given usergroup
105 for external_usergroup in module.list_resource("external_usergroups", params=params):
106 if external_usergroup['name'] == module.foreman_params['name']:
107 entity = external_usergroup
108
109 module.set_entity('entity', entity)
110
111 auth_source = module.lookup_entity('auth_source')
112 if auth_source.get('type') == 'AuthSourceExternal':
113 module.set_entity('auth_source_external', auth_source)
114 elif auth_source.get('type') == 'AuthSourceLdap':
115 module.set_entity('auth_source_ldap', auth_source)
116 else:
117 module.fail_json(msg="Unsupported authentication source type: {0}".format(auth_source.get('type')))
118
119 module.run(params=params)
120
121
122 if __name__ == '__main__':
123 main()