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

Update pubsub samples #1092

Merged
merged 1 commit into from
Aug 28, 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
14 changes: 9 additions & 5 deletions appengine/flexible/pubsub/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import os

from flask import current_app, Flask, render_template, request
from google.cloud import pubsub
from google.cloud import pubsub_v1


app = Flask(__name__)
Expand All @@ -30,6 +30,7 @@
app.config['PUBSUB_VERIFICATION_TOKEN'] = \
os.environ['PUBSUB_VERIFICATION_TOKEN']
app.config['PUBSUB_TOPIC'] = os.environ['PUBSUB_TOPIC']
app.config['PROJECT'] = os.environ['GCLOUD_PROJECT']


# Global list to storage messages received by this instance.
Expand All @@ -42,11 +43,14 @@ def index():
if request.method == 'GET':
return render_template('index.html', messages=MESSAGES)

ps = pubsub.Client()
topic = ps.topic(current_app.config['PUBSUB_TOPIC'])
data = request.form.get('payload', 'Example payload').encode('utf-8')

topic.publish(
request.form.get('payload', 'Example payload').encode('utf-8'))
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(
current_app.config['PROJECT'],
current_app.config['PUBSUB_TOPIC'])

publisher.publish(topic_path, data=data)

return 'OK', 200
# [END index]
Expand Down
2 changes: 1 addition & 1 deletion appengine/flexible/pubsub/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask==0.12.2
google-cloud-pubsub==0.27.0
google-cloud-pubsub==0.28.2
gunicorn==19.7.1
138 changes: 67 additions & 71 deletions pubsub/cloud-client/iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,122 +23,121 @@

import argparse

from google.cloud import pubsub
from google.cloud import pubsub_v1


def get_topic_policy(topic_name):
def get_topic_policy(project, topic_name):
"""Prints the IAM policy for the given topic."""
pubsub_client = pubsub.Client()
topic = pubsub_client.topic(topic_name)
client = pubsub_v1.PublisherClient()
topic_path = client.topic_path(project, topic_name)

policy = topic.get_iam_policy()
policy = client.get_iam_policy(topic_path)

print('Policy for topic {}:'.format(topic.name))
print('Version: {}'.format(policy.version))
print('Owners: {}'.format(policy.owners))
print('Editors: {}'.format(policy.editors))
print('Viewers: {}'.format(policy.viewers))
print('Publishers: {}'.format(policy.publishers))
print('Subscribers: {}'.format(policy.subscribers))
print('Policy for topic {}:'.format(topic_path))
for binding in policy.bindings:
print('Role: {}, Members: {}'.format(binding.role, binding.members))


def get_subscription_policy(topic_name, subscription_name):
def get_subscription_policy(project, subscription_name):
"""Prints the IAM policy for the given subscription."""
pubsub_client = pubsub.Client()
topic = pubsub_client.topic(topic_name)
subscription = topic.subscription(subscription_name)
client = pubsub_v1.SubscriberClient()
subscription_path = client.subscription_path(project, subscription_name)

policy = subscription.get_iam_policy()
policy = client.get_iam_policy(subscription_path)

print('Policy for subscription {} on topic {}:'.format(
subscription.name, topic.name))
print('Version: {}'.format(policy.version))
print('Owners: {}'.format(policy.owners))
print('Editors: {}'.format(policy.editors))
print('Viewers: {}'.format(policy.viewers))
print('Publishers: {}'.format(policy.publishers))
print('Subscribers: {}'.format(policy.subscribers))
print('Policy for subscription {}:'.format(subscription_path))
for binding in policy.bindings:
print('Role: {}, Members: {}'.format(binding.role, binding.members))


def set_topic_policy(topic_name):
def set_topic_policy(project, topic_name):
"""Sets the IAM policy for a topic."""
pubsub_client = pubsub.Client()
topic = pubsub_client.topic(topic_name)
policy = topic.get_iam_policy()
client = pubsub_v1.PublisherClient()
topic_path = client.topic_path(project, topic_name)

policy = client.get_iam_policy(topic_path)

# Add all users as viewers.
policy['roles/pubsub.viewer'] = [policy.all_users()]
# Add a group as publisherss.
publishers = policy.get('roles/pubsub.publisher', [])
publishers.add(policy.group('cloud-logs@google.com'))
policy['roles/pubsub.publisher'] = publishers
policy.bindings.add(
role='roles/pubsub.viewer',
members=['allUsers'])

# Add a group as a publisher.
policy.bindings.add(
role='roles/pubsub.publisher',
members=['group:cloud-logs@google.com'])

# Set the policy
topic.set_iam_policy(policy)
policy = client.set_iam_policy(topic_path, policy)

print('IAM policy for topic {} set.'.format(topic.name))
print('IAM policy for topic {} set: {}'.format(
topic_name, policy))


