Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2019 Christoffer Reijer (Basalt AB)
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: role
25 version_added: 1.0.0
26 short_description: Manage Roles
27 description:
28 - Create, update, and delete Roles
29 author:
30 - "Christoffer Reijer (@ephracis) Basalt AB"
31 options:
32 name:
33 description: The name of the role
34 required: true
35 type: str
36 description:
37 description: Description of the role
38 required: false
39 type: str
40 filters:
41 description: Filters with permissions for this role
42 required: false
43 type: list
44 elements: dict
45 suboptions:
46 permissions:
47 description: List of permissions
48 required: true
49 type: list
50 elements: str
51 search:
52 description: Filter condition for the resources
53 required: false
54 type: str
55 extends_documentation_fragment:
56 - theforeman.foreman.foreman
57 - theforeman.foreman.foreman.entity_state
58 - theforeman.foreman.foreman.taxonomy
59 '''
60
61 EXAMPLES = '''
62 - name: role
63 theforeman.foreman.role:
64 name: "Provisioner"
65 description: "Only provision on libvirt"
66 locations:
67 - "Uppsala"
68 organizations:
69 - "ACME"
70 filters:
71 - permissions:
72 - view_hosts
73 search: "owner_type = Usergroup and owner_id = 4"
74 server_url: "https://foreman.example.com"
75 username: "admin"
76 password: "changeme"
77 state: present
78 '''
79
80 RETURN = '''
81 entity:
82 description: Final state of the affected entities grouped by their type.
83 returned: success
84 type: dict
85 contains:
86 roles:
87 description: List of roles.
88 type: list
89 elements: dict
90 '''
91
92 import copy
93
94 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanTaxonomicEntityAnsibleModule
95
96
97 filter_foreman_spec = dict(
98 id=dict(invisible=True),
99 permissions=dict(type='entity_list', required=True, resolve=False),
100 search=dict(),
101 )
102
103
104 class ForemanRoleModule(ForemanTaxonomicEntityAnsibleModule):
105 pass
106
107
108 def main():
109 module = ForemanRoleModule(
110 foreman_spec=dict(
111 name=dict(required=True),
112 description=dict(),
113 filters=dict(type='nested_list', foreman_spec=filter_foreman_spec),
114 ),
115 )
116
117 with module.api_connection():
118 entity = module.lookup_entity('entity')
119 new_entity = module.run()
120
121 filters = module.foreman_params.get("filters")
122 if not module.desired_absent and filters is not None:
123 scope = {'role_id': new_entity['id']}
124
125 if entity:
126 current_filters = [module.show_resource('filters', filter['id']) for filter in entity['filters']]
127 else:
128 current_filters = []
129 desired_filters = copy.deepcopy(filters)
130
131 for desired_filter in desired_filters:
132 # search for an existing filter
133 for current_filter in current_filters:
134 if desired_filter.get('search') == current_filter['search']:
135 if set(desired_filter.get('permissions', [])) == set(perm['name'] for perm in current_filter['permissions']):
136 current_filters.remove(current_filter)
137 break
138 else:
139 desired_filter['permissions'] = module.find_resources_by_name('permissions', desired_filter['permissions'], thin=True)
140 module.ensure_entity('filters', desired_filter, None, params=scope, state='present', foreman_spec=filter_foreman_spec)
141 for current_filter in current_filters:
142 module.ensure_entity('filters', None, {'id': current_filter['id']}, params=scope, state='absent', foreman_spec=filter_foreman_spec)
143
144
145 if __name__ == '__main__':
146 main()