Ejemplo n.º 1
0
def create_port_group(session, pg_name, vswitch_name, vlan_id=0, cluster=None):
    """Creates a port group on the host system with the vlan tags
    supplied. VLAN id 0 means no vlan id association.
    """
    client_factory = session.vim.client.factory
    add_prt_grp_spec = vm_util.get_add_vswitch_port_group_spec(
                    client_factory,
                    vswitch_name,
                    pg_name,
                    vlan_id)
    host_mor = vm_util.get_host_ref(session, cluster)
    network_system_mor = session._call_method(vutil,
                                              "get_object_property",
                                              host_mor,
                                              "configManager.networkSystem")
    LOG.debug("Creating Port Group with name %s on "
              "the ESX host", pg_name)
    try:
        session._call_method(session.vim,
                "AddPortGroup", network_system_mor,
                portgrp=add_prt_grp_spec)
    except vexc.AlreadyExistsException:
        # There can be a race condition when two instances try
        # adding port groups at the same time. One succeeds, then
        # the other one will get an exception. Since we are
        # concerned with the port group being created, which is done
        # by the other call, we can ignore the exception.
        LOG.debug("Port Group %s already exists.", pg_name)
    LOG.debug("Created Port Group with name %s on "
              "the ESX host", pg_name)
Ejemplo n.º 2
0
def get_vswitch_for_vlan_interface(session, vlan_interface, cluster=None):
    """
    Gets the vswitch associated with the physical network adapter
    with the name supplied.
    """
    # Get the list of vSwicthes on the Host System
    host_mor = vm_util.get_host_ref(session, cluster)
    vswitches_ret = session._call_method(vim_util,
                "get_dynamic_property", host_mor,
                "HostSystem", "config.network.vswitch")
    # Meaning there are no vSwitches on the host. Shouldn't be the case,
    # but just doing code check
    if not vswitches_ret:
        return
    vswitches = vswitches_ret.HostVirtualSwitch
    # Get the vSwitch associated with the network adapter
    for elem in vswitches:
        try:
            for nic_elem in elem.pnic:
                if str(nic_elem).split('-')[-1].find(vlan_interface) != -1:
                    return elem.name
        # Catching Attribute error as a vSwitch may not be associated with a
        # physical NIC.
        except AttributeError:
            pass
Ejemplo n.º 3
0
def get_vswitch_for_vlan_interface(session, vlan_interface, cluster=None):
    """Gets the vswitch associated with the physical network adapter
    with the name supplied.
    """
    # Get the list of vSwicthes on the Host System
    host_mor = vm_util.get_host_ref(session, cluster)
    vswitches_ret = session._call_method(vutil,
                                         "get_object_property",
                                         host_mor,
                                         "config.network.vswitch")
    # Meaning there are no vSwitches on the host. Shouldn't be the case,
    # but just doing code check
    if not vswitches_ret:
        return
    vswitches = vswitches_ret.HostVirtualSwitch
    # Get the vSwitch associated with the network adapter
    for elem in vswitches:
        try:
            for nic_elem in elem.pnic:
                if str(nic_elem).split('-')[-1].find(vlan_interface) != -1:
                    return elem.name
        # Catching Attribute error as a vSwitch may not be associated with a
        # physical NIC.
        except AttributeError:
            pass
Ejemplo n.º 4
0
def create_port_group(session, pg_name, vswitch_name, vlan_id=0,
                      cluster=None, promiscuous_mode=None):
    """
    Creates a port group on the host system with the vlan tags
    supplied. VLAN id 0 means no vlan id association.
    """
    client_factory = session._get_vim().client.factory
    add_prt_grp_spec = vm_util.get_add_vswitch_port_group_spec(
                    client_factory,
                    vswitch_name,
                    pg_name,
                    vlan_id,
                    promiscuous_mode)
    host_mor = vm_util.get_host_ref(session, cluster)
    network_system_mor = session._call_method(vim_util,
        "get_dynamic_property", host_mor,
        "HostSystem", "configManager.networkSystem")
    try:
        session._call_method(session._get_vim(),
                "AddPortGroup", network_system_mor,
                portgrp=add_prt_grp_spec)
    except error_util.VimFaultException, exc:
        # There can be a race condition when two instances try
        # adding port groups at the same time. One succeeds, then
        # the other one will get an exception. Since we are
        # concerned with the port group being created, which is done
        # by the other call, we can ignore the exception.
        if error_util.FAULT_ALREADY_EXISTS not in exc.fault_list:
            raise Exception(exc)
