1+ import requests
2+ import os
3+ import json
4+ import base64
5+ from requests .auth import HTTPBasicAuth
6+
7+ def read_json_file (file_path ):
8+ try :
9+ with open (file_path , "r" , encoding = 'utf-8' ) as f :
10+ return json .load (f )
11+ except Exception as e :
12+ print (f"Error reading JSON file at { file_path } : { e } " , flush = True )
13+ return None
14+
15+ def get (url , username , password , params ):
16+ """Makes a GET API call and returns the response data or prints an error if the response is non-200."""
17+ try :
18+ response = requests .get (url , auth = HTTPBasicAuth (username , password ), params = params )
19+
20+ # Check if the response status code is not 200
21+ if response .status_code != 200 :
22+ print (f"Error: Received status code { response .status_code } ." )
23+ print ("Response message:" , response .text ) # Print the error message from the response
24+ return None
25+
26+ return response .json ()
27+ except requests .exceptions .RequestException as e :
28+ print (f"Error during API call: { e } " )
29+ return None
30+
31+ def delete (url , username , password ):
32+ """Makes a DELETE API call and returns the response data or prints an error if the response is non-200."""
33+ try :
34+ response = requests .delete (url , auth = HTTPBasicAuth (username , password ))
35+
36+ # Check if the response status code is not 200
37+ if response .status_code != 200 :
38+ print (f"Error: Received status code { response .status_code } ." )
39+ print ("Response message:" , response .text ) # Print the error message from the response
40+ return None
41+
42+ return response .json ()
43+ except requests .exceptions .RequestException as e :
44+ print (f"Error during API call: { e } " )
45+ return None
46+
47+ if __name__ == '__main__' :
48+ # Load control planes and secrets
49+ control_planes = read_json_file ('control_planes.json' )
50+ secrets = read_json_file ('secrets.json' )
51+ feature_branch_name = os .getenv ('GITHUB_REF_NAME' , 'refs/heads/develop' )
52+
53+ for key , value in control_planes .items ():
54+ cp_url = value .get ('URL' , "" )
55+ username = value .get ('Username' , "" )
56+ token = secrets .get (value .get ('TokenRef' , "" ), '' )
57+ # Assuming the first URL is used for API calls
58+ modules = get (cp_url + '/cc-ui/v1/modules/all' , username , token , {'allowPreviewModules' : 'true' })
59+
60+ for module in modules :
61+ version = module .get ('version' , '' )
62+ if f'-{ feature_branch_name } ' in version :
63+ module_id = module .get ('id' , '' )
64+ delete (cp_url + f'/cc-ui/v1/modules/{ module_id } ' , username , token )
65+ print (f"Deleted module with ID: { module_id } from control plane { cp_url } " )
0 commit comments