Skip to content

Commit f6ca4ea

Browse files
committed
Delete associated virtual disk as part of destroy flow
Signed-off-by: Manjunath-A-C <[email protected]>
1 parent 7916690 commit f6ca4ea

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

cli/utils/command_util.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,44 @@ def remove_vopt_device(config, cookies, vios, vopt_name):
7676
raise e
7777
return
7878

79+
80+
def remove_virtual_disk(config, cookies, vios_uuid, vg_id, vdisk):
81+
try:
82+
url = f"https://{util.get_host_address(config)}/rest/api/uom/VirtualIOServer/{vios_uuid}/VolumeGroup/{vg_id}"
83+
headers = {"x-api-key": util.get_session_key(
84+
config), "Content-Type": "application/vnd.ibm.powervm.uom+xml; type=VolumeGroup"}
85+
response = requests.get(url, headers=headers,
86+
cookies=cookies, verify=False)
87+
if response.status_code != 200:
88+
logger.error(
89+
f"failed to get volumegroup details, error: {response.text}")
90+
raise Exception(
91+
f"failed to get volumegroup details, error: {response.text}")
92+
soup = BeautifulSoup(response.text, 'xml')
93+
94+
# remove virtual disk
95+
vdisks = soup.find_all("VirtualDisk")
96+
for disk in vdisks:
97+
if disk.find("DiskName", string=vdisk) is not None:
98+
disk.decompose()
99+
100+
headers = {"x-api-key": util.get_session_key(
101+
config), "Content-Type": "application/vnd.ibm.powervm.uom+xml; type=VolumeGroup"}
102+
# Now update the modified media repositoy list after delete
103+
response = requests.post(url, data=str(soup),
104+
headers=headers, cookies=cookies, verify=False)
105+
if response.status_code != 200:
106+
logger.error(
107+
f"failed to update volume group after deleting virtual disk, error: {response.text}")
108+
return
109+
110+
logger.debug(
111+
f"Virtualdisk '{vdisk}' has been deleted successfully")
112+
except Exception as e:
113+
raise e
114+
return
115+
116+
79117
def check_if_scsi_mapping_exist(partition_uuid, vios, media_dev_name):
80118
found = False
81119
vscsi = None

cli/vios/vios.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from bs4 import BeautifulSoup
55

66
import cli.storage.storage as storage
7+
import cli.storage.virtual_storage as vstorage
78
import cli.utils.common as common
89
import cli.utils.command_util as command_util
910
import cli.utils.string_util as util
@@ -160,20 +161,33 @@ def cleanup_vios(config, cookies, sys_uuid, partition_uuid, vios_uuid_list):
160161
vios = get_vios_details(config, cookies, sys_uuid, vios_uuid)
161162

162163
# remove SCSI mappings from VIOS
163-
found, phys_disk = storage.check_if_storage_attached(
164+
phys_disk_found, phys_disk = storage.check_if_storage_attached(
164165
vios, partition_uuid)
165-
if found and not storage_cleaned:
166+
if phys_disk_found and not storage_cleaned:
166167
logger.info(
167168
f"Removing SCSI mapping for physical disk '{phys_disk}'")
168169
command_util.remove_scsi_mappings(
169170
config, cookies, sys_uuid, partition_uuid, vios_uuid, vios, phys_disk)
170171
storage_cleaned = True
172+
# Check if attached disk is virtual disk
173+
if not phys_disk_found:
174+
vdisk_found, vdisk = storage.check_if_vdisk_attached(vios, partition_uuid)
175+
if vdisk_found:
176+
logger.info(
177+
f"Removing SCSI mapping for virtual disk '{vdisk}'")
178+
command_util.remove_scsi_mappings(
179+
config, cookies, sys_uuid, partition_uuid, vios_uuid, vios, vdisk)
180+
if not util.use_existing_vd(config):
181+
# delete associated virtual disk
182+
vg_id = get_volume_group(config, cookies, vios_uuid, util.get_volume_group_name(config))
183+
command_util.remove_virtual_disk(config, cookies, vios_uuid, vg_id, vdisk)
184+
logger.info(f"Delete virtualdisk '{vdisk}' associated to partition '{util.get_partition_name(config)}'")
185+
storage_cleaned = True
171186

172187
vios = get_vios_details(config, cookies, sys_uuid, vios_uuid)
173188
logger.info(f"Removing SCSI mapping for vOPT device '{vopt}'")
174189
command_util.remove_scsi_mappings(
175190
config, cookies, sys_uuid, partition_uuid, vios_uuid, vios, vopt)
176-
# TODO: delete virtual disk, volumegroup if created by the script during launch
177191

178192
vios = get_vios_details(config, cookies, sys_uuid, vios_uuid)
179193

@@ -189,13 +203,26 @@ def cleanup_vios(config, cookies, sys_uuid, partition_uuid, vios_uuid_list):
189203
if vios_uuid not in processed_vios_list:
190204
vios = get_vios_details(
191205
config, cookies, sys_uuid, vios_uuid)
192-
found, phys_disk = storage.check_if_storage_attached(
206+
phys_disk_found, phys_disk = storage.check_if_storage_attached(
193207
vios, partition_uuid)
194-
if found:
208+
if phys_disk_found:
195209
logger.debug(
196210
f"Removing SCSI mapping for physical disk '{phys_disk}'")
197211
command_util.remove_scsi_mappings(
198212
config, cookies, sys_uuid, partition_uuid, vios_uuid, vios, phys_disk)
213+
# Check if attached disk is virtual disk
214+
if not phys_disk_found:
215+
vdisk_found, vdisk = storage.check_if_vdisk_attached(vios, partition_uuid)
216+
if vdisk_found:
217+
logger.info(
218+
f"Removing SCSI mapping for virtual disk '{vdisk}'")
219+
command_util.remove_scsi_mappings(
220+
config, cookies, sys_uuid, partition_uuid, vios_uuid, vios, vdisk)
221+
# delete associated virtual disk
222+
vg_id = get_volume_group(config, cookies, vios_uuid, util.get_volume_group_name(config))
223+
command_util.remove_virtual_disk(config, cookies, vios_uuid, vg_id, vdisk)
224+
logger.info(
225+
f"Delete virtualdisk '{vdisk}' associated to partition '{util.get_partition_name(config)}'")
199226
except Exception as e:
200227
logger.error(f"failed to clean up VIOS, error: {e}")
201228

0 commit comments

Comments
 (0)