diff --git a/samples/api-client/manager/manager.py b/samples/api-client/manager/manager.py index 8fb3adf3..96ab7f99 100644 --- a/samples/api-client/manager/manager.py +++ b/samples/api-client/manager/manager.py @@ -42,23 +42,19 @@ from googleapiclient.errors import HttpError -def create_iot_topic(topic_name): +def create_iot_topic(project, topic_name): """Creates a PubSub Topic and grants access to Cloud IoT Core.""" - pubsub_client = pubsub.Client() - topic = pubsub_client.topic(topic_name) - topic.create() - - topic = pubsub_client.topic(topic_name) - policy = topic.get_iam_policy() - publishers = policy.get('roles/pubsub.publisher', []) - if hasattr(publishers, "append"): - publishers.append(policy.service_account( - 'cloud-iot@system.gserviceaccount.com')) - else: - publishers.add(policy.service_account( - 'cloud-iot@system.gserviceaccount.com')) - policy['roles/pubsub.publisher'] = publishers - topic.set_iam_policy(policy) + pubsub_client = pubsub.PublisherClient() + topic_path = pubsub_client.topic_path(project, topic_name) + + topic = pubsub_client.create_topic(topic_path) + policy = pubsub_client.get_iam_policy(topic_path) + + policy.bindings.add( + role='roles/pubsub.publisher', + members=['serviceAccount:cloud-iot@system.gserviceaccount.com']) + + pubsub_client.set_iam_policy(topic_path, policy) return topic @@ -476,7 +472,7 @@ def run_command(args): args.cloud_region, args.pubsub_topic, args.registry_id) elif args.command == 'create-topic': - create_iot_topic(args.pubsub_topic) + create_iot_topic(args.project_id, args.pubsub_topic) elif args.command == 'delete-device': delete_device( diff --git a/samples/api-client/manager/manager_test.py b/samples/api-client/manager/manager_test.py index ddd16750..7565967c 100644 --- a/samples/api-client/manager/manager_test.py +++ b/samples/api-client/manager/manager_test.py @@ -15,11 +15,10 @@ import os import time +from google.cloud import pubsub import pytest - import manager - cloud_region = 'us-central1' device_id_template = 'test-device-{}' es_cert_path = 'resources/ec_public.pem' @@ -35,12 +34,13 @@ @pytest.fixture(scope='module') def test_topic(): - topic = manager.create_iot_topic(topic_id) + topic = manager.create_iot_topic(project_id, topic_id) yield topic - if topic.exists(): - topic.delete() + pubsub_client = pubsub.PublisherClient() + topic_path = pubsub_client.topic_path(project_id, topic_id) + pubsub_client.delete_topic(topic_path) def test_create_delete_registry(test_topic, capsys): diff --git a/samples/api-client/manager/requirements.txt b/samples/api-client/manager/requirements.txt index 17cb1475..7b604e1d 100644 --- a/samples/api-client/manager/requirements.txt +++ b/samples/api-client/manager/requirements.txt @@ -1,4 +1,4 @@ google-api-python-client==1.6.4 google-auth-httplib2==0.0.2 google-auth==1.2.0 -google-cloud==0.28.0 +google-cloud==0.29.0