Ejemplo n.º 1
0
    def update_guest_by_userid(self, userid, meta=None, comments=None):
        userid = userid.upper()
        if (meta is None) and (comments is None):
            msg = ("Update guest with userid: %s failed, no field "
                   "specified to be updated." % userid)
            LOG.error(msg)
            raise exception.DatabaseException(msg=msg)

        # First check whether the guest exist in db table
        self._check_existence_by_userid(userid)
        # Start update
        sql_cmd = "UPDATE guests SET"
        sql_var = []
        if meta is not None:
            sql_cmd += " metadata=?,"
            sql_var.append(meta)
        if comments is not None:
            sql_cmd += " comments=?,"
            sql_var.append(comments)

        # remove the tailing comma
        sql_cmd = sql_cmd.strip(',')
        # Add the id filter
        sql_cmd += " WHERE userid=?"
        sql_var.append(userid)

        with get_db_conn() as conn:
            conn.execute(sql_cmd, sql_var)
Ejemplo n.º 2
0
    def update_volume(self, volume):
        """Update a volume in database.
        The volume is represented by a dict of all volume properties:
        id: volume id, must be specified.
        protocol_type: protocol type to access the volume, like 'fc' or
                      'iscsi', can not update, don't set.
        size: volume size in Terabytes, Gigabytes or Megabytes, optional.
        status: volume status, optional.
        image_id: source image id when boot-from-volume, optional.
        snapshot_id: snapshot id when a volume comes from a snapshot, optional.
        deleted: if deleted, can not be updated, use delete_volume() to delete.
        deleted_at: auto generated and can not be specified.
        comment: any comment, optional.
        """
        if not (isinstance(volume, dict) and
                'id' in volume.keys()):
            msg = "Invalid volume database entry %s !" % volume
            raise exception.DatabaseException(msg=msg)

        # get current volume properties
        volume_id = volume['id']
        old_volume = self.get_volume_by_id(volume_id)
        if not old_volume:
            msg = "Volume %s not found in database!" % volume_id
            raise exception.DatabaseException(msg=msg)
        else:
            (_, _, size, status, image_id, snapshot_id, _, _, comment
             ) = old_volume

        if 'size' in volume.keys():
            size = volume['size']
        if 'status' in volume.keys():
            status = volume['status']
        if 'image_id' in volume.keys():
            image_id = volume['image_id']
        if 'snapshot_id' in volume.keys():
            snapshot_id = volume['snapshot_id']
        if 'comment' in volume.keys():
            comment = volume['comment']

        with get_db_conn() as conn:
            conn.execute(' '.join((
                "UPDATE volumes",
                "SET size=?, status=?, image_id=?, snapshot_id=?, comment=?"
                "WHERE id=?")),
                (size, status, image_id, snapshot_id, comment, volume_id))
Ejemplo n.º 3
0
    def delete_volume(self, volume_id):
        """Delete a volume from database."""
        if not volume_id:
            msg = "Volume id must be specified!"
            raise exception.DatabaseException(msg=msg)

        volume = self.get_volume_by_id(volume_id)
        if not volume:
            msg = "Volume %s not found in database!" % volume_id
            raise exception.DatabaseException(msg=msg)

        time = str(datetime.now())
        with get_db_conn() as conn:
            conn.execute(' '.join((
                "UPDATE volumes",
                "SET deleted=1, deleted_at=?",
                "WHERE id=?")),
                (time, volume_id))
Ejemplo n.º 4
0
def get_db_conn():
    """Get a database connection object to execute some SQL statements
    and release the connection object finally.
    """
    _op = get_DbOperator()
    (i, conn) = _op.get_connection()
    try:
        yield conn
    except Exception as err:
        LOG.error("Execute SQL statements error: %s", six.text_type(err))
        raise exception.DatabaseException(msg=err)
    finally:
        _op.release_connection(i)
Ejemplo n.º 5
0
    def insert_volume(self, volume):
        """Insert a volume into database.
        The volume is represented by a dict of all volume properties:
        id: volume id, auto generated and can not be specified.
        protocol_type: protocol type to access the volume, like 'fc' or
                      'iscsi', must be specified.
        size: volume size in Terabytes, Gigabytes or Megabytes, must be
              specified.
        status: volume status, auto generated and can not be specified.
        image_id: source image id when boot-from-volume, optional.
        snapshot_id: snapshot id when a volume comes from a snapshot, optional.
        deleted: if deleted, auto generated and can not be specified.
        deleted_at: auto generated and can not be specified.
        comment: any comment, optional.
        """
        if not (isinstance(volume, dict) and
                'protocol_type' in volume.keys() and
                'size' in volume.keys()):
            msg = "Invalid volume database entry %s !" % volume
            raise exception.DatabaseException(msg=msg)

        volume_id = str(uuid.uuid4())
        protocol_type = volume['protocol_type']
        size = volume['size']
        status = self._VOLUME_STATUS_FREE
        image_id = None
        if 'image_id' in volume.keys():
            image_id = volume['image_id']
        snapshot_id = None
        if 'snapshot_id' in volume.keys():
            snapshot_id = volume['snapshot_id']
        deleted = '0'
        deleted_at = None
        comment = None
        if 'comment' in volume.keys():
            comment = volume['comment']

        with get_db_conn() as conn:
            conn.execute(
                "INSERT INTO volumes VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
                (volume_id, protocol_type, size, status, image_id, snapshot_id,
                 deleted, deleted_at, comment))

        return volume_id
Ejemplo n.º 6
0
    def get_volume_by_id(self, volume_id):
        """Query a volume form database by its id.
        The id must be a 36-character string.
        """
        if not volume_id:
            msg = "Volume id must be specified!"
            raise exception.DatabaseException(msg=msg)

        with get_db_conn() as conn:
            result_list = conn.execute(
                "SELECT * FROM volumes WHERE id=:id AND deleted=0",
                {'id': volume_id}
                ).fetchall()

        if len(result_list) == 1:
            return result_list[0]
        elif len(result_list) == 0:
            LOG.debug("Volume with id: %s not found!" % volume_id)
            return None
Ejemplo n.º 7
0
 def _check_existence_by_userid(self, userid):
     guest = self.get_guest_by_userid(userid)
     if guest is None:
         msg = 'Guest with userid: %s does not exist in DB.' % userid
         LOG.error(msg)
         raise exception.DatabaseException(msg=msg)