Example #1
0
    def get_by_values(self,
                      container_id,
                      name,
                      URL,
                      suppress_exception=False,
                      show_deleted=False,
                      session=None):
        session = self.get_session(session)

        try:
            query = session.query(models.ContainerConsumerMetadatum)
            query = query.filter_by(container_id=container_id,
                                    name=name,
                                    URL=URL)

            if not show_deleted:
                query.filter_by(deleted=False)
            consumer = query.one()
        except sa_orm.exc.NoResultFound:
            if not suppress_exception:
                raise exception.NotFound(
                    u._("Could not find {entity_name}").format(
                        entity_name=self._do_entity_name()))

        return consumer
Example #2
0
    def get_by_create_date(self,
                           keystone_id,
                           offset_arg=None,
                           limit_arg=None,
                           name=None,
                           alg=None,
                           mode=None,
                           bits=0,
                           suppress_exception=False,
                           session=None):
        """Returns a list of secrets, ordered by the date they were created at
        and paged based on the offset and limit fields. The keystone_id is
        external-to-Barbican value assigned to the tenant by Keystone.
        """

        offset, limit = clean_paging_values(offset_arg, limit_arg)

        session = self.get_session(session)
        utcnow = timeutils.utcnow()

        try:
            query = session.query(models.Secret) \
                           .order_by(models.Secret.created_at) \
                           .filter_by(deleted=False)

            # Note: Must use '== None' below, not 'is None'.
            query = query.filter(
                or_(models.Secret.expiration == None,
                    models.Secret.expiration > utcnow))

            if name:
                query = query.filter(models.Secret.name.like(name))
            if alg:
                query = query.filter(models.Secret.algorithm.like(alg))
            if mode:
                query = query.filter(models.Secret.mode.like(mode))
            if bits > 0:
                query = query.filter(models.Secret.bit_length == bits)

            query = query.join(models.TenantSecret,
                               models.Secret.tenant_assocs) \
                         .join(models.Tenant, models.TenantSecret.tenants) \
                         .filter(models.Tenant.keystone_id == keystone_id)

            start = offset
            end = offset + limit
            LOG.debug('Retrieving from {0} to {1}'.format(start, end))
            total = query.count()
            entities = query[start:end]
            LOG.debug('Number entities retrieved: {0} out of {1}'.format(
                len(entities), total))

        except sa_orm.exc.NoResultFound:
            entities = None
            total = 0
            if not suppress_exception:
                raise exception.NotFound("No %s's found" %
                                         (self._do_entity_name()))

        return entities, offset, limit, total
Example #3
0
    def get(self,
            entity_id,
            keystone_id=None,
            force_show_deleted=False,
            suppress_exception=False,
            session=None):
        """Get an entity or raise if it does not exist."""
        session = self.get_session(session)

        try:
            query = self._do_build_get_query(entity_id, keystone_id, session)

            # filter out deleted entities if requested
            if not force_show_deleted:
                query = query.filter_by(deleted=False)

            entity = query.one()

        except sa_orm.exc.NoResultFound:
            LOG.exception("Not found for {0}".format(entity_id))
            entity = None
            if not suppress_exception:
                raise exception.NotFound("No %s found with ID %s" %
                                         (self._do_entity_name(), entity_id))

        return entity
 def test_should_throw_exception_for_delete_when_trans_key_not_found(self):
     self.repo.delete_entity_by_id.side_effect = excep.NotFound(
         "Test not found exception")
     resp = self.app.delete('/%s/transport_keys/%s/' %
                            (self.tenant_keystone_id, self.tkey.id),
                            expect_errors=True)
     self.assertEqual(resp.status_int, 404)
Example #5
0
 def test_should_404_consumer_bad_container_id(self):
     self.container_repo.get.side_effect = excep.NotFound()
     resp = self.app.post_json(
         '/containers/{0}/consumers/'.format('bad_id'),
         self.consumer_ref,
         expect_errors=True)
     self.container_repo.get.side_effect = None
     self.assertEqual(404, resp.status_int)
Example #6
0
 def test_should_404_on_delete_when_consumer_not_found_later(self):
     self.consumer_repo.delete_entity_by_id.side_effect = excep.NotFound()
     resp = self.app.delete_json('/containers/{0}/consumers/'.format(
         self.container.id
     ), self.consumer_ref, expect_errors=True)
     self.consumer_repo.delete_entity_by_id.side_effect = None
     self.assertEqual(404, resp.status_int)
     # Error response should have json content type
     self.assertEqual("application/json", resp.content_type)