Ejemplo n.º 5
0
def get_network_with_the_name(session, network_name="vmnet0", cluster=None):
    """
    Gets reference to the network whose name is passed as the
    argument.
    """
    host = vm_util.get_host_ref(session, cluster)
    if cluster is not None:
        vm_networks_ret = session._call_method(vim_util,
                                               "get_dynamic_property", cluster,
                                               "ClusterComputeResource",
                                               "network")
    else:
        vm_networks_ret = session._call_method(vim_util,
                                               "get_dynamic_property", host,
                                               "HostSystem", "network")

    # Meaning there are no networks on the host. suds responds with a ""
    # in the parent property field rather than a [] in the
    # ManagedObjectReference property field of the parent
    if not vm_networks_ret:
        return None
    vm_networks = vm_networks_ret.ManagedObjectReference
    networks = session._call_method(vim_util,
                       "get_properties_for_a_collection_of_objects",
                       "Network", vm_networks, ["summary.name"])
    network_obj = {}
    for network in vm_networks:
        # 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
            if props.name == network_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
        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
    if (len(network_obj) > 0):
        return network_obj
    else:
        return None
Ejemplo n.º 6
0
def check_if_vlan_interface_exists(session, vlan_interface, cluster=None):
    """Checks if the vlan_interface exists on the esx host."""
    host_mor = vm_util.get_host_ref(session, cluster)
    physical_nics_ret = session._call_method(vim_util,
                "get_dynamic_property", host_mor,
                "HostSystem", "config.network.pnic")
    # Meaning there are no physical nics on the host
    if not physical_nics_ret:
        return False
    physical_nics = physical_nics_ret.PhysicalNic
    for pnic in physical_nics:
        if vlan_interface == pnic.device:
            return True
    return False
Ejemplo n.º 7
0
def get_vlanid_and_vswitch_for_portgroup(session, pg_name, cluster=None):
    """Get the vlan id and vswicth associated with the port group."""
    host_mor = vm_util.get_host_ref(session, cluster)
    port_grps_on_host_ret = session._call_method(vim_util,
                "get_dynamic_property", host_mor,
                "HostSystem", "config.network.portgroup")
    if not port_grps_on_host_ret:
        LOG.error("ESX SOAP server returned an empty port group "
                  "for the host system in its response")
        raise Exception(msg)
    port_grps_on_host = port_grps_on_host_ret.HostPortGroup
    for p_gp in port_grps_on_host:
        if p_gp.spec.name == pg_name:
            p_grp_vswitch_name = p_gp.vswitch.split("-")[-1]
            return p_gp.spec.vlanId, p_grp_vswitch_name
Ejemplo n.º 8
0
    def _iscsi_get_host_iqn(self, instance):
        """Return the host iSCSI IQN."""
        try:
            host_mor = vm_util.get_host_ref_for_vm(self._session, instance)
        except Exception:
            host_mor = vm_util.get_host_ref(self._session, self._cluster)

        hbas_ret = self._session._call_method(
                                      vutil,
                                      "get_object_property",
                                      host_mor,
                                      "config.storageDevice.hostBusAdapter")

        # Meaning there are no host bus adapters on the host
        if hbas_ret is None:
            return
        host_hbas = hbas_ret.HostHostBusAdapter
        if not host_hbas:
            return
        for hba in host_hbas:
            if hba.__class__.__name__ == 'HostInternetScsiHba':
                return hba.iScsiName
