def image_location_update(context, image_id, location, session=None):
    loc_id = location.get('id')
    if loc_id is None:
        # msg = _("The location data has an invalid ID: %d") % loc_id
        raise Exception("exception.Invalid(msg)")

    try:
        session = session or get_session()
        location_ref = session.query(models.ImageLocation).filter_by(
            id=loc_id).filter_by(image_id=image_id).one()

        deleted = location['status'] in ('deleted', 'pending_delete')
        updated_time = timeutils.utcnow()
        delete_time = updated_time if deleted else None

        location_ref.update({"value": location['url'],
                             "meta_data": location['metadata'],
                             "status": location['status'],
                             "deleted": deleted,
                             "updated_at": updated_time,
                             "deleted_at": delete_time})
        location_ref.save(session=session)
    except sa_orm.exc.NoResultFound:
        # msg = (_("No location found with ID %(loc)s from image %(img)s") %
        #        dict(loc=loc_id, img=image_id))
        # LOG.warn(msg)
        raise Exception("exception.NotFound(msg)")
Exemple #2
0
def image_location_update(context, image_id, location, session=None):
    loc_id = location.get('id')
    if loc_id is None:
        # msg = _("The location data has an invalid ID: %d") % loc_id
        raise Exception("exception.Invalid(msg)")

    try:
        session = session or get_session()
        location_ref = session.query(models.ImageLocation).filter_by(
            id=loc_id).filter_by(image_id=image_id).one()

        deleted = location['status'] in ('deleted', 'pending_delete')
        updated_time = timeutils.utcnow()
        delete_time = updated_time if deleted else None

        location_ref.update({
            "value": location['url'],
            "meta_data": location['metadata'],
            "status": location['status'],
            "deleted": deleted,
            "updated_at": updated_time,
            "deleted_at": delete_time
        })
        location_ref.save(session=session)
    except sa_orm.exc.NoResultFound:
        # msg = (_("No location found with ID %(loc)s from image %(img)s") %
        #        dict(loc=loc_id, img=image_id))
        # LOG.warn(msg)
        raise Exception("exception.NotFound(msg)")
Exemple #3
0
def image_location_delete(context,
                          image_id,
                          location_id,
                          status,
                          delete_time=None,
                          session=None):
    if status not in ('deleted', 'pending_delete'):
        # msg = _("The status of deleted image location can only be set to "
        #         "'pending_delete' or 'deleted'")
        raise Exception("exception.Invalid(msg)")

    try:
        session = session or get_session()
        location_ref = session.query(models.ImageLocation).filter_by(
            id=location_id).filter_by(image_id=image_id).one()

        delete_time = delete_time or timeutils.utcnow()

        location_ref.update({
            "deleted": True,
            "status": status,
            "updated_at": delete_time,
            "deleted_at": delete_time
        })
        location_ref.save(session=session)
    except sa_orm.exc.NoResultFound:
        # msg = (_("No location found with ID %(loc)s from image %(img)s") %
        #        dict(loc=location_id, img=image_id))
        # LOG.warn(msg)
        raise Exception("exception.NotFound(msg)")
Exemple #4
0
def image_location_add(context, image_id, location, session=None):
    deleted = location['status'] in ('deleted', 'pending_delete')
    delete_time = timeutils.utcnow() if deleted else None
    location_ref = models.ImageLocation(image_id=image_id,
                                        value=location['url'],
                                        meta_data=location['metadata'],
                                        status=location['status'],
                                        deleted=deleted,
                                        deleted_at=delete_time)
    session = session or get_session()
    location_ref.save(session=session)
def image_location_add(context, image_id, location, session=None):
    deleted = location['status'] in ('deleted', 'pending_delete')
    delete_time = timeutils.utcnow() if deleted else None
    location_ref = models.ImageLocation(image_id=image_id,
                                        value=location['url'],
                                        meta_data=location['metadata'],
                                        status=location['status'],
                                        deleted=deleted,
                                        deleted_at=delete_time)
    session = session or get_session()
    location_ref.save(session=session)
