-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute.py
65 lines (52 loc) · 2.3 KB
/
compute.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from typing import Iterable
import google
from google.cloud import compute_v1
class GoogleComputeEngine:
instance_client = None
def __init__(self, credentials: google.auth.credentials.Credentials):
if credentials == None:
self.instance_client = compute_v1.InstancesClient()
else:
self.instance_client = compute_v1.InstancesClient(credentials)
def list_instances(self, project_id: str, zone: str) -> Iterable[compute_v1.Instance]:
"""
List all instances in the given zone in the specified project.
Args:
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone you want to use.
Returns:
An iterable collection of Instance objects.
"""
return self.instance_client.list(project=project_id, zone=zone)
def filter_by_label(self, instances: Iterable[compute_v1.Instance], label: str, value: str):
"""
Returns a filtered list of instances based on the label name and value.
Args:
instances: Iterable[compute_v1.Instance]
label: The label to search for
value: The value the label needs to contain.
Returns:
An iterable collection of Instance objects.
"""
filtered_instances = []
for instance in instances:
if label in instance.labels:
if value == instance.labels[label]:
filtered_instances.append(instance)
return filtered_instances
def start_stop_instance(self, instance: compute_v1.Instance, project: str, zone: str, action: str):
"""
Start or Stop the named instance
Args:
instance: compute_v1.instance to start/stop
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone you want to use.
Returns:
A instance start/stop response
"""
if action.lower == 'start':
return self.instance_client.start(project=project, zone=zone, instance=instance, timeout=30)
elif action.lower == 'stop':
return self.instance_client.stop(project=project, zone=zone, instance=instance, timeout=30)
else:
return