def _list_vsphere_volumes(self):
        """ Lists vsphere volumes in configured
            datastore only.
        :return:
        """
        datastore = self._find_datastore()
        vms = datastore.vm
        logging.debug('Found {} VMs'.format(len(vms)))
        searchResults = self._find_virtual_disks()
        content = self._si.RetrieveContent()
        virtualDiskManager = content.virtualDiskManager

        vol_list = {}
        for result in searchResults:
            for file in result.file:
                volume_path = result.folderPath + file.path
                disk_uuid = virtualDiskManager.QueryVirtualDiskUuid(
                    name=volume_path, datacenter=self._dc)
                disk_uuid = self._normalize_uuid(disk_uuid)
                str_dataset_id = file.path
                str_dataset_id = str_dataset_id[:-5]
                # logging.debug(str_dataset_id)
                dataset_id = uuid.UUID(str_dataset_id)
                # logging.debug(dataset_id)
                volume = BlockDeviceVolume(size=int(
                    KiB(file.capacityKb).to_Byte().value),
                                           dataset_id=dataset_id,
                                           blockdevice_id=unicode(disk_uuid))
                vsphere_volume = VsphereBlockDeviceVolume(
                    blockDeviceVolume=volume, path=volume_path)
                for vm in vms:
                    if isinstance(vm, vim.VirtualMachine) and \
                            hasattr(vm.config, 'hardware'):
                        devices = vm.config.hardware.device
                        for device in devices:
                            if hasattr(device.backing, 'diskMode'):
                                device_disk_uuid = device.backing.uuid
                                device_disk_uuid = self._normalize_uuid(
                                    device_disk_uuid)
                                if device_disk_uuid == disk_uuid:
                                    volume = volume.set(
                                        'attached_to',
                                        unicode(vm.config.instanceUuid))
                                    vsphere_volume.blockDeviceVolume = volume
                                    vsphere_volume.vm = vm
                                    vsphere_volume.uuid = vm.config.instanceUuid
                                    vsphere_volume.device = device
                                    break
                vol_list[unicode(disk_uuid)] = vsphere_volume

        logging.debug("Found {} vSphere volumes: {}".format(
            len(vol_list), '.'.join(vol_list.keys())))
        return vol_list
    def _list_vsphere_volumes(self):
        """ Lists vsphere volumes in configured
            datastore only.
        :return:
        """
        datastore = self._find_datastore()
        vms = datastore.vm
        logging.debug('Found {} VMs'.format(len(vms)))
        searchResults = self._find_virtual_disks()
        content = self._si.RetrieveContent()
        virtualDiskManager = content.virtualDiskManager

        vol_list = {}
        for result in searchResults:
            for file in result.file:
                volume_path = result.folderPath + file.path
                disk_uuid = virtualDiskManager.QueryVirtualDiskUuid(
                    name=volume_path, datacenter=self._dc)
                disk_uuid = self._normalize_uuid(disk_uuid)
                str_dataset_id = file.path
                str_dataset_id = str_dataset_id[:-5]
                # logging.debug(str_dataset_id)
                dataset_id = uuid.UUID(str_dataset_id)
                # logging.debug(dataset_id)
                volume = BlockDeviceVolume(
                    size=int(KiB(file.capacityKb).to_Byte().value),
                    dataset_id=dataset_id,
                    blockdevice_id=unicode(disk_uuid))
                vsphere_volume = VsphereBlockDeviceVolume(blockDeviceVolume=volume,
                                                          path=volume_path)
                for vm in vms:
                    if isinstance(vm, vim.VirtualMachine) and \
                            hasattr(vm.config, 'hardware'):
                        devices = vm.config.hardware.device
                        for device in devices:
                            if hasattr(device.backing, 'diskMode'):
                                device_disk_uuid = device.backing.uuid
                                device_disk_uuid = self._normalize_uuid(
                                    device_disk_uuid)
                                if device_disk_uuid == disk_uuid:
                                    volume = volume.set(
                                        'attached_to', unicode(vm.config.instanceUuid))
                                    vsphere_volume.blockDeviceVolume = volume
                                    vsphere_volume.vm = vm
                                    vsphere_volume.uuid = vm.config.instanceUuid
                                    vsphere_volume.device = device
                                    break
                vol_list[unicode(disk_uuid)] = vsphere_volume

        logging.debug("Found {} vSphere volumes: {}".format(len(vol_list),
                                                            '.'.join(vol_list.keys())))
        return vol_list
    def _list_vsphere_volumes(self):
        vms = self._find_all_vms()

        searchResults = self._find_virtual_disks()
        content = self._si.RetrieveContent()
        virtualDiskManager = content.virtualDiskManager

        vol_list = {}
        for result in searchResults:
            for file in result.file:
                logging.debug(str(file))
                volume_path = result.folderPath + file.path
                disk_uuid = virtualDiskManager.QueryVirtualDiskUuid(
                    name=volume_path, datacenter=self._dc)
                disk_uuid = self._normalize_uuid(disk_uuid)
                str_dataset_id = file.path
                str_dataset_id = str_dataset_id[:-5]
                logging.debug(str_dataset_id)
                dataset_id = uuid.UUID(str_dataset_id)
                logging.debug(dataset_id)

                volume = BlockDeviceVolume(
                    size=int(KiB(file.capacityKb).to_Byte().value),
                    dataset_id=dataset_id,
                    blockdevice_id=unicode(disk_uuid))
                vsphere_volume = VsphereBlockDeviceVolume(blockDeviceVolume=volume,
                                                          path=volume_path)
                for vm in vms:
                    if isinstance(vm, vim.VirtualMachine) and \
                            hasattr(vm.config, 'hardware'):
                        devices = vm.config.hardware.device
                        for device in devices:
                            if hasattr(device.backing, 'diskMode'):
                                device_disk_uuid = device.backing.uuid
                                device_disk_uuid = self._normalize_uuid(
                                    device_disk_uuid)
                                if device_disk_uuid == disk_uuid:
                                    volume = volume.set(
                                        'attached_to', unicode(vm._moId))
                                    vsphere_volume.blockDeviceVolume = volume
                                    vsphere_volume.vm = vm
                                    vsphere_volume.device = device
                                    break

                logging.debug(str(vsphere_volume))
                vol_list[unicode(disk_uuid)] = vsphere_volume

        logging.debug(str(vol_list))
        return vol_list
