def test_get_used_devices(self): TEST_VOLUME1 = {'host': 'localhost', 'provider_location': '1 2 3 /dev/loop1'} TEST_VOLUME2 = {'host': 'localhost', 'provider_location': '1 2 3 /dev/loop2'} self.mox.StubOutWithMock(api, 'volume_get_all_by_host') self.mox.StubOutWithMock(context, 'get_admin_context') context.get_admin_context() api.volume_get_all_by_host(None, self.host) \ .AndReturn([TEST_VOLUME1, TEST_VOLUME2]) self.mox.StubOutWithMock(self.drv, 'local_path') path1 = self.drv.local_path(TEST_VOLUME1).AndReturn('/dev/loop1') path2 = self.drv.local_path(TEST_VOLUME2).AndReturn('/dev/loop2') self.mox.ReplayAll() self.assertEqual(self.drv._get_used_devices(), set([path1, path2]))
def test_get_used_devices(self): TEST_VOLUME1 = {'host': 'localhost', 'provider_location': '1 2 3 /dev/loop1'} TEST_VOLUME2 = {'host': 'localhost', 'provider_location': '1 2 3 /dev/loop2'} self.mox.StubOutWithMock(api, 'volume_get_all_by_host') self.mox.StubOutWithMock(context, 'get_admin_context') context.get_admin_context() api.volume_get_all_by_host(None, self.configuration.host) \ .AndReturn([TEST_VOLUME1, TEST_VOLUME2]) self.mox.StubOutWithMock(self.drv, 'local_path') path1 = self.drv.local_path(TEST_VOLUME1).AndReturn('/dev/loop1') path2 = self.drv.local_path(TEST_VOLUME2).AndReturn('/dev/loop2') self.mox.ReplayAll() self.assertEqual(self.drv._get_used_devices(), set([path1, path2]))
def _get_used_devices(self): lst = api.volume_get_all_by_host(context.get_admin_context(), self.configuration.host) used_devices = set() for volume in lst: local_path = self.local_path(volume) if local_path: used_devices.add(local_path) return used_devices
def _list_volumes(self, req, body): """ Returns Cinder volume lists for specific query params :param req: python-cinderclient request :param body: python-cinderclient request's body {"list-volumes": {"node_id": "<node_id>"}} :return: {"count": <count>, "volumes": [ {<volume data 1st volume>}, {<volume data 2nd volume>}, ... ]} """ cinder_context = req.environ['cinder.context'] authorize_list_volumes(cinder_context) admin_context = cinder.context.get_admin_context() kwargs = SafeDict(body).get('list-volumes', {}) tenant_id = 'admin' lunr_client = lunrclient.client.LunrClient(tenant_id) data_name = "volumes" if 'node_id' in kwargs: lunr_node = lunr_except_handler(lambda: lunr_client.nodes.get(node_id=kwargs['node_id'])) hostname = lunr_node['cinder_host'] cinder_volumes = cinder_list_handler(volume_get_all_by_host(admin_context, host=hostname), data_name) return cinder_volumes if 'restore_of' in kwargs: lunr_volumes = lunr_except_handler(lambda: lunr_client.volumes.list(restore_of=kwargs['restore_of'])) cinder_volumes_list = [] if len(lunr_volumes) > 0: for volume in lunr_volumes: if 'id' in volume: cinder_volumes_list.append(volume_get(admin_context, volume_id=volume['id'])) if isinstance(cinder_volumes_list, list): cinder_volumes = {"count": len(cinder_volumes_list), data_name: cinder_volumes_list} else: cinder_volumes = {"count": 0, "volumes": cinder_volumes_list} return cinder_volumes elif 'id' in kwargs: cinder_volumes = cinder_list_handler(volume_get(admin_context, volume_id=kwargs['id']), data_name) return cinder_volumes elif 'account_id' in kwargs: filters = {'project_id': kwargs['account_id']} cinder_volumes = cinder_list_handler(volume_get_all(admin_context, marker=None, limit=None, sort_keys=['project_id'], sort_dirs=['asc'], filters=filters), data_name) return cinder_volumes elif 'host' in kwargs: filters = {'host': kwargs['host']} cinder_volumes = cinder_list_handler(volume_get_all(admin_context, marker=None, limit=None, sort_keys=['project_id'], sort_dirs=['asc'], filters=filters), data_name) return cinder_volumes raise exc.HTTPBadRequest( explanation=_("Must specify node_id, restore_of, id, account_id, or host"))
def shared_targets_online_data_migration(ctxt, max_count): """Update existing volumes shared_targets flag based on capabilities.""" non_shared_hosts = [] completed = 0 non_shared_hosts, total_vols_to_update = _get_non_shared_target_hosts(ctxt) for host in non_shared_hosts: # We use the api call here instead of going direct to # db query to take advantage of parsing out the host # correctly vrefs = db_api.volume_get_all_by_host(ctxt, host, filters={'shared_targets': True}) if len(vrefs) > max_count: del vrefs[-(len(vrefs) - max_count):] max_count -= len(vrefs) for v in vrefs: db.volume_update(ctxt, v['id'], {'shared_targets': 0}) completed += 1 return total_vols_to_update, completed
def shared_targets_online_data_migration(ctxt, max_count): """Update existing volumes shared_targets flag based on capabilities.""" non_shared_hosts = [] completed = 0 non_shared_hosts, total_vols_to_update = _get_non_shared_target_hosts(ctxt) for host in non_shared_hosts: # We use the api call here instead of going direct to # db query to take advantage of parsing out the host # correctly vrefs = db_api.volume_get_all_by_host( ctxt, host, filters={'shared_targets': True}) if len(vrefs) > max_count: del vrefs[-(len(vrefs) - max_count):] max_count -= len(vrefs) for v in vrefs: db.volume_update( ctxt, v['id'], {'shared_targets': 0}) completed += 1 return total_vols_to_update, completed
def _list_volumes(self, req, body): """ Returns Cinder volume data for queries with Lunr kwargs filters :param req: python-cinderclient request :param body: python-cinderclient request's body {"list-volumes": {"node_id": "<node_id>"}} :return: {"count": <count>, "volumes": [ {<volume data 1st volume>}, {<volume data 2nd volume>}, ... ]} """ cinder_context = req.environ['cinder.context'] authorize_list_volumes(cinder_context) kwargs = SafeDict(body).get('list-volumes', {}) tenant_id = 'admin' lunr_client = lunrclient.client.LunrClient(tenant_id) data_name = "volumes" if 'node_id' in kwargs: lunr_node = lunr_except_handler( lambda: lunr_client.nodes.get(**kwargs)) hostname = lunr_node['cinder_host'] cinder_volumes = cinder_list_handler( volume_get_all_by_host(cinder_context, host=hostname), data_name) return cinder_volumes if 'restore_of' in kwargs: lunr_volumes = lunr_except_handler( lambda: lunr_client.volumes.list(**kwargs)) cinder_volumes_list = [] if len(lunr_volumes) > 0: for volume in lunr_volumes: if 'id' in volume: cinder_volumes_list.append( volume_get(cinder_context, volume_id=volume['id'])) if isinstance(cinder_volumes_list, list): cinder_volumes = { "count": len(cinder_volumes_list), data_name: cinder_volumes_list } else: cinder_volumes = {"count": 0, "volumes": cinder_volumes_list} return cinder_volumes elif 'id' in kwargs: cinder_volumes = cinder_list_handler( volume_get(cinder_context, volume_id=kwargs['id']), data_name) return cinder_volumes elif 'account_id' in kwargs: project_id = kwargs['account_id'] kwargs.clear() kwargs.update({'project_id': project_id}) cinder_volumes = cinder_list_handler( volume_get_all(cinder_context, marker=None, limit=None, sort_key='project_id', sort_dir='asc', filters=kwargs), data_name) return cinder_volumes else: cinder_volumes = cinder_list_handler( volume_get_all(cinder_context, marker=None, limit=None, sort_key='project_id', sort_dir='asc', filters=kwargs), data_name) return cinder_volumes