def get_portgroup_and_datacenter_id_by_name(session, dvs_name, port_group_name): """ Returns portgroup and datacenter mor id . """ portgroup_id = None datacenter_id = None port_group_mors = get_all_portgroup_mors_for_switch(session, dvs_name) if port_group_mors: port_groups = session._call_method( vim_util, "get_properties_for_a_collection_of_objects", "DistributedVirtualPortgroup", port_group_mors, ["summary.name", 'parent']) folder_mor = None for port_group in port_groups: propset_dict = common_util.convert_propset_to_dict( port_group.propSet) if propset_dict["summary.name"] == port_group_name: portgroup_id = port_group.obj.value folder_mor = propset_dict["parent"] break if folder_mor is not None: props = session._call_method(vim_util, "get_dynamic_properties", folder_mor, ["parent"]) datacenter_id = props['parent'].value return (portgroup_id, datacenter_id)
def get_host_mor_by_name(session, host_name): """ Return Host mor from its name """ host_mors = session._call_method( vim_util, "get_objects", "HostSystem", ["name"]) for host_mor in host_mors: propset_dict = common_util.convert_propset_to_dict(host_mor.propSet) if propset_dict['name'] == host_name: return host_mor.obj return None
def get_vm_mor_by_name(session, vm_name): """ Return VM mor from its name """ vm_mors = session._call_method( vim_util, "get_objects", "VirtualMachine", ["name"]) for vm_mor in vm_mors: propset_dict = common_util.convert_propset_to_dict(vm_mor.propSet) if propset_dict['name'] == vm_name: return vm_mor.obj return None
def get_dvs_mor_by_name(session, dvs_name): """ Return DVS mor from its name """ dvs_mors = session._call_method( vim_util, "get_objects", "DistributedVirtualSwitch", ["name"]) for dvs_mor in dvs_mors: propset_dict = common_util.convert_propset_to_dict(dvs_mor.propSet) if propset_dict['name'] == dvs_name: return dvs_mor.obj return None
def get_vm_mor_for_uuid(session, vm_uuid): if cache.VCCache.get_vm_mor_for_uuid(vm_uuid): return cache.VCCache.get_vm_mor_for_uuid(vm_uuid) #TODO(NAME) : use config.uuid instead of config.extraConfig["nvp.vm-uuid"] # once fixed in nova vm_mors = session._call_method(vim_util, "get_objects", "VirtualMachine", ['config.extraConfig["nvp.vm-uuid"]']) for vm_mor in vm_mors: if hasattr(vm_mor, "propSet"): propset_dict = common_util.convert_propset_to_dict(vm_mor.propSet) if (vm_uuid == propset_dict['config.extraConfig["nvp.vm-uuid"]'].value): cache.VCCache.add_vm_mor_for_uuid(vm_uuid, vm_mor.obj) return vm_mor.obj return None
def get_unused_portgroup_names(session, dvs_name): """ Returns a list Managed Object Reference for all the portgroups attached to the specified dvs and are not connected to any virtual Machine. """ unsed_port_group_names = [] port_group_mors = get_all_portgroup_mors_for_switch(session, dvs_name) if port_group_mors: port_groups = session._call_method( vim_util, "get_properties_for_a_collection_of_objects", "DistributedVirtualPortgroup", port_group_mors, ["summary.name", "tag", "vm"]) for port_group in port_groups: propset_dict = common_util.convert_propset_to_dict( port_group.propSet) if not propset_dict['vm'] and not propset_dict['tag']: unsed_port_group_names.append(propset_dict['summary.name']) return unsed_port_group_names