Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor vmware_guest_disk #1358

Open
mariolenz opened this issue Jun 18, 2022 · 0 comments
Open

Refactor vmware_guest_disk #1358

mariolenz opened this issue Jun 18, 2022 · 0 comments

Comments

@mariolenz
Copy link
Collaborator

SUMMARY

As @Tireg pointed out in #1321, the bug fixed in this PR might also be present in module_utils/vmware.py. Looking at get_recommended_datastore in both the module and the module util, I should say they are very similar.

Maybe we can refactor things and avoid duplicated code.

ISSUE TYPE
  • Feature Idea
COMPONENT NAME

vmware_guest_disk
module_utils/vmware

ADDITIONAL INFORMATION

vmware_guest_disk:

def get_recommended_datastore(self, datastore_cluster_obj, disk_spec_obj):
"""
Return Storage DRS recommended datastore from datastore cluster
Args:
datastore_cluster_obj: datastore cluster managed object
Returns: Name of recommended datastore from the given datastore cluster,
Returns None if no datastore recommendation found.
"""
# Check if Datastore Cluster provided by user is SDRS ready
sdrs_status = datastore_cluster_obj.podStorageDrsEntry.storageDrsConfig.podConfig.enabled
if sdrs_status:
# We can get storage recommendation only if SDRS is enabled on given datastorage cluster
disk_loc = vim.storageDrs.PodSelectionSpec.DiskLocator()
pod_config = vim.storageDrs.PodSelectionSpec.VmPodConfig()
pod_config.storagePod = datastore_cluster_obj
pod_config.disk = [disk_loc]
pod_sel_spec = vim.storageDrs.PodSelectionSpec()
pod_sel_spec.initialVmConfig = [pod_config]
storage_spec = vim.storageDrs.StoragePlacementSpec()
storage_spec.configSpec = vim.vm.ConfigSpec()
storage_spec.configSpec.deviceChange.append(disk_spec_obj)
storage_spec.resourcePool = self.vm.resourcePool
storage_spec.podSelectionSpec = pod_sel_spec
storage_spec.vm = self.vm
storage_spec.type = 'reconfigure'
try:
rec = self.content.storageResourceManager.RecommendDatastores(storageSpec=storage_spec)
rec_action = rec.recommendations[0].action[0]
return rec_action.destination.name
except Exception:
# There is some error so we fall back to general workflow
pass
datastore = None
datastore_freespace = 0
for ds in datastore_cluster_obj.childEntity:
if ds.summary.maintenanceMode == "inMaintenance":
continue
if ds.summary.freeSpace > datastore_freespace:
# If datastore field is provided, filter destination datastores
datastore = ds
datastore_freespace = ds.summary.freeSpace
if datastore:
return datastore.name
return None

module_utils/vmware:

def get_recommended_datastore(self, datastore_cluster_obj=None):
"""
Return Storage DRS recommended datastore from datastore cluster
Args:
datastore_cluster_obj: datastore cluster managed object
Returns: Name of recommended datastore from the given datastore cluster
"""
if datastore_cluster_obj is None:
return None
# Check if Datastore Cluster provided by user is SDRS ready
sdrs_status = datastore_cluster_obj.podStorageDrsEntry.storageDrsConfig.podConfig.enabled
if sdrs_status:
# We can get storage recommendation only if SDRS is enabled on given datastorage cluster
pod_sel_spec = vim.storageDrs.PodSelectionSpec()
pod_sel_spec.storagePod = datastore_cluster_obj
storage_spec = vim.storageDrs.StoragePlacementSpec()
storage_spec.podSelectionSpec = pod_sel_spec
storage_spec.type = 'create'
try:
rec = self.content.storageResourceManager.RecommendDatastores(storageSpec=storage_spec)
rec_action = rec.recommendations[0].action[0]
return rec_action.destination.name
except Exception:
# There is some error so we fall back to general workflow
pass
datastore = None
datastore_freespace = 0
for ds in datastore_cluster_obj.childEntity:
if isinstance(ds, vim.Datastore) and ds.summary.freeSpace > datastore_freespace:
# If datastore field is provided, filter destination datastores
if not self.is_datastore_valid(datastore_obj=ds):
continue
datastore = ds
datastore_freespace = ds.summary.freeSpace
if datastore:
return datastore.name
return None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant