Skip to content

Commit

Permalink
Add Snippet for Listing All Subscriptions in a Project (#1169)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelawyu authored and Jon Wayne Parrott committed Oct 31, 2017
1 parent e5c0ff2 commit a86a6b8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
26 changes: 20 additions & 6 deletions pubsub/cloud-client/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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__)
Expand All @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions pubsub/cloud-client/subscriber_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a86a6b8

Please sign in to comment.