8000 Final additions in private beta [(#1136)](https://github.com/GoogleCl… · googleapis/python-iot@0ad6027 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Oct 29, 2023. It is now read-only.

Commit 0ad6027

Browse files
gguussJon Wayne Parrott
authored andcommitted
Final additions in private beta [(#1136)](GoogleCloudPlatform/python-docs-samples#1136)
* Final additions in private beta * Adds HTTP client and state support * Fixes rouge space * Remove invalid message on error.
1 parent a2f75ea commit 0ad6027

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

samples/api-client/manager/manager.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def get_client(service_account_json, api_key):
6969
provided API key and creating a service object using the service account
7070
credentials JSON."""
7171
api_scopes = ['https://www.googleapis.com/auth/cloud-platform']
72-
api_version = 'v1beta1'
72+
api_version = 'v1'
7373
discovery_api = 'https://cloudiot.googleapis.com/$discovery/rest'
7474
service_name = 'cloudiotcore'
7575

@@ -173,7 +173,7 @@ def delete_device(
173173

174174

175175
def delete_registry(
176-
service_account_json, api_key, project_id, cloud_region, registry_id):
176+
service_account_json, api_key, project_id, cloud_region, registry_id):
177177
"""Deletes the specified registry."""
178178
print('Delete registry')
179179
client = get_client(service_account_json, api_key)
@@ -216,6 +216,23 @@ def get_device(
216216
return device
217217

218218

219+
def get_state(
220+
service_account_json, api_key, project_id, cloud_region, registry_id,
221+
device_id):
222+
"""Retrieve a device's state blobs."""
223+
client = get_client(service_account_json, api_key)
224+
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
225+
project_id, cloud_region, registry_id)
226+
227+
device_name = '{}/devices/{}'.format(registry_name, device_id)
228+
devices = client.projects().locations().registries().devices()
229+
state = devices.states().list(name=device_name, numStates=5).execute()
230+
231+
print('State: {}\n'.format(state))
232+
233+
return state
234+
235+
219236
def list_devices(
220237
service_account_json, api_key, project_id, cloud_region, registry_id):
221238
"""List all devices in the registry."""
@@ -261,9 +278,9 @@ def create_registry(
261278
project_id,
262279
cloud_region)
263280
body = {
264-
'eventNotificationConfig': {
281+
'eventNotificationConfigs': [{
265282
'pubsubTopicName': pubsub_topic
266-
},
283+
}],
267284
'id': registry_id
268285
}
269286
request = client.projects().locations().registries().create(
@@ -274,6 +291,7 @@ def create_registry(
274291
print('Created registry')
275292
return response
276293
except HttpError:
294+
print('Error, registry not created')
277295
return ""
278296

279297

@@ -425,7 +443,8 @@ def parse_command_line_args():
425443
command.add_parser('delete-device', help=delete_device.__doc__)
426444
command.add_parser('delete-registry', help=delete_registry.__doc__)
427445
command.add_parser('get', help=get_device.__doc__)
428-
command.add_parser('get-registry', help=get_device.__doc__)
446+
command.add_parser('get-registry', help=get_registry.__doc__)
447+
command.add_parser('get-state', help=get_state.__doc__)
429448
command.add_parser('list', help=list_devices.__doc__)
430449
command.add_parser('list-registries', help=list_registries.__doc__)
431450
command.add_parser('patch-es256', help=patch_es256_auth.__doc__)
@@ -436,6 +455,10 @@ def parse_command_line_args():
436455

437456
def run_command(args):
438457
"""Calls the program using the specified command."""
458+
if args.project_id is None:
459+
print('You must specify a project ID or set the environment variable.')
460+
return
461+
439462
if args.command == 'create-rsa256':
440463
create_rs256_device(
441464
args.service_account_json, args.api_key, args.project_id,
@@ -476,6 +499,11 @@ def run_command(args):
476499
args.service_account_json, args.api_key, args.project_id,
477500
args.cloud_region, args.registry_id, args.device_id)
478501

502+
elif args.command == 'get-state':
503+
get_state(
504+
args.service_account_json, args.api_key, args.project_id,
505+
args.cloud_region, args.registry_id, args.device_id)
506+
479507
elif args.command == 'list':
480508
list_devices(
481509
args.service_account_json, args.api_key, args.project_id,

samples/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)
0