Exemplo n.º 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.subscriptions'.

    Raises:
      util.RequestFailedError: if any of the requests to the API failed.
    """
        client = subscriptions.SubscriptionsClient()

        topic_ref = util.ParseTopic(args.topic, args.topic_project)
        push_config = util.ParsePushConfig(args.push_endpoint)
        retain_acked_messages = getattr(args, 'retain_acked_messages', None)
        retention_duration = getattr(args, 'message_retention_duration', None)
        if retention_duration:
            retention_duration = util.FormatDuration(retention_duration)

        labels = labels_util.UpdateLabels(
            None,
            client.messages.Subscription.LabelsValue,
            update_labels=labels_util.GetUpdateLabelsDictFromArgs(args))

        failed = []
        for subscription_name in args.subscription:
            subscription_ref = util.ParseSubscription(subscription_name)

            try:
                result = client.Create(subscription_ref,
                                       topic_ref,
                                       args.ack_deadline,
                                       push_config,
                                       retain_acked_messages,
                                       retention_duration,
                                       labels=labels)
            except api_ex.HttpError as error:
                exc = exceptions.HttpException(error)
                log.CreatedResource(subscription_ref.RelativeName(),
                                    kind='subscription',
                                    failed=exc.payload.status_message)
                failed.append(subscription_name)
                continue

            result = util.SubscriptionDisplayDict(result)
            log.CreatedResource(subscription_ref.RelativeName(),
                                kind='subscription')

            yield result

        if failed:
            raise util.RequestsFailedError(failed, 'create')
Exemplo n.º 2
0
def _Run(args, enable_labels=False, legacy_output=False):
    """Creates one or more subscriptions."""
    client = subscriptions.SubscriptionsClient()

    topic_ref = args.CONCEPTS.topic.Parse()
    push_config = util.ParsePushConfig(args)
    retain_acked_messages = getattr(args, 'retain_acked_messages', None)
    retention_duration = getattr(args, 'message_retention_duration', None)
    if retention_duration:
        retention_duration = util.FormatDuration(retention_duration)

    no_expiration = False
    expiration_period = getattr(args, 'expiration_period', None)
    if expiration_period:
        if expiration_period == subscriptions.NEVER_EXPIRATION_PERIOD_VALUE:
            no_expiration = True
            expiration_period = None

    labels = None
    if enable_labels:
        labels = labels_util.ParseCreateArgs(
            args, client.messages.Subscription.LabelsValue)

    failed = []
    for subscription_ref in args.CONCEPTS.subscription.Parse():

        try:
            result = client.Create(subscription_ref,
                                   topic_ref,
                                   args.ack_deadline,
                                   push_config,
                                   retain_acked_messages,
                                   retention_duration,
                                   labels=labels,
                                   no_expiration=no_expiration,
                                   expiration_period=expiration_period)
        except api_ex.HttpError as error:
            exc = exceptions.HttpException(error)
            log.CreatedResource(subscription_ref.RelativeName(),
                                kind='subscription',
                                failed=exc.payload.status_message)
            failed.append(subscription_ref.subscriptionsId)
            continue

        if legacy_output:
            result = util.SubscriptionDisplayDict(result)

        log.CreatedResource(subscription_ref.RelativeName(),
                            kind='subscription')
        yield result

    if failed:
        raise util.RequestsFailedError(failed, 'create')
Exemplo n.º 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:
      A serialized object (dict) describing the results of the operation.

    Raises:
      An HttpException if there was a problem calling the
      API topics.Patch command.
    """
        client = topics.TopicsClient()
        topic_ref = args.CONCEPTS.topic.Parse()

        message_retention_duration = getattr(args,
                                             'message_retention_duration',
                                             None)
        if message_retention_duration:
            message_retention_duration = util.FormatDuration(
                message_retention_duration)
        clear_message_retention_duration = getattr(
            args, 'clear_message_retention_duration', None)

        labels_update = labels_util.ProcessUpdateArgsLazy(
            args,
            client.messages.Topic.LabelsValue,
            orig_labels_thunk=lambda: client.Get(topic_ref).labels)

        result = None
        try:
            result = client.Patch(topic_ref, labels_update.GetOrNone(),
                                  _GetKmsKeyNameFromArgs(args),
                                  message_retention_duration,
                                  clear_message_retention_duration,
                                  args.recompute_message_storage_policy,
                                  args.message_storage_policy_allowed_regions)
        except topics.NoFieldsSpecifiedError:
            operations = [
                'clear_labels', 'update_labels', 'remove_labels',
                'recompute_message_storage_policy',
                'message_storage_policy_allowed_regions'
            ]
            if not any(args.IsSpecified(arg) for arg in operations):
                raise
            log.status.Print('No update to perform.')
        else:
            log.UpdatedResource(topic_ref.RelativeName(), kind='topic')
        return result