Example #7
0
    def test_should_throw_exception_for_delete_when_order_not_found(self):
        self.order_repo.delete_entity_by_id.side_effect = excep.NotFound(
            "Test not found exception")

        with self.assertRaises(falcon.HTTPError) as cm:
            self.resource.on_delete(self.req, self.resp,
                                    self.tenant_keystone_id, self.order.id)

        exception = cm.exception
        self.assertEqual(falcon.HTTP_404, exception.status)
Example #8
0
    def delete_entity_by_id(self, entity_id, keystone_id):
        """Remove the entity by its ID"""

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

            entity = self.get(entity_id=entity_id,
                              keystone_id=keystone_id,
                              session=session)

            try:
                entity.delete(session=session)
            except sqlalchemy.exc.IntegrityError:
                LOG.exception('Problem finding entity to delete')
                raise exception.NotFound("Entity ID %s not found" % entity_id)
Example #9
0
    def get_by_create_date(self,
                           keystone_id,
                           offset_arg=None,
                           limit_arg=None,
                           suppress_exception=False,
                           session=None):
        """
        Returns a list of orders, ordered by the date they were created at
        and paged based on the offset and limit fields.

        :param keystone_id: The keystone id for the tenant.
        :param offset_arg: The entity number where the query result should
                           start.
        :param limit_arg: The maximum amount of entities in the result set.
        :param suppress_exception: Whether NoResultFound exceptions should be
                                   suppressed.
        :param session: SQLAlchemy session object.

        :returns: Tuple consisting of (list_of_entities, offset, limit, total).
        """

        offset, limit = clean_paging_values(offset_arg, limit_arg)

        session = self.get_session(session)

        try:
            query = session.query(models.Order) \
                           .order_by(models.Order.created_at)
            query = query.filter_by(deleted=False) \
                         .join(models.Tenant, models.Order.tenant) \
                         .filter(models.Tenant.keystone_id == keystone_id)

            start = offset
            end = offset + limit
            LOG.debug('Retrieving from {0} to {1}'.format(start, end))
            total = query.count()
            entities = query[start:end]
            LOG.debug('Number entities retrieved: {0} out of {1}'.format(
                len(entities), total))

        except sa_orm.exc.NoResultFound:
            entities = None
            total = 0
            if not suppress_exception:
                raise exception.NotFound("No %s's found" %
                                         (self._do_entity_name()))

        return entities, offset, limit, total
Example #10
0
    def _update(self, entity_id, values, purge_props=False):
        """
        Used internally by update()

        :param values: A dict of attributes to set
        :param entity_id: If None, create the entity, otherwise,
                          find and update it
        """
        session = get_session()
        with session.begin():

            if entity_id:
                entity_ref = self.get(entity_id, session=session)
                values['updated_at'] = timeutils.utcnow()
            else:
                self._do_convert_values(values)
                entity_ref = self._do_create_instance()

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

            entity_ref.update(values)

            # Validate the attributes before we go any further. From my
            # (unknown Glance developer) investigation, the @validates
            # decorator does not validate
            # on new records, only on existing records, which is, well,
            # idiotic.
            self._do_validate(entity_ref.to_dict())
            self._update_values(entity_ref, values)

            try:
                entity_ref.save(session=session)
            except sqlalchemy.exc.IntegrityError:
                LOG.exception('Problem saving entity for _update')
                if entity_id:
                    raise exception.NotFound("Entity ID %s not found" %
                                             entity_id)
                else:
                    raise exception.Duplicate("Entity ID %s already exists!" %
                                              values['id'])

        return self.get(entity_ref.id)
Example #11
0
    def find_by_keystone_id(self,
                            keystone_id,
                            suppress_exception=False,
                            session=None):
        session = self.get_session(session)

        try:
            query = session.query(
                models.Tenant).filter_by(keystone_id=keystone_id)

            entity = query.one()

        except sa_orm.exc.NoResultFound:
            LOG.exception("Problem getting Tenant {0}".format(keystone_id))
            entity = None
            if not suppress_exception:
                raise exception.NotFound("No %s found with keystone-ID %s" %
                                         (self._do_entity_name(), keystone_id))

        return entity
