1
+ import logging
2
+ import json
3
+
4
+ from gremlinapi .cli import register_cli_action
5
+ from gremlinapi .exceptions import (
6
+ GremlinParameterError ,
7
+ ProxyError ,
8
+ ClientError ,
9
+ HTTPTimeout ,
10
+ HTTPError
11
+ )
12
+
13
+ from typing import Union , Type
14
+
15
+ from gremlinapi .gremlinapi import GremlinAPI
16
+ from gremlinapi .http_clients import (
17
+ get_gremlin_httpclient ,
18
+ GremlinAPIHttpClient
19
+ )
20
+
21
+ log = logging .getLogger ("GremlinAPI.client" )
22
+
23
+
24
+ class GremlinAPIReliabilityTests (GremlinAPI ):
25
+
26
+
27
+ @classmethod
28
+ def __validate_reliability_test_id (
29
+ cls ,
30
+ reliability_test_id : str ,
31
+ https_client : Type [GremlinAPIHttpClient ] = get_gremlin_httpclient ()
32
+ ) -> None :
33
+ '''
34
+ Ensure that a reliablity test ID is valid
35
+ '''
36
+ all_tests = [ x ['guid' ] for x in cls .list_reliability_test_types ()['global' ] ]
37
+ if reliability_test_id not in all_tests :
38
+ raise GremlinParameterError (f'Reliability test ID { reliability_test_id } is not valid.' )
39
+
40
+ @classmethod
41
+ def validate_reliability_test_id (cls ,
42
+ reliability_test_id : str
43
+ ):
44
+ '''
45
+ '''
46
+ cls .__validate_reliability_test_id (reliability_test_id )
47
+
48
+ @classmethod
49
+ def __reliability_test_id_requires_dependency_id (
50
+ cls ,
51
+ reliability_test_id : str
52
+ ) -> bool :
53
+ if reliability_test_id in ('blackhole-test' ,'latency-test' ,'certificate-expiry' ):
54
+ return True
55
+
56
+
57
+ @classmethod
58
+ def list_reliability_test_types (
59
+ cls ,
60
+ https_client : Type [GremlinAPIHttpClient ] = get_gremlin_httpclient (),
61
+ * args : tuple ,
62
+ ** kwargs : dict ,
63
+ ) -> dict :
64
+ """
65
+ List all types of reliability tests.
66
+ These types represent the different types of tests/experiments
67
+ that can be run against a service in Gremlin.
68
+ """
69
+ method = "GET"
70
+ endpoint = cls ._required_team_endpoint ("/reliability-tests" , ** kwargs )
71
+ payload = cls ._payload (** {"headers" : https_client .header ()})
72
+ (resp , body ) = https_client .api_call (method , endpoint , ** payload )
73
+ return body
74
+
75
+
76
+ @classmethod
77
+ def list_service_reliability_test_runs (
78
+ cls ,
79
+ https_client : Type [GremlinAPIHttpClient ] = get_gremlin_httpclient (),
80
+ * args : tuple ,
81
+ ** kwargs : dict ,
82
+ ) -> dict :
83
+ '''
84
+ List all reliability tests that have been run for a particular service ID
85
+ '''
86
+ method = "GET"
87
+ service_id = cls ._error_if_not_param ("service_id" , ** kwargs )
88
+ endpoint = cls ._required_team_endpoint (
89
+ f'/reliability-tests/runs/?serviceId={ service_id } ' , ** kwargs
90
+ )
91
+ payload = cls ._payload (** {"headers" : https_client .header ()})
92
+ (resp , body ) = https_client .api_call (method , endpoint , ** payload )
93
+ return body
94
+
95
+
96
+ @classmethod
97
+ def list_service_reliability_test_runs_by_type (
98
+ cls ,
99
+ https_client : Type [GremlinAPIHttpClient ] = get_gremlin_httpclient (),
100
+ * args : tuple ,
101
+ ** kwargs : dict ,
102
+ ) -> dict :
103
+ '''
104
+ List all reliability tests of a specific type that have been run for a particular service ID
105
+ '''
106
+ method = "GET"
107
+ service_id = cls ._error_if_not_param ("service_id" , ** kwargs )
108
+ reliability_test_id = cls ._error_if_not_param ("reliability_test_id" , ** kwargs )
109
+ # TODO: Removing page_size for now, since it's not fully working.
110
+ #page_size = cls._info_if_not_param("pageSize", **kwargs)
111
+ #if not page_size:
112
+ # endpoint = cls._required_team_endpoint(
113
+ # f'/reliability-tests/{reliability_test_id}/runs/?serviceId={service_id}', **kwargs)
114
+ #else:
115
+ # endpoint = cls._required_team_endpoint(
116
+ # f'/reliability-tests/{reliability_test_id}/runs/?serviceId={service_id}&pageSize={page_size}', **kwargs)
117
+ endpoint = cls ._required_team_endpoint (
118
+ f'/reliability-tests/{ reliability_test_id } /runs/?serviceId={ service_id } ' , ** kwargs )
119
+ payload = cls ._payload (** {"headers" : https_client .header ()})
120
+ (resp , body ) = https_client .api_call (method , endpoint , ** payload )
121
+ return body
122
+
123
+
124
+ @classmethod
125
+ def list_reliability_test_notifications (
126
+ cls ,
127
+ https_client : Type [GremlinAPIHttpClient ] = get_gremlin_httpclient (),
128
+ * args : tuple ,
129
+ ** kwargs : dict ,
130
+ ) -> dict :
131
+ '''
132
+ List all reliability test notifications for this team
133
+ '''
134
+ method = "GET"
135
+ endpoint = cls ._required_team_endpoint ("/reliability-tests/notifications" , ** kwargs )
136
+ payload = cls ._payload (** {"headers" : https_client .header ()})
137
+ (resp , body ) = https_client .api_call (method , endpoint , ** payload )
138
+ return body
139
+
140
+
141
+ @classmethod
142
+ def run_single_reliability_test (
143
+ cls ,
144
+ https_client : Type [GremlinAPIHttpClient ] = get_gremlin_httpclient (),
145
+ * args : tuple ,
146
+ ** kwargs : dict ,
147
+ ) -> dict :
148
+ '''
149
+ Run a single reliability test against a service
150
+ '''
151
+ method = "POST"
152
+
153
+ reliability_test_id = cls ._error_if_not_param ("reliability_test_id" , ** kwargs )
154
+ cls .__validate_reliability_test_id (reliability_test_id )
155
+
156
+ service_id = cls ._error_if_not_param ("service_id" , ** kwargs )
157
+ data = {
158
+ 'serviceId' : service_id
159
+ }
160
+
161
+ if cls .__reliability_test_id_requires_dependency_id (reliability_test_id ):
162
+ dependency_id = cls ._error_if_not_param ("dependency_id" , ** kwargs )
163
+ data ['dependencyId' ] = dependency_id
164
+ else :
165
+ if 'dependency_id' in kwargs or 'dependencyId' in kwargs :
166
+ raise GremlinParameterError (
167
+ 'The reliability test you are trying to run does not require a dependency ID.'
168
+ )
169
+
170
+ endpoint = cls ._required_team_endpoint (f"/reliability-tests/{ reliability_test_id } /runs" , ** kwargs )
171
+ payload = cls ._payload (** {"headers" : https_client .header (), "data" : json .dumps (data )})
172
+ (resp , body ) = https_client .api_call (method , endpoint , ** payload )
173
+ return body
174
+
175
+
176
+ @classmethod
177
+ def run_all_reliability_tests (
178
+ cls ,
179
+ https_client : Type [GremlinAPIHttpClient ] = get_gremlin_httpclient (),
180
+ * args : tuple ,
181
+ ** kwargs : dict ,
182
+ ) -> dict :
183
+ '''
184
+ Run all reliability tests for a service
185
+ '''
186
+ method = "POST"
187
+ service_id = cls ._error_if_not_param ("service_id" , ** kwargs )
188
+ endpoint = cls ._required_team_endpoint (f"/services/{ service_id } /baseline" , ** kwargs )
189
+ payload = cls ._payload (** {"headers" : https_client .header ()})
190
+ data = {
191
+ 'startBaselineRequest' : ''
192
+ }
193
+ payload = cls ._payload (** {"headers" : https_client .header (), "data" : json .dumps (data )})
194
+ (resp , body ) = https_client .api_call (method , endpoint , ** payload )
195
+ return body
196
+
197
+ @classmethod
198
+ def get_service_reliability_score (
199
+ cls ,
200
+ https_client : Type [GremlinAPIHttpClient ] = get_gremlin_httpclient (),
201
+ * args : tuple ,
202
+ ** kwargs : dict ,
203
+ ) -> int :
204
+ '''
205
+ Retrieve current score for a service
206
+ '''
207
+ method = "GET"
208
+ service_id = cls ._error_if_not_param ("service_id" , ** kwargs )
209
+ endpoint = cls ._required_team_endpoint (f"/reliability-management/services/{ service_id } " , ** kwargs )
210
+ payload = cls ._payload (** {"headers" : https_client .header ()})
211
+ (resp , body ) = https_client .api_call (method , endpoint , ** payload )
212
+ return body ['score' ]
0 commit comments