def add_capacity_to_diskgroup(service_instance, vsan_disk_mgmt_system, host_ref, diskgroup, new_capacity_disks): """ Adds capacity disk(s) to a disk group. service_instance Service instance to the host or vCenter vsan_disk_mgmt_system vim.VimClusterVsanVcDiskManagemenetSystem representing the vSan disk management system retrieved from the vsan endpoint. host_ref vim.HostSystem object representing the target host the disk group will be created on diskgroup The vsan.HostDiskMapping object representing the host's diskgroup where the additional capacity needs to be added new_capacity_disks List of vim.HostScsiDisk objects representing the disks to be added as capacity disks. Can be either ssd or non-ssd. There must be a minimum of 1 new capacity disk in the list. """ hostname = salt.utils.vmware.get_managed_object_name(host_ref) cache_disk = diskgroup.ssd cache_disk_id = cache_disk.canonicalName log.debug( "Adding capacity to disk group with cache disk '%s' on host '%s'", cache_disk_id, hostname, ) log.trace("new_capacity_disk_ids = %s", [c.canonicalName for c in new_capacity_disks]) spec = vim.VimVsanHostDiskMappingCreationSpec() spec.cacheDisks = [cache_disk] spec.capacityDisks = new_capacity_disks # All new capacity disks must be either ssd or non-ssd (mixed disks are not # supported); also they need to match the type of the existing capacity # disks; we assume disks are already validated spec.creationType = ("allFlash" if getattr(new_capacity_disks[0], "ssd") else "hybrid") spec.host = host_ref try: task = vsan_disk_mgmt_system.InitializeDiskMappings(spec) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError("Not enough permissions. Required privilege: " "{0}".format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.fault.MethodNotFound as exc: log.exception(exc) raise VMwareRuntimeError("Method '{0}' not found".format(exc.method)) except vmodl.RuntimeFault as exc: raise VMwareRuntimeError(exc.msg) _wait_for_tasks([task], service_instance) return True
def create_diskgroup(service_instance, vsan_disk_mgmt_system, host_ref, cache_disk, capacity_disks): """ Creates a disk group service_instance Service instance to the host or vCenter vsan_disk_mgmt_system vim.VimClusterVsanVcDiskManagemenetSystem representing the vSan disk management system retrieved from the vsan endpoint. host_ref vim.HostSystem object representing the target host the disk group will be created on cache_disk The vim.HostScsidisk to be used as a cache disk. It must be an ssd disk. capacity_disks List of vim.HostScsiDisk objects representing of disks to be used as capacity disks. Can be either ssd or non-ssd. There must be a minimum of 1 capacity disk in the list. """ hostname = salt.utils.vmware.get_managed_object_name(host_ref) cache_disk_id = cache_disk.canonicalName log.debug( "Creating a new disk group with cache disk '%s' on host '%s'", cache_disk_id, hostname, ) log.trace("capacity_disk_ids = %s", [c.canonicalName for c in capacity_disks]) spec = vim.VimVsanHostDiskMappingCreationSpec() spec.cacheDisks = [cache_disk] spec.capacityDisks = capacity_disks # All capacity disks must be either ssd or non-ssd (mixed disks are not # supported) spec.creationType = "allFlash" if getattr(capacity_disks[0], "ssd") else "hybrid" spec.host = host_ref try: task = vsan_disk_mgmt_system.InitializeDiskMappings(spec) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError( "Not enough permissions. Required privilege: {}".format( exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.fault.MethodNotFound as exc: log.exception(exc) raise VMwareRuntimeError("Method '{}' not found".format(exc.method)) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg) _wait_for_tasks([task], service_instance) return True
def get_profile_manager(service_instance): """ Returns a profile manager service_instance Service instance to the host or vCenter """ stub = salt.utils.vmware.get_new_service_instance_stub(service_instance, ns="pbm/2.0", path="/pbm/sdk") pbm_si = pbm.ServiceInstance("ServiceInstance", stub) try: profile_manager = pbm_si.RetrieveContent().profileManager except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError( "Not enough permissions. Required privilege: {}".format( exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg) return profile_manager
def get_cluster_vsan_info(cluster_ref): """ Returns the extended cluster vsan configuration object (vim.VsanConfigInfoEx). cluster_ref Reference to the cluster """ cluster_name = salt.utils.vmware.get_managed_object_name(cluster_ref) log.trace("Retrieving cluster vsan info of cluster '%s'", cluster_name) si = salt.utils.vmware.get_service_instance_from_managed_object( cluster_ref) vsan_cl_conf_sys = get_vsan_cluster_config_system(si) try: return vsan_cl_conf_sys.VsanClusterGetConfig(cluster_ref) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError("Not enough permissions. Required privilege: " "{0}".format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg)
def assign_default_storage_policy_to_datastore(profile_manager, policy, datastore): ''' Assigns a storage policy as the default policy to a datastore. profile_manager Reference to the profile manager. policy Reference to the policy to assigned. datastore Reference to the datastore. ''' placement_hub = pbm.placement.PlacementHub(hubId=datastore._moId, hubType='Datastore') log.trace('placement_hub = {0}'.format(placement_hub)) try: profile_manager.AssignDefaultRequirementProfile( policy.profileId, [placement_hub]) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError('Not enough permissions. Required privilege: ' '{0}'.format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg)
def get_default_storage_policy_of_datastore(profile_manager, datastore): ''' Returns the default storage policy reference assigned to a datastore. profile_manager Reference to the profile manager. datastore Reference to the datastore. ''' # Retrieve all datastores visible hub = pbm.placement.PlacementHub(hubId=datastore._moId, hubType='Datastore') log.trace('placement_hub = {0}'.format(hub)) try: policy_id = profile_manager.QueryDefaultRequirementProfile(hub) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError('Not enough permissions. Required privilege: ' '{0}'.format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg) policy_refs = get_policies_by_id(profile_manager, [policy_id]) if not policy_refs: raise VMwareObjectRetrievalError('Storage policy with id \'{0}\' was ' 'not found'.format(policy_id)) return policy_refs[0]
def reconfigure_cluster_vsan(cluster_ref, cluster_vsan_spec): """ Reconfigures the VSAN system of a cluster. cluster_ref Reference to the cluster cluster_vsan_spec Cluster VSAN reconfigure spec (vim.vsan.ReconfigSpec). """ cluster_name = salt.utils.vmware.get_managed_object_name(cluster_ref) log.trace("Reconfiguring vsan on cluster '%s': %s", cluster_name, cluster_vsan_spec) si = salt.utils.vmware.get_service_instance_from_managed_object( cluster_ref) vsan_cl_conf_sys = salt.utils.vsan.get_vsan_cluster_config_system(si) try: task = vsan_cl_conf_sys.VsanClusterReconfig(cluster_ref, cluster_vsan_spec) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError("Not enough permissions. Required privilege: " "{0}".format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg) _wait_for_tasks([task], si)
def update_storage_policy(profile_manager, policy, policy_spec): ''' Updates a storage policy. profile_manager Reference to the profile manager. policy Reference to the policy to be updated. policy_spec Policy update spec. ''' try: profile_manager.Update(policy.profileId, policy_spec) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError('Not enough permissions. Required privilege: ' '{0}'.format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg)
def get_capability_definitions(profile_manager): ''' Returns a list of all capability definitions. profile_manager Reference to the profile manager. ''' res_type = pbm.profile.ResourceType( resourceType=pbm.profile.ResourceTypeEnum.STORAGE) try: cap_categories = profile_manager.FetchCapabilityMetadata(res_type) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError('Not enough permissions. Required privilege: ' '{0}'.format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg) cap_definitions = [] for cat in cap_categories: cap_definitions.extend(cat.capabilityMetadata) return cap_definitions
def remove_diskgroup(service_instance, host_ref, diskgroup, hostname=None, host_vsan_system=None, erase_disk_partitions=False, data_accessibility=True): ''' Removes a disk group. service_instance Service instance to the host or vCenter host_ref Reference to the ESXi host diskgroup The vsan.HostDiskMapping object representing the host's diskgroup from where the capacity needs to be removed hostname Name of ESXi host. Default value is None. host_vsan_system ESXi host's VSAN system. Default value is None. data_accessibility Specifies whether to ensure data accessibility. Default value is True. ''' if not hostname: hostname = salt.utils.vmware.get_managed_object_name(host_ref) cache_disk_id = diskgroup.ssd.canonicalName log.debug('Removing disk group with cache disk \'%s\' on ' 'host \'%s\'', cache_disk_id, hostname) if not host_vsan_system: host_vsan_system = get_host_vsan_system( service_instance, host_ref, hostname) # Set to evacuate all data before removing the disks maint_spec = vim.HostMaintenanceSpec() maint_spec.vsanMode = vim.VsanHostDecommissionMode() object_action = vim.VsanHostDecommissionModeObjectAction if data_accessibility: maint_spec.vsanMode.objectAction = \ object_action.ensureObjectAccessibility else: maint_spec.vsanMode.objectAction = object_action.noAction try: task = host_vsan_system.RemoveDiskMapping_Task( mapping=[diskgroup], maintenanceSpec=maint_spec) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError('Not enough permissions. Required privilege: ' '{0}'.format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg) salt.utils.vmware.wait_for_task(task, hostname, 'remove_diskgroup') log.debug('Removed disk group with cache disk \'%s\' on host \'%s\'', cache_disk_id, hostname) return True
def get_storage_policies(profile_manager, policy_names=None, get_all_policies=False): """ Returns a list of the storage policies, filtered by name. profile_manager Reference to the profile manager. policy_names List of policy names to filter by. Default is None. get_all_policies Flag specifying to return all policies, regardless of the specified filter. """ res_type = pbm.profile.ResourceType( resourceType=pbm.profile.ResourceTypeEnum.STORAGE) try: policy_ids = profile_manager.QueryProfile(res_type) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError( "Not enough permissions. Required privilege: {}".format( exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg) log.trace("policy_ids = %s", policy_ids) # More policies are returned so we need to filter again policies = [ p for p in get_policies_by_id(profile_manager, policy_ids) if p.resourceType.resourceType == pbm.profile.ResourceTypeEnum.STORAGE ] if get_all_policies: return policies if not policy_names: policy_names = [] return [p for p in policies if p.name in policy_names]
def _wait_for_tasks(tasks, service_instance): """ Wait for tasks created via the VSAN API """ log.trace("Waiting for vsan tasks: {0}", ", ".join([six.text_type(t) for t in tasks])) try: vsanapiutils.WaitForTasks(tasks, service_instance) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError("Not enough permissions. Required privilege: " "{0}".format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg) log.trace("Tasks %s finished successfully", ", ".join([six.text_type(t) for t in tasks]))
def _wait_for_tasks(tasks, service_instance): ''' Wait for tasks created via the VSAN API ''' log.trace('Waiting for vsan tasks: {0}' ''.format(', '.join([str(t) for t in tasks]))) try: vsanapiutils.WaitForTasks(tasks, service_instance) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError('Not enough permissions. Required privilege: ' '{0}'.format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg) log.trace('Tasks {0} finished successfully' ''.format(', '.join([str(t) for t in tasks])))
def get_policies_by_id(profile_manager, policy_ids): ''' Returns a list of policies with the specified ids. profile_manager Reference to the profile manager. policy_ids List of policy ids to retrieve. ''' try: return profile_manager.RetrieveContent(policy_ids) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError('Not enough permissions. Required privilege: ' '{0}'.format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg)
def create_storage_policy(profile_manager, policy_spec): """ Creates a storage policy. profile_manager Reference to the profile manager. policy_spec Policy update spec. """ try: profile_manager.Create(policy_spec) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError("Not enough permissions. Required privilege: " "{0}".format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg)
def vsan_supported(service_instance): """ Returns whether vsan is supported on the vCenter: api version needs to be 6 or higher service_instance Service instance to the host or vCenter """ try: api_version = service_instance.content.about.apiVersion except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError("Not enough permissions. Required privilege: " "{0}".format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg) if int(api_version.split(".")[0]) < 6: return False return True
def get_placement_solver(service_instance): ''' Returns a placement solver service_instance Service instance to the host or vCenter ''' stub = salt.utils.vmware.get_new_service_instance_stub( service_instance, ns='pbm/2.0', path='/pbm/sdk') pbm_si = pbm.ServiceInstance('ServiceInstance', stub) try: profile_manager = pbm_si.RetrieveContent().placementSolver except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError('Not enough permissions. Required privilege: ' '{0}'.format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg) return profile_manager
def remove_capacity_from_diskgroup( service_instance, host_ref, diskgroup, capacity_disks, data_evacuation=True, hostname=None, host_vsan_system=None, ): """ Removes capacity disk(s) from a disk group. service_instance Service instance to the host or vCenter host_vsan_system ESXi host's VSAN system host_ref Reference to the ESXi host diskgroup The vsan.HostDiskMapping object representing the host's diskgroup from where the capacity needs to be removed capacity_disks List of vim.HostScsiDisk objects representing the capacity disks to be removed. Can be either ssd or non-ssd. There must be a minimum of 1 capacity disk in the list. data_evacuation Specifies whether to gracefully evacuate the data on the capacity disks before removing them from the disk group. Default value is True. hostname Name of ESXi host. Default value is None. host_vsan_system ESXi host's VSAN system. Default value is None. """ if not hostname: hostname = salt.utils.vmware.get_managed_object_name(host_ref) cache_disk = diskgroup.ssd cache_disk_id = cache_disk.canonicalName log.debug( "Removing capacity from disk group with cache disk '%s' on host '%s'", cache_disk_id, hostname, ) log.trace("capacity_disk_ids = %s", [c.canonicalName for c in capacity_disks]) if not host_vsan_system: host_vsan_system = get_host_vsan_system(service_instance, host_ref, hostname) # Set to evacuate all data before removing the disks maint_spec = vim.HostMaintenanceSpec() maint_spec.vsanMode = vim.VsanHostDecommissionMode() if data_evacuation: maint_spec.vsanMode.objectAction = ( vim.VsanHostDecommissionModeObjectAction.evacuateAllData) else: maint_spec.vsanMode.objectAction = ( vim.VsanHostDecommissionModeObjectAction.noAction) try: task = host_vsan_system.RemoveDisk_Task(disk=capacity_disks, maintenanceSpec=maint_spec) except vim.fault.NoPermission as exc: log.exception(exc) raise VMwareApiError("Not enough permissions. Required privilege: " "{0}".format(exc.privilegeId)) except vim.fault.VimFault as exc: log.exception(exc) raise VMwareApiError(exc.msg) except vmodl.RuntimeFault as exc: log.exception(exc) raise VMwareRuntimeError(exc.msg) salt.utils.vmware.wait_for_task(task, hostname, "remove_capacity") return True