Skip to content

Commit b663d00

Browse files
authored
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
1 parent a213f9a commit b663d00

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

sdcclient/_common.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import requests
55
from requests.adapters import HTTPAdapter
6-
from requests.packages.urllib3.util.retry import Retry
6+
from urllib3.util.retry import Retry
77

88

99
class SysdigHTTPAdapter(HTTPAdapter):
@@ -710,12 +710,17 @@ def get_teams(self, team_filter='', product_filter=''):
710710
**Success Return Value**
711711
The teams that match the filter.
712712
'''
713-
res = self.http.get(self.url + '/api/teams', headers=self.hdrs, verify=self.ssl_verify)
713+
url = f'{self.url}/api/teams'
714+
if product_filter:
715+
if product_filter not in ['SDC', 'SDS']:
716+
return [False, 'invalid product header, allowed only "SDC" or "SDS"']
717+
url = f'{url}?product={product_filter}'
718+
719+
res = self.http.get(url, headers=self.hdrs, verify=self.ssl_verify)
714720
if not self._checkResponse(res):
715721
return [False, self.lasterr]
716722
ret = [t for t in res.json()['teams'] if team_filter in t['name']]
717-
if product_filter:
718-
ret = [t for t in ret if product_filter in t['products']]
723+
719724
return [True, ret]
720725

721726
def get_team_by_id(self, id):

specs/_common/team_spec.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import os
2+
import uuid
3+
from expects import expect, equal
4+
from mamba import before, description, it
5+
from sdcclient import SdSecureClient, SdMonitorClient
6+
from specs import be_successful_api_call
7+
from collections import defaultdict
8+
9+
TEAM_PREFIX_NAME = 'sysdig-sdk - '
10+
11+
with description("Teams", "integration", "teams") as self:
12+
with before.all:
13+
self.secure_client = SdSecureClient(
14+
sdc_url=os.getenv("SDC_SECURE_URL", "https://secure.sysdig.com"),
15+
token=os.getenv("SDC_SECURE_TOKEN")
16+
)
17+
self.monitor_client = SdMonitorClient(
18+
sdc_url=os.getenv("SDC_MONITOR_URL", "https://app.sysdigcloud.com"),
19+
token=os.getenv("SDC_MONITOR_TOKEN")
20+
)
21+
22+
with before.each:
23+
self.team_name = f'{TEAM_PREFIX_NAME}{uuid.uuid4()}'
24+
25+
with it("it should list all teams"):
26+
ok, teams_monitor = self.monitor_client.get_teams()
27+
expect((ok, teams_monitor)).to(be_successful_api_call)
28+
29+
ok, teams_secure = self.secure_client.get_teams()
30+
expect((ok, teams_secure)).to(be_successful_api_call)
31+
32+
count_monitor = defaultdict(int)
33+
count_secure = defaultdict(int)
34+
35+
def count_products(teams, count):
36+
for team in teams:
37+
for product in team['products']:
38+
count[product] += 1
39+
40+
count_products(teams_monitor, count_monitor)
41+
count_products(teams_secure, count_secure)
42+
43+
expect(len(count_secure)).to(equal(len(count_monitor)))
44+
for k, v in count_monitor.items():
45+
expect(count_secure[k]).to(equal(v))
46+
expect(len(teams_secure)).to(equal(len(teams_monitor)))
47+
48+
with it("it should list only monitor teams"):
49+
ok, team = self.secure_client.create_team(self.team_name)
50+
expect((ok, team)).to(be_successful_api_call)
51+
52+
ok, teams = self.monitor_client.get_teams(product_filter='SDC')
53+
expect((ok, teams)).to(be_successful_api_call)
54+
55+
secure_teams = [t for t in teams if 'SDS' in t['products']]
56+
expect(len(secure_teams)).to(equal(0))
57+
58+
ok, res = self.secure_client.delete_team(self.team_name)
59+
expect((ok, res)).to(be_successful_api_call)
60+
61+
with it("it should list only secure teams"):
62+
ok, team = self.monitor_client.create_team(self.team_name)
63+
expect((ok, team)).to(be_successful_api_call)
64+
65+
ok, teams = self.secure_client.get_teams(product_filter='SDS')
66+
expect((ok, teams)).to(be_successful_api_call)
67+
68+
monitor_teams = [t for t in teams if 'SDC' in t['products']]
69+
expect(len(monitor_teams)).to(equal(0))
70+
71+
ok, res = self.monitor_client.delete_team(self.team_name)
72+
expect((ok, res)).to(be_successful_api_call)

0 commit comments

Comments
 (0)