8000 Updates pubsub client (#1195) · johnmanong/python-docs-samples@ea085c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit ea085c9

Browse files
gguussJon Wayne Parrott
authored andcommitted
Updates pubsub client (GoogleCloudPlatform#1195)
* Updates pubsub client * Fixes lint error.
1 parent c9cce53 commit ea085c9

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

iot/api-client/manager/manager.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,19 @@
4242
from googleapiclient.errors import HttpError
4343

4444

45-
def create_iot_topic(topic_name):
45+
def create_iot_topic(project, topic_name):
4646
"""Creates a PubSub Topic and grants access to Cloud IoT Core."""
47-
pubsub_client = pubsub.Client()
48-
topic = pubsub_client.topic(topic_name)
49-
topic.create()
50-
51-
topic = pubsub_client.topic(topic_name)
52-
policy = topic.get_iam_policy()
53-
publishers = policy.get('roles/pubsub.publisher', [])
54-
if hasattr(publishers, "append"):
55-
publishers.append(policy.service_account(
56-
'cloud-iot@system.gserviceaccount.com'))
57-
else:
58-
publishers.add(policy.service_account(
59-
'cloud-iot@system.gserviceaccount.com'))
60-
policy['roles/pubsub.publisher'] = publishers
61-
topic.set_iam_policy(policy)
47+
pubsub_client = pubsub.PublisherClient()
48+
topic_path = pubsub_client.topic_path(project, topic_name)
49+
50+
topic = pubsub_client.create_topic(topic_path)
51+
policy = pubsub_client.get_iam_policy(topic_path)
52+
53+
policy.bindings.add(
54+
role='roles/pubsub.publisher',
55+
members=['serviceAccount:cloud-iot@system.gserviceaccount.com'])
56+
57+
pubsub_client.set_iam_policy(topic_path, policy)
6258

6359
return topic
6460

@@ -476,7 +472,7 @@ def run_command(args):
476472
args.cloud_region, args.pubsub_topic, args.registry_id)
477473

478474
elif args.command == 'create-topic':
479-
create_iot_topic(args.pubsub_topic)
475+
create_iot_topic(args.project_id, args.pubsub_topic)
480476

481477
elif args.command == 'delete-device':
482478
delete_device(

iot/api-client/manager/manager_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
import os
1616
import time
1717

18+
from google.cloud import pubsub
1819
import pytest
19-
2020
import manager
2121

22-
2322
cloud_region = 'us-central1'
2423
device_id_template = 'test-device-{}'
2524
es_cert_path = 'resources/ec_public.pem'
@@ -35,12 +34,13 @@
3534

3635
@pytest.fixture(scope='module')
3736
def test_topic():
38-
topic = manager.create_iot_topic(topic_id)
37+
topic = manager.create_iot_topic(project_id, topic_id)
3938

4039
yield topic
4140

42-
if topic.exists():
43-
topic.delete()
41+
pubsub_client = pubsub.PublisherClient()
42+
topic_path = pubsub_client.topic_path(project_id, topic_id)
43+
pubsub_client.delete_topic(topic_path)
4444

4545

4646
def test_create_delete_registry(test_topic, capsys):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
google-api-python-client==1.6.4
22
google-auth-httplib2==0.0.2
33
google-auth==1.2.0
4-
google-cloud==0.28.0
4+
google-cloud==0.29.0

0 commit comments

Comments
 (0)
0