-
Notifications
You must be signed in to change notification settings - Fork 39
/
team.py
649 lines (478 loc) · 20.8 KB
/
team.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: team.py
#
# Copyright 2018 Costas Tyfoxylos
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
"""
Main code for team.
.. _Google Python Style Guide:
http://google.github.io/styleguide/pyguide.html
"""
import logging
from towerlib.towerlibexceptions import (InvalidUser,
PermissionNotFound,
InvalidProject,
InvalidJobTemplate,
InvalidInventory,
InvalidCredential,
InvalidValue,
InvalidOrganization)
from .core import (Entity,
EntityManager,
validate_max_length)
__author__ = '''Costas Tyfoxylos <ctyfoxylos@schubergphilis.com>'''
__docformat__ = '''google'''
__date__ = '''2018-01-03'''
__copyright__ = '''Copyright 2018, Costas Tyfoxylos'''
__credits__ = ["Costas Tyfoxylos"]
__license__ = '''MIT'''
__maintainer__ = '''Costas Tyfoxylos'''
__email__ = '''<ctyfoxylos@schubergphilis.com>'''
__status__ = '''Development''' # "Prototype", "Development", "Production".
# This is the main prefix used for logging
LOGGER_BASENAME = '''team'''
LOGGER = logging.getLogger(LOGGER_BASENAME)
LOGGER.addHandler(logging.NullHandler())
class Team(Entity): # pylint: disable=too-many-public-methods
"""Models the team entity of ansible tower."""
def __init__(self, tower_instance, data):
Entity.__init__(self, tower_instance, data)
self._object_roles = None
@property
def name(self):
"""The name of the team.
Returns:
string: The name of the team.
"""
return self._data.get('name')
@name.setter
def name(self, value):
"""Update the name of the team.
Returns:
None:
"""
max_characters = 512
conditions = [validate_max_length(value, max_characters)]
if all(conditions):
self._update_values('name', value)
else:
raise InvalidValue('{value} is invalid. Condition max_characters must be less or equal to '
'{max_characters}'.format(value=value, max_characters=max_characters))
@property
def description(self):
"""The description of the team.
Returns:
string: The description of the team.
"""
return self._data.get('description')
@description.setter
def description(self, value):
"""Update the description of the team.
Returns:
None:
"""
self._update_values('description', value)
@property
def organization(self):
"""The Organization of the team.
Returns:
Organization: The organization of the team.
"""
return self._tower.get_organization_by_id(self._data.get('organization'))
@organization.setter
def organization(self, value):
"""Update the organization of the team.
Returns:
None:
"""
organization = self._tower.get_organization_by_name(value)
if not organization:
raise InvalidOrganization(value)
self._update_values('organization', organization.id)
@property
def roles(self):
"""The roles.
Returns:
EntityManager: EntityManager of the roles.
"""
url = self._data.get('related', {}).get('roles')
return EntityManager(self._tower,
entity_object='Role',
primary_match_field='name',
url=url)
@property
def object_roles(self):
"""The object roles.
Returns:
EntityManager: EntityManager of the object roles supported.
"""
if not self._object_roles:
url = self._data.get('related', {}).get('object_roles')
self._object_roles = EntityManager(self._tower,
entity_object='ObjectRole',
primary_match_field='name',
url=url)
return self._object_roles
@property
def object_role_names(self):
"""The names of the object roles.
Returns:
list: A list of strings for the object_roles.
"""
return (object_role.name for object_role in self.object_roles)
@property
def users(self):
"""The users of the team.
Returns:
EntityManager: EntityManager of the users.
"""
url = self._data.get('related', {}).get('users')
return EntityManager(self._tower,
entity_object='User',
primary_match_field='username',
url=url)
@property
def credentials(self):
"""The credentials of the team.
Returns:
EntityManager: EntityManager of the credentials.
"""
url = self._data.get('related', {}).get('credentials')
return EntityManager(self._tower,
entity_object='Credential',
primary_match_field='name',
url=url)
@property
def projects(self):
"""The projects of the team.
Returns:
EntityManager: EntityManager of the projects.
"""
url = self._data.get('related', {}).get('projects')
return EntityManager(self._tower,
entity_object='Project',
primary_match_field='name',
url=url)
def get_user_by_username(self, username):
"""Retrieves a user of the team by its username.
Args:
username: The username of the user to retrieve.
Returns:
user (User) on success, None otherwise.
"""
return next((user for user in self.users
if user.username.lower() == username.lower()), None)
def add_user_as_member(self, username):
"""Adds a user as a member of the team.
Args:
username: The username of the user to add.
Returns:
True on success, False otherwise.
"""
return self._post_user_with_permission(username, 'member')
def remove_user_as_member(self, username):
"""Removes a user as a member of the team.
Args:
username: The username of the user to remove.
Returns:
True on success, False otherwise.
"""
return self._post_user_with_permission(username, 'member', remove=True)
def add_user_as_admin(self, username):
"""Adds a user as an admin of the team.
Args:
username: The username of the user to add.
Returns:
True on success, False otherwise.
"""
return self._post_user_with_permission(username, 'admin')
def remove_user_as_admin(self, username):
"""Removes a user as an admin of the team.
Args:
username: The username of the user to remove.
Returns:
True on success, False otherwise.
"""
return self._post_user_with_permission(username, 'admin', remove=True)
@staticmethod
def _get_permission(role_name, object_roles):
permission = next((role for role in object_roles
if role.name.lower() == role_name.lower()), None)
if not permission:
raise PermissionNotFound(role_name)
return permission
def _post_user_with_permission(self, username, role_name, remove=False):
permission = self._get_permission(role_name, self.object_roles)
user = self._tower.get_user_by_username(username)
if not user:
raise InvalidUser(username)
url = '{api}/users/{id}/roles/'.format(api=self._tower.api,
id=user.id)
payload = {'id': permission.id}
if remove:
roles_ids = [role.id for role in user.roles]
if permission.id not in roles_ids:
self._logger.warning('"%s" is not part of the team', username)
return False
payload['disassociate'] = True
response = self._tower.session.post(url, json=payload)
if not response.ok:
self._logger.error('Error posting to url "%s", response was: "%s"', url, response.text)
return response.ok
def add_project_permission_admin(self, project_name):
"""Adds a project with admin permissions.
Args:
project_name: The name of the project to add.
Returns:
True on success, False otherwise.
"""
return self._post_project_permission(project_name, 'admin')
def remove_project_permission_admin(self, project_name):
"""Removes a project with admin permissions.
Args:
project_name: The name of the project to remove.
Returns:
True on success, False otherwise.
"""
return self._post_project_permission(project_name, 'admin', remove=True)
def add_project_permission_update(self, project_name):
"""Adds a project with update permissions.
Args:
project_name: The name of the project to add.
Returns:
True on success, False otherwise.
"""
return self._post_project_permission(project_name, 'update')
def remove_project_permission_update(self, project_name):
"""Removes a project with update permissions.
Args:
project_name: The name of the project to remove.
Returns:
True on success, False otherwise.
"""
return self._post_project_permission(project_name, 'update', remove=True)
def add_project_permission_use(self, project_name):
"""Adds a project with use permissions.
Args:
project_name: The name of the project to add.
Returns:
True on success, False otherwise.
"""
return self._post_project_permission(project_name, 'use')
def remove_project_permission_use(self, project_name):
"""Removes a project with use permissions.
Args:
project_name: The name of the project to remove.
Returns:
True on success, False otherwise.
"""
return self._post_project_permission(project_name, 'use', remove=True)
def add_job_template_permission_admin(self, job_template_name):
"""Adds a job template with admin permissions.
Args:
job_template_name: The name of the job template to add.
Returns:
True on success, False otherwise.
"""
return self._post_job_template_permission(job_template_name, 'admin')
def remove_job_template_permission_admin(self, job_template_name):
"""Removes a job template with admin permissions.
Args:
job_template_name: The name of the job template to remove.
Returns:
True on success, False otherwise.
"""
return self._post_job_template_permission(job_template_name, 'admin', remove=True)
def add_job_template_permission_execute(self, job_template_name):
"""Adds a job template with execute permissions.
Args:
job_template_name: The name of the job template to add.
Returns:
True on success, False otherwise.
"""
return self._post_job_template_permission(job_template_name, 'execute')
def remove_job_template_permission_execute(self, job_template_name):
"""Removes a job template with execute permissions.
Args:
job_template_name: The name of the job template to remove.
Returns:
True on success, False otherwise.
"""
return self._post_job_template_permission(job_template_name, 'execute', remove=True)
def add_inventory_permission_admin(self, inventory_name):
"""Adds an inventory with admin permissions.
Args:
inventory_name: The name of the inventory to add.
Returns:
True on success, False otherwise.
"""
return self._post_inventory_permission(inventory_name, 'admin')
def remove_inventory_permission_admin(self, inventory_name):
"""Removes an inventory with admin permissions.
Args:
inventory_name: The name of the inventory to remove.
Returns:
True on success, False otherwise.
"""
return self._post_inventory_permission(inventory_name, 'admin', remove=True)
def add_inventory_permission_use(self, inventory_name):
"""Adds an inventory with use permissions.
Args:
inventory_name: The name of the inventory to add.
Returns:
True on success, False otherwise.
"""
return self._post_inventory_permission(inventory_name, 'use')
def remove_inventory_permission_use(self, inventory_name):
"""Removes an inventory with use permissions.
Args:
inventory_name: The name of the inventory to remove.
Returns:
True on success, False otherwise.
"""
return self._post_inventory_permission(inventory_name, 'use', remove=True)
def add_inventory_permission_update(self, inventory_name):
"""Adds an inventory with update permissions.
Args:
inventory_name: The name of the inventory to add.
Returns:
True on success, False otherwise.
"""
return self._post_inventory_permission(inventory_name, 'update')
def remove_inventory_permission_update(self, inventory_name):
"""Removes an inventory with update permissions.
Args:
inventory_name: The name of the inventory to remove.
Returns:
True on success, False otherwise.
"""
return self._post_inventory_permission(inventory_name, 'update', remove=True)
def add_inventory_permission_ad_hoc(self, inventory_name):
"""Adds an inventory with ad hoc permissions.
Args:
inventory_name: The name of the inventory to add.
Returns:
True on success, False otherwise.
"""
return self._post_inventory_permission(inventory_name, 'ad hoc')
def remove_inventory_permission_ad_hoc(self, inventory_name):
"""Removes an inventory with ad hoc permissions.
Args:
inventory_name: The name of the inventory to remove.
Returns:
True on success, False otherwise.
"""
return self._post_inventory_permission(inventory_name, 'ad hoc', remove=True)
def add_credential_permission_admin(self, credential_name, credential_type):
"""Adds a credential with admin permissions.
Args:
credential_name: The name of the credential to add.
credential_type (str): The type of the credential to use
Returns:
True on success, False otherwise.
"""
return self._post_credential_permission(credential_name, credential_type, 'admin')
def remove_credential_permission_admin(self, credential_name, credential_type):
"""Removes a credential with admin permissions.
Args:
credential_name: The name of the credential to remove.
credential_type (str): The type of the credential to use
Returns:
True on success, False otherwise.
"""
return self._post_credential_permission(credential_name, credential_type, 'admin', remove=True)
def add_credential_permission_use(self, credential_name, credential_type):
"""Adds a credential with admin permissions.
Args:
credential_name: The name of the credential to add.
credential_type (str): The type of the credential to use
Returns:
True on success, False otherwise.
"""
return self._post_credential_permission(credential_name, credential_type, 'use')
def remove_credential_permission_use(self, credential_name, credential_type):
"""Removes a credential with use permissions.
Args:
credential_name: The name of the credential to remove.
credential_type (str): The type of the credential to use
Returns:
True on success, False otherwise.
"""
return self._post_credential_permission(credential_name, credential_type, 'use', remove=True)
def add_organization_role_by_name(self, organization_name, role_name):
"""Adds an organization role to the team.
Args:
organization_name (str): The name of the organization to search roles for.
role_name (str): The name of the role to add.
Returns:
True on success, False otherwise.
"""
return self._post_organization_role(organization_name, role_name)
def remove_organization_role_by_name(self, organization_name, role_name):
"""Removes an organization role from the team.
Args:
organization_name (str): The name of the organization to search roles for.
role_name (str): The name of the role to add.
Returns:
True on success, False otherwise.
"""
return self._post_organization_role(organization_name, role_name, remove=True)
def _post_organization_role(self, organization_name, role_name, remove=False):
organization = self._tower.get_organization_by_name(organization_name)
if not organization:
raise InvalidOrganization(organization_name)
return self._post_permission(organization.object_roles, role_name, remove)
def _post_project_permission(self, project_name, permission_name, remove=False):
project = self.organization.get_project_by_name(project_name)
if not project:
raise InvalidProject(project_name)
return self._post_permission(project.object_roles, permission_name, remove)
def _post_job_template_permission(self, job_template_name, permission_name, remove=False):
job_template = self._tower.get_job_template_by_name(job_template_name)
if not job_template:
raise InvalidJobTemplate(job_template_name)
return self._post_permission(job_template.object_roles, permission_name, remove)
def _post_inventory_permission(self, inventory_name, permission_name, remove=False):
inventory = self.organization.get_inventory_by_name(inventory_name)
if not inventory:
raise InvalidInventory(inventory_name)
return self._post_permission(inventory.object_roles, permission_name, remove)
def _post_credential_permission(self, credential_name, credential_type, permission_name, remove=False):
credential = self.organization.get_credential_by_name(credential_name, credential_type)
if not credential:
raise InvalidCredential(credential_name)
return self._post_permission(credential.object_roles, permission_name, remove)
def _post_permission(self, roles, permission_name, remove=False):
permission = self._get_permission(permission_name, roles)
if remove:
url = '{api}/roles/{id}/teams/'.format(api=self._tower.api,
id=permission.id)
payload = {'id': self.id,
'disassociate': True}
else:
url = '{api}/teams/{id}/roles/'.format(api=self._tower.api,
id=self.id)
payload = {'id': permission.id}
response = self._tower.session.post(url, json=payload)
if not response.ok:
self._logger.error('Error posting to url "%s", response was "%s"', url, response.text)
return response.ok