Skip to content

Commit

Permalink
gcp: delete disk
Browse files Browse the repository at this point in the history
  • Loading branch information
karmab committed Apr 9, 2024
1 parent dee05fd commit 64aeb3c
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions kvirt/providers/gcp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,22 @@ def update_reserveip(self, name):
return {'result': 'success'}

def create_disk(self, name, size, pool=None, thin=True, image=None):
print("not implemented")
conn = self.conn
project = self.project
zone = self.zone
body = {'sizeGb': size, 'name': name}
conn.disks().insert(zone=zone, project=project, body=body).execute()
timeout = 0
while True:
if timeout > 60:
return {'result': 'failure', 'reason': 'timeout waiting for new disk to be ready'}
newdisk = conn.disks().get(zone=zone, project=project, disk=name).execute()
if newdisk['status'] == 'READY':
break
else:
timeout += 5
sleep(5)
pprint("Waiting for disk to be ready")
return

def add_disk(self, name, size, pool=None, thin=True, image=None, shareable=False, existing=None,
Expand Down Expand Up @@ -988,11 +1003,31 @@ def delete_disk(self, name=None, diskname=None, pool=None, novm=False):
conn = self.conn
project = self.project
zone = self.zone
if novm:
try:
conn.disks().delete(zone=zone, project=project, disk=diskname).execute()
except Exception as e:
error(e)
return {'result': 'failure', 'reason': e}
return {'result': 'success'}
try:
conn.disks().delete(zone=zone, project=project, disk=diskname).execute()
vm = conn.instances().get(zone=zone, project=project, instance=name).execute()
except:
return {'result': 'failure', 'reason': f"Disk {diskname} not found"}
return
return {'result': 'failure', 'reason': f"VM {name} not found"}
for disk in vm['disks']:
devname = disk['deviceName']
source = os.path.basename(disk['source'])
if devname == diskname or source == diskname:
operation = conn.instances().detachDisk(project=project, zone=zone, instance=name,
deviceName=devname).execute()
self._wait_for_operation(operation)
try:
conn.disks().delete(zone=zone, project=project, disk=diskname).execute()
except Exception as e:
error(e)
return {'result': 'failure', 'reason': e}
break
return {'result': 'success'}

def list_disks(self):
disks = {}
Expand Down

0 comments on commit 64aeb3c

Please sign in to comment.