Beispiel #1
0
    def test_should_raise_constraint_create_same_composite_key_no_id(self):
        session = self.repo.get_session()

        project = models.Project()
        project.external_id = "my keystone id"
        project.save(session=session)

        container = models.Container()
        container.project_id = project.id
        container.save(session=session)

        # Create a consumer.
        consumer = models.ContainerConsumerMetadatum(
            container.id, project.id, {'name': 'name', 'URL': 'www.foo.com'})
        consumer.save(session=session)

        # Commit things so far, because the 'create_from' call below will
        # handle consumer metadata with same composite key items already
        # existing, and then rollback this session's transaction, which would
        # remove the items added above and result in a not-found error below.
        session.commit()

        # Create a new entity with the same composite key as the first one.
        consumer2 = models.ContainerConsumerMetadatum(
            container.id, project.id, {'name': 'name', 'URL': 'www.foo.com'})

        exception_result = self.assertRaises(
            exception.ConstraintCheck,
            self.repo.create_from,
            consumer2,
            session=session)
        self.assertIn(
            "SQL constraint check failed",
            six.text_type(exception_result))
Beispiel #2
0
    def test_should_update_with_duplicate_consumer(self):
        session = self.repo.get_session()

        project = models.Project()
        project.external_id = "my keystone id"
        project.save(session=session)

        container = models.Container()
        container.project_id = project.id
        container.save(session=session)

        # Create a consumer.
        consumer = models.ContainerConsumerMetadatum(
            container.id, project.id, {'name': 'name', 'URL': 'www.foo.com'})
        consumer.save(session=session)

        # Commit things so far, because the 'create_or_update_from' call below
        # will handle consumer metadata with same composite key items already
        # existing, and then rollback this session's transaction, which would
        # remove the items added above and result in a not-found error below.
        session.commit()

        # Try to create a consumer on the container...should re-use the
        # one added above.
        consumer2 = models.ContainerConsumerMetadatum(
            container.id, project.id, {'name': 'name', 'URL': 'www.foo.com'})
        self.repo.create_or_update_from(consumer2, container, session=session)

        container2 = self.repo_container.get(
            container.id, project.external_id, session=session)
        self.assertEqual(1, len(container2.consumers))
Beispiel #3
0
    def test_should_get_count_one_after_delete(self):
        session = self.repo.get_session()

        project = models.Project()
        project.external_id = "my keystone id"
        project.save(session=session)

        container = models.Container()
        container.project_id = project.id
        container.save(session=session)

        consumer = models.ContainerConsumerMetadatum(
            container.id, project.id, {'name': 'name1', 'URL': 'www.foo.com'})
        consumer.save(session=session)

        consumer = models.ContainerConsumerMetadatum(
            container.id, project.id, {'name': 'name2', 'URL': 'www.foo.com'})
        consumer.save(session=session)
        session.commit()

        count = self.repo.get_count(project.id, session=session)
        self.assertEqual(2, count)

        self.repo.delete_entity_by_id(consumer.id, "my keystone id",
                                      session=session)
        session.commit()

        count = self.repo.get_count(project.id, session=session)
        self.assertEqual(1, count)
Beispiel #4
0
 def test_new_consumer_has_correct_hash(self):
     consumer_one = models.ContainerConsumerMetadatum(
         self.container_id, self.parsed_consumer)
     consumer_two = models.ContainerConsumerMetadatum(
         self.container_id, self.parsed_consumer)
     different_container = '67890container'
     consumer_three = models.ContainerConsumerMetadatum(
         different_container, self.parsed_consumer)
     self.assertEqual(consumer_one.data_hash, consumer_two.data_hash)
     self.assertNotEqual(consumer_one.data_hash, consumer_three.data_hash)
Beispiel #5
0
def create_consumer(container_id, project_id, id_ref):
    """Generate a ContainerConsumerMetadatum entity instance."""
    data = {'name': 'test name', 'URL': 'http://test/url'}
    consumer = models.ContainerConsumerMetadatum(container_id, project_id,
                                                 data)
    consumer.id = id_ref
    return consumer
 def test_new_consumer_is_created_from_dict(self):
     consumer = models.ContainerConsumerMetadatum(self.container_id,
                                                  self.project_id,
                                                  self.parsed_consumer)
     self.assertEqual(self.parsed_consumer['name'], consumer.name)
     self.assertEqual(self.parsed_consumer['URL'], consumer.URL)
     self.assertEqual(models.States.ACTIVE, consumer.status)
Beispiel #7
0
    def on_post(self, external_project_id, **kwargs):

        project = res.get_or_create_project(external_project_id)
        data = api.load_body(pecan.request, validator=self.validator)
        LOG.debug('Start on_post...%s', data)

        try:
            container = self.container_repo.get(self.container_id,
                                                external_project_id)
        except exception.NotFound:
            controllers.containers.container_not_found()

        new_consumer = models.ContainerConsumerMetadatum(self.container_id,
                                                         data)
        new_consumer.project_id = project.id
        self.consumer_repo.create_or_update_from(new_consumer, container)

        url = hrefs.convert_consumer_to_href(new_consumer.container_id)
        pecan.response.headers['Location'] = url

        LOG.info(u._LI('Created a consumer for project: %s'),
                 external_project_id)

        return self._return_container_data(self.container_id,
                                           external_project_id)
Beispiel #8
0
def create_container_consumer_meta(container=None,
                                   parsed_request=None,
                                   session=None):
    if not parsed_request:
        parsed_request = {"name": "name", "URL": "URL"}
    container_consumer_meta = models.ContainerConsumerMetadatum(
        container_id=container.id,
        project_id=container.project_id,
        parsed_request=parsed_request,
    )
    cont_cons_meta_repo = repositories.get_container_consumer_repository()
    cont_cons_meta_repo.create_from(container_consumer_meta, session=session)
    return container_consumer_meta
Beispiel #9
0
    def on_post(self, external_project_id, **kwargs):

        project = res.get_or_create_project(external_project_id)
        data = api.load_body(pecan.request, validator=self.validator)
        LOG.debug('Start on_post...%s', data)

        self.quota_enforcer.enforce(project)

        new_consumer = models.ContainerConsumerMetadatum(
            self.container_id, project.id, data)
        self.consumer_repo.create_or_update_from(new_consumer, self.container)

        url = hrefs.convert_consumer_to_href(new_consumer.container_id)
        pecan.response.headers['Location'] = url

        LOG.info('Created a container consumer for project: %s',
                 external_project_id)

        return self._return_container_data()
Beispiel #10
0
    def on_post(self, external_project_id, **kwargs):

        project = res.get_or_create_project(external_project_id,
                                            self.project_repo)
        data = api.load_body(pecan.request, validator=self.validator)
        LOG.debug('Start on_post...%s', data)

        try:
            container = self.container_repo.get(self.container_id,
                                                external_project_id)
        except exception.NotFound:
            controllers.containers.container_not_found()

        new_consumer = models.ContainerConsumerMetadatum(
            self.container_id, data)
        new_consumer.project_id = project.id
        self.consumer_repo.create_or_update_from(new_consumer, container)

        pecan.response.headers['Location'] = (
            '/containers/{0}/consumers'.format(new_consumer.container_id))

        return self._return_container_data(self.container_id,
                                           external_project_id)