import json import requests stagingUrl = "https://us-west-2.staging.shared.aws.tidbcloud.com" nightlyUrl = "https://us-west-2.nightly.shared.aws.tidbcloud.com" def fetch_recycle_bin(env, tokens, tenantId, projectid ): baseUrl = "" if env == 'nightly': baseUrl = nightlyUrl if env == 'staging': baseUrl = stagingUrl header = {'Authorization': tokens} print( "fetch recycle-bins " + baseUrl + "/api/v1/orgs/{}/projects/{}/recycle-bin".format( tenantId, projectid )) r = requests.get( baseUrl + "/api/v1/orgs/{}/projects/{}/recycle-bin".format( tenantId, projectid ), headers = header ) print( r.text ) contents = json.loads( r.text ) clusters = [] if 'items' in contents: for cluster in contents['items']: clusters.append( cluster['id'] ) return clusters def delete_recycle_bin(env, tokens, tenantId, projectid, clusterid): baseUrl = "" if env == 'nightly': baseUrl = nightlyUrl if env == 'staging': baseUrl = stagingUrl header = {'Authorization': tokens} print( "delete recycle-bins " + baseUrl + "/api/v1/orgs/{}/projects/{}" "/recycle-bin/{}/backups".format( tenantId, projectid, clusterid )) r = requests.delete( baseUrl + "/api/v1/orgs/{}/projects/{}/recycle-bin/{}/backups".format( tenantId, projectid, clusterid ), headers = header ) contents = json.loads( r.text ) if "message" in contents: msg = contents['message'] if 'Before deleting the project, please confirm that the following clusters ' \ 'and their backups in the recycle bin have been deleted:' in msg: msg = msg.replace("Before deleting the project, please confirm that the following clusters " "and their backups in the recycle bin have been deleted:", "").replace(".", "").replace(" ","") clusters = msg.split(",") print(clusters) for cluster in clusters: print("Delete Recycle-Bin {}".format(cluster) ) r = requests.delete( baseUrl + "/api/v1/orgs/{}/projects/{}/recycle-bin/{}/backups".format( tenantId, projectid, cluster ), headers = header ) if "__main__" == __name__: import projects import tokens email = projects.find_project_user_emails('staging', '1369847559691367403') token = tokens.fetchDBaaSToken("staging", email) clusters = fetch_recycle_bin("staging", token, "1369847559690647294", "1369847559691367403" ) print(clusters) delete_recycle_bin( "staging", token, "1369847559690647294", "1369847559691367403", clusters[0] )