コード例 #1
0
ファイル: delete.py プロジェクト: wsong/google-cloud-sdk
  def Run(self, args):
    """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      A 2-tuple of lists, one populated with the topic paths that were
      successfully deleted, the other one with the list of topic paths
      that could not be deleted.
    """
    msgs = self.context['pubsub_msgs']
    pubsub = self.context['pubsub']

    succeeded = []
    failed = []

    for topic_name in args.topic:
      delete_req = msgs.PubsubProjectsTopicsDeleteRequest(
          topic=util.TopicFormat(topic_name))

      try:
        pubsub.projects_topics.Delete(delete_req)
        succeeded.append(delete_req.topic)
      except api_ex.HttpError as exc:
        failed.append(
            (delete_req.topic, json.loads(exc.content)['error']['message']))

    return succeeded, failed
コード例 #2
0
ファイル: create.py プロジェクト: wsong/google-cloud-sdk
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      A 2-tuple of lists, one populated with the subscription paths that were
      successfully created, the other one with the list of subscription names
      that could not be created.
    """
        msgs = self.context['pubsub_msgs']
        pubsub = self.context['pubsub']

        succeeded = []
        failed = []

        for subscription in args.subscription:
            create_req = msgs.Subscription(
                name=util.SubscriptionFormat(subscription),
                topic=util.TopicFormat(args.topic, args.topic_project),
                ackDeadlineSeconds=args.ack_deadline)
            if args.push_endpoint:
                create_req.pushConfig = msgs.PushConfig(
                    pushEndpoint=args.push_endpoint)

            try:
                succeeded.append(
                    pubsub.projects_subscriptions.Create(create_req))
            except api_ex.HttpError as exc:
                failed.append((subscription,
                               json.loads(exc.content)['error']['message']))

        return succeeded, failed
コード例 #3
0
  def Display(self, args, result):
    """This method is called to print the result of the Run() method.

    Args:
      args: The arguments that command was run with.
      result: The value returned from the Run() method.
    """
    succeeded, failed = result

    subscription_type = 'pull'
    if args.push_endpoint:
      subscription_type = 'push'

    if succeeded:
      success_printer = io.ListPrinter(
          '{0} {1} subscription(s) created successfully'.format(
              len(succeeded), subscription_type))
      success_printer.Print([subscription.name for subscription in succeeded])

      log.out.Print('for topic "{0}"'.format(
          util.TopicFormat(args.topic, args.topic_project)))

      log.out.Print(
          'Acknowledgement deadline: {0} seconds'.format(args.ack_deadline))

      if args.push_endpoint:
        log.out.Print('Push endpoint: "{0}"'.format(args.push_endpoint))

    if failed:
      fail_printer = io.ListPrinter(
          '{0} subscription(s) failed'.format(len(failed)))
      fail_printer.Print(
          ['{0} (reason: {1})'.format(topic, desc) for topic, desc in failed])
コード例 #4
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Yields:
      Subscriptions paths that match the regular expression in args.name_filter.
    """
        msgs = self.context['pubsub_msgs']
        pubsub = self.context['pubsub']

        page_token = None
        subscriptions_listed = 0
        should_truncate_res = args.max_results and not args.name_filter

        try:
            while True:
                list_subscriptions_req = (
                    msgs.PubsubProjectsTopicsSubscriptionsListRequest(
                        topic=util.TopicFormat(args.topic),
                        pageToken=page_token))

                if should_truncate_res:
                    list_subscriptions_req.pageSize = min(
                        args.max_results, util.MAX_LIST_RESULTS)

                list_subscriptions_result = pubsub.projects_topics_subscriptions.List(
                    list_subscriptions_req)

                for subscription in list_subscriptions_result.subscriptions:
                    if not util.SubscriptionMatches(subscription,
                                                    args.name_filter):
                        continue

                    # If max_results > 0 and we have already sent that
                    # amount of subscriptions, just raise (StopIteration) iff name_filter
                    # is not set, else this limit wouldn't make sense.
                    if should_truncate_res and subscriptions_listed >= args.max_results:
                        raise StopIteration()

                    subscriptions_listed += 1
                    yield subscription

                page_token = list_subscriptions_result.nextPageToken
                if not page_token:
                    break

        except re.error as e:
            raise sdk_ex.HttpException(str(e))
コード例 #5
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Yields:
      Subscriptions paths that match the regular expression in args.name_filter.
    """
        msgs = self.context['pubsub_msgs']
        pubsub = self.context['pubsub']

        page_size = None
        page_token = None

        if args.page_size:
            page_size = min(args.page_size, util.MAX_LIST_RESULTS)

        if not args.filter and args.limit:
            page_size = min(args.limit, page_size or util.MAX_LIST_RESULTS)

        while True:
            list_subscriptions_req = (
                msgs.PubsubProjectsTopicsSubscriptionsListRequest(
                    topic=util.TopicFormat(args.topic),
                    pageSize=page_size,
                    pageToken=page_token))

            list_subscriptions_result = pubsub.projects_topics_subscriptions.List(
                list_subscriptions_req)

            for subscription in list_subscriptions_result.subscriptions:
                yield TopicSubscriptionDict(subscription)

            page_token = list_subscriptions_result.nextPageToken
            if not page_token:
                break

            yield resource_printer_base.PageMarker()
コード例 #6
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      PublishResponse with the response of the Publish operation.

    Raises:
      sdk_ex.HttpException: If attributes are malformed, or if none of
      MESSAGE_BODY or ATTRIBUTE are given.
    """
        msgs = self.context['pubsub_msgs']
        pubsub = self.context['pubsub']

        attributes = []
        if args.attribute:
            for key, value in sorted(args.attribute.iteritems()):
                attributes.append(
                    msgs.PubsubMessage.AttributesValue.AdditionalProperty(
                        key=key, value=value))

        if not args.message_body and not attributes:
            raise sdk_ex.HttpException(
                ('You cannot send an empty message.'
                 ' You must specify either a MESSAGE_BODY,'
                 ' one or more ATTRIBUTE, or both.'))

        message = msgs.PubsubMessage(
            data=args.message_body,
            attributes=msgs.PubsubMessage.AttributesValue(
                additionalProperties=attributes))

        return pubsub.projects_topics.Publish(
            msgs.PubsubProjectsTopicsPublishRequest(
                publishRequest=msgs.PublishRequest(messages=[message]),
                topic=util.TopicFormat(args.topic)))