-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsegments_api.py
More file actions
127 lines (103 loc) · 4.34 KB
/
Copy pathsegments_api.py
File metadata and controls
127 lines (103 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from splitcli.split_apis import http_client
# URLs
def segment_base_url(workspace_id):
return f"segments/ws/{workspace_id}"
def segment_environment_url(workspace_id, environment_name):
base_url = segment_base_url(workspace_id)
return f"{base_url}/environments/{environment_name}"
def segment_create_url(workspace_id, traffic_type_name):
base_url = segment_base_url(workspace_id)
return f"{base_url}/trafficTypes/{traffic_type_name}"
def segment_metadata_url(workspace_id, segment_name):
base_url = segment_base_url(workspace_id)
return f"{base_url}/{segment_name}"
def segment_instance_url(segment_name, environment_name):
return f"segments/{environment_name}/{segment_name}"
def segment_action_url(segment_name, environment_name, action):
base_url = segment_instance_url(segment_name, environment_name)
return f"{base_url}/{action}"
# Segment Metadata
def list_segments(workspace_id):
all_segments = []
offset, limit = (0, 20)
# Stop once a batch is smaller than the limit
while len(all_segments) % limit == 0:
result = list_segments_batch(workspace_id, offset, limit)
if len(result) != 0:
all_segments.extend(result)
else:
break
offset += limit
return all_segments
def list_segments_batch(workspace_id, offset, limit):
path = segment_base_url(workspace_id) + f"?offset={offset}&limit={limit}"
return http_client.get(path)['objects']
def list_segments_environment(workspace_id, environment_name):
all_segments = []
offset, limit = (0, 20)
# Stop once a batch is smaller than the limit
while len(all_segments) % limit == 0:
result = list_segments_environment_batch(workspace_id, environment_name, offset, limit)
if len(result) != 0:
all_segments.extend(result)
else:
break
offset += limit
return all_segments
def list_segments_environment_batch(workspace_id, environment_name, offset, limit):
path = segment_environment_url(workspace_id, environment_name) + f"?offset={offset}&limit={limit}"
return http_client.get(path)['objects']
def create_segment(workspace_id, traffic_type_name, segment_name, segment_description):
path = segment_create_url(workspace_id, traffic_type_name)
content = {
"name": segment_name,
"description": segment_description
}
result = http_client.post(path, content)
return result
def delete_segment(workspace_id, segment_name):
path = segment_metadata_url(workspace_id, segment_name)
return http_client.delete(path)
def delete_all_segments(workspace_id):
segments = list_segments(workspace_id)
for segment in segments:
delete_segment(workspace_id, segment['name'])
def get_segment(workspace_id, segment_name, environment_name):
segments = list_segments_environment(workspace_id, environment_name)
result = list(filter(lambda x: x['name'] == segment_name, segments))
if len(result) == 1:
return result[0]
else:
return None
def activate_segment(segment_name, environment_name):
path = segment_instance_url(segment_name, environment_name)
return http_client.post(path)
def deactivate_segment(segment_name, environment_name):
path = segment_instance_url(segment_name, environment_name)
return http_client.delete(path)
def get_segment_keys(segment_name, environment_name, page=0):
limit = 100
offset = page * limit
path = segment_action_url(segment_name, environment_name, "keys") + f"?offset={offset}&limit={limit}"
return http_client.get(path)
def add_segment_keys(segment_name, environment_name, keys, replace=False, comment=""):
content = {
"keys": keys,
"comment": comment
}
path = segment_action_url(segment_name, environment_name, "uploadKeys")
if replace:
path += "?replace"
return http_client.put(path, content)
def upload_segment_keys(segment_name, environment_name, file_path, replace=False):
path = segment_action_url(segment_name, environment_name, "upload")
if replace:
path += "?replace"
return http_client.put(path)
def remove_segment_keys(segment_name, environment_name, keys, comment=""):
content = {
"keys": keys,
"comment": comment
}
path = segment_action_url(segment_name, environment_name, "removeKeys")
return http_client.put(path, content)