Skip to content
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
30 changes: 13 additions & 17 deletions iot/api-client/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions iot/api-client/manager/manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion iot/api-client/manager/requirements.txt
Original file line number Diff line number Diff line change
@@ -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