def _Run(args, message_body, legacy_output=False): """Publishes a message to a topic.""" client = topics.TopicsClient() attributes = util.ParseAttributes(args.attribute, messages=client.messages) ordering_key = getattr(args, 'ordering_key', None) topic_ref = args.CONCEPTS.topic.Parse() result = client.Publish(topic_ref, http_encoding.Encode(message_body), attributes, ordering_key) if legacy_output: # We only allow to publish one message at a time, so do not return a # list of messageId. result = resource_projector.MakeSerializable(result) result['messageIds'] = result['messageIds'][0] return result
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.') 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')
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')
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() 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(), 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
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')
def Run(self, args): client = topics.TopicsClient() topic_ref = args.CONCEPTS.topic.Parse() return client.Get(topic_ref)
def Run(self, args): client = topics.TopicsClient() topic_ref = args.CONCEPTS.topic.Parse() return client.RemoveIamPolicyBinding(topic_ref, args.member, args.role)
def _Run(args, legacy_output=False): client = topics.TopicsClient() for topic in client.List(util.ParseProject(), page_size=args.page_size): if legacy_output: topic = util.ListTopicDisplayDict(topic) yield topic
def Run(self, args): client = topics.TopicsClient() topic_ref = args.CONCEPTS.topic.Parse() return client.ListSnapshots(topic_ref, page_size=args.page_size)
def Run(self, args): client = topics.TopicsClient() topic_ref = util.ParseTopic(args.topic) return client.RemoveIamPolicyBinding(topic_ref, args.member, args.role)
def SetUp(self): self.topics_client = topics.TopicsClient(self.client, self.msgs) self.topics_service = self.client.projects_topics
def Run(self, args): client = topics.TopicsClient() topic_ref = util.ParseTopic(args.topic) return client.GetIamPolicy(topic_ref)