def reservation_expire(context):
    session = get_session()
    with session.begin():
        current_time = timeutils.utcnow()
        reservation_query = model_query(context, models.Reservation,
                                        session=session, read_deleted="no").\
                            filter(models.Reservation.expire < current_time)

        for row in reservation_query.join(models.QuotaUsage).all():
            reservation = row[0] if len(row) > 1 else row
            if reservation.delta >= 0:
                reservation.usage.reserved -= reservation.delta
                session.add(reservation.usage)
        session.flush()
        reservation_query.soft_delete(synchronize_session=False)
Exemple #7
0
def compute_node_update(context, compute_id, values):
    """Updates the ComputeNode record with the most recent data."""

    session = get_session()
    with session.begin():
        compute_ref = _compute_node_get(context, compute_id, session=session)
        # Always update this, even if there's going to be no other
        # changes in data.  This ensures that we invalidate the
        # scheduler cache of compute node data in case of races.
        values['updated_at'] = timeutils.utcnow()
        datetime_keys = ('created_at', 'deleted_at', 'updated_at')
        convert_objects_related_datetimes(values, *datetime_keys)
        compute_ref.update(values)
        # TODO (Jonathan): add a "session.add" to ease the session management :)
        session.add(compute_ref)

    return compute_ref
def compute_node_update(context, compute_id, values):
    """Updates the ComputeNode record with the most recent data."""

    session = get_session()
    with session.begin():
        compute_ref = _compute_node_get(context, compute_id, session=session)
        # Always update this, even if there's going to be no other
        # changes in data.  This ensures that we invalidate the
        # scheduler cache of compute node data in case of races.
        values['updated_at'] = timeutils.utcnow()
        datetime_keys = ('created_at', 'deleted_at', 'updated_at')
        convert_objects_related_datetimes(values, *datetime_keys)
        compute_ref.update(values)
        # TODO (Jonathan): add a "session.add" to ease the session management :)
        session.add(compute_ref)


    return compute_ref
def image_location_delete(context, image_id, location_id, status,
                          delete_time=None, session=None):
    if status not in ('deleted', 'pending_delete'):
        # msg = _("The status of deleted image location can only be set to "
        #         "'pending_delete' or 'deleted'")
        raise Exception("exception.Invalid(msg)")

    try:
        session = session or get_session()
        location_ref = session.query(models.ImageLocation).filter_by(
            id=location_id).filter_by(image_id=image_id).one()

        delete_time = delete_time or timeutils.utcnow()

        location_ref.update({"deleted": True,
                             "status": status,
                             "updated_at": delete_time,
                             "deleted_at": delete_time})
        location_ref.save(session=session)
    except sa_orm.exc.NoResultFound:
        # msg = (_("No location found with ID %(loc)s from image %(img)s") %
        #        dict(loc=location_id, img=image_id))
        # LOG.warn(msg)
        raise Exception("exception.NotFound(msg)")
