From b663d00b29686eebcd1fc3299e35b125f75b4aee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Tubi=C4=87?= Date: Mon, 19 Jun 2023 09:30:48 +0200 Subject: [PATCH] fix(team): get team will fetch only from selected product (#243) * fix(team): get team will fetch only from selected product * fix(team): add test for team fetching --- sdcclient/_common.py | 13 ++++--- specs/_common/team_spec.py | 72 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 specs/_common/team_spec.py diff --git a/sdcclient/_common.py b/sdcclient/_common.py index 57c15f62..94fff62b 100644 --- a/sdcclient/_common.py +++ b/sdcclient/_common.py @@ -3,7 +3,7 @@ import requests from requests.adapters import HTTPAdapter -from requests.packages.urllib3.util.retry import Retry +from urllib3.util.retry import Retry class SysdigHTTPAdapter(HTTPAdapter): @@ -710,12 +710,17 @@ def get_teams(self, team_filter='', product_filter=''): **Success Return Value** The teams that match the filter. ''' - res = self.http.get(self.url + '/api/teams', headers=self.hdrs, verify=self.ssl_verify) + url = f'{self.url}/api/teams' + if product_filter: + if product_filter not in ['SDC', 'SDS']: + return [False, 'invalid product header, allowed only "SDC" or "SDS"'] + url = f'{url}?product={product_filter}' + + res = self.http.get(url, headers=self.hdrs, verify=self.ssl_verify) if not self._checkResponse(res): 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']] + return [True, ret] def get_team_by_id(self, id): diff --git a/specs/_common/team_spec.py b/specs/_common/team_spec.py new file mode 100644 index 00000000..9579686c --- /dev/null +++ b/specs/_common/team_spec.py @@ -0,0 +1,72 @@ +import os +import uuid +from expects import expect, equal +from mamba import before, description, it +from sdcclient import SdSecureClient, SdMonitorClient +from specs import be_successful_api_call +from collections import defaultdict + +TEAM_PREFIX_NAME = 'sysdig-sdk - ' + +with description("Teams", "integration", "teams") as self: + with before.all: + self.secure_client = SdSecureClient( + sdc_url=os.getenv("SDC_SECURE_URL", "https://secure.sysdig.com"), + token=os.getenv("SDC_SECURE_TOKEN") + ) + self.monitor_client = SdMonitorClient( + sdc_url=os.getenv("SDC_MONITOR_URL", "https://app.sysdigcloud.com"), + token=os.getenv("SDC_MONITOR_TOKEN") + ) + + with before.each: + self.team_name = f'{TEAM_PREFIX_NAME}{uuid.uuid4()}' + + with it("it should list all teams"): + ok, teams_monitor = self.monitor_client.get_teams() + expect((ok, teams_monitor)).to(be_successful_api_call) + + ok, teams_secure = self.secure_client.get_teams() + expect((ok, teams_secure)).to(be_successful_api_call) + + count_monitor = defaultdict(int) + count_secure = defaultdict(int) + + def count_products(teams, count): + for team in teams: + for product in team['products']: + count[product] += 1 + + count_products(teams_monitor, count_monitor) + count_products(teams_secure, count_secure) + + expect(len(count_secure)).to(equal(len(count_monitor))) + for k, v in count_monitor.items(): + expect(count_secure[k]).to(equal(v)) + expect(len(teams_secure)).to(equal(len(teams_monitor))) + + with it("it should list only monitor teams"): + ok, team = self.secure_client.create_team(self.team_name) + expect((ok, team)).to(be_successful_api_call) + + ok, teams = self.monitor_client.get_teams(product_filter='SDC') + expect((ok, teams)).to(be_successful_api_call) + + secure_teams = [t for t in teams if 'SDS' in t['products']] + expect(len(secure_teams)).to(equal(0)) + + ok, res = self.secure_client.delete_team(self.team_name) + expect((ok, res)).to(be_successful_api_call) + + with it("it should list only secure teams"): + ok, team = self.monitor_client.create_team(self.team_name) + expect((ok, team)).to(be_successful_api_call) + + ok, teams = self.secure_client.get_teams(product_filter='SDS') + expect((ok, teams)).to(be_successful_api_call) + + monitor_teams = [t for t in teams if 'SDC' in t['products']] + expect(len(monitor_teams)).to(equal(0)) + + ok, res = self.monitor_client.delete_team(self.team_name) + expect((ok, res)).to(be_successful_api_call)