Example #4
0
class TestBlockDeviceAPIAttachDetach(unittest.TestCase):
    """
    Unit testing for IBMStorageBlockDeviceAPI Class attach detach methods
    """
    # pylint: disable=W0212

    def setUp(self):
        self.mock_client = MagicMock()
        self.mock_client.con_info = CONF_INFO_MOCK
        self.mock_client.backend_type = messages.SCBE_STRING
        self.mock_client.map_volume = MagicMock()
        self.mock_client.unmap_volume = MagicMock()
        self.driver_obj = driver.IBMStorageBlockDeviceAPI(
            UUID1, self.mock_client, DRIVER_BASIC_CONF)
        self.driver_obj._host_ops.rescan_scsi = MagicMock()

        self.expacted_blockdevicevolume = BlockDeviceVolume(
            blockdevice_id=unicode('999'),
            size=10,
            attached_to=None,
            dataset_id=UUID(UUID1_STR),
        )

    def test_attach_volume_already_attached(self):
        self.expacted_blockdevicevolume = \
            self.expacted_blockdevicevolume.set(attached_to=u'fake-host')
        self.driver_obj._get_volume = \
            MagicMock(return_value=self.expacted_blockdevicevolume)

        self.assertRaises(
            AlreadyAttachedVolume, self.driver_obj.attach_volume,
            unicode(UUID1_STR), 'fake-host')

    def test_attach_volume_succeed(self):
        self.expacted_blockdevicevolume = \
            self.expacted_blockdevicevolume.set(attached_to=None)
        self.driver_obj._get_volume = \
            MagicMock(return_value=self.expacted_blockdevicevolume)

        attached_to = self.driver_obj.attach_volume(
            unicode(UUID1_STR), u'fake-host').attached_to
        self.assertEqual(attached_to, 'fake-host')

    def test_attach_volume_already_detached(self):
        self.expacted_blockdevicevolume = \
            self.expacted_blockdevicevolume.set(attached_to=None)
        self.driver_obj._get_volume = \
            MagicMock(return_value=self.expacted_blockdevicevolume)

        self.assertRaises(
            UnattachedVolume, self.driver_obj.detach_volume,
            unicode(UUID1_STR))

    def test_detach_volume_succeed(self):
        self.expacted_blockdevicevolume = \
            self.expacted_blockdevicevolume.set(attached_to=u'fake-host')
        self.driver_obj._get_volume = \
            MagicMock(return_value=self.expacted_blockdevicevolume)
        self.driver_obj._clean_up_device_before_unmap = Mock()

        self.assertEqual(
            None,
            self.driver_obj.detach_volume(unicode(UUID1_STR)))