-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
42 lines (35 loc) · 1.18 KB
/
main.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
import base64
import os
import googleapiclient.discovery
class Config:
name = os.environ.get("NAME")
project = os.environ.get("PROJECT")
zone = os.environ.get("ZONE")
def start_vm(event, context):
compute = googleapiclient.discovery.build('compute', 'v1', cache_discovery=False)
try:
name = (
base64.b64decode(event["data"]).decode("utf-8")
if "data" in event
else Config.name
)
except ValueError:
name = Config.name
if Config.project is not None:
compute.instances().start(project=Config.project, zone=Config.zone, instance=name).execute()
else:
print("The .env.yaml is not found.")
def stop_vm(event, context):
compute = googleapiclient.discovery.build('compute', 'v1', cache_discovery=False)
try:
name = (
base64.b64decode(event["data"]).decode("utf-8")
if "data" in event
else Config.name
)
except ValueError:
name = Config.name
if Config.project is not None:
compute.instances().stop(project=Config.project, zone=Config.zone, instance=name).execute()
else:
print("The .env.yaml is not found.")