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])
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'] try: list_subscriptions_result = pubsub.projects_topics_subscriptions.List( msgs.PubsubProjectsTopicsSubscriptionsListRequest( topic=util.TopicFormat(args.topic), pageSize=min(args.max_results, MAX_SUBSCRIPTIONS_RESULTS))) for subscription in list_subscriptions_result.subscriptions: if util.SubscriptionMatches(subscription, args.name_filter): yield subscription if list_subscriptions_result.nextPageToken: log.err.Print( 'More subscriptions exist, but the result was truncated.') except re.error as e: raise sdk_ex.HttpException(str(e))
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
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
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)))