import os import requests import pymysql host = "tidb.e23bdccc.cb17aaf2.us-west-2.prod.aws.tidbcloud.com" passwd = os.getenv('passwd') stagingUrl = "https://us-west-2.staging.shared.aws.tidbcloud.com" nightlyUrl = "https://us-west-2.nightly.shared.aws.tidbcloud.com" def find_vpcpeerings(env, projectid): awsPeerings = [] gcpPeerings = [] if env == 'nightly': env = 'dev' dbName = env + '_central' db = pymysql.connect(host=host, port=4000, user='root', password=passwd, db=dbName, charset='utf8' ) cursor = db.cursor() cursor.execute("select id from aws_vpc_peerings where status in ('active','pending') and project_id = {}" .format(projectid) ) results = cursor.fetchall() for row in results: id = row[0] awsPeerings.append( id ) cursor.execute("select id from gcp_vpc_peerings where status in ('active','pending') and project_id = {}" .format(projectid) ) results = cursor.fetchall() for row in results: id = row[0] gcpPeerings.append( id ) return awsPeerings, gcpPeerings def delete_vpcpeering(tokens, env, tenantId, projectid): baseUrl = "" if env == 'nightly': baseUrl = nightlyUrl if env == 'staging': baseUrl = stagingUrl awsPeerings, gcpPeerings = find_vpcpeerings(env, projectid) header = {'Authorization': tokens} for peering in gcpPeerings: print("delete vpcpeering " + baseUrl + "/api/v1/orgs/{}/projects/{}/gcp_vpc_peerings/{}".format( tenantId, projectid, peering, )) # r = requests.delete( # baseUrl + "/api/v1/orgs/{}/projects/{}/gcp_vpc_peerings/{}". # format( # tenantId, # projectid, # peering, # ), # headers = header # ) # print( r.text ) for peering in awsPeerings: print("delete vpcpeering " + baseUrl + "/api/v1/orgs/{}/projects/{}/aws_vpc_peerings/{}".format( tenantId, projectid, peering, )) # r = requests.delete( # baseUrl + "/api/v1/orgs/{}/projects/{}/aws_vpc_peerings/{}". # format( # tenantId, # projectid, # peering, # ), # headers = header # ) # print( r.text )