22import os
33import random as rd
44import sys
5- from time import sleep
6- from typing import Any , Dict
75
8- import requests as req
6+ from aura_api_ci import AuraApiCI
97
108logging .basicConfig (level = logging .INFO )
119
12- CLIENT_ID = os .environ ["AURA_API_CLIENT_ID" ]
13- CLIENT_SECRET = os .environ ["AURA_API_CLIENT_SECRET" ]
14-
15-
16- def get_access_token () -> str :
17- data = {
18- "grant_type" : "client_credentials" ,
19- }
20-
21- # getting a token like {'access_token':'X','expires_in':3600,'token_type':'bearer'}
22- response = req .post ("https://api-staging.neo4j.io/oauth/token" , data = data , auth = (CLIENT_ID , CLIENT_SECRET ))
23-
24- response .raise_for_status ()
25-
26- return response .json ()["access_token" ] # type: ignore
27-
28-
29- def create_instance (name : str , access_token : str ) -> Dict [str , Any ]:
30- CREATE_OK_MAX_WAIT_TIME = 10
31-
32- data = {
33- "name" : name ,
34- "memory" : "8GB" ,
35- "version" : "5" ,
36- "region" : "europe-west1" ,
37- "type" : "gds" ,
38- "cloud_provider" : "gcp" ,
39- "tenant_id" : get_tenant_id (access_token ),
40- }
41-
42- should_retry = True
43- wait_time = 1
44-
45- while should_retry :
46- sleep (wait_time )
47- wait_time *= 2
48-
49- response = req .post (
50- "https://api-staging.neo4j.io/v1/instances" ,
51- json = data ,
52- headers = {"Authorization" : f"Bearer { access_token } " },
53- )
54- should_retry = response .status_code in [500 , 502 , 503 , 504 , 405 ] and CREATE_OK_MAX_WAIT_TIME > wait_time
55-
56- if should_retry :
57- logging .debug (f"Error code: { response .status_code } - Retrying in { wait_time } s" )
58-
59- response .raise_for_status ()
60-
61- return response .json ()["data" ] # type: ignore
62-
63-
64- def check_running (access_token : str , db_id : str ) -> None :
65- RUNNING_MAX_WAIT_TIME = 60 * 5
66-
67- should_retry = True
68- wait_time = 1
69-
70- while should_retry :
71- sleep (wait_time )
72- wait_time *= 2
73-
74- response = req .get (
75- f"https://api-staging.neo4j.io/v1/instances/{ db_id } " ,
76- headers = {"Authorization" : f"Bearer { access_token } " },
77- )
78-
79- instance_status = "?"
80- if response .status_code == 200 :
81- instance_status = response .json ()["data" ]["status" ]
82-
83- should_retry = (
84- response .status_code in [500 , 502 , 503 , 504 ] or instance_status == "creating"
85- ) and RUNNING_MAX_WAIT_TIME > wait_time
86-
87- if should_retry :
88- logging .debug (f"Status code: { response .status_code } , Status: { instance_status } - Retrying in { wait_time } s" )
89-
90- response .raise_for_status ()
91-
92-
93- def get_tenant_id (access_token : str ) -> str :
94- response = req .get (
95- "https://api-staging.neo4j.io/v1/tenants" ,
96- headers = {"Authorization" : f"Bearer { access_token } " },
97- )
98- response .raise_for_status ()
99-
100- raw_data = response .json ()["data" ]
101- assert len (raw_data ) == 1
102-
103- return raw_data [0 ]["id" ] # type: ignore
104-
10510
10611def run_tests (uri : str , username : str , password : str ) -> None :
10712 cmd = (
@@ -120,45 +25,21 @@ def run_notebooks(uri: str, username: str, password: str) -> None:
12025 raise Exception ("Failed to run notebooks" )
12126
12227
123- def teardown_instance (access_token : str , db_id : str ) -> None :
124- TEARDOWN_MAX_WAIT_TIME = 10
125-
126- should_retry = True
127- wait_time = 1
128-
129- while should_retry :
130- sleep (wait_time )
131- wait_time *= 2
132-
133- response = req .delete (
134- f"https://api-staging.neo4j.io/v1/instances/{ db_id } " ,
135- headers = {"Authorization" : f"Bearer { access_token } " },
136- )
137-
138- if response .status_code == 202 :
139- should_retry = False
140-
141- should_retry = (response .status_code in [500 , 502 , 503 , 504 ]) and TEARDOWN_MAX_WAIT_TIME > wait_time
142-
143- if should_retry :
144- logging .debug (f"Status code: { response .status_code } - Retrying in { wait_time } s" )
145-
146- response .raise_for_status ()
147-
148-
14928def main () -> None :
150- access_token = get_access_token ()
151- logging .info ("Access token for creation acquired" )
29+ client_id = os .environ ["AURA_API_CLIENT_ID" ]
30+ client_secret = os .environ ["AURA_API_CLIENT_SECRET" ]
31+ tenant_id = os .environ .get ("TENANT_ID" )
32+ aura_api = AuraApiCI (client_id = client_id , client_secret = client_secret , tenant_id = tenant_id )
15233
15334 MAX_INT = 1000000
15435 instance_name = f"ci-build-{ sys .argv [2 ]} " if len (sys .argv ) > 1 else "ci-instance-" + str (rd .randint (0 , MAX_INT ))
15536
156- create_result = create_instance (instance_name , access_token )
37+ create_result = aura_api . create_ds_instance (instance_name )
15738 instance_id = create_result ["id" ]
15839 logging .info ("Creation of database accepted" )
15940
16041 try :
161- check_running (access_token , instance_id )
42+ aura_api . check_running (instance_id )
16243 logging .info ("Database %s up and running" , instance_id )
16344
16445 if sys .argv [1 ] == "tests" :
@@ -178,10 +59,7 @@ def main() -> None:
17859 else :
17960 logging .error (f"Invalid target: { sys .argv [1 ]} " )
18061 finally :
181- access_token = get_access_token ()
182- logging .info ("Access token for teardown acquired" )
183-
184- teardown_instance (access_token , instance_id )
62+ aura_api .teardown_instance (instance_id )
18563 logging .info ("Teardown of instance %s successful" , instance_id )
18664
18765
0 commit comments