|
9 | 9 | from cloudproxy.providers.config import set_auth |
10 | 10 | from cloudproxy.providers.settings import config |
11 | 11 |
|
12 | | -gcp = config["providers"]["gcp"] |
13 | | -if gcp["enabled"] == 'True': |
| 12 | +gcp = None |
| 13 | +compute = None |
| 14 | + |
| 15 | +def get_client(instance_config=None): |
| 16 | + """ |
| 17 | + Initialize and return a GCP client based on the provided configuration. |
| 18 | + |
| 19 | + Args: |
| 20 | + instance_config: The specific instance configuration |
| 21 | + |
| 22 | + Returns: |
| 23 | + tuple: (config, gcp_client) |
| 24 | + """ |
| 25 | + |
| 26 | + global gcp, compute |
| 27 | + if gcp is not None and compute is not None: |
| 28 | + return gcp, compute |
| 29 | + |
| 30 | + if instance_config is None: |
| 31 | + instance_config = config["providers"]["gcp"]["instances"]["default"] |
| 32 | + |
| 33 | + gcp = config["providers"]["gcp"] |
14 | 34 | try: |
15 | | - credentials = service_account.Credentials.from_service_account_info( |
16 | | - json.loads(gcp["secrets"]["service_account_key"]) |
17 | | - ) |
| 35 | + if 'sa_json' in instance_config["secrets"]: |
| 36 | + credentials = service_account.Credentials.from_service_account_file( |
| 37 | + instance_config["secrets"]["sa_json"] |
| 38 | + ) |
| 39 | + else: |
| 40 | + credentials = service_account.Credentials.from_service_account_info( |
| 41 | + json.loads(instance_config["secrets"]["service_account_key"]) |
| 42 | + ) |
18 | 43 | compute = googleapiclient.discovery.build('compute', 'v1', credentials=credentials) |
| 44 | + |
| 45 | + return gcp, compute |
19 | 46 | except TypeError: |
20 | 47 | logger.error("GCP -> Invalid service account key") |
21 | 48 |
|
22 | 49 |
|
23 | | -def create_proxy(): |
| 50 | +def create_proxy(instance_config=None): |
| 51 | + """ |
| 52 | + Create a GCP proxy instance. |
| 53 | + |
| 54 | + Args: |
| 55 | + instance_config: The specific instance configuration |
| 56 | + """ |
| 57 | + if instance_config is None: |
| 58 | + instance_config = config["providers"]["gcp"]["instances"]["default"] |
| 59 | + |
| 60 | + gcp, compute = get_client(instance_config) |
| 61 | + |
24 | 62 | image_response = compute.images().getFromFamily( |
25 | | - project=gcp["image_project"], |
26 | | - family=gcp["image_family"] |
| 63 | + project=instance_config["image_project"], |
| 64 | + family=instance_config["image_family"] |
27 | 65 | ).execute() |
28 | 66 | source_disk_image = image_response['selfLink'] |
29 | 67 |
|
30 | 68 | body = { |
31 | 69 | 'name': 'cloudproxy-' + str(uuid.uuid4()), |
32 | 70 | 'machineType': |
33 | | - f"zones/{gcp['zone']}/machineTypes/{gcp['size']}", |
| 71 | + f"zones/{instance_config['zone']}/machineTypes/{instance_config['size']}", |
34 | 72 | 'tags': { |
35 | 73 | 'items': [ |
36 | 74 | 'cloudproxy' |
@@ -67,48 +105,93 @@ def create_proxy(): |
67 | 105 | } |
68 | 106 |
|
69 | 107 | return compute.instances().insert( |
70 | | - project=gcp["project"], |
71 | | - zone=gcp["zone"], |
| 108 | + project=instance_config["project"], |
| 109 | + zone=instance_config["zone"], |
72 | 110 | body=body |
73 | 111 | ).execute() |
74 | 112 |
|
75 | | -def delete_proxy(name): |
| 113 | +def delete_proxy(name, instance_config=None): |
| 114 | + """ |
| 115 | + Delete a GCP proxy instance. |
| 116 | + |
| 117 | + Args: |
| 118 | + name: Name of the instance to delete |
| 119 | + instance_config: The specific instance configuration |
| 120 | + """ |
| 121 | + if instance_config is None: |
| 122 | + instance_config = config["providers"]["gcp"]["instances"]["default"] |
| 123 | + |
| 124 | + gcp, compute = get_client(instance_config) |
| 125 | + |
76 | 126 | try: |
77 | 127 | return compute.instances().delete( |
78 | | - project=gcp["project"], |
79 | | - zone=gcp["zone"], |
| 128 | + project=instance_config["project"], |
| 129 | + zone=instance_config["zone"], |
80 | 130 | instance=name |
81 | 131 | ).execute() |
82 | 132 | except googleapiclient.errors.HttpError: |
83 | 133 | logger.info(f"GCP --> HTTP Error when trying to delete proxy {name}. Probably has already been deleted.") |
84 | 134 | return None |
85 | 135 |
|
86 | | -def stop_proxy(name): |
| 136 | +def stop_proxy(name, instance_config=None): |
| 137 | + """ |
| 138 | + Stop a GCP proxy instance. |
| 139 | + |
| 140 | + Args: |
| 141 | + name: Name of the instance to stop |
| 142 | + instance_config: The specific instance configuration |
| 143 | + """ |
| 144 | + if instance_config is None: |
| 145 | + instance_config = config["providers"]["gcp"]["instances"]["default"] |
| 146 | + |
87 | 147 | try: |
88 | 148 | return compute.instances().stop( |
89 | | - project=gcp["project"], |
90 | | - zone=gcp["zone"], |
| 149 | + project=instance_config["project"], |
| 150 | + zone=instance_config["zone"], |
91 | 151 | instance=name |
92 | 152 | ).execute() |
93 | 153 | except googleapiclient.errors.HttpError: |
94 | 154 | logger.info(f"GCP --> HTTP Error when trying to stop proxy {name}. Probably has already been deleted.") |
95 | 155 | return None |
96 | 156 |
|
97 | | -def start_proxy(name): |
| 157 | +def start_proxy(name, instance_config=None): |
| 158 | + """ |
| 159 | + Start a GCP proxy instance. |
| 160 | + |
| 161 | + Args: |
| 162 | + name: Name of the instance to start |
| 163 | + instance_config: The specific instance configuration |
| 164 | + """ |
| 165 | + if instance_config is None: |
| 166 | + instance_config = config["providers"]["gcp"]["instances"]["default"] |
| 167 | + |
| 168 | + gcp, compute = get_client(instance_config) |
| 169 | + |
98 | 170 | try: |
99 | 171 | return compute.instances().start( |
100 | | - project=gcp["project"], |
101 | | - zone=gcp["zone"], |
| 172 | + project=instance_config["project"], |
| 173 | + zone=instance_config["zone"], |
102 | 174 | instance=name |
103 | 175 | ).execute() |
104 | 176 | except googleapiclient.errors.HttpError: |
105 | 177 | logger.info(f"GCP --> HTTP Error when trying to start proxy {name}. Probably has already been deleted.") |
106 | 178 | return None |
107 | 179 |
|
108 | | -def list_instances(): |
| 180 | +def list_instances(instance_config=None): |
| 181 | + """ |
| 182 | + List all GCP proxy instances. |
| 183 | + |
| 184 | + Args: |
| 185 | + instance_config: The specific instance configuration |
| 186 | + """ |
| 187 | + if instance_config is None: |
| 188 | + instance_config = config["providers"]["gcp"]["instances"]["default"] |
| 189 | + |
| 190 | + gcp, compute = get_client(instance_config) |
| 191 | + |
109 | 192 | result = compute.instances().list( |
110 | | - project=gcp["project"], |
111 | | - zone=gcp["zone"], |
| 193 | + project=instance_config["project"], |
| 194 | + zone=instance_config["zone"], |
112 | 195 | filter='labels.cloudproxy eq cloudproxy' |
113 | 196 | ).execute() |
114 | 197 | return result['items'] if 'items' in result else [] |
0 commit comments