def delete(self, store, uuid): """Delete the stored file with the UUID under the given store. :param store: The store class, used for routing the storage. :type store: tuskar.storage.stores._BaseStore :param uuid: UUID of the object to delete. :type uuid: str :return: Returns nothing on success. Exceptions are expected for errors :rtype: None :raises: tuskar.storage.exceptions.UnknownUUID if the UUID can't be found """ session = get_session() session.begin() stored_file = self._retrieve(store.object_type, uuid) try: session.delete(stored_file) session.commit() finally: session.close()
def retrieve_by_name(self, store, name, version=None): """Returns the stored file for a given store that matches the provided name and optionally version. :param store: The store class, used for routing the storage. :type store: tuskar.storage.stores._BaseStore :param name: name of the object to retrieve. :type name: str :param version: Version of the object to retrieve. If the version isn't provided, the latest will be returned. :type version: int :return: StoredFile instance containing the file metadata and contents :rtype: tuskar.storage.models.StoredFile :raises: tuskar.storage.exceptions.UnknownName if the name can't be found :raises: tuskar.storage.exceptions.UnknownVersion if the version can't be found """ object_type = store.object_type session = get_session() try: query = session.query(StoredFile).filter_by( name=name, object_type=object_type, ) if version is not None: query = query.filter_by(version=version) else: query = query.filter_by( version=self._get_latest_version(store, name)) stored_file = query.one() return self._to_storage_model(store, stored_file) except NoResultFound: name_query = session.query(StoredFile).filter_by( name=name, object_type=object_type, ) if name_query.count() == 0: msg = "No {0}s found for the name: {1}".format( object_type, name) raise UnknownName(msg) elif name_query.filter_by(version=version).count() == 0: msg = "No {0}s found for the Version: {1}".format( object_type, name) raise UnknownVersion(msg) raise finally: session.close()
def delete(self, store, uuid): """Delete the stored file with the UUID under the given store. :param store: The store class, used for routing the storage. :type store: tuskar.storage.stores._BaseStore :param uuid: UUID of the object to delete. :type uuid: str :return: Returns nothing on success. Exceptions are expected for errors :rtype: None :raises: tuskar.storage.exceptions.UnknownUUID if the UUID can't be found """ session = get_session() session.begin() stored_file = self._retrieve(store.object_type, uuid) try: session.delete(stored_file) session.commit() finally: session.close()
def _get_latest_version(self, store, name): session = get_session() try: return session.query(func.max(StoredFile.version)).filter_by( object_type=store.object_type, name=name).scalar() finally: session.close()
def _upsert(self, store, stored_file): session = get_session() session.begin() try: session.add(stored_file) session.commit() return self._to_storage_model(store, stored_file) finally: session.close()
def _retrieve(self, object_type, uuid): session = get_session() try: return session.query(StoredFile).filter_by( uuid=uuid, object_type=object_type).one() except NoResultFound: msg = "No {0}s for the UUID: {1}".format(object_type, uuid) raise UnknownUUID(msg) finally: session.close()
def _upsert(self, store, stored_file): session = get_session() session.begin() try: session.add(stored_file) session.commit() return self._to_storage_model(store, stored_file) finally: session.close()
def test_delete_overcloud(self): # Setup saved = self.connection.create_overcloud(self.overcloud_1) # Test self.connection.delete_overcloud_by_id(saved.id) # Verify found = self.connection.get_overclouds() self.assertEqual(0, len(found)) # Ensure the joined tables are clear too session = dbapi.get_session() all_counts = session.query(models.OvercloudAttribute).all() session.close() self.assertEqual(0, len(all_counts)) session = dbapi.get_session() all_counts = session.query(models.OvercloudRoleCount).all() session.close() self.assertEqual(0, len(all_counts))
def test_delete_overcloud(self): # Setup saved = self.connection.create_overcloud(self.overcloud_1) # Test self.connection.delete_overcloud_by_id(saved.id) # Verify found = self.connection.get_overclouds() self.assertEqual(0, len(found)) # Ensure the joined tables are clear too session = dbapi.get_session() all_counts = session.query(models.OvercloudAttribute).all() session.close() self.assertEqual(0, len(all_counts)) session = dbapi.get_session() all_counts = session.query(models.OvercloudRoleCount).all() session.close() self.assertEqual(0, len(all_counts))
def _get_latest_version(self, store, name): session = get_session() try: return session.query( func.max(StoredFile.version) ).filter_by( object_type=store.object_type, name=name ).scalar() finally: session.close()
def _retrieve(self, object_type, uuid): session = get_session() try: return session.query(StoredFile).filter_by( uuid=uuid, object_type=object_type ).one() except NoResultFound: msg = "No {0}s for the UUID: {1}".format(object_type, uuid) raise UnknownUUID(msg) finally: session.close()
def list(self, store, only_latest=False): """Return a list of all the stored objects for a given store. Optionally only_latest can be set to True to return only the most recent version of each objects (grouped by name). :param store: The store class, used for routing the storage. :type store: tuskar.storage.stores._BaseStore :param only_latest: If set to True only the latest versions of each object will be returned. :type only_latest: bool :return: List of StoredFile instances :rtype: [tuskar.storage.models.StoredFile] """ object_type = store.object_type session = get_session() try: files = session.query(StoredFile).filter_by( object_type=object_type ) if only_latest: # When only_latest is provided, then we want to select only the # stored files with the latest version. To do this we use a # subquery to get a set of names and latest versions for the # object type. After we have that, we join in the name and # version to make sure we match it. stmt = session.query( StoredFile.name, func.max(StoredFile.version).label("version") ).filter_by( object_type=object_type ).group_by( StoredFile.name ).subquery() # join our existing query on the subquery. files = files.join( stmt, and_( StoredFile.name == stmt.c.name, StoredFile.version == stmt.c.version, ) ) return [self._to_storage_model(store, file_) for file_ in files] finally: session.close()
def list(self, store, only_latest=False): """Return a list of all the stored objects for a given store. Optionally only_latest can be set to True to return only the most recent version of each objects (grouped by name). :param store: The store class, used for routing the storage. :type store: tuskar.storage.stores._BaseStore :param only_latest: If set to True only the latest versions of each object will be returned. :type only_latest: bool :return: List of StoredFile instances :rtype: [tuskar.storage.models.StoredFile] """ object_type = store.object_type session = get_session() try: files = session.query(StoredFile).filter_by( object_type=object_type) if only_latest: # When only_latest is provided, then we want to select only the # stored files with the latest version. To do this we use a # subquery to get a set of names and latest versions for the # object type. After we have that, we join in the name and # version to make sure we match it. stmt = session.query( StoredFile.name, func.max(StoredFile.version).label("version")).filter_by( object_type=object_type).group_by( StoredFile.name).subquery() # join our existing query on the subquery. files = files.join( stmt, and_( StoredFile.name == stmt.c.name, StoredFile.version == stmt.c.version, )) return [self._to_storage_model(store, file_) for file_ in files] finally: session.close()
def retrieve_by_name(self, store, name, version=None): """Returns the stored file for a given store that matches the provided name and optionally version. :param store: The store class, used for routing the storage. :type store: tuskar.storage.stores._BaseStore :param name: name of the object to retrieve. :type name: str :param version: Version of the object to retrieve. If the version isn't provided, the latest will be returned. :type version: int :return: StoredFile instance containing the file metadata and contents :rtype: tuskar.storage.models.StoredFile :raises: tuskar.storage.exceptions.UnknownName if the name can't be found :raises: tuskar.storage.exceptions.UnknownVersion if the version can't be found """ object_type = store.object_type session = get_session() try: query = session.query(StoredFile).filter_by( name=name, object_type=object_type, ) if version is not None: query = query.filter_by(version=version) else: query = query.filter_by( version=self._get_latest_version(store, name) ) stored_file = query.one() return self._to_storage_model(store, stored_file) except NoResultFound: name_query = session.query(StoredFile).filter_by( name=name, object_type=object_type, ) if name_query.count() == 0: msg = "No {0}s found for the name: {1}".format( object_type, name ) raise UnknownName(msg) elif name_query.filter_by(version=version).count() == 0: msg = "No {0}s found for the Version: {1}".format( object_type, name ) raise UnknownVersion(msg) raise finally: session.close()