From c275f57cd8a9ab307379298ce8f1ff7d0340ad0c Mon Sep 17 00:00:00 2001 From: Gus Class Date: Fri, 22 Sep 2017 14:11:36 -0700 Subject: [PATCH] Final additions in private beta --- iot/api-client/manager/manager.py | 34 ++++++++++++++++++++++++-- iot/api-client/manager/manager_test.py | 10 ++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/iot/api-client/manager/manager.py b/iot/api-client/manager/manager.py index 72305f34d92b..ee260b3ea234 100644 --- a/iot/api-client/manager/manager.py +++ b/iot/api-client/manager/manager.py @@ -32,6 +32,7 @@ """ import argparse +import base64 import io import os import sys @@ -69,7 +70,7 @@ def get_client(service_account_json, api_key): provided API key and creating a service object using the service account credentials JSON.""" api_scopes = ['https://www.googleapis.com/auth/cloud-platform'] - api_version = 'v1beta1' + api_version = 'v1' discovery_api = 'https://cloudiot.googleapis.com/$discovery/rest' service_name = 'cloudiotcore' @@ -216,6 +217,23 @@ def get_device( return device +def get_state( + service_account_json, api_key, project_id, cloud_region, registry_id, + device_id): + """Retrieve a device's state blobs.""" + client = get_client(service_account_json, api_key) + registry_name = 'projects/{}/locations/{}/registries/{}'.format( + project_id, cloud_region, registry_id) + + device_name = '{}/devices/{}'.format(registry_name, device_id) + devices = client.projects().locations().registries().devices() + state = devices.states().list(name=device_name, numStates=5).execute() + + print('State: {}\n'.format(state)) + + return state + + def list_devices( service_account_json, api_key, project_id, cloud_region, registry_id): """List all devices in the registry.""" @@ -274,6 +292,8 @@ def create_registry( print('Created registry') return response except HttpError: + print(dir(HttpError)) + print('Error {}'.format(HttpError)) return "" @@ -425,7 +445,8 @@ 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('get-state', help=get_state.__doc__) + command.add_parser('get-registry', help=get_registry.__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__) @@ -436,6 +457,10 @@ def parse_command_line_args(): def run_command(args): """Calls the program using the specified command.""" + if args.project_id is None: + print ("You must specify a project ID or set the environment variable.") + return + if args.command == 'create-rsa256': create_rs256_device( args.service_account_json, args.api_key, args.project_id, @@ -476,6 +501,11 @@ def run_command(args): args.service_account_json, args.api_key, args.project_id, args.cloud_region, args.registry_id, args.device_id) + elif args.command == 'get-state': + get_state( + args.service_account_json, args.api_key, args.project_id, + args.cloud_region, args.registry_id, args.device_id) + elif args.command == 'list': list_devices( args.service_account_json, args.api_key, args.project_id, diff --git a/iot/api-client/manager/manager_test.py b/iot/api-client/manager/manager_test.py index ab7c29853c38..9cd3d2a6b301 100644 --- a/iot/api-client/manager/manager_test.py +++ b/iot/api-client/manager/manager_test.py @@ -101,6 +101,10 @@ def test_add_delete_rs256_device(test_topic, capsys): service_account_json, api_key, project_id, cloud_region, registry_id, device_id) + manager.get_state( + service_account_json, api_key, project_id, cloud_region, + registry_id, device_id) + manager.delete_device( service_account_json, api_key, project_id, cloud_region, registry_id, device_id) @@ -111,6 +115,7 @@ def test_add_delete_rs256_device(test_topic, capsys): out, _ = capsys.readouterr() assert 'format : RSA_X509_PEM' in out + assert 'State : {}' in out def test_add_delete_es256_device(test_topic, capsys): @@ -127,6 +132,10 @@ def test_add_delete_es256_device(test_topic, capsys): service_account_json, api_key, project_id, cloud_region, registry_id, device_id) + manager.get_state( + service_account_json, api_key, project_id, cloud_region, + registry_id, device_id) + manager.delete_device( service_account_json, api_key, project_id, cloud_region, registry_id, device_id) @@ -137,6 +146,7 @@ def test_add_delete_es256_device(test_topic, capsys): out, _ = capsys.readouterr() assert 'format : ES256_PEM' in out + assert 'State : {}' in out def test_add_patch_delete_rs256(test_topic, capsys):