Skip to content

Commit fd94cb1

Browse files
authored
Merge pull request #5 from splitio/new-workspaces-api
Implemented new workspace and environment management api
2 parents 5d87b51 + 31d2470 commit fd94cb1

20 files changed

Lines changed: 706 additions & 54 deletions

splitapiclient/main/apiclient.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ def groups(self):
6464
@abc.abstractproperty
6565
def apikeys(self):
6666
pass
67+
68+
@abc.abstractproperty
69+
def restrictions(self):
70+
pass

splitapiclient/main/sync_apiclient.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from splitapiclient.microclients import UserMicroClient
1717
from splitapiclient.microclients import GroupMicroClient
1818
from splitapiclient.microclients import APIKeyMicroClient
19+
from splitapiclient.microclients import RestrictionMicroClient
1920

2021

2122
class SyncApiClient(BaseApiClient):
@@ -64,6 +65,7 @@ def __init__(self, config):
6465
self._user_client = UserMicroClient(http_client)
6566
self._group_client = GroupMicroClient(http_client)
6667
self._apikey_client = APIKeyMicroClient(http_client)
68+
self._restriction_client = RestrictionMicroClient(http_client)
6769

6870
@property
6971
def traffic_types(self):
@@ -116,3 +118,7 @@ def groups(self):
116118
@property
117119
def apikeys(self):
118120
return self._apikey_client
121+
122+
@property
123+
def restrictions(self):
124+
return self._restriction_client

splitapiclient/microclients/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
from splitapiclient.microclients.user_microclient import UserMicroClient
1212
from splitapiclient.microclients.group_microclient import GroupMicroClient
1313
from splitapiclient.microclients.apikey_microclient import APIKeyMicroClient
14+
from splitapiclient.microclients.restriction_microclient import RestrictionMicroClient