Exemple #10
0
def _image_update(context,
                  values,
                  image_id,
                  purge_props=False,
                  from_state=None):
    """
    Used internally by image_create and image_update
    :param context: Request context
    :param values: A dict of attributes to set
    :param image_id: If None, create the image, otherwise, find and update it
    """

    # NOTE(jbresnah) values is altered in this so a copy is needed
    values = values.copy()

    session = get_session()
    with session.begin():

        # Remove the properties passed in the values mapping. We
        # handle properties separately from base image attributes,
        # and leaving properties in the values mapping will cause
        # a SQLAlchemy model error because SQLAlchemy expects the
        # properties attribute of an Image model to be a list and
        # not a dict.
        properties = values.pop('properties', {})

        location_data = values.pop('locations', None)

        new_status = values.get('status', None)
        if image_id:
            image_ref = _image_get(context, image_id, session=session)
            session.add(image_ref)
            current = image_ref.status
            # Perform authorization check
            _check_mutate_authorization(context, image_ref)
        else:
            if values.get('size') is not None:
                values['size'] = int(values['size'])

            if 'min_ram' in values:
                values['min_ram'] = int(values['min_ram'] or 0)

            if 'min_disk' in values:
                values['min_disk'] = int(values['min_disk'] or 0)

            values['is_public'] = bool(values.get('is_public', False))
            values['protected'] = bool(values.get('protected', False))
            image_ref = models.Image()
            session.add(image_ref)

        # Need to canonicalize ownership
        if 'owner' in values and not values['owner']:
            values['owner'] = None

        if image_id:
            # Don't drop created_at if we're passing it in...
            _drop_protected_attrs(models.Image, values)
            # NOTE(iccha-sethi): updated_at must be explicitly set in case
            #                   only ImageProperty table was modifited
            values['updated_at'] = timeutils.utcnow()

        if image_id:
            query = session.query(models.Image).filter_by(id=image_id)
            if from_state:
                query = query.filter_by(status=from_state)

            mandatory_status = True if new_status else False
            _validate_image(values, mandatory_status=mandatory_status)

            # Validate fields for Images table. This is similar to what is done
            # for the query result update except that we need to do it prior
            # in this case.
            # TODO(dosaboy): replace this with a dict comprehension once py26
            #                support is deprecated.
            keys = values.keys()
            for k in keys:
                if k not in image_ref.to_dict():
                    del values[k]
            updated = query.update(values, synchronize_session='fetch')

            if not updated:
                # msg = ('cannot transition from %(current)s to '
                #          '%(next)s in update (wanted '
                #          'from_state=%(from)s)' %
                #        {'current': current, 'next': new_status,
                #         'from': from_state})
                raise Exception("exception.Conflict(msg)")

            image_ref = _image_get(context, image_id, session=session)
        else:
            image_ref.update(values)
            # Validate the attributes before we go any further. From my
            # investigation, the @validates decorator does not validate
            # on new records, only on existing records, which is, well,
            # idiotic.
            values = _validate_image(image_ref.to_dict())
            _update_values(image_ref, values)

            try:
                image_ref.save(session=session)
            except:
                raise Exception(
                    """exception.Duplicate("Image ID %s already exists!" % values['id'])"""
                )

        _set_properties_for_image(context, image_ref, properties, purge_props,
                                  session)

        if location_data:
            _image_locations_set(context,
                                 image_ref.id,
                                 location_data,
                                 session=session)
    # TODO: manually flush session
    session.flush()
    return image_get(context, image_ref.id)
Exemple #11
0
 def soft_delete(self, session):
     """Mark this object as deleted."""
     self.deleted = self.id
     self.deleted_at = timeutils.utcnow()
     self.save(session=session)
Exemple #12
0
class TimestampMixin(object):
    created_at = Column(DateTime, default=lambda: timeutils.utcnow())
    updated_at = Column(DateTime, onupdate=lambda: timeutils.utcnow())
