From 1422d12d454ea35da4e2865904f626e8bf36efe6 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 27 Feb 2018 16:37:37 -0800 Subject: [PATCH] Cleanup Cloud IoT region tags. --- .../http_example/cloudiot_http_example.py | 8 +++++ iot/api-client/manager/manager.py | 34 +++++++++++++++++++ .../mqtt_example/cloudiot_mqtt_example.py | 3 +- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/iot/api-client/http_example/cloudiot_http_example.py b/iot/api-client/http_example/cloudiot_http_example.py index ffa22ceeb0d..74904e44ed0 100644 --- a/iot/api-client/http_example/cloudiot_http_example.py +++ b/iot/api-client/http_example/cloudiot_http_example.py @@ -21,6 +21,7 @@ README in the parent folder. """ +# [START iot_http_includes] import argparse import base64 import datetime @@ -30,11 +31,13 @@ from google.api_core import retry import jwt import requests +# [END iot_http_includes] _BASE_URL = 'https://cloudiotdevice.googleapis.com/v1' _BACKOFF_DURATION = 60 +# [START iot_http_jwt] def create_jwt(project_id, private_key_file, algorithm): token = { # The time the token was issued. @@ -53,11 +56,13 @@ def create_jwt(project_id, private_key_file, algorithm): algorithm, private_key_file)) return jwt.encode(token, private_key, algorithm=algorithm).decode('ascii') +# [END iot_http_jwt] @retry.Retry( predicate=retry.if_exception_type(AssertionError), deadline=_BACKOFF_DURATION) +# [START iot_http_publish] def publish_message( message, message_type, base_url, project_id, cloud_region, registry_id, device_id, jwt_token): @@ -92,6 +97,7 @@ def publish_message( raise AssertionError('Not OK response: {}'.format(resp.status_code)) return resp +# [END iot_http_publish] @retry.Retry( @@ -172,6 +178,7 @@ def parse_command_line_args(): return parser.parse_args() +# [START iot_http_run] def main(): args = parse_command_line_args() @@ -208,6 +215,7 @@ def main(): # Send events every second. State should not be updated as often time.sleep(1 if args.message_type == 'event' else 5) print('Finished.') +# [END iot_http_run] if __name__ == '__main__': diff --git a/iot/api-client/manager/manager.py b/iot/api-client/manager/manager.py index f8b954ca0bb..931daab968c 100644 --- a/iot/api-client/manager/manager.py +++ b/iot/api-client/manager/manager.py @@ -82,6 +82,7 @@ def get_client(service_account_json): credentials=scoped_credentials) +# [START iot_create_rsa_device] def create_rs256_device( service_account_json, project_id, cloud_region, registry_id, device_id, certificate_file): @@ -107,8 +108,10 @@ def create_rs256_device( devices = client.projects().locations().registries().devices() return devices.create(parent=registry_name, body=device_template).execute() +# [END iot_create_rsa_device] +# [START iot_create_es_device] def create_es256_device( service_account_json, project_id, cloud_region, registry_id, device_id, public_key_file): @@ -134,8 +137,10 @@ def create_es256_device( devices = client.projects().locations().registries().devices() return devices.create(parent=registry_name, body=device_template).execute() +# [END iot_create_es_device] +# [START iot_create_unauth_device] def create_unauth_device( service_account_json, project_id, cloud_region, registry_id, device_id): @@ -150,8 +155,10 @@ def create_unauth_device( devices = client.projects().locations().registries().devices() return devices.create(parent=registry_name, body=device_template).execute() +# [END iot_create_unauth_device] +# [START iot_delete_device] def delete_device( service_account_json, project_id, cloud_region, registry_id, device_id): @@ -165,8 +172,10 @@ def delete_device( devices = client.projects().locations().registries().devices() return devices.delete(name=device_name).execute() +# [END iot_delete_device] +# [START iot_delete_registry] def delete_registry( service_account_json, project_id, cloud_region, registry_id): """Deletes the specified registry.""" @@ -177,8 +186,10 @@ def delete_registry( registries = client.projects().locations().registries() return registries.delete(name=registry_name).execute() +# [END iot_delete_registry] +# [START iot_get_device] def get_device( service_account_json, project_id, cloud_region, registry_id, device_id): @@ -209,8 +220,10 @@ def get_device( 'cloudUpdateTime'))) return device +# [END iot_get_device] +# [START iot_get_device_state] def get_state( service_account_json, project_id, cloud_region, registry_id, device_id): @@ -226,8 +239,10 @@ def get_state( print('State: {}\n'.format(state)) return state +# [END iot_get_device_state] +# [START iot_list_devices] def list_devices( service_account_json, project_id, cloud_region, registry_id): """List all devices in the registry.""" @@ -244,8 +259,10 @@ def list_devices( device.get('id'))) return devices +# [END iot_list_devices] +# [START iot_list_registries] def list_registries(service_account_json, project_id, cloud_region): """List all registries in the project.""" print('Listing Registries') @@ -261,8 +278,10 @@ def list_registries(service_account_json, project_id, cloud_region): registry.get('name'))) return registries +# [END iot_list_devices] +# [START iot_create_registry] def create_registry( service_account_json, project_id, cloud_region, pubsub_topic, registry_id): @@ -288,8 +307,10 @@ def create_registry( except HttpError: print('Error, registry not created') return "" +# [END iot_create_registry] +# [START iot_get_registry] def get_registry( service_account_json, project_id, cloud_region, registry_id): """ Retrieves a device registry.""" @@ -300,6 +321,7 @@ def get_registry( topic_name = '{}/registries/{}'.format(registry_parent, registry_id) request = client.projects().locations().registries().get(name=topic_name) return request.execute() +# [END iot_get_registry] def open_registry( @@ -325,6 +347,7 @@ def open_registry( print(response) +# [START iot_patch_es] def patch_es256_auth( service_account_json, project_id, cloud_region, registry_id, device_id, public_key_file): @@ -350,8 +373,10 @@ def patch_es256_auth( return client.projects().locations().registries().devices().patch( name=device_name, updateMask='credentials', body=patch).execute() +# [END iot_patch_es] +# [START iot_patch_rsa] def patch_rsa256_auth( service_account_json, project_id, cloud_region, registry_id, device_id, public_key_file): @@ -377,8 +402,10 @@ def patch_rsa256_auth( return client.projects().locations().registries().devices().patch( name=device_name, updateMask='credentials', body=patch).execute() +# [END iot_patch_rsa] +# [START iot_set_device_config] def set_config( service_account_json, project_id, cloud_region, registry_id, device_id, version, config): @@ -397,8 +424,10 @@ def set_config( ).locations().registries( ).devices().modifyCloudToDeviceConfig( name=device_path, body=config_body).execute() +# [END iot_set_device_config] +# [START iot_get_device_configs] def get_config_versions( service_account_json, project_id, cloud_region, registry_id, device_id): @@ -420,8 +449,10 @@ def get_config_versions( config.get('binaryData'))) return configs +# [END iot_get_device_configs] +# [START iot_get_iam_policy] def get_iam_permissions( service_account_json, project_id, cloud_region, registry_id): """Retrieves IAM permissions for the given registry.""" @@ -433,8 +464,10 @@ def get_iam_permissions( resource=registry_path, body={}).execute() return policy +# [END iot_get_iam_policy] +# [START iot_set_iam_policy] def set_iam_permissions( service_account_json, project_id, cloud_region, registry_id, role, member): @@ -456,6 +489,7 @@ def set_iam_permissions( return client.projects().locations().registries().setIamPolicy( resource=registry_path, body=body).execute() +# [END iot_set_iam_policy] def parse_command_line_args(): diff --git a/iot/api-client/mqtt_example/cloudiot_mqtt_example.py b/iot/api-client/mqtt_example/cloudiot_mqtt_example.py index a3f48a84283..bcc2f72b690 100644 --- a/iot/api-client/mqtt_example/cloudiot_mqtt_example.py +++ b/iot/api-client/mqtt_example/cloudiot_mqtt_example.py @@ -21,6 +21,7 @@ for this sample. """ +# [START iot_mqtt_includes] import argparse import datetime import os @@ -30,7 +31,7 @@ import jwt import paho.mqtt.client as mqtt - +# [END iot_mqtt_includes] # The initial backoff time after a disconnection occurs, in seconds. minimum_backoff_time = 1