def _select_datastore(data_stores, best_match, datastore_regex=None): """Find the most preferable datastore in a given RetrieveResult object. :param data_stores: a RetrieveResult object from vSphere API call :param best_match: the current best match for datastore :param datastore_regex: an optional regular expression to match names :return: datastore_ref, datastore_name, capacity, freespace """ # data_stores is actually a RetrieveResult object from vSphere API call for obj_content in data_stores.objects: # the propset attribute "need not be set" by returning API if not hasattr(obj_content, 'propSet'): continue propdict = vm_util.propset_dict(obj_content.propSet) if _is_datastore_valid(propdict, datastore_regex): new_ds = Datastore(ref=obj_content.obj, name=propdict['summary.name'], capacity=propdict['summary.capacity'], freespace=propdict['summary.freeSpace']) # favor datastores with more free space if (best_match is None or new_ds.freespace > best_match.freespace): best_match = new_ds return best_match
def _select_datastore(data_stores, best_match, datastore_regex=None): """Find the most preferable datastore in a given RetrieveResult object. :param data_stores: a RetrieveResult object from vSphere API call :param best_match: the current best match for datastore :param datastore_regex: an optional regular expression to match names :return: datastore_ref, datastore_name, capacity, freespace """ # data_stores is actually a RetrieveResult object from vSphere API call for obj_content in data_stores.objects: # the propset attribute "need not be set" by returning API if not hasattr(obj_content, 'propSet'): continue propdict = vm_util.propset_dict(obj_content.propSet) # Local storage identifier vSphere doesn't support CIFS or # vfat for datastores, therefore filtered ds_type = propdict['summary.type'] ds_name = propdict['summary.name'] if ((ds_type == 'VMFS' or ds_type == 'NFS') and propdict.get('summary.accessible')): if datastore_regex is None or datastore_regex.match(ds_name): new_ds = Datastore( ref=obj_content.obj, name=ds_name, capacity=propdict['summary.capacity'], freespace=propdict['summary.freeSpace']) # favor datastores with more free space if (best_match is None or new_ds.freespace > best_match.freespace): best_match = new_ds return best_match
def _select_datastore(data_stores, best_match, datastore_regex=None): """Find the most preferable datastore in a given RetrieveResult object. :param data_stores: a RetrieveResult object from vSphere API call :param best_match: the current best match for datastore :param datastore_regex: an optional regular expression to match names :return: datastore_ref, datastore_name, capacity, freespace """ # data_stores is actually a RetrieveResult object from vSphere API call for obj_content in data_stores.objects: # the propset attribute "need not be set" by returning API if not hasattr(obj_content, 'propSet'): continue propdict = vm_util.propset_dict(obj_content.propSet) if _is_datastore_valid(propdict, datastore_regex): new_ds = Datastore( ref=obj_content.obj, name=propdict['summary.name'], capacity=propdict['summary.capacity'], freespace=propdict['summary.freeSpace']) # favor datastores with more free space if (best_match is None or new_ds.freespace > best_match.freespace): best_match = new_ds return best_match
def test_propset_dict_simple(self): ObjectContent = collections.namedtuple("ObjectContent", ["propSet"]) DynamicProperty = collections.namedtuple("Property", ["name", "val"]) object = ObjectContent(propSet=[DynamicProperty(name="foo", val="bar")]) propdict = vm_util.propset_dict(object.propSet) self.assertEqual("bar", propdict["foo"])
def test_propset_dict_simple(self): ObjectContent = collections.namedtuple('ObjectContent', ['propSet']) DynamicProperty = collections.namedtuple('Property', ['name', 'val']) object = ObjectContent(propSet=[ DynamicProperty(name='foo', val="bar")]) propdict = vm_util.propset_dict(object.propSet) self.assertEqual("bar", propdict['foo'])
def test_propset_dict_simple(self): ObjectContent = collections.namedtuple('ObjectContent', ['propSet']) DynamicProperty = collections.namedtuple('Property', ['name', 'val']) object = ObjectContent( propSet=[DynamicProperty(name='foo', val="bar")]) propdict = vm_util.propset_dict(object.propSet) self.assertEqual("bar", propdict['foo'])
def _get_network_obj(session, network_objects, network_name): """Gets the network object for the requested network. The network object will be used when creating the VM configuration spec. The network object contains the relevant network details for the specific network type, for example, a distributed port group. The method will search for the network_name in the list of network_objects. :param session: vCenter soap session :param network_objects: group of networks :param network_name: the requested network :return: network object """ network_obj = {} # network_objects is actually a RetrieveResult object from vSphere API call for obj_content in network_objects: # the propset attribute "need not be set" by returning API if not hasattr(obj_content, 'propSet'): continue prop_dict = vm_util.propset_dict(obj_content.propSet) network_refs = prop_dict.get('network') if network_refs: network_refs = network_refs.ManagedObjectReference for network in network_refs: # Get network properties if network._type == 'DistributedVirtualPortgroup': props = session._call_method(vutil, "get_object_property", network, "config") # NOTE(asomya): This only works on ESXi if the port binding # is set to ephemeral # For a VLAN the network name will be the UUID. For a VXLAN # network this will have a VXLAN prefix and then the # network name. if network_name in props.name: network_obj['type'] = 'DistributedVirtualPortgroup' network_obj['dvpg'] = props.key dvs_props = session._call_method(vutil, "get_object_property", props.distributedVirtualSwitch, "uuid") network_obj['dvsw'] = dvs_props return network_obj else: props = session._call_method(vutil, "get_object_property", network, "summary.name") if props == network_name: network_obj['type'] = 'Network' network_obj['name'] = network_name return network_obj
def _get_network_obj(session, network_objects, network_name): """Gets the network object for the requested network. The network object will be used when creating the VM configuration spec. The network object contains the relevant network details for the specific network type, for example, a distributed port group. The method will search for the network_name in the list of network_objects. :param session: vCenter soap session :param network_objects: group of networks :param network_name: the requested network :return: network object """ network_obj = {} # network_objects is actually a RetrieveResult object from vSphere API call for obj_content in network_objects: # the propset attribute "need not be set" by returning API if not hasattr(obj_content, 'propSet'): continue prop_dict = vm_util.propset_dict(obj_content.propSet) network_refs = prop_dict.get('network') if network_refs: network_refs = network_refs.ManagedObjectReference for network in network_refs: # Get network properties if network._type == 'DistributedVirtualPortgroup': props = session._call_method( vim_util, "get_dynamic_property", network, "DistributedVirtualPortgroup", "config") # NOTE(asomya): This only works on ESXi if the port binding # is set to ephemeral # For a VLAN the network name will be the UUID. For a VXLAN # network this will have a VXLAN prefix and then the # network name. if network_name in props.name: network_obj['type'] = 'DistributedVirtualPortgroup' network_obj['dvpg'] = props.key dvs_props = session._call_method( vim_util, "get_dynamic_property", props.distributedVirtualSwitch, "VmwareDistributedVirtualSwitch", "uuid") network_obj['dvsw'] = dvs_props return network_obj else: props = session._call_method(vim_util, "get_dynamic_property", network, "Network", "summary.name") if props == network_name: network_obj['type'] = 'Network' network_obj['name'] = network_name return network_obj
def _get_allowed_datastores(data_stores, datastore_regex): allowed = [] for obj_content in data_stores.objects: # the propset attribute "need not be set" by returning API if not hasattr(obj_content, "propSet"): continue propdict = vm_util.propset_dict(obj_content.propSet) if _is_datastore_valid(propdict, datastore_regex, ALL_SUPPORTED_DS_TYPES): allowed.append(Datastore(ref=obj_content.obj, name=propdict["summary.name"])) return allowed
def _get_allowed_datastores(data_stores, datastore_regex): allowed = [] for obj_content in data_stores.objects: # the propset attribute "need not be set" by returning API if not hasattr(obj_content, 'propSet'): continue propdict = vm_util.propset_dict(obj_content.propSet) if _is_datastore_valid(propdict, datastore_regex): allowed.append( Datastore(ref=obj_content.obj, name=propdict['summary.name'])) return allowed
def _get_allowed_datastores(datastores, datastore_regex): for obj_content in datastores: # the propset attribute "need not be set" by returning API if not hasattr(obj_content, 'propSet'): continue propdict = vm_util.propset_dict(obj_content.propSet) if _is_datastore_valid(propdict, datastore_regex, ALL_SUPPORTED_DS_TYPES): yield (ds_obj.Datastore(ref=obj_content.obj, name=propdict['summary.name'], capacity=propdict['summary.capacity'], freespace=propdict['summary.freeSpace']))
def test_propset_dict_complex(self): ObjectContent = collections.namedtuple('ObjectContent', ['propSet']) DynamicProperty = collections.namedtuple('Property', ['name', 'val']) MoRef = collections.namedtuple('Val', ['value']) object = ObjectContent(propSet=[ DynamicProperty(name='foo', val="bar"), DynamicProperty(name='some.thing', val=MoRef(value='else')), DynamicProperty(name='another.thing', val='value') ]) propdict = vm_util.propset_dict(object.propSet) self.assertEqual("bar", propdict['foo']) self.assertTrue(hasattr(propdict['some.thing'], 'value')) self.assertEqual("else", propdict['some.thing'].value) self.assertEqual("value", propdict['another.thing'])
def test_propset_dict_complex(self): ObjectContent = collections.namedtuple('ObjectContent', ['propSet']) DynamicProperty = collections.namedtuple('Property', ['name', 'val']) MoRef = collections.namedtuple('Val', ['value']) object = ObjectContent(propSet=[ DynamicProperty(name='foo', val="bar"), DynamicProperty(name='some.thing', val=MoRef(value='else')), DynamicProperty(name='another.thing', val='value')]) propdict = vm_util.propset_dict(object.propSet) self.assertEqual("bar", propdict['foo']) self.assertTrue(hasattr(propdict['some.thing'], 'value')) self.assertEqual("else", propdict['some.thing'].value) self.assertEqual("value", propdict['another.thing'])
def _get_allowed_datastores(data_stores, datastore_regex, allowed_types): allowed = [] for obj_content in data_stores.objects: # the propset attribute "need not be set" by returning API if not hasattr(obj_content, 'propSet'): continue propdict = vm_util.propset_dict(obj_content.propSet) # Local storage identifier vSphere doesn't support CIFS or # vfat for datastores, therefore filtered ds_type = propdict['summary.type'] ds_name = propdict['summary.name'] if (propdict['summary.accessible'] and ds_type in allowed_types): if datastore_regex is None or datastore_regex.match(ds_name): allowed.append(Datastore(ref=obj_content.obj, name=ds_name)) return allowed
def test_propset_dict_complex(self): ObjectContent = collections.namedtuple("ObjectContent", ["propSet"]) DynamicProperty = collections.namedtuple("Property", ["name", "val"]) MoRef = collections.namedtuple("Val", ["value"]) object = ObjectContent( propSet=[ DynamicProperty(name="foo", val="bar"), DynamicProperty(name="some.thing", val=MoRef(value="else")), DynamicProperty(name="another.thing", val="value"), ] ) propdict = vm_util.propset_dict(object.propSet) self.assertEqual("bar", propdict["foo"]) self.assertTrue(hasattr(propdict["some.thing"], "value")) self.assertEqual("else", propdict["some.thing"].value) self.assertEqual("value", propdict["another.thing"])
def _select_datastore(session, data_stores, best_match, datastore_regex=None, storage_policy=None, allowed_ds_types=ALL_SUPPORTED_DS_TYPES): """Find the most preferable datastore in a given RetrieveResult object. :param session: vmwareapi session :param data_stores: a RetrieveResult object from vSphere API call :param best_match: the current best match for datastore :param datastore_regex: an optional regular expression to match names :param storage_policy: storage policy for the datastore :param allowed_ds_types: a list of acceptable datastore type names :return: datastore_ref, datastore_name, capacity, freespace """ if storage_policy: matching_ds = _filter_datastores_matching_storage_policy( session, data_stores, storage_policy) if not matching_ds: return best_match else: matching_ds = data_stores # data_stores is actually a RetrieveResult object from vSphere API call for obj_content in matching_ds.objects: # the propset attribute "need not be set" by returning API if not hasattr(obj_content, 'propSet'): continue propdict = vm_util.propset_dict(obj_content.propSet) if _is_datastore_valid(propdict, datastore_regex, allowed_ds_types): new_ds = ds_obj.Datastore(ref=obj_content.obj, name=propdict['summary.name'], capacity=propdict['summary.capacity'], freespace=propdict['summary.freeSpace']) # favor datastores with more free space if (best_match is None or new_ds.freespace > best_match.freespace): best_match = new_ds return best_match
def _update_datacenter_cache_from_objects(session, dcs): """Updates the datastore/datacenter cache.""" with vutil.WithRetrieval(session.vim, dcs) as dc_objs: for dco in dc_objs: dc_ref = dco.obj ds_refs = [] prop_dict = vm_util.propset_dict(dco.propSet) name = prop_dict.get('name') vmFolder = prop_dict.get('vmFolder') datastore_refs = prop_dict.get('datastore') if datastore_refs: datastore_refs = datastore_refs.ManagedObjectReference for ds in datastore_refs: ds_refs.append(vutil.get_moref_value(ds)) else: LOG.debug("Datacenter %s doesn't have any datastore " "associated with it, ignoring it", name) for ds_ref in ds_refs: _DS_DC_MAPPING[ds_ref] = DcInfo(ref=dc_ref, name=name, vmFolder=vmFolder)
def _update_datacenter_cache_from_objects(session, dcs): """Updates the datastore/datacenter cache.""" with vutil.WithRetrieval(session.vim, dcs) as dc_objs: for dco in dc_objs: dc_ref = dco.obj ds_refs = [] prop_dict = vm_util.propset_dict(dco.propSet) name = prop_dict.get('name') vmFolder = prop_dict.get('vmFolder') datastore_refs = prop_dict.get('datastore') if datastore_refs: datastore_refs = datastore_refs.ManagedObjectReference for ds in datastore_refs: ds_refs.append(ds.value) else: LOG.debug("Datacenter %s doesn't have any datastore " "associated with it, ignoring it", name) for ds_ref in ds_refs: _DS_DC_MAPPING[ds_ref] = DcInfo(ref=dc_ref, name=name, vmFolder=vmFolder)
def _select_datastore(session, data_stores, best_match, datastore_regex=None, storage_policy=None, allowed_ds_types=ALL_SUPPORTED_DS_TYPES): """Find the most preferable datastore in a given RetrieveResult object. :param session: vmwareapi session :param data_stores: a RetrieveResult object from vSphere API call :param best_match: the current best match for datastore :param datastore_regex: an optional regular expression to match names :param storage_policy: storage policy for the datastore :param allowed_ds_types: a list of acceptable datastore type names :return: datastore_ref, datastore_name, capacity, freespace """ if storage_policy: matching_ds = _filter_datastores_matching_storage_policy( session, data_stores, storage_policy) if not matching_ds: return best_match else: matching_ds = data_stores # data_stores is actually a RetrieveResult object from vSphere API call for obj_content in matching_ds.objects: # the propset attribute "need not be set" by returning API if not hasattr(obj_content, 'propSet'): continue propdict = vm_util.propset_dict(obj_content.propSet) if _is_datastore_valid(propdict, datastore_regex, allowed_ds_types): new_ds = ds_obj.Datastore( ref=obj_content.obj, name=propdict['summary.name'], capacity=propdict['summary.capacity'], freespace=propdict['summary.freeSpace']) # favor datastores with more free space if (best_match is None or new_ds.freespace > best_match.freespace): best_match = new_ds return best_match
def _get_datastores_for_cluster(driver, cluster_mor, datastore_regex): """ Gets all the datastores associated with the given custer that match the datastore regex """ datastore_ret = driver._session._call_method(oslo_vutil, "get_object_property", cluster_mor, "datastore") if not datastore_ret: return [] data_stores = driver._session._call_method( vim_util, "get_properties_for_a_collection_of_objects", "Datastore", datastore_ret.ManagedObjectReference, ["summary.name"]) data_store_refs = [] for obj_content in data_stores.objects: propdict = vm_util.propset_dict(obj_content.propSet) if datastore_regex is None or \ datastore_regex.match(propdict['summary.name']): data_store_refs.append(obj_content.obj) return data_store_refs