Exemple #1
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:
      A serialized object (dict) describing the results of the operation.
      This description fits the Resource described in the ResourceRegistry under
      'pubsub.projects.topics'.
    """
        msgs = self.context['pubsub_msgs']
        pubsub = self.context['pubsub']

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

            try:
                pubsub.projects_topics.Delete(delete_req)
                failed = None
            except api_ex.HttpError as error:
                exc = exceptions.HttpException(error)
                failed = exc.payload.status_message

            result = util.TopicDisplayDict(topic, failed)
            log.DeletedResource(topic.name, kind='topic', failed=failed)
            yield result
Exemple #2
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:
      A serialized object (dict) describing the results of the operation.
      This description fits the Resource described in the ResourceRegistry under
      'pubsub.projects.topics'.
    """
    msgs = self.context['pubsub_msgs']
    pubsub = self.context['pubsub']

    for topic_name in args.topic:
      topic_name = resources.REGISTRY.Parse(
          topic_name, collection=self.Collection()).Name()
      topic = msgs.Topic(name=util.TopicFormat(topic_name))
      delete_req = msgs.PubsubProjectsTopicsDeleteRequest(
          topic=util.TopicFormat(topic.name))

      try:
        pubsub.projects_topics.Delete(delete_req)
        yield util.TopicDisplayDict(topic)
      except api_ex.HttpError as exc:
        yield util.TopicDisplayDict(topic,
                                    json.loads(exc.content)['error']['message'])
Exemple #3
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.'))

    topic_name = resources.REGISTRY.Parse(
        args.topic, collection=util.TOPICS_COLLECTION).Name()

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

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

    if not result.messageIds:
      # If we got a result with empty messageIds, then we've got a problem.
      raise sdk_ex.HttpException('Publish operation failed with Unknown error.')

    # We only allow to publish one message at a time, so do not return a
    # list of messageId.
    resource = resource_projector.MakeSerializable(result)
    resource['messageIds'] = result.messageIds[0]
    return resource
Exemple #4
0
def _Run(cmd, args, field_adder):
  """Common function to run the Create command.

  Args:
    cmd: a base.CreateCommand object
    args: an argparse namespace. All the arguments that were provided to this
      command invocation.
    field_adder: Function that populates additional fields in a subscription.

  Yields:
    A serialized object (dict) describing the results of the operation.
    This description fits the Resource described in the ResourceRegistry under
    'pubsub.projects.subscriptions'.

  Raises:
    An HttpException if there was a problem calling the
    API subscriptions.Create command.
  """
  msgs = cmd.context['pubsub_msgs']
  pubsub = cmd.context['pubsub']

  topic_project = ''
  if args.topic_project:
    topic_project = projects_util.ParseProject(args.topic_project).Name()
  topic_name = args.topic

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

    field_adder(subscription, args)

    # TODO(b/32275310): Conform to gcloud error handling guidelines.
    try:
      result = pubsub.projects_subscriptions.Create(subscription)
      failed = None
    except api_ex.HttpError as error:
      result = subscription
      exc = exceptions.HttpException(error)
      failed = exc.payload.status_message

    result = util.SubscriptionDisplayDict(result, failed)
    log.CreatedResource(name, kind='subscription', failed=failed)

    yield result
  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:
      A serialized object (dict) describing the results of the operation.
      This description fits the Resource described in the ResourceRegistry under
      'pubsub.projects.topics'.

    Raises:
      An HttpException if there was a problem calling the
      API subscriptions.Create command.
    """
    msgs = self.context['pubsub_msgs']
    pubsub = self.context['pubsub']

    topic_project = ''
    if args.topic_project:
      topic_project = projects_util.ParseProject(args.topic_project).Name()
    topic_name = args.topic

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

      try:
        result = pubsub.projects_subscriptions.Create(subscription)
        failed = None
      except api_ex.HttpError as error:
        result = subscription
        exc = exceptions.HttpException(error)
        failed = exc.payload.status_message

      result = util.SubscriptionDisplayDict(result, failed)
      log.CreatedResource(subscription_name, kind='subscription', failed=failed)
      yield result
Exemple #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.

    Yields:
      A serialized object (dict) describing the results of the operation.
      This description fits the Resource described in the ResourceRegistry under
      'pubsub.projects.topics'.

    Raises:
      An HttpException if there was a problem calling the
      API subscriptions.Create command.
    """
        msgs = self.context['pubsub_msgs']
        pubsub = self.context['pubsub']

        topic_project = ''
        if args.topic_project:
            topic_project = projects_util.ParseProject(
                args.topic_project).Name()
        topic_name = resources.REGISTRY.Parse(
            args.topic, collection=util.TOPICS_COLLECTION).Name()

        for subscription in args.subscription:
            subscription_name = resources.REGISTRY.Parse(
                subscription, collection=util.SUBSCRIPTIONS_COLLECTION).Name()
            create_req = msgs.Subscription(
                name=util.SubscriptionFormat(subscription_name),
                topic=util.TopicFormat(topic_name, topic_project),
                ackDeadlineSeconds=args.ack_deadline)
            if args.push_endpoint:
                create_req.pushConfig = msgs.PushConfig(
                    pushEndpoint=args.push_endpoint)

            try:
                yield SubscriptionDisplayDict(
                    pubsub.projects_subscriptions.Create(create_req))
            except api_ex.HttpError as exc:
                raise exceptions.HttpException(
                    json.loads(exc.content)['error']['message'])
Exemple #7
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()