Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Snippet for Listing All Subscriptions in a Project #1169

Merged
merged 1 commit into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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