def set_subscription_policy(topic_name, subscription_name):
def set_subscription_policy(project, subscription_name):
"""Sets the IAM policy for a topic."""
pubsub_client = pubsub.Client()
topic = pubsub_client.topic(topic_name)
subscription = topic.subscription(subscription_name)
policy = subscription.get_iam_policy()
client = pubsub_v1.SubscriberClient()
subscription_path = client.subscription_path(project, subscription_name)

policy = client.get_iam_policy(subscription_path)

# Add all users as viewers.
policy['roles/viewer'] = [policy.all_users()]
# # Add a group as editors.
editors = policy.get('roles/editor', [])
editors.add(policy.group('cloud-logs@google.com'))
policy['roles/editor'] = editors
policy.bindings.add(
role='roles/pubsub.viewer',
members=['allUsers'])

# Add a group as an editor.
policy.bindings.add(
role='roles/editor',
members=['group:cloud-logs@google.com'])

# Set the policy
subscription.set_iam_policy(policy)
policy = client.set_iam_policy(subscription_path, policy)

print('IAM policy for subscription {} on topic {} set.'.format(
topic.name, subscription.name))
print('IAM policy for subscription {} set: {}'.format(
subscription_name, policy))


def check_topic_permissions(topic_name):
def check_topic_permissions(project, topic_name):
"""Checks to which permissions are available on the given topic."""
pubsub_client = pubsub.Client()
topic = pubsub_client.topic(topic_name)
client = pubsub_v1.PublisherClient()
topic_path = client.topic_path(project, topic_name)

permissions_to_check = [
'pubsub.topics.publish',
'pubsub.topics.update'
]

allowed_permissions = topic.check_iam_permissions(permissions_to_check)
allowed_permissions = client.test_iam_permissions(
topic_path, permissions_to_check)

print('Allowed permissions for topic {}: {}'.format(
topic.name, allowed_permissions))
topic_path, allowed_permissions))


def check_subscription_permissions(topic_name, subscription_name):
def check_subscription_permissions(project, subscription_name):
"""Checks to which permissions are available on the given subscription."""
pubsub_client = pubsub.Client()
topic = pubsub_client.topic(topic_name)
subscription = topic.subscription(subscription_name)
client = pubsub_v1.SubscriberClient()
subscription_path = client.subscription_path(project, subscription_name)

permissions_to_check = [
'pubsub.subscriptions.consume',
'pubsub.subscriptions.update'
]

allowed_permissions = subscription.check_iam_permissions(
permissions_to_check)
allowed_permissions = client.test_iam_permissions(
subscription_path, permissions_to_check)

print('Allowed permissions for subscription {} on topic {}: {}'.format(
subscription.name, topic.name, allowed_permissions))
print('Allowed permissions for subscription {}: {}'.format(
subscription_path, allowed_permissions))


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument('project', help='Your Google Cloud project ID')

subparsers = parser.add_subparsers(dest='command')

Expand All @@ -148,7 +147,6 @@ def check_subscription_permissions(topic_name, subscription_name):

get_subscription_policy_parser = subparsers.add_parser(
'get-subscription-policy', help=get_subscription_policy.__doc__)
get_subscription_policy_parser.add_argument('topic_name')
get_subscription_policy_parser.add_argument('subscription_name')

set_topic_policy_parser = subparsers.add_parser(
Expand All @@ -157,7 +155,6 @@ def check_subscription_permissions(topic_name, subscription_name):

set_subscription_policy_parser = subparsers.add_parser(
'set-subscription-policy', help=set_subscription_policy.__doc__)
set_subscription_policy_parser.add_argument('topic_name')
set_subscription_policy_parser.add_argument('subscription_name')

check_topic_permissions_parser = subparsers.add_parser(
Expand All @@ -167,20 +164,19 @@ def check_subscription_permissions(topic_name, subscription_name):
check_subscription_permissions_parser = subparsers.add_parser(
'check-subscription-permissions',
help=check_subscription_permissions.__doc__)
check_subscription_permissions_parser.add_argument('topic_name')
check_subscription_permissions_parser.add_argument('subscription_name')

args = parser.parse_args()

if args.command == 'get-topic-policy':
get_topic_policy(args.topic_name)
get_topic_policy(args.project, args.topic_name)
elif args.command == 'get-subscription-policy':
get_subscription_policy(args.topic_name, args.subscription_name)
get_subscription_policy(args.project, args.subscription_name)
elif args.command == 'set-topic-policy':
set_topic_policy(args.topic_name)
set_topic_policy(args.project, args.topic_name)
elif args.command == 'set-subscription-policy':
set_subscription_policy(args.topic_name, args.subscription_name)
set_subscription_policy(args.project, args.subscription_name)
elif args.command == 'check-topic-permissions':
check_topic_permissions(args.topic_name)
check_topic_permissions(args.project, args.topic_name)
elif args.command == 'check-subscription-permissions':
check_subscription_permissions(args.topic_name, args.subscription_name)
check_subscription_permissions(args.project, args.subscription_name)
Loading