From a213f9a1329d29df58ac2fa3132888f0dda8328f Mon Sep 17 00:00:00 2001 From: Luca Zenobi <83784767+luzeno@users.noreply.github.com> Date: Fri, 5 May 2023 13:22:44 +0200 Subject: [PATCH] [SP-748] Fix get_team method in sdccli python lib (#239) * Use team endpoint to fetch team by name instead of in memory filtering * Remove conditional memberships * Fix linter --- sdcclient/_common.py | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/sdcclient/_common.py b/sdcclient/_common.py index d8d0483..57c15f6 100644 --- a/sdcclient/_common.py +++ b/sdcclient/_common.py @@ -715,9 +715,28 @@ def get_teams(self, team_filter='', product_filter=''): return [False, self.lasterr] ret = [t for t in res.json()['teams'] if team_filter in t['name']] if product_filter: - ret = [t for t in ret if product_filter in t['products']] + ret = [t for t in ret if product_filter in t['products']] return [True, ret] + def get_team_by_id(self, id): + '''**Description** + Return the team with the specified team ID, if it is present. + + **Arguments** + - **id**: the ID of the team to return + + **Success Return Value** + The requested team. + + **Example** + `examples/user_team_mgmt.py `_ + ''' + res = self.http.get(self.url + '/api/teams/' + str(id), headers=self.hdrs, verify=self.ssl_verify) + if not self._checkResponse(res): + return [False, self.lasterr] + + return [True, res.json()['team']] + def get_team(self, name): '''**Description** Return the team with the specified team name, if it is present. @@ -731,13 +750,18 @@ def get_team(self, name): **Example** `examples/user_team_mgmt.py `_ ''' - ok, res = self.get_teams(name) + res = self.http.get(self.url + '/api/v2/teams/light/name/' + str(name), headers=self.hdrs, verify=self.ssl_verify) + if not self._checkResponse(res): + return [False, self.lasterr] + + light_team = res.json()['team'] + + ok, team_with_memberships = self.get_team_by_id(light_team['id']) + if not ok: - return ok, res - for team in res: - if team['name'] == name: - return [True, team] - return [False, 'Could not find team'] + return [False, self.lasterr] + + return [True, team_with_memberships] def get_team_ids(self, teams): res = self.http.get(self.url + '/api/teams', headers=self.hdrs, verify=self.ssl_verify)