forked from Nosmoht/ansible-module-foreman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foreman_filter.py
197 lines (174 loc) · 6.71 KB
/
foreman_filter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Ansible module to manage Foreman filter resources.
#
# This module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software. If not, see <http://www.gnu.org/licenses/>.
#
# (C) 2017 Guido Günther <[email protected]>
DOCUMENTATION = '''
---
module: foreman_filter
short_description: Manage Foreman Filter using Foreman API v2
description:
- Create and delete Foreman Filters using Foreman API v2
options:
role:
description: role this filter belongs tue
required: true
resource_type:
description: resource type this permission affects
required: true
permissions:
description: List of permissions
required: true
state:
description: State of filter
required: false
default: present
choices: ["present", "absent"]
foreman_host:
description: Hostname or IP address of Foreman system
required: false
default: 127.0.0.1
foreman_port:
description: Port of Foreman API
required: false
default: 443
foreman_user:
description: Username to be used to authenticate on Foreman
required: true
foreman_pass:
description: Password to be used to authenticate user on Foreman
required: true
foreman_ssl:
description: Enable SSL when connecting to Foreman API
required: false
default: true
notes:
- Requires the python-foreman package to be installed. See https://github.com/Nosmoht/python-foreman.
version_added: "2.0"
author: "Thomas Krahn (@nosmoht)"
'''
EXAMPLES = '''
- name: Ensure Production filter is present
foreman_filter:
role: "Host Power"
resource: Host
permissions:
- power_hosts
state: present
foreman_host: 127.0.0.1
foreman_port: 443
foreman_user: admin
foreman_pass: secret
'''
try:
from foreman.foreman import *
foremanclient_found = True
except ImportError:
foremanclient_found = False
def get_permission_ids(module, theforeman, resource_type, permissions):
result = []
for i in range(0, len(permissions)):
try:
permission = theforeman.search_permission(data={'resource_type': resource_type,
'name': permissions[i]})
if not permission:
module.fail_json(msg='Could not find Permission {0} for {1}'.format(permissions[i],
resource_type))
result.append(permission.get('id'))
except ForemanError as e:
module.fail_json(msg='Could not get Permissions: {0}'.format(e.message))
return result
def get_role_id(module, theforeman, rolename):
try:
role = theforeman.search_role(data={'name': rolename})
if not role:
module.fail_json(msg='Could not find role {0}'.format(rolename))
i = role.get('id')
if not i:
module.fail_json(msg='Can\'t find id for {0} in {1}'.format(rolename, role))
except ForemanError as e:
module.fail_json(msg='Could not get role: {0}'.format(e.message))
return role['id']
def ensure(module):
state = module.params['state']
role = module.params['role']
resource_type = module.params['resource_type']
permissions = module.params['permissions']
foreman_host = module.params['foreman_host']
foreman_port = module.params['foreman_port']
foreman_user = module.params['foreman_user']
foreman_pass = module.params['foreman_pass']
foreman_ssl = module.params['foreman_ssl']
theforeman = Foreman(hostname=foreman_host,
port=foreman_port,
username=foreman_user,
password=foreman_pass,
ssl=foreman_ssl)
data = dict()
data['permission_ids'] = get_permission_ids(module, theforeman, resource_type, permissions)
data['role_id'] = get_role_id(module, theforeman, role)
filters = theforeman.search_filter(data={'role_id': data['role_id']})
if not filters and state == 'present':
try:
filtr = theforeman.create_filter(data=data)
return True, filtr
except ForemanError as e:
module.fail_json(msg='Could not create filter: {0}'.format(e.message))
if filters:
if type(filters) != type([]):
filters = [filters]
if state == 'present':
for f in filters:
if sorted(p['id'] for p in f['permissions']) == sorted(data['permission_ids']):
return False, f
try:
# We have no way to decide if perms should be updated so best
# we can do is create a new one
filtr = theforeman.create_filter(data=data)
return True, filtr
except ForemanError as e:
module.fail_json(msg='Could not create filter: {0}'.format(e.message))
elif state == 'absent':
for f in filters:
if sorted(p['id'] for p in f['permissions']) == sorted(data['permission_ids']):
try:
filtr = theforeman.delete_filter(id=f['id'])
return True, filtr
except ForemanError as e:
module.fail_json(msg='Could not delete filter: {0}'.format(e.message))
return False, filtr
def main():
module = AnsibleModule(
argument_spec=dict(
state=dict(type='str', default='present', choices=['present', 'absent']),
role=dict(type='str', required=True),
resource_type=dict(type='str', required=True),
permissions=dict(type='list', required=True),
foreman_host=dict(type='str', default='127.0.0.1'),
foreman_port=dict(type='str', default='443'),
foreman_user=dict(type='str', required=True),
foreman_pass=dict(type='str', required=True, no_log=True),
foreman_ssl=dict(type='bool', default=True)
),
)
if not foremanclient_found:
module.fail_json(msg='python-foreman module is required. See https://github.com/Nosmoht/python-foreman.')
changed, filtr = ensure(module)
module.exit_json(changed=changed, filter=filtr)
from ansible.module_utils.basic import *
if __name__ == '__main__':
main()