示例#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_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'])
示例#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 in args.topic:
            topic_name = topic
            topic = msgs.Topic(name=util.TopicFormat(topic_name))

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

            result = util.TopicDisplayDict(result, failed)
            log.CreatedResource(topic_name, kind='topic', failed=failed)
            yield result
示例#3
0
def _Run(args, enable_labels=False, legacy_output=False):
    """Creates one or more topics."""
    client = topics.TopicsClient()

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

    failed = []
    for topic_ref in args.CONCEPTS.topic.Parse():

        try:
            result = client.Create(topic_ref, labels=labels)
        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')
示例#4
0
def _Run(args, legacy_output=False):
    """Deletes one or more topics."""
    client = topics.TopicsClient()

    failed = []
    for topic_ref in args.CONCEPTS.topic.Parse():

        try:
            result = client.Delete(topic_ref)
        except api_ex.HttpError as error:
            exc = exceptions.HttpException(error)
            log.DeletedResource(topic_ref.RelativeName(),
                                kind='topic',
                                failed=exc.payload.status_message)
            failed.append(topic_ref.topicsId)
            continue

        topic = client.messages.Topic(name=topic_ref.RelativeName())
        if legacy_output:
            result = util.TopicDisplayDict(topic)
        log.DeletedResource(topic_ref.RelativeName(), kind='topic')
        yield result

    if failed:
        raise util.RequestsFailedError(failed, 'delete')
示例#5
0
def _Run(args,
         enable_labels=False,
         legacy_output=False,
         enable_kms=False,
         enable_geofencing=False):
    """Creates one or more topics."""
    client = topics.TopicsClient()

    labels = None
    if enable_labels:
        labels = labels_util.ParseCreateArgs(args,
                                             client.messages.Topic.LabelsValue)
    kms_key = None
    if enable_kms:
        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.')

    message_storage_policy_allowed_regions = None
    if enable_geofencing:
        message_storage_policy_allowed_regions = args.message_storage_policy_allowed_regions

    failed = []
    for topic_ref in args.CONCEPTS.topic.Parse():

        try:
            result = client.Create(topic_ref,
                                   labels=labels,
                                   kms_key=kms_key,
                                   message_storage_policy_allowed_regions=
                                   message_storage_policy_allowed_regions)
        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')
示例#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:
      util.RequestFailedError: if any of the requests to the API failed.
    """
        client = topics.TopicsClient()

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

        failed = []
        for topic_name in args.topic:
            topic_ref = util.ParseTopic(topic_name)

            try:
                result = client.Create(topic_ref, labels=labels)
            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_name)
                continue

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

        if failed:
            raise util.RequestsFailedError(failed, 'create')
示例#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')