def test_local_cache_no_file_no_context(self): """Test local cache without a context manager.""" c = utils.LocalCache() cache = c.lc_open() cache['test_key3'] = True try: self.assertTrue('test_key3' in cache) finally: c.lc_close() with utils.LocalCache() as c: self.assertTrue('test_key3' in c)
def get_flavors(self): """Retrieve all of flavors. :returns: dict """ flavors = dict() with utils.LocalCache() as c: for flavor in self.conn.compute.flavors(): _flavor = flavor.to_dict() cache_key = 'flavor_' + str(flavor.id) c.set(cache_key, _flavor, expire=43200, tag='flavors') entry = flavors[flavor.id] = dict() entry.update(_flavor) return flavors
def get_projects(self): """Retrieve a list of projects. :returns: list """ _consumers = list() with utils.LocalCache() as c: for project in self.conn.identity.projects(): _consumers.append(project) cache_key = 'projects_' + str(project.id) c.set(cache_key, project.to_dict(), expire=43200, tag='projects') return _consumers
def get_flavor(self, flavor_id): """Retrieve a flavor. :param flavor_id: ID of a given flavor to lookup. :type flavor_id: int || str :returns: dict """ flavor = None cache_key = 'flavor_{}'.format(flavor_id) with utils.LocalCache() as c: try: flavor = c.get(cache_key) if not flavor: raise LookupError except LookupError: flavor_info = self.conn.compute.get_flavor(flavor_id) flavor = flavor_info.to_dict() c.set(cache_key, flavor, expire=43200, tag='flavors') finally: return flavor
def get_project(self, project_id): """Retrieve project data. :param project_id: ID of a given project to lookup. :type project_id: str || uuid :returns: dict """ project = None cache_key = 'projects_{}'.format(project_id) with utils.LocalCache() as c: try: project = c.get(cache_key) if not project: raise LookupError except LookupError: project_info = self.conn.identity.get_project(project_id) project = project_info.to_dict() c.set(cache_key, project, expire=43200, tag='projects') finally: return project
def test_local_cache_file(self): """Test local cache.""" with utils.LocalCache(cache_path=self.t_testfile) as c: c['test_key2'] = True self.assertTrue('test_key2' in c)
def test_local_cache_no_file(self): """Test local cache.""" with utils.LocalCache() as c: c['test_key1'] = True self.assertTrue('test_key1' in c)
def test_local_cache_named_ext(self): """Test local cache without loading anything with a named extension.""" utils.LocalCache(cache_path='{}.cache'.format(self.t_testfile))
def test_local_cache_no_load(self): """Test local cache without loading anything.""" c = utils.LocalCache(cache_path=self.t_testfile) c.lc_close()