-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
50 lines (33 loc) · 1.13 KB
/
api.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
from time import sleep
from googleapiclient import discovery
# Constants:
SNAPSHOT_READY = 'READY'
PROJECT_ID = 'project_id'
service = discovery.build('compute', 'v1')
def check_disk(disk, zone):
try:
request = service.disks().get(project=PROJECT_ID, zone=zone, disk=disk)
request.execute()
except:
print("ERROR: Name or zone is not valid.")
exit(0)
def create_snapshot(name, description, disk, zone):
snapshot_body = {
"name": name,
"description": description,
}
request = service.disks().createSnapshot(project=PROJECT_ID, zone=zone, disk=disk, body=snapshot_body)
request.execute()
check_snapshot_status(name)
def check_snapshot_status(name):
while True:
sleep(20)
request = service.snapshots().get(project=PROJECT_ID, snapshot=name)
response = request.execute()
status = response.get('status')
if status == SNAPSHOT_READY:
print("Snapshot {0} created".format(name))
break
def delete_snapshot(name):
request = service.snapshots().delete(project=PROJECT_ID, snapshot=name)
request.execute()