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

Changes for snippets #1105

Merged
merged 3 commits into from
Sep 12, 2017
Merged
Changes from 2 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
107 changes: 74 additions & 33 deletions iot/api-client/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def create_iot_topic(topic_name):
topic = pubsub_client.topic(topic_name)
policy = topic.get_iam_policy()
publishers = policy.get('roles/pubsub.publisher', [])
publishers.add(policy.service_account(
publishers.append(policy.service_account(
'cloud-iot@system.gserviceaccount.com'))
policy['roles/pubsub.publisher'] = publishers
topic.set_iam_policy(policy)
Expand All @@ -64,7 +64,6 @@ def get_client(service_account_json, api_key):
"""Returns an authorized API client by discovering the IoT API using the
provided API key and creating a service object using the service account
credentials JSON."""
# [START authorize]
api_scopes = ['https://www.googleapis.com/auth/cloud-platform']
api_version = 'v1beta1'
discovery_api = 'https://cloudiot.googleapis.com/$discovery/rest'
Expand All @@ -82,15 +81,13 @@ def get_client(service_account_json, api_key):
api_version,
discoveryServiceUrl=discovery_url,
credentials=scoped_credentials)
# [END authorize]


def create_rs256_device(
service_account_json, api_key, project_id, cloud_region, registry_id,
device_id, certificate_file):
"""Create a new device with the given id, using RS256 for
authentication."""
# [START create_rs256_device]
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
project_id, cloud_region, registry_id)

Expand All @@ -111,15 +108,13 @@ def create_rs256_device(

devices = client.projects().locations().registries().devices()
return devices.create(parent=registry_name, body=device_template).execute()
# [END create_rs256_device]


def create_es256_device(
service_account_json, api_key, project_id, cloud_region, registry_id,
device_id, public_key_file):
"""Create a new device with the given id, using ES256 for
authentication."""
# [START create_rs256_device]
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
project_id, cloud_region, registry_id)

Expand All @@ -140,14 +135,12 @@ def create_es256_device(

devices = client.projects().locations().registries().devices()
return devices.create(parent=registry_name, body=device_template).execute()
# [END create_rs256_device]


def create_unauth_device(
service_account_json, api_key, project_id, cloud_region, registry_id,
device_id):
"""Create a new device without authentication."""
# [START create_noauth_device]
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
project_id, cloud_region, registry_id)

Expand All @@ -158,14 +151,12 @@ def create_unauth_device(

devices = client.projects().locations().registries().devices()
return devices.create(parent=registry_name, body=device_template).execute()
# [END create_noauth_device]


def delete_device(
service_account_json, api_key, project_id, cloud_region, registry_id,
device_id):
"""Delete the device with the given id."""
# [START delete_device]
print('Delete device')
client = get_client(service_account_json, api_key)
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
Expand All @@ -175,28 +166,24 @@ def delete_device(

devices = client.projects().locations().registries().devices()
return devices.delete(name=device_name).execute()
# [END delete_device]


def delete_registry(
service_account_json, api_key, project_id, cloud_region, registry_id):
"""Deletes the specified registry."""
# [START delete_registry]
print('Delete registry')
client = get_client(service_account_json, api_key)
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
project_id, cloud_region, registry_id)

registries = client.projects().locations().registries()
return registries.delete(name=registry_name).execute()
# [END delete_registry]


def get_device(
service_account_json, api_key, project_id, cloud_region, registry_id,
device_id):
"""Retrieve the device with the given id."""
# [START delete_device]
print('Getting device')
client = get_client(service_account_json, api_key)
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
Expand All @@ -223,13 +210,11 @@ def get_device(
'cloudUpdateTime')))

return device
# [END delete_device]


def list_devices(
service_account_json, api_key, project_id, cloud_region, registry_id):
"""List all devices in the registry."""
# [START list_devices]
print('Listing devices')
registry_path = 'projects/{}/locations/{}/registries/{}'.format(
project_id, cloud_region, registry_id)
Expand All @@ -243,14 +228,30 @@ def list_devices(
device.get('id')))

return devices
# [list_devices]


def open_registry(
def list_registries(service_account_json, api_key, project_id, cloud_region):
"""List all registries in the project."""
print('Listing Registries')
registry_path = 'projects/{}/locations/{}'.format(
project_id, cloud_region)
client = get_client(service_account_json, api_key)
registries = client.projects().locations().registries().list(
parent=registry_path).execute().get('deviceRegistries', [])

for registry in registries:
print('id: {}\n\tname: {}'.format(
registry.get('id'),
registry.get('name')))

return registries


def create_registry(
service_account_json, api_key, project_id, cloud_region, pubsub_topic,
registry_id):
"""Gets or creates a device registry."""
print('Creating registry')
""" Creates a registry and returns the result. Returns an empty result if
the registry already exists."""
client = get_client(service_account_json, api_key)
registry_parent = 'projects/{}/locations/{}'.format(
project_id,
Expand All @@ -264,21 +265,49 @@ def open_registry(
request = client.projects().locations().registries().create(
parent=registry_parent, body=body)

print (request)

try:
response = request.execute()
print('Created registry', registry_id)
print(response)
except HttpError as e:
if e.resp.status == 409:
# Device registry already exists
print(
'Registry', registry_id,
'already exists - looking it up instead.')
topic_name = '{}/registries/{}'.format(
registry_parent, registry_id)
request = client.projects().locations().registries(
).get(name=topic_name)
request.execute()
print('Created registry')
return response
except HttpError:
return ""


def get_registry(
service_account_json, api_key, project_id, cloud_region, registry_id):
""" Retrieves a device registry."""
client = get_client(service_account_json, api_key)
registry_parent = 'projects/{}/locations/{}'.format(
project_id,
cloud_region)
topic_name = '{}/registries/{}'.format(registry_parent, registry_id)
request = client.projects().locations().registries().get(name=topic_name)
return request.execute()


def open_registry(
service_account_json, api_key, project_id, cloud_region, pubsub_topic,
registry_id):
"""Gets or creates a device registry."""
print('Creating registry')

response = create_registry(
service_account_json, api_key, project_id, cloud_region,
pubsub_topic, registry_id)

if (response is ""):
# Device registry already exists
print(
'Registry {} already exists - looking it up instead.'.format(
registry_id))
response = get_registry(
service_account_json, api_key, project_id, cloud_region,
registry_id)

print('Registry {} opened: '.format(response.get('name')))
print(response)


def patch_es256_auth(
Expand Down Expand Up @@ -394,7 +423,9 @@ def parse_command_line_args():
command.add_parser('delete-device', help=delete_device.__doc__)
command.add_parser('delete-registry', help=delete_registry.__doc__)
command.add_parser('get', help=get_device.__doc__)
command.add_parser('get-registry', help=get_device.__doc__)
command.add_parser('list', help=list_devices.__doc__)
command.add_parser('list-registries', help=list_registries.__doc__)
command.add_parser('patch-es256', help=patch_es256_auth.__doc__)
command.add_parser('patch-rs256', help=patch_rsa256_auth.__doc__)

Expand Down Expand Up @@ -448,6 +479,16 @@ def run_command(args):
args.service_account_json, args.api_key, args.project_id,
args.cloud_region, args.registry_id)

elif args.command == 'get-registry':
print(get_registry(
args.service_account_json, args.api_key, args.project_id,
args.cloud_region, args.registry_id))

elif args.command == 'list-registries':
list_registries(
args.service_account_json, args.api_key, args.project_id,
args.cloud_region)

elif args.command == 'patch-es256':
if (args.ec_public_key_file is None):
sys.exit('Error: specify --ec_public_key_file')
Expand Down