This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
k5_project.py
307 lines (219 loc) · 8.97 KB
/
k5_project.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/python
import datetime
ANSIBLE_METADATA = {'status': ['preview'],
'supported_by': 'community',
'version': '1.0'}
DOCUMENTATION = '''
---
module: k5_project
short_description: create / delete projects
version_added: "1.0"
description:
- returns project id
options:
project_name:
description:
- Name project to create / delete
required: true
default: None
status:
description:
- present / absent
required: true
default: None
k5_auth:
description:
- dict of k5_auth module output.
required: true
default: None
requirements:
- "python >= 2.6"
'''
EXAMPLES = '''
- k5_project:
project_name: zzCrossNproj
status: present
k5_auth: "{{ k5_auth_facts }}"
'''
RETURN = '''
k5_project:
description: project details.
returned: On success when the project is created
type: dict
sample:
#TODO
'''
import requests
import os
import json
from ansible.module_utils.basic import *
############## Common debug ###############
k5_debug = False
k5_debug_out = []
def k5_debug_get():
"""Return our debug list"""
return k5_debug_out
def k5_debug_clear():
"""Clear our debug list"""
k5_debug_out = []
def k5_debug_add(s):
"""Add string to debug list if env K5_DEBUG is defined"""
global k5_debug_out
if k5_debug:
k5_debug_out.append(s)
############## k5 functions #############
def k5_get_endpoint(e,name):
"""Pull particular endpoint name from dict"""
return e['endpoints'][name]
def k5_list_projects(module):
"""Get project list"""
global k5_debug
if 'auth_spec' in module.params['k5_auth']:
k5_facts = module.params['k5_auth']
else:
module.fail_json(msg="k5_auth_facts not found, have you run k5_auth?")
endpoint = k5_facts['endpoints']['identityv3']
auth_token = k5_facts['auth_token']
user_id = k5_facts['user']['id']
project_name = module.params['project_name']
k5_debug_add('auth_token: {0}'.format(auth_token))
k5_debug_add('project_name: {0}'.format(project_name))
k5_debug_add('user_id: {0}'.format(user_id))
session = requests.Session()
headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'X-Auth-Token': auth_token }
url = endpoint + '/users/' + user_id + '/projects'
k5_debug_add('REQ: {0}'.format(url))
try:
response = session.request('GET', url, headers=headers)
except requests.exceptions.RequestException as e:
module.fail_json(msg=e)
if response.status_code not in (200,):
module.fail_json(msg="RESP: HTTP Code:" + str(response.status_code) + " " + str(response.content), debug=k5_debug_out)
#k5_debug_add('RESP: {0}'.format(response.json()))
return response.json()['projects']
def k5_create_project(module):
"""Create a project"""
if 'auth_spec' in module.params['k5_auth']:
k5_facts = module.params['k5_auth']
else:
module.fail_json(msg="k5_auth_facts not found, have you run k5_auth?")
endpoint = k5_facts['endpoints']['identityv3']
auth_token = k5_facts['auth_token']
contract_id = k5_facts['user']['domain']['id']
project_name = module.params['project_name']
projects_list = k5_list_projects(module) # get projects available to the user the token was created by
matched_projects = [x for x in projects_list if x['name'] == project_name]
if len(matched_projects) > 0:
module.exit_json(changed=False, msg="Project already exists", project=matched_projects[0])
k5_debug_add('auth_token: {0}'.format(auth_token))
k5_debug_add('project_name: {0}'.format(project_name))
k5_debug_add('contract_id: {0}'.format(contract_id))
k5_debug_add('projects: {0}'.format(projects_list))
# module.fail_json(msg="fail",zzK5_DEBUG=k5_debug_out)
session = requests.Session()
headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'X-Auth-Token': auth_token }
url = endpoint + '/projects?domain_id=' + contract_id
query_json = { "project":
{ "description": "Ansible created project - " + str(datetime.datetime.now()),
"domain_id": contract_id,
"enabled": True,
"is_domain": False,
"name": project_name
}
}
k5_debug_add('REQ: {0}'.format(url))
try:
response = session.request('POST', url, headers=headers, json=query_json)
except requests.exceptions.RequestException as e:
module.fail_json(msg=e)
# we failed to make a change
if response.status_code not in (201,):
module.fail_json(msg="RESP: HTTP Code:" + str(response.status_code) + " " + str(response.content), debug=k5_debug_out)
#k5_debug_add('RESP: {0}'.format(response.json()))
if k5_debug:
module.exit_json(changed=True, msg="Create Project Successful", debug=k5_debug_out, project=response.json()['project'] )
module.exit_json(changed=True, msg="Create Project Successful", project=response.json()['project'] )
def k5_delete_project(module):
"""delete a project"""
module.exit_json(changed=False, msg="There is no delete function for projects on K5!!!" )
def k5_patch_project(module):
"""Change state of a project"""
if 'auth_spec' in module.params['k5_auth']:
k5_facts = module.params['k5_auth']
else:
module.fail_json(msg="k5_auth_facts not found, have you run k5_auth?")
endpoint = k5_facts['endpoints']['identityv3']
auth_token = k5_facts['auth_token']
contract_id = k5_facts['user']['domain']['id']
project_name = module.params['project_name']
projects_list = k5_list_projects(module) # get projects available to the user the token was created by
#k5_debug_add('projects: {0}'.format(projects_list))
matched_projects = [x for x in projects_list if x['name'] == project_name]
if len(matched_projects) == 0:
module.exit_json(changed=False, msg="Project does not exist")
project_id = matched_projects[0]['id']
state = module.params['state']
if state == 'enabled':
set_state = True
else:
set_state = False
k5_debug_add('auth_token: {0}'.format(auth_token))
k5_debug_add('project_name: {0}'.format(project_name))
k5_debug_add('project_id: {0}'.format(project_id))
k5_debug_add('contract_id: {0}'.format(contract_id))
k5_debug_add('projects: {0}'.format(projects_list))
# module.fail_json(msg="fail",zzK5_DEBUG=k5_debug_out)
session = requests.Session()
headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'X-Auth-Token': auth_token }
url = endpoint + '/projects/' + project_id
query_json = { "project":
{ "description": "Ansible updated project - " + str(datetime.datetime.now()),
"enabled": set_state,
"name": project_name
}
}
k5_debug_add('REQ: {0}'.format(url))
try:
response = session.request('PATCH', url, headers=headers, json=query_json)
except requests.exceptions.RequestException as e:
module.fail_json(msg=e)
# we failed to make a change
if response.status_code not in (200,):
module.fail_json(msg="RESP: HTTP Code:" + str(response.status_code) + " " + str(response.content), debug=k5_debug_out)
if k5_debug:
module.exit_json(changed=True, msg="Create Project Successful", project=response.json()['project'], debug=k5_debug_out )
module.exit_json(changed=True, msg="Create Project Successful", project=response.json()['project'] )
######################################################################################
def main():
global k5_debug
module = AnsibleModule( argument_spec=dict(
project_name = dict(default=None, type='str'),
state = dict(choices=['list', 'present', 'absent', 'enable', 'disable'], default='list'),
k5_auth = dict(required=True, default=None, type='dict')
),
# constraints
required_if=[
('state', 'present', ['project_name']),
('state', 'absent', ['project_name']),
('state', 'enable', ['project_name']),
('state', 'disable', ['project_name']),
]
)
if 'K5_DEBUG' in os.environ:
k5_debug = True
#k5_debug_clear()
mp = module.params
if mp['state'] == 'present':
k5_create_project(module)
if mp['state'] == 'absent':
k5_delete_project(module)
if mp['state'] == 'disable':
k5_patch_project(module)
if mp['state'] == 'enable':
k5_patch_project(module)
if mp['state'] == 'list':
ps = k5_list_projects(module)
module.exit_json(changed=False, msg="List Projects Successful", k5_projects=ps )
######################################################################################
if __name__ == '__main__':
main()