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(vim_util, "get_dynamic_property", host_mor, "HostSystem", "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)
def test_get_neutron_network_bridge_network_not_found(self): self.mox.StubOutWithMock(vm_util, 'get_host_ref') self.mox.StubOutWithMock(self.session, '_call_method') self.mox.StubOutWithMock(network_util, 'get_network_with_the_name') vm_util.get_host_ref(self.session, self.cluster).AndReturn('fake-host') opaque = fake.DataObject() opaque.HostOpaqueNetworkInfo = ['fake-network-info'] self.session._call_method( vim_util, "get_dynamic_property", 'fake-host', 'HostSystem', 'config.network.opaqueNetwork').AndReturn(None) network_util.get_network_with_the_name(self.session, 0, self.cluster).AndReturn(None) self.mox.ReplayAll() self.assertRaises(exception.NetworkNotFoundForBridge, vif.get_neutron_network, self.session, self.vif['network']['id'], self.cluster, self.vif)
def test_get_neutron_network_bridge_network_not_found(self): self.mox.StubOutWithMock(vm_util, 'get_host_ref') self.mox.StubOutWithMock(self.session, '_call_method') self.mox.StubOutWithMock(network_util, 'get_network_with_the_name') vm_util.get_host_ref(self.session, self.cluster).AndReturn('fake-host') opaque = fake.DataObject() opaque.HostOpaqueNetworkInfo = ['fake-network-info'] self.session._call_method(vim_util, "get_dynamic_property", 'fake-host', 'HostSystem', 'config.network.opaqueNetwork').AndReturn(None) network_util.get_network_with_the_name(self.session, 0, self.cluster).AndReturn(None) self.mox.ReplayAll() self.assertRaises(exception.NetworkNotFoundForBridge, vif.get_neutron_network, self.session, self.vif['network']['id'], self.cluster, self.vif)
def _get_opaque_network(session, cluster): host = vm_util.get_host_ref(session, cluster) try: opaque = session._call_method(vim_util, "get_dynamic_property", host, "HostSystem", "config.network.opaqueNetwork") except vexc.InvalidPropertyException: opaque = None return opaque
def test_get_neutron_network(self): self.mox.StubOutWithMock(vm_util, 'get_host_ref') self.mox.StubOutWithMock(self.session, '_call_method') self.mox.StubOutWithMock(vif, '_get_network_ref_from_opaque') vm_util.get_host_ref(self.session, self.cluster).AndReturn('fake-host') opaque = fake.DataObject() opaque.HostOpaqueNetworkInfo = ['fake-network-info'] self.session._call_method( vim_util, "get_dynamic_property", 'fake-host', 'HostSystem', 'config.network.opaqueNetwork').AndReturn(opaque) vif._get_network_ref_from_opaque( opaque.HostOpaqueNetworkInfo, CONF.vmware.integration_bridge, self.vif['network']['id']).AndReturn('fake-network-ref') self.mox.ReplayAll() network_ref = vif.get_neutron_network(self.session, self.vif['network']['id'], self.cluster, self.vif) self.assertEqual(network_ref, 'fake-network-ref')
def test_get_neutron_network(self): self.mox.StubOutWithMock(vm_util, 'get_host_ref') self.mox.StubOutWithMock(self.session, '_call_method') self.mox.StubOutWithMock(vif, '_get_network_ref_from_opaque') vm_util.get_host_ref(self.session, self.cluster).AndReturn('fake-host') opaque = fake.DataObject() opaque.HostOpaqueNetworkInfo = ['fake-network-info'] self.session._call_method(vim_util, "get_dynamic_property", 'fake-host', 'HostSystem', 'config.network.opaqueNetwork').AndReturn(opaque) vif._get_network_ref_from_opaque(opaque.HostOpaqueNetworkInfo, CONF.vmware.integration_bridge, self.vif['network']['id']).AndReturn('fake-network-ref') self.mox.ReplayAll() network_ref = vif.get_neutron_network(self.session, self.vif['network']['id'], self.cluster, self.vif) self.assertEqual(network_ref, 'fake-network-ref')
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
def _iscsi_get_host_iqn(self): """Return the host iSCSI IQN.""" host_mor = vm_util.get_host_ref(self._session, self._cluster) hbas_ret = self._session._call_method( vim_util, "get_dynamic_property", host_mor, "HostSystem", "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
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: msg = _("ESX SOAP server returned an empty port group " "for the host system in its response") LOG.error(msg) raise exception.PatronException(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
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( vim_util, "get_dynamic_property", host_mor, "HostSystem", "configManager.storageSystem") hbas_ret = self._session._call_method( vim_util, "get_dynamic_property", storage_system_mor, "HostStorageSystem", "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)
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( vim_util, "get_dynamic_property", host_mor, "HostSystem", "configManager.storageSystem") hbas_ret = self._session._call_method( vim_util, "get_dynamic_property", storage_system_mor, "HostStorageSystem", "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)
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
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(vim_util, "get_dynamic_properties", host_mor, "HostSystem", 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
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( vim_util, "get_dynamic_properties", host_mor, "HostSystem", 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