Skip to content

Commit c275f57

Browse files
committed
Final additions in private beta
1 parent b9e2401 commit c275f57

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

iot/api-client/manager/manager.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"""
3333

3434
import argparse
35+
import base64
3536
import io
3637
import os
3738
import sys
@@ -69,7 +70,7 @@ def get_client(service_account_json, api_key):
6970
provided API key and creating a service object using the service account
7071
credentials JSON."""
7172
api_scopes = ['https://www.googleapis.com/auth/cloud-platform']
72-
api_version = 'v1beta1'
73+
api_version = 'v1'
7374
discovery_api = 'https://cloudiot.googleapis.com/$discovery/rest'
7475
service_name = 'cloudiotcore'
7576

@@ -216,6 +217,23 @@ def get_device(
216217
return device
217218

218219

220+
def get_state(
221+
service_account_json, api_key, project_id, cloud_region, registry_id,
222+
device_id):
223+
"""Retrieve a device's state blobs."""
224+
client = get_client(service_account_json, api_key)
225+
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
226+
project_id, cloud_region, registry_id)
227+
228+
device_name = '{}/devices/{}'.format(registry_name, device_id)
229+
devices = client.projects().locations().registries().devices()
230+
state = devices.states().list(name=device_name, numStates=5).execute()
231+
232+
print('State: {}\n'.format(state))
233+
234+
return state
235+
236+
219237
def list_devices(
220238
service_account_json, api_key, project_id, cloud_region, registry_id):
221239
"""List all devices in the registry."""
@@ -274,6 +292,8 @@ def create_registry(
274292
print('Created registry')
275293
return response
276294
except HttpError:
295+
print(dir(HttpError))
296+
print('Error {}'.format(HttpError))
277297
return ""
278298

279299

@@ -425,7 +445,8 @@ def parse_command_line_args():
425445
command.add_parser('delete-device', help=delete_device.__doc__)
426446
command.add_parser('delete-registry', help=delete_registry.__doc__)
427447
command.add_parser('get', help=get_device.__doc__)
428-
command.add_parser('get-registry', help=get_device.__doc__)
448+
command.add_parser('get-state', help=get_state.__doc__)
449+
command.add_parser('get-registry', help=get_registry.__doc__)
429450
command.add_parser('list', help=list_devices.__doc__)
430451
command.add_parser('list-registries', help=list_registries.__doc__)
431452
command.add_parser('patch-es256', help=patch_es256_auth.__doc__)
@@ -436,6 +457,10 @@ def parse_command_line_args():
436457

437458
def run_command(args):
438459
"""Calls the program using the specified command."""
460+
if args.project_id is None:
461+
print ("You must specify a project ID or set the environment variable.")
462+
return
463+
439464
if args.command == 'create-rsa256':
440465
create_rs256_device(
441466
args.service_account_json, args.api_key, args.project_id,
@@ -476,6 +501,11 @@ def run_command(args):
476501
args.service_account_json, args.api_key, args.project_id,
477502
args.cloud_region, args.registry_id, args.device_id)
478503

504+
elif args.command == 'get-state':
505+
get_state(
506+
args.service_account_json, args.api_key, args.project_id,
507+
args.cloud_region, args.registry_id, args.device_id)
508+
479509
elif args.command == 'list':
480510
list_devices(
481511
args.service_account_json, args.api_key, args.project_id,

iot/api-client/manager/manager_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def test_add_delete_rs256_device(test_topic, capsys):
101101
service_account_json, api_key, project_id, cloud_region,
102102
registry_id, device_id)
103103

104+
manager.get_state(
105+
service_account_json, api_key, project_id, cloud_region,
106+
registry_id, device_id)
107+
104108
manager.delete_device(
105109
service_account_json, api_key, project_id, cloud_region,
106110
registry_id, device_id)
@@ -111,6 +115,7 @@ def test_add_delete_rs256_device(test_topic, capsys):
111115

112116
out, _ = capsys.readouterr()
113117
assert 'format : RSA_X509_PEM' in out
118+
assert 'State : {}' in out
114119

115120

116121
def test_add_delete_es256_device(test_topic, capsys):
@@ -127,6 +132,10 @@ def test_add_delete_es256_device(test_topic, capsys):
127132
service_account_json, api_key, project_id, cloud_region,
128133
registry_id, device_id)
129134

135+
manager.get_state(
136+
service_account_json, api_key, project_id, cloud_region,
137+
registry_id, device_id)
138+
130139
manager.delete_device(
131140
service_account_json, api_key, project_id, cloud_region,
132141
registry_id, device_id)
@@ -137,6 +146,7 @@ def test_add_delete_es256_device(test_topic, capsys):
137146

138147
out, _ = capsys.readouterr()
139148
assert 'format : ES256_PEM' in out
149+
assert 'State : {}' in out
140150

141151

142152
def test_add_patch_delete_rs256(test_topic, capsys):

0 commit comments

Comments
 (0)