Exemplo n.º 4
0
def ParseExpirationPeriodWithNeverSentinel(value):
  if value == subscriptions.NEVER_EXPIRATION_PERIOD_VALUE:
    return value
  return util.FormatDuration(arg_parsers.Duration()(value))
Exemplo n.º 5
0
def ParseRetentionDurationWithDefault(value):
  if value == subscriptions.DEFAULT_MESSAGE_RETENTION_VALUE:
    return value
  return util.FormatDuration(arg_parsers.Duration()(value))
Exemplo n.º 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:
      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.Patch command.
    """
    flags.ValidateDeadLetterPolicy(args)

    client = subscriptions.SubscriptionsClient()
    subscription_ref = args.CONCEPTS.subscription.Parse()
    dead_letter_topic = getattr(args, 'dead_letter_topic', None)
    max_delivery_attempts = getattr(args, 'max_delivery_attempts', None)
    clear_dead_letter_policy = getattr(args, 'clear_dead_letter_policy', None)
    clear_retry_policy = getattr(args, 'clear_retry_policy', None)

    labels_update = labels_util.ProcessUpdateArgsLazy(
        args, client.messages.Subscription.LabelsValue,
        orig_labels_thunk=lambda: client.Get(subscription_ref).labels)

    no_expiration = False
    expiration_period = getattr(args, 'expiration_period', None)
    if expiration_period:
      if expiration_period == subscriptions.NEVER_EXPIRATION_PERIOD_VALUE:
        no_expiration = True
        expiration_period = None

    if dead_letter_topic:
      dead_letter_topic = args.CONCEPTS.dead_letter_topic.Parse().RelativeName()

    min_retry_delay = getattr(args, 'min_retry_delay', None)
    if min_retry_delay:
      min_retry_delay = util.FormatDuration(min_retry_delay)
    max_retry_delay = getattr(args, 'max_retry_delay', None)
    if max_retry_delay:
      max_retry_delay = util.FormatDuration(max_retry_delay)

    try:
      result = client.Patch(
          subscription_ref,
          ack_deadline=args.ack_deadline,
          push_config=util.ParsePushConfig(args),
          retain_acked_messages=args.retain_acked_messages,
          labels=labels_update.GetOrNone(),
          message_retention_duration=args.message_retention_duration,
          no_expiration=no_expiration,
          expiration_period=expiration_period,
          dead_letter_topic=dead_letter_topic,
          max_delivery_attempts=max_delivery_attempts,
          clear_dead_letter_policy=clear_dead_letter_policy,
          clear_retry_policy=clear_retry_policy,
          min_retry_delay=min_retry_delay,
          max_retry_delay=max_retry_delay)
    except subscriptions.NoFieldsSpecifiedError:
      if not any(args.IsSpecified(arg) for arg in ('clear_labels',
                                                   'update_labels',
                                                   'remove_labels')):
        raise
      log.status.Print('No update to perform.')
      result = None
    else:
      log.UpdatedResource(subscription_ref.RelativeName(), kind='subscription')
    return result
Exemplo n.º 7
0
def _Run(args, legacy_output=False):
    """Creates one or more topics."""
    client = topics.TopicsClient()

    labels = labels_util.ParseCreateArgs(args,
                                         client.messages.Topic.LabelsValue)

    kms_key = None
    kms_ref = args.CONCEPTS.kms_key.Parse()
    if kms_ref:
        kms_key = kms_ref.RelativeName()
    else:
        # Did user supply any topic-encryption-key flags?
        for keyword in [
                'topic-encryption-key', 'topic-encryption-key-project',
                'topic-encryption-key-location', 'topic-encryption-key-keyring'
        ]:
            if args.IsSpecified(keyword.replace('-', '_')):
                raise core_exceptions.Error(
                    '--topic-encryption-key was not fully specified.')

    retention_duration = getattr(args, 'message_retention_duration', None)
    if retention_duration:
        retention_duration = util.FormatDuration(retention_duration)

    message_storage_policy_allowed_regions = args.message_storage_policy_allowed_regions

    schema = getattr(args, 'schema', None)
    if schema:
        schema = args.CONCEPTS.schema.Parse().RelativeName()
    message_encoding_list = getattr(args, 'message_encoding', None)
    message_encoding = None
    if message_encoding_list:
        message_encoding = message_encoding_list[0]

    failed = []
    for topic_ref in args.CONCEPTS.topic.Parse():
        try:
            result = client.Create(
                topic_ref,
                labels=labels,
                kms_key=kms_key,
                message_retention_duration=retention_duration,
                message_storage_policy_allowed_regions=
                message_storage_policy_allowed_regions,
                schema=schema,
                message_encoding=message_encoding)
        except api_ex.HttpError as error:
            exc = exceptions.HttpException(error)
            log.CreatedResource(topic_ref.RelativeName(),
                                kind='topic',
                                failed=exc.payload.status_message)
            failed.append(topic_ref.topicsId)
            continue

        if legacy_output:
            result = util.TopicDisplayDict(result)
        log.CreatedResource(topic_ref.RelativeName(), kind='topic')
        yield result

    if failed:
        raise util.RequestsFailedError(failed, 'create')
Exemplo n.º 8
0
def _Run(args, enable_labels=False, legacy_output=False):
  """Creates one or more subscriptions."""
  flags.ValidateDeadLetterPolicy(args)

  client = subscriptions.SubscriptionsClient()

  topic_ref = args.CONCEPTS.topic.Parse()
  push_config = util.ParsePushConfig(args)
  enable_message_ordering = getattr(args, 'enable_message_ordering', None)
  filter_string = getattr(args, 'message_filter', None)
  dead_letter_topic = getattr(args, 'dead_letter_topic', None)
  max_delivery_attempts = getattr(args, 'max_delivery_attempts', None)
  retain_acked_messages = getattr(args, 'retain_acked_messages', None)
  retention_duration = getattr(args, 'message_retention_duration', None)
  if retention_duration:
    retention_duration = util.FormatDuration(retention_duration)
  min_retry_delay = getattr(args, 'min_retry_delay', None)
  if min_retry_delay:
    min_retry_delay = util.FormatDuration(min_retry_delay)
  max_retry_delay = getattr(args, 'max_retry_delay', None)
  if max_retry_delay:
    max_retry_delay = util.FormatDuration(max_retry_delay)

  no_expiration = False
  expiration_period = getattr(args, 'expiration_period', None)
  if expiration_period:
    if expiration_period == subscriptions.NEVER_EXPIRATION_PERIOD_VALUE:
      no_expiration = True
      expiration_period = None

  if dead_letter_topic:
    dead_letter_topic = args.CONCEPTS.dead_letter_topic.Parse().RelativeName()

  labels = None
  if enable_labels:
    labels = labels_util.ParseCreateArgs(
        args, client.messages.Subscription.LabelsValue)

  failed = []
  for subscription_ref in args.CONCEPTS.subscription.Parse():

    try:
      result = client.Create(
          subscription_ref,
          topic_ref,
          args.ack_deadline,
          push_config,
          retain_acked_messages,
          retention_duration,
          labels=labels,
          no_expiration=no_expiration,
          expiration_period=expiration_period,
          enable_message_ordering=enable_message_ordering,
          filter_string=filter_string,
          dead_letter_topic=dead_letter_topic,
          max_delivery_attempts=max_delivery_attempts,
          min_retry_delay=min_retry_delay,
          max_retry_delay=max_retry_delay)
    except api_ex.HttpError as error:
      exc = exceptions.HttpException(error)
      log.CreatedResource(subscription_ref.RelativeName(),
                          kind='subscription',
                          failed=exc.payload.status_message)
      failed.append(subscription_ref.subscriptionsId)
      continue

    if legacy_output:
      result = util.SubscriptionDisplayDict(result)

    log.CreatedResource(subscription_ref.RelativeName(), kind='subscription')
    yield result

  if failed:
    raise util.RequestsFailedError(failed, 'create')