Skip to content

Commit

Permalink
Adapt user-team management code to use new REST API format (#8)
Browse files Browse the repository at this point in the history
Adapt user-team management code to use new REST API format
  • Loading branch information
aponjavic authored Feb 6, 2018
1 parent a306f25 commit b4d4993
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions kube_obj_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def __init__(self, type, customer_admin_sdclient, customer_id, sdc_url, team_pre
self._type = type

def parse(self, objdata):
uids = []
users = []
user_id_map = {}

###################################################################
# TEAM CREATION
Expand Down Expand Up @@ -79,10 +78,9 @@ def parse(self, objdata):
Logger.log('cannot get user %s: %s' % (uname, res[1]), 'error')
continue

uids.append(res[1]['id'])
users.append(uname)
user_id_map[uname] = res[1]['id']

if len(users) == 0:
if len(user_id_map) == 0:
Logger.log('No users specified for this team. Skipping.', 'error')
return False

Expand Down Expand Up @@ -123,21 +121,23 @@ def parse(self, objdata):
if res[0] == False:
if res[1] == TEAM_NOT_EXISTING_ERR:
team_exists = False
new_memberships = dict(map(lambda u: (u, 'ROLE_TEAM_EDIT'), user_id_map.keys()))
else:
teaminfo = res[1]
teamid = teaminfo['id']
old_memberships = dict(map(lambda m: (m['userId'], m['role']), teaminfo['userRoles']))
new_memberships = dict(map(lambda u: (u, 'ROLE_TEAM_EDIT') if user_id_map[u] not in old_memberships else (u, old_memberships[user_id_map[u]]), user_id_map.keys()))

if team_exists:
# Team exists. Detect if there are users to add and edit the team users list.
newusers = []
team_uids = set(old_memberships.keys())

if teaminfo['users'] != uids:
if team_uids != set(user_id_map.values()):
Logger.log("Detected modified %s %s, editing team %s" % (self._type, obj_name, team_name))
for j in range(0, len(uids)):
if not uids[j] in teaminfo['users']:
newusers.append(users[j])
newusers.append([u for u in user_id_map.keys() if user_id_map[u] not in team_uids])

res = self._customer_admin_sdclient.edit_team(team_name, users=users)
res = self._customer_admin_sdclient.edit_team(team_name, memberships=new_memberships)
if res[0] == False:
Logger.log('Team editing failed: ' + res[1], 'error')
return False
Expand All @@ -152,12 +152,12 @@ def parse(self, objdata):
elif self._type == 'namespace':
flt = 'kubernetes.namespace.name = "%s"' % ns_name
desc = 'automatically generated team based on deployment annotations'
res = self._customer_admin_sdclient.create_team(team_name, filter=flt, description=desc, show='container', users=users)
res = self._customer_admin_sdclient.create_team(team_name, filter=flt, description=desc, show='container', memberships=new_memberships)
if res[0] == False:
Logger.log('Team creation failed: ' + res[1], 'error')
return False
teamid = res[1]['team']['id']
newusers = users
newusers = user_id_map.keys()

###################################################################
# TEAM CONFIGURATION
Expand Down Expand Up @@ -271,7 +271,7 @@ def parse(self, objdata):
#
# Go through the list of new users and set them up for this team
#
for user in users:
for user in user_id_map.keys():

#
# First of all, we need to impersonate the users in this team
Expand Down

0 comments on commit b4d4993

Please sign in to comment.