From d965d9a9eb5c98779b958969993f0ea5518637de Mon Sep 17 00:00:00 2001 From: michaelawyu Date: Tue, 31 Oct 2017 10:44:51 -0700 Subject: [PATCH] Add Snippet for Listing All Subscriptions in a Project [(#1169)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1169) --- samples/snippets/subscriber.py | 26 ++++++++++++++++++++------ samples/snippets/subscriber_test.py | 12 ++++++++++-- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/samples/snippets/subscriber.py b/samples/snippets/subscriber.py index 401faaab3..9dded25b1 100644 --- a/samples/snippets/subscriber.py +++ b/samples/snippets/subscriber.py @@ -27,7 +27,7 @@ from google.cloud import pubsub_v1 -def list_subscriptions(project, topic_name): +def list_subscriptions_in_topic(project, topic_name): """Lists all subscriptions for a given topic.""" subscriber = pubsub_v1.SubscriberClient() topic_path = subscriber.topic_path(project, topic_name) @@ -36,6 +36,15 @@ def list_subscriptions(project, topic_name): print(subscription.name) +def list_subscriptions_in_project(project): + """Lists all subscriptions in the current project.""" + subscriber = pubsub_v1.SubscriberClient() + project_path = subscriber.project_path(project) + + for subscription in subscriber.list_subscriptions(project_path): + print(subscription.name) + + def create_subscription(project, topic_name, subscription_name): """Create a new pull subscription on the given topic.""" subscriber = pubsub_v1.SubscriberClient() @@ -109,9 +118,12 @@ def callback(message): parser.add_argument('project', help='Your Google Cloud project ID') subparsers = parser.add_subparsers(dest='command') - list_parser = subparsers.add_parser( - 'list', help=list_subscriptions.__doc__) - list_parser.add_argument('topic_name') + list_in_topic_parser = subparsers.add_parser( + 'list_in_topic', help=list_subscriptions_in_topic.__doc__) + list_in_topic_parser.add_argument('topic_name') + + list_in_project_parser = subparsers.add_parser( + 'list_in_project', help=list_subscriptions_in_project.__doc__) create_parser = subparsers.add_parser( 'create', help=create_subscription.__doc__) @@ -133,8 +145,10 @@ def callback(message): args = parser.parse_args() - if args.command == 'list': - list_subscriptions(args.project, args.topic_name) + if args.command == 'list_in_topic': + list_subscriptions_in_topic(args.project, args.topic_name) + elif args.command == 'list_in_project': + list_subscriptions_in_project(args.project) elif args.command == 'create': create_subscription( args.project, args.topic_name, args.subscription_name) diff --git a/samples/snippets/subscriber_test.py b/samples/snippets/subscriber_test.py index 2cc955d9d..8b5e97ac4 100644 --- a/samples/snippets/subscriber_test.py +++ b/samples/snippets/subscriber_test.py @@ -66,10 +66,18 @@ def subscription(subscriber_client, topic): yield subscription_path -def test_list(subscription, capsys): +def test_list_in_topic(subscription, capsys): @eventually_consistent.call def _(): - subscriber.list_subscriptions(PROJECT, TOPIC) + subscriber.list_subscriptions_in_topic(PROJECT, TOPIC) + out, _ = capsys.readouterr() + assert subscription in out + + +def test_list_in_project(subscription, capsys): + @eventually_consistent.call + def _(): + subscriber.list_subscriptions_in_project(PROJECT) out, _ = capsys.readouterr() assert subscription in out