Ejemplo n.º 9
0
 def _iscsi_rescan_hba(self, target_portal):
     """Rescan the iSCSI HBA to discover iSCSI targets."""
     host_mor = vm_util.get_host_ref(self._session, self._cluster)
     storage_system_mor = self._session._call_method(
                                             vutil,
                                             "get_object_property",
                                             host_mor,
                                             "configManager.storageSystem")
     hbas_ret = self._session._call_method(
                                         vutil,
                                         "get_object_property",
                                         storage_system_mor,
                                         "storageDeviceInfo.hostBusAdapter")
     # Meaning there are no host bus adapters on the host
     if hbas_ret is None:
         return
     host_hbas = hbas_ret.HostHostBusAdapter
     if not host_hbas:
         return
     for hba in host_hbas:
         if hba.__class__.__name__ == 'HostInternetScsiHba':
             hba_device = hba.device
             if target_portal:
                 # Check if iscsi host is already in the send target host
                 # list
                 send_targets = getattr(hba, 'configuredSendTarget', [])
                 send_tgt_portals = ['%s:%s' % (s.address, s.port) for s in
                                     send_targets]
                 if target_portal not in send_tgt_portals:
                     self._iscsi_add_send_target_host(storage_system_mor,
                                                      hba_device,
                                                      target_portal)
             break
     else:
         return
     LOG.debug("Rescanning HBA %s", hba_device)
     self._session._call_method(self._session.vim,
         "RescanHba", storage_system_mor, hbaDevice=hba_device)
     LOG.debug("Rescanned HBA %s ", hba_device)
Ejemplo n.º 10
0
    def _iscsi_get_target(self, data):
        """Return the iSCSI Target given a volume info."""
        target_portal = data['target_portal']
        target_iqn = data['target_iqn']
        host_mor = vm_util.get_host_ref(self._session, self._cluster)

        lst_properties = ["config.storageDevice.hostBusAdapter",
                          "config.storageDevice.scsiTopology",
                          "config.storageDevice.scsiLun"]
        prop_dict = self._session._call_method(vutil,
                                               "get_object_properties_dict",
                                               host_mor,
                                               lst_properties)
        result = (None, None)
        hbas_ret = None
        scsi_topology = None
        scsi_lun_ret = None
        if prop_dict:
            hbas_ret = prop_dict.get('config.storageDevice.hostBusAdapter')
            scsi_topology = prop_dict.get('config.storageDevice.scsiTopology')
            scsi_lun_ret = prop_dict.get('config.storageDevice.scsiLun')

        # Meaning there are no host bus adapters on the host
        if hbas_ret is None:
            return result
        host_hbas = hbas_ret.HostHostBusAdapter
        if not host_hbas:
            return result
        for hba in host_hbas:
            if hba.__class__.__name__ == 'HostInternetScsiHba':
                hba_key = hba.key
                break
        else:
            return result

        if scsi_topology is None:
            return result
        host_adapters = scsi_topology.adapter
        if not host_adapters:
            return result
        scsi_lun_key = None
        for adapter in host_adapters:
            if adapter.adapter == hba_key:
                if not getattr(adapter, 'target', None):
                    return result
                for target in adapter.target:
                    if (getattr(target.transport, 'address', None) and
                        target.transport.address[0] == target_portal and
                            target.transport.iScsiName == target_iqn):
                        if not target.lun:
                            return result
                        for lun in target.lun:
                            if 'host.ScsiDisk' in lun.scsiLun:
                                scsi_lun_key = lun.scsiLun
                                break
                        break
                break

        if scsi_lun_key is None:
            return result

        if scsi_lun_ret is None:
            return result
        host_scsi_luns = scsi_lun_ret.ScsiLun
        if not host_scsi_luns:
            return result
        for scsi_lun in host_scsi_luns:
            if scsi_lun.key == scsi_lun_key:
                return (scsi_lun.deviceName, scsi_lun.uuid)

        return result