Example #12
0
    def save(self, entity):
        """Saves the state of the entity.

        :raises NotFound if entity does not exist.
        """
        session = get_session()
        with session.begin():
            entity.updated_at = timeutils.utcnow()

            # Validate the attributes before we go any further. From my
            # (unknown Glance developer) investigation, the @validates
            # decorator does not validate
            # on new records, only on existing records, which is, well,
            # idiotic.
            self._do_validate(entity.to_dict())

            try:
                entity.save(session=session)
            except sqlalchemy.exc.IntegrityError:
                LOG.exception('Problem saving entity for update')
                raise exception.NotFound("Entity ID %s not found" % entity.id)
Example #13
0
    def get_by_create_date(self,
                           keystone_id,
                           offset_arg=None,
                           limit_arg=None,
                           suppress_exception=False,
                           session=None):
        """
        Returns a list of containers, ordered by the date they were
        created at and paged based on the offset and limit fields. The
        keystone_id is external-to-Barbican value assigned to the tenant
        by Keystone.
        """

        offset, limit = clean_paging_values(offset_arg, limit_arg)

        session = self.get_session(session)

        try:
            query = session.query(models.Container) \
                           .order_by(models.Container.created_at)
            query = query.filter_by(deleted=False) \
                         .join(models.Tenant, models.Container.tenant) \
                         .filter(models.Tenant.keystone_id == keystone_id)

            start = offset
            end = offset + limit
            LOG.debug('Retrieving from {0} to {1}'.format(start, end))
            total = query.count()
            entities = query[start:end]
            LOG.debug('Number entities retrieved: {0} out of {1}'.format(
                len(entities), total))

        except sa_orm.exc.NoResultFound:
            entities = None
            total = 0
            if not suppress_exception:
                raise exception.NotFound("No %s's found" %
                                         (self._do_entity_name()))

        return entities, offset, limit, total
Example #14
0
    def get_by_create_date(self,
                           plugin_name=None,
                           offset_arg=None,
                           limit_arg=None,
                           suppress_exception=False,
                           session=None):
        """Returns a list of transport keys, ordered from latest created first.
        The search accepts plugin_id as an optional parameter for the search.
        """

        offset, limit = clean_paging_values(offset_arg, limit_arg)

        session = self.get_session(session)

        try:
            query = session.query(models.TransportKey) \
                           .order_by(models.TransportKey.created_at)
            if plugin_name is not None:
                query = session.query(models.TransportKey).\
                    filter_by(deleted=False, plugin_name=plugin_name)
            else:
                query = query.filter_by(deleted=False)

            start = offset
            end = offset + limit
            LOG.debug('Retrieving from {0} to {1}'.format(start, end))
            total = query.count()
            entities = query[start:end]
            LOG.debug('Number of entities retrieved: {0} out of {1}'.format(
                len(entities), total))

        except sa_orm.exc.NoResultFound:
            entities = None
            total = 0
            if not suppress_exception:
                raise exception.NotFound("No {0}'s found".format(
                    self._do_entity_name()))

        return entities, offset, limit, total
Example #15
0
    def find_by_external_project_id(self,
                                    external_project_id,
                                    suppress_exception=False,
                                    session=None):
        session = self.get_session(session)

        try:
            query = session.query(models.Project)
            query = query.filter_by(external_id=external_project_id)

            entity = query.one()

        except sa_orm.exc.NoResultFound:
            entity = None
            if not suppress_exception:
                LOG.exception(u._LE("Problem getting Project %s"),
                              external_project_id)
                raise exception.NotFound(
                    u._("No {entity_name} found with keystone-ID {id}").format(
                        entity_name=self._do_entity_name(),
                        id=external_project_id))

        return entity
Example #16
0
def _raise_no_entities_found(entity_name):
    raise exception.NotFound(
        u._("No entities of type {entity_name} found").format(
            entity_name=entity_name))
Example #17
0
def _raise_entity_id_not_found(entity_id):
    raise exception.NotFound(
        u._("Entity ID {entity_id} not "
            "found").format(entity_id=entity_id))
Example #18
0
def _raise_entity_not_found(entity_name, entity_id):
    raise exception.NotFound(
        u._("No {entity} found with ID {id}").format(entity=entity_name,
                                                     id=entity_id))