Esempio n. 1
0
    def test_subscription_get_hit(self):
        from google.pubsub.v1.pubsub_pb2 import PushConfig
        from google.pubsub.v1.pubsub_pb2 import Subscription

        push_cfg_pb = PushConfig(push_endpoint=self.PUSH_ENDPOINT)
        sub_pb = Subscription(name=self.SUB_PATH,
                              topic=self.TOPIC_PATH,
                              push_config=push_cfg_pb)
        gax_api = _GAXSubscriberAPI(_get_subscription_response=sub_pb)
        client = _Client(self.PROJECT)
        api = self._makeOne(gax_api, client)

        resource = api.subscription_get(self.SUB_PATH)

        expected = {
            'name': self.SUB_PATH,
            'topic': self.TOPIC_PATH,
            'pushConfig': {
                'pushEndpoint': self.PUSH_ENDPOINT,
            },
        }
        self.assertEqual(resource, expected)
        sub_path, options = gax_api._get_subscription_called_with
        self.assertEqual(sub_path, self.SUB_PATH)
        self.assertIsNone(options)
Esempio n. 2
0
    def subscription_create(self,
                            subscription_path,
                            topic_path,
                            ack_deadline=None,
                            push_endpoint=None):
        """API call:  create a subscription

        See:
        https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/create

        :type subscription_path: str
        :param subscription_path:
            the fully-qualified path of the new subscription, in format
            ``projects/<PROJECT>/subscriptions/<SUB_NAME>``.

        :type topic_path: str
        :param topic_path: the fully-qualified path of the topic being
                           subscribed, in format
                           ``projects/<PROJECT>/topics/<TOPIC_NAME>``.

        :type ack_deadline: int
        :param ack_deadline:
            (Optional) the deadline (in seconds) by which messages pulled from
            the back-end must be acknowledged.

        :type push_endpoint: str
        :param push_endpoint:
            (Optional) URL to which messages will be pushed by the back-end.
            If not set, the application must pull messages.

        :rtype: dict
        :returns: ``Subscription`` resource returned from the API.
        """
        if push_endpoint is not None:
            push_config = PushConfig(push_endpoint=push_endpoint)
        else:
            push_config = None

        if ack_deadline is None:
            ack_deadline = 0

        try:
            sub_pb = self._gax_api.create_subscription(
                subscription_path,
                topic_path,
                push_config=push_config,
                ack_deadline_seconds=ack_deadline)
        except GaxError as exc:
            if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION:
                raise Conflict(topic_path)
            raise
        return MessageToDict(sub_pb)
Esempio n. 3
0
    def test_list_subscriptions_with_paging(self):
        from google.pubsub.v1.pubsub_pb2 import PushConfig
        from google.pubsub.v1.pubsub_pb2 import Subscription as SubscriptionPB
        from google.cloud._testing import _GAXPageIterator
        from google.cloud.pubsub.client import Client
        from google.cloud.pubsub.subscription import Subscription
        from google.cloud.pubsub.topic import Topic

        SIZE = 23
        TOKEN = 'TOKEN'
        NEW_TOKEN = 'NEW_TOKEN'
        push_cfg_pb = PushConfig(push_endpoint=self.PUSH_ENDPOINT)
        local_sub_path = '%s/subscriptions/%s' % (self.PROJECT_PATH,
                                                  self.SUB_NAME)
        sub_pb = SubscriptionPB(name=local_sub_path,
                                topic=self.TOPIC_PATH,
                                push_config=push_cfg_pb)
        response = _GAXPageIterator([sub_pb], page_token=NEW_TOKEN)
        gax_api = _GAXSubscriberAPI(_list_subscriptions_response=response)
        client = _Client(self.PROJECT)
        creds = _Credentials()
        client = Client(project=self.PROJECT, credentials=creds)
        api = self._makeOne(gax_api, client)

        iterator = api.list_subscriptions(self.PROJECT,
                                          page_size=SIZE,
                                          page_token=TOKEN)
        subscriptions = list(iterator)
        next_token = iterator.next_page_token

        # Check the token returned.
        self.assertEqual(next_token, NEW_TOKEN)
        # Check the subscription object returned.
        self.assertEqual(len(subscriptions), 1)
        subscription = subscriptions[0]
        self.assertIsInstance(subscription, Subscription)
        self.assertEqual(subscription.name, self.SUB_NAME)
        self.assertIsInstance(subscription.topic, Topic)
        self.assertEqual(subscription.topic.name, self.TOPIC_NAME)
        self.assertIs(subscription._client, client)
        self.assertEqual(subscription._project, self.PROJECT)
        self.assertIsNone(subscription.ack_deadline)
        self.assertEqual(subscription.push_endpoint, self.PUSH_ENDPOINT)

        name, page_size, options = gax_api._list_subscriptions_called_with
        self.assertEqual(name, self.PROJECT_PATH)
        self.assertEqual(page_size, 23)
        self.assertEqual(options.page_token, TOKEN)
Esempio n. 4
0
    def subscription_modify_push_config(self, subscription_path,
                                        push_endpoint):
        """API call:  update push config of a subscription

        See:
        https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/modifyPushConfig

        :type subscription_path: str
        :param subscription_path:
            the fully-qualified path of the new subscription, in format
            ``projects/<PROJECT>/subscriptions/<SUB_NAME>``.

        :type push_endpoint: str
        :param push_endpoint:
            (Optional) URL to which messages will be pushed by the back-end.
            If not set, the application must pull messages.
        """
        push_config = PushConfig(push_endpoint=push_endpoint)
        try:
            self._gax_api.modify_push_config(subscription_path, push_config)
        except GaxError as exc:
            if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
                raise NotFound(subscription_path)
            raise