Exemplo n.º 1
0
 def fetch_names_from_project(self, project_name):
     try:
         images = self.connection.session.query(Image).filter(
             Image.project.has(name=project_name))
         return [image.name for image in images]
     except SQLAlchemyError as e:
         raise db_exceptions.ORMException(e.message)
Exemplo n.º 2
0
 def fetch_names_with_public(self):
     try:
         img_list = self.connection.session.query(Image).filter_by(
             is_public=True)
         return [image.name for image in img_list]
     except SQLAlchemyError as e:
         raise db_exceptions.ORMException(e.message)
Exemplo n.º 3
0
    def copy_image(self, src_project_name, name, dest_pid, new_name=None):
        try:

            project = self.connection.session.query(Project).filter_by(
                name=src_project_name).one_or_none()

            if project is None:
                raise db_exceptions.ProjectNotFoundException(src_project_name)

            image = self.connection.session.query(Image). \
                filter(Image.project.has(name=src_project_name)).filter_by(
                name=name).one_or_none()

            if image is None:
                raise db_exceptions.ImageNotFoundException(name)

            new_image = Image()
            if new_name is not None:
                new_image.name = new_name
            else:
                new_image.name = name

            new_image.project_id = dest_pid
            new_image.is_public = image.is_public
            if project.id == dest_pid:
                new_image.is_snapshot = image.is_snapshot
                new_image.parent_id = image.parent_id
            else:
                new_image.is_snapshot = False
                new_image.parent_id = None
            self.connection.session.add(new_image)
            self.connection.session.commit()
        except SQLAlchemyError as e:
            self.connection.session.rollback()
            raise db_exceptions.ORMException(e.message)
Exemplo n.º 4
0
 def fetch_projects(self):
     try:
         projects = self.connection.session.query(Project)
         return [[project.id, project.name, project.provision_network]
                 for project in projects]
     except SQLAlchemyError as e:
         raise db_exceptions.ORMException(e.message)
Exemplo n.º 5
0
 def fetch_project_with_id(self, id):
     try:
         image = self.connection.session.query(Image).filter_by(
             id=id).one_or_none()
         if image is not None:
             return image.project.name
     except SQLAlchemyError as e:
         raise db_exceptions.ORMException(e.message)
Exemplo n.º 6
0
 def fetch_id_with_name(self, name):
     try:
         project = self.connection.session.query(Project).filter_by(
             name=name).one_or_none()
         if project is not None:
             return project.id
     except SQLAlchemyError as e:
         raise db_exceptions.ORMException(e.message)
Exemplo n.º 7
0
 def fetch_snapshots_from_project(self, project_name):
     try:
         images = self.connection.session.query(Image).filter(
             Image.project.has(name=project_name)).filter_by(
                 is_snapshot=True)
         return [[image.name, image.parent.name] for image in images]
     except SQLAlchemyError as e:
         raise db_exceptions.ORMException(e.message)
Exemplo n.º 8
0
 def fetch_parent_id(self, project_name, name):
     try:
         image = self.connection.session.query(Image). \
             filter(Image.project.has(name=project_name)).filter_by(
             name=name).one_or_none()
         if image is not None and image.parent_id is not None:
             return image.parent_id
     except SQLAlchemyError as e:
         raise db_exceptions.ORMException(e.message)
Exemplo n.º 9
0
 def delete_with_name(self, name):
     try:
         project = self.connection.session.query(Project).filter_by(
             name=name).one_or_none()
         if project is not None:
             self.connection.session.delete(project)
             self.connection.session.commit()
     except SQLAlchemyError as e:
         self.connection.session.rollback()
         raise db_exceptions.ORMException(e.message)
Exemplo n.º 10
0
 def insert(self, name, id=None):
     try:
         p = Project()
         p.name = name
         if id is not None:
             p.id = id
         self.connection.session.add(p)
         self.connection.session.commit()
     except SQLAlchemyError as e:
         self.connection.session.rollback()
         raise db_exceptions.ORMException(e.message)
Exemplo n.º 11
0
 def fetch_images_from_project(self, project_name):
     try:
         images = self.connection.session.query(Image).filter(
             Image.project.has(name=project_name))
         names = []
         for image in images:
             if image.parent is None:
                 names.append(image.name)
         return names
     except SQLAlchemyError as e:
         raise db_exceptions.ORMException(e.message)
Exemplo n.º 12
0
 def fetch_clones_from_project(self, project_name):
     try:
         images = self.connection.session.query(Image).filter(
             Image.project.has(name=project_name)).filter_by(
                 is_snapshot=False)
         rows = []
         for image in images:
             row = []
             if image.parent is not None:
                 row.append(image.name)
                 row.append(image.parent.name)
                 rows.append(row)
         return rows
     except SQLAlchemyError as e:
         raise db_exceptions.ORMException(e.message)
Exemplo n.º 13
0
 def fetch_all_images(self):
     try:
         images = self.connection.session.query(Image)
         rows = []
         for image in images:
             row = [
                 image.id, image.name, image.project.name, image.is_public,
                 image.is_snapshot
             ]
             if image.parent is not None:
                 row.append(image.parent.name)
             else:
                 row.append('')
             rows.append(row)
         return rows
     except SQLAlchemyError as e:
         raise db_exceptions.ORMException(e.message)
Exemplo n.º 14
0
    def fetch_id_with_name_from_project(self, name, project_name):
        """
        Searches for image by name and returns image id

        :param name: name of the image
        :param project_name: name of the project
        :return: the id of the image.
        """

        try:
            image = self.connection.session.query(Image). \
                filter(Image.project.has(name=project_name)).filter_by(
                name=name).one_or_none()
            if image is None:
                raise db_exceptions.ImageNotFoundException(name)
            return image.id
        except SQLAlchemyError as e:
            raise db_exceptions.ORMException(e.message)
Exemplo n.º 15
0
    def delete_with_name_from_project(self, name, project_name):
        try:
            image = self.connection.session.query(Image). \
                filter(Image.project.has(name=project_name)).filter_by(
                name=name).one_or_none()
            if image is not None:

                if self.__image_has_clones(image):
                    raise db_exceptions.ImageHasClonesException(image)

                for child in image.children:
                    child.parent_id = None
                    child.is_snapshot = False
                self.connection.session.delete(image)
                self.connection.session.commit()
            else:
                raise db_exceptions.ImageNotFoundException(name)
        except SQLAlchemyError as e:
            self.connection.session.rollback()
            raise db_exceptions.ORMException(e.message)
Exemplo n.º 16
0
 def insert(self,
            image_name,
            project_id,
            parent_id=None,
            is_public=False,
            is_snapshot=False,
            id=None):
     try:
         img = Image()
         img.name = image_name
         img.project_id = project_id
         img.is_public = is_public
         img.is_snapshot = is_snapshot
         img.parent_id = parent_id
         if id is not None:
             img.id = id
         self.connection.session.add(img)
         self.connection.session.commit()
     except SQLAlchemyError as e:
         self.connection.session.rollback()
         raise db_exceptions.ORMException(e.message)
Exemplo n.º 17
0
 def fetch_images(self):
     try:
         images = self.connection.session.query(Image)
         return images
     except SQLAlchemyError as e:
         raise db_exceptions.ORMException(e.message)