def _image_update(context, values, image_id, purge_props=False,
                  from_state=None):
    """
    Used internally by image_create and image_update
    :param context: Request context
    :param values: A dict of attributes to set
    :param image_id: If None, create the image, otherwise, find and update it
    """

    # NOTE(jbresnah) values is altered in this so a copy is needed
    values = values.copy()

    session = get_session()
    with session.begin():

        # Remove the properties passed in the values mapping. We
        # handle properties separately from base image attributes,
        # and leaving properties in the values mapping will cause
        # a SQLAlchemy model error because SQLAlchemy expects the
        # properties attribute of an Image model to be a list and
        # not a dict.
        properties = values.pop('properties', {})

        location_data = values.pop('locations', None)

        new_status = values.get('status', None)
        if image_id:
            image_ref = _image_get(context, image_id, session=session)
            session.add(image_ref)
            current = image_ref.status
            # Perform authorization check
            _check_mutate_authorization(context, image_ref)
        else:
            if values.get('size') is not None:
                values['size'] = int(values['size'])

            if 'min_ram' in values:
                values['min_ram'] = int(values['min_ram'] or 0)

            if 'min_disk' in values:
                values['min_disk'] = int(values['min_disk'] or 0)

            values['is_public'] = bool(values.get('is_public', False))
            values['protected'] = bool(values.get('protected', False))
            image_ref = models.Image()
            session.add(image_ref)

        # Need to canonicalize ownership
        if 'owner' in values and not values['owner']:
            values['owner'] = None

        if image_id:
            # Don't drop created_at if we're passing it in...
            _drop_protected_attrs(models.Image, values)
            # NOTE(iccha-sethi): updated_at must be explicitly set in case
            #                   only ImageProperty table was modifited
            values['updated_at'] = timeutils.utcnow()

        if image_id:
            query = session.query(models.Image).filter_by(id=image_id)
            if from_state:
                query = query.filter_by(status=from_state)

            mandatory_status = True if new_status else False
            _validate_image(values, mandatory_status=mandatory_status)

            # Validate fields for Images table. This is similar to what is done
            # for the query result update except that we need to do it prior
            # in this case.
            # TODO(dosaboy): replace this with a dict comprehension once py26
            #                support is deprecated.
            keys = values.keys()
            for k in keys:
                if k not in image_ref.to_dict():
                    del values[k]
            updated = query.update(values, synchronize_session='fetch')

            if not updated:
                # msg = ('cannot transition from %(current)s to '
                #          '%(next)s in update (wanted '
                #          'from_state=%(from)s)' %
                #        {'current': current, 'next': new_status,
                #         'from': from_state})
                raise Exception("exception.Conflict(msg)")

            image_ref = _image_get(context, image_id, session=session)
        else:
            image_ref.update(values)
            # Validate the attributes before we go any further. From my
            # investigation, the @validates decorator does not validate
            # on new records, only on existing records, which is, well,
            # idiotic.
            values = _validate_image(image_ref.to_dict())
            _update_values(image_ref, values)

            try:
                image_ref.save(session=session)
            except:
                raise Exception("""exception.Duplicate("Image ID %s already exists!" % values['id'])""")

        _set_properties_for_image(context, image_ref, properties, purge_props,
                                  session)

        if location_data:
            _image_locations_set(context, image_ref.id, location_data,
                                 session=session)
    # TODO: manually flush session
    session.flush()
    return image_get(context, image_ref.id)
Exemple #14
0
    return compute_ref


class Context(object):
    def __init__(self, project_id, user_id):
        self.project_id = project_id
        self.user_id = user_id


if __name__ == '__main__':

    logging.getLogger().setLevel(logging.DEBUG)

    context = Context("project1", "user1")
    some_date = timeutils.utcnow()
    print(some_date)
    print("%s" % (some_date))

    date_str = "Oct 25 2015 21:37:46"
    date_str = "2015-10-25 23:04:23.452603"
    date_str = "2015-10-25T21:15:16.000000"
    result = None
    timezone = "UTC"
    result = datetime.datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S.%f')
    result.strftime('%Y-%m-%dT%H:%M:%S.%f')
    if timezone == "UTC":
        result = pytz.utc.localize(result)
    print(result)
    print("%s" % (result))
    print("%s" % (result.isoformat()))
        session.add(compute_ref)


    return compute_ref

class Context(object):
    def __init__(self, project_id, user_id):
        self.project_id = project_id
        self.user_id = user_id

if __name__ == '__main__':

    logging.getLogger().setLevel(logging.DEBUG)

    context = Context("project1", "user1")
    some_date = timeutils.utcnow()
    print(some_date)
    print("%s" % (some_date))

    date_str = "Oct 25 2015 21:37:46"
    date_str = "2015-10-25 23:04:23.452603"
    date_str = "2015-10-25T21:15:16.000000"
    result = None
    timezone = "UTC"
    result = datetime.datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S.%f')
    result.strftime('%Y-%m-%dT%H:%M:%S.%f')
    if timezone == "UTC":
        result = pytz.utc.localize(result)
    print(result)
    print("%s" % (result))
    print("%s" % (result.isoformat()))