splitapiclient/microclients/environment_microclient.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class EnvironmentMicroClient:
1919
'query_string': [],
2020
'response': True,
2121
},
22-
'update_name': {
22+
'update': {
2323
'method': 'PATCH',
24-
'url_template': ('environments/ws/{workspaceId}/{environmentName}'),
24+
'url_template': ('environments/ws/{workspaceId}/{environmentId}'),
2525
'headers': [{
2626
'name': 'Authorization',
2727
'template': 'Bearer {value}',
@@ -32,7 +32,7 @@ class EnvironmentMicroClient:
3232
},
3333
'delete': {
3434
'method': 'DELETE',
35-
'url_template': ('environments/ws/{workspaceId}/{environmentName}'),
35+
'url_template': ('environments/ws/{workspaceId}/{environmentId}'),
3636
'headers': [{
3737
'name': 'Authorization',
3838
'template': 'Bearer {value}',
@@ -103,31 +103,35 @@ def add(self, environment, workspace_id):
103103
data = as_dict(environment)
104104
response = self._http_client.make_request(
105105
self._endpoint['create'],
106-
body=data,
106+
body = data,
107107
workspaceId = workspace_id
108108
)
109109
return Environment(response, workspace_id, self._http_client)
110110

111-
def update_name(self, new_name, environment, workspace_id):
111+
def update(self, environment_id, workspace_id, fieldName, fieldValue):
112112
'''
113113
update environment
114114
115-
:param environment: environment instance or dict containing name and production flag
116-
properties
117-
115+
:param workspace_id: workspace id
116+
:param environment_id: workspace id
117+
:param fieldName: field to be changed
118+
:param fieldValue: new field value
119+
118120
:returns: updated environment
119121
:rtype: Environment
120122
'''
121-
data = as_dict(environment)
123+
data = [{'op': 'replace',
124+
'path': '/' + fieldName,
125+
'value': fieldValue }]
122126
response = self._http_client.make_request(
123-
self._endpoint['update_name'],
124-
body= [as_dict({'op': 'replace', 'path': '/name', 'value':new_name})],
125-
workspaceId =workspace_id,
126-
environmentName = data['name']
127+
self._endpoint['update'],
128+
body=data,
129+
workspaceId = workspace_id,
130+
environmentId = environment_id
127131
)
128-
return response
132+
return Environment(response, workspace_id, self._http_client)
129133

130-
def delete(self, environment_name, workspace_id):
134+
def delete(self, environment_id, workspace_id):
131135
'''
132136
delete an environment
133137
@@ -139,8 +143,8 @@ def delete(self, environment_name, workspace_id):
139143
'''
140144
response = self._http_client.make_request(
141145
self._endpoint['delete'],
142-
workspaceId =workspace_id,
143-
environmentName = environment_name
146+
workspaceId = workspace_id,
147+
environmentId = environment_id
144148
)
145149
return response
146150

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from splitapiclient.resources import Restriction
2+
from splitapiclient.util.exceptions import HTTPResponseError, \
3+
UnknownApiClientError
4+
from splitapiclient.util.logger import LOGGER
5+
from splitapiclient.util.helpers import as_dict
6+
7+
class RestrictionMicroClient:
8+
'''
9+
'''
10+
_endpoint = {
11+
'all_items': {
12+
'method': 'GET',
13+
'url_template': 'restrictions?limit=20&offset={offset}&resource_type={resourceType}&resource_id={resourceId}',
14+
'headers': [{
15+
'name': 'Authorization',
16+
'template': 'Bearer {value}',
17+
'required': True,
18+
}],
19+
'query_string': [],
20+
'response': True,
21+
},
22+
'create': {
23+
'method': 'PUT',
24+
'url_template': 'restrictions',
25+
'headers': [{
26+
'name': 'Authorization',
27+
'template': 'Bearer {value}',
28+
'required': True,
29+
}],
30+
'query_string': [],
31+
'response': True,
32+
},
33+
}
34+
35+
def __init__(self, http_client):
36+
'''
37+
Constructor
38+
'''
39+
self._http_client = http_client
40+
41+
def list(self, resource_type, resource_id):
42+
'''
43+
Returns a list of restriction objects.
44+
:rtype: list(Restriction)
45+
'''
46+
offset_val = 0
47+
final_list = []
48+
while True:
49+
response = self._http_client.make_request(
50+
self._endpoint['all_items'],
51+
offset = offset_val,
52+
resourceType = resource_type,
53+
resourceId = resource_id
54+
)
55+
for item in response['objects']:
56+
final_list.append(item)
57+
offset = int(response['offset'])
58+
totalCount = int(response['totalCount'])
59+
limit = int(response['limit'])
60+
if totalCount>(offset+limit):
61+
offset_val = offset_val + limit
62+
continue
63+
else:
64+
break
65+
return [Restriction(item, self._http_client) for item in final_list]
66+
67+
def add(self, resource_type, resource_id, restrictions_list):
68+
'''
69+
add a new restriction to existing object
70+
71+
:param Resource Type: object type (workspace, environment, etc.)
72+
:returns: newly created restrictions
73+
:rtype: Restriction
74+
'''
75+
data = {"resource":{"id": resource_id, "type": resource_type}, "operations":{"view":True}, "resourcePermissions":{"view": restrictions_list}}
76+
77+
# data = as_dict(final_body)
78+
response = self._http_client.make_request(
79+
self._endpoint['create'],
80+
body=data
81+
)
82+
return Restriction(response, self._http_client)

splitapiclient/microclients/split_microclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def remove_from_environment(self, split_name, environment_id, workspace_id):
231231

232232
def associate_tags(self, split_name, tags, workspace_id):
233233
'''
234-
Add split to environment
234+
Associate tags with Split
235235
236236
:param split: split name, tags string array, workspace id
237237

splitapiclient/microclients/traffic_type_microclient.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ class TrafficTypeMicroClient:
1919
'query_string': [],
2020
'response': True,
2121
},
22+
'delete': {
23+
'method': 'DELETE',
24+
'url_template': ('trafficTypes/{trafficTypeId}'),
25+
'headers': [{
26+
'name': 'Authorization',
27+
'template': 'Bearer {value}',
28+
'required': True,
29+
}],
30+
'query_string': [],
31+
'response': True,
32+
},
2233
}
2334

2435
def __init__(self, http_client):
@@ -56,3 +67,18 @@ def find(self, traffic_type_name, workspace_id):
5667
return TrafficType(item, workspace_id, self._http_client)
5768
LOGGER.error("TrafficType Name does not exist")
5869
return None
70+
71+
def delete(self, traffic_type_id):
72+
'''
73+
delete a traffic type
74+
75+
:param traffic type id:
76+
77+
:returns:
78+
:rtype: True if successful
79+
'''
80+
response = self._http_client.make_request(
81+
self._endpoint['delete'],
82+
trafficTypeId = traffic_type_id,
83+
)
84+
return response

splitapiclient/microclients/workspace_microclient.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from splitapiclient.util.exceptions import HTTPResponseError, \
33
UnknownApiClientError
44
from splitapiclient.util.logger import LOGGER
5+
from splitapiclient.util.helpers import as_dict
6+
57

68
class WorkspaceMicroClient:
79
'''
@@ -29,6 +31,39 @@ class WorkspaceMicroClient:
2931
'query_string': [],
3032
'response': True,
3133
},
34+
'create': {
35+
'method': 'POST',
36+
'url_template': ('workspaces'),
37+
'headers': [{
38+
'name': 'Authorization',
39+
'template': 'Bearer {value}',
40+
'required': True,
41+
}],
42+
'query_string': [],
43+
'response': True,
44+
},
45+
'update': {
46+
'method': 'PATCH',
47+
'url_template': ('workspaces/{workspaceId}'),
48+
'headers': [{
49+
'name': 'Authorization',
50+
'template': 'Bearer {value}',
51+
'required': True,
52+
}],
53+
'query_string': [],
54+
'response': True,
55+
},
56+
'delete': {
57+
'method': 'DELETE',
58+
'url_template': ('workspaces/{workspaceId}'),
59+
'headers': [{
60+
'name': 'Authorization',
61+
'template': 'Bearer {value}',
62+
'required': True,
63+
}],
64+
'query_string': [],
65+
'response': True,
66+
},
3267
}
3368

3469
def __init__(self, http_client):
@@ -90,3 +125,53 @@ def get_rollout_statuses(self, workspace_id):
90125
workspaceId = workspace_id
91126
)
92127
return response
128+
129+
def add(self, workspace):
130+
'''
131+
add a workspace
132+
133+
:param workspace: workspace instance
134+
:returns: newly created workspace
135+
:rtype: Workspace
136+
'''
137+
data = as_dict(workspace)
138+
response = self._http_client.make_request(
139+
self._endpoint['create'],
140+
body=data
141+
)
142+
return Workspace(response, self._http_client)
143+
144+
def update(self, workspace_id, fieldName, fieldValue):
145+
'''
146+
update a workspace
147+
148+
:param workspace_id: workspace id
149+
:param fieldName: field to be changed
150+
:param fieldValue: new field value
151+
:returns: newly updated workspace
152+
:rtype: Workspace
153+
'''
154+
data = [{'op': 'replace',
155+
'path': '/' + fieldName,
156+
'value': fieldValue }]
157+
response = self._http_client.make_request(
158+
self._endpoint['update'],
159+
body=data,
160+
workspaceId = workspace_id
161+
)
162+
return Workspace(response, self._http_client)
163+
164+
def delete(self, workspace_id):
165+
'''
166+
delete a workspace
167+
168+
:param workspace id:
169+
170+
:returns:
171+
:rtype: True if successful
172+
'''
173+
response = self._http_client.make_request(
174+
self._endpoint['delete'],
175+
workspaceId =workspace_id,
176+
)
177+
return response

splitapiclient/resources/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
debug = True
12
from splitapiclient.resources.traffic_type import TrafficType
23
from splitapiclient.resources.environment import Environment
34
from splitapiclient.resources.split import Split
@@ -11,3 +12,4 @@
1112
from splitapiclient.resources.user import User
1213
from splitapiclient.resources.group import Group
1314
from splitapiclient.resources.apikey import APIKey
15+
from splitapiclient.resources.restriction import Restriction

0 commit comments

Comments
 (0)