8000 iot: switch to Cloud Client (#2418) · rmardiko/python-docs-samples@c8a2ce2 · GitHub
[go: up one dir, main page]

Skip to content

Commit c8a2ce2

Browse files
authored
iot: switch to Cloud Client (GoogleCloudPlatform#2418)
* First part of Cloud client library migration, separating as this part affects tests
1 parent 2a6cf74 commit c8a2ce2

File tree

7 files changed

+79
-64
lines changed

7 files changed

+79
-64
lines changed

iot/api-client/http_example/cloudiot_http_example_test.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ def test_event(test_topic, capsys):
7474
'hello', 'event', _BASE_URL, project_id, cloud_region,
7575
registry_id, device_id, jwt_token))
7676

77-
manager.get_state(
78-
service_account_json, project_id, cloud_region, registry_id,
79-
device_id)
80-
8177
manager.delete_device(
8278
service_account_json, project_id, cloud_region, registry_id,
8379
device_id)
@@ -87,7 +83,6 @@ def test_event(test_topic, capsys):
8783

8884
out, _ = capsys.readouterr()
8985
assert 'format : RSA_X509_PEM' in out
90-
assert 'State: {' in out
9186
assert '200' in out
9287

9388

@@ -125,8 +120,7 @@ def test_state(test_topic, capsys):
125120

126121
out, _ = capsys.readouterr()
127122
assert 'format : RSA_X509_PEM' in out
128-
assert 'State: {' in out
129-
assert 'aGVsbG8=' in out
123+
assert 'binary_data: "hello"' in out
130124
assert '200' in out
131125

132126

@@ -164,5 +158,4 @@ def test_config(test_topic, capsys):
164158

165159
out, _ = capsys.readouterr()
166160
assert 'format : RSA_X509_PEM' in out
167-
assert 'State: {' in out
168161
assert '"version": "1"' in out

iot/api-client/http_example/requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ cryptography==2.5
22
google-api-python-client==1.7.8
33
google-auth-httplib2==0.0.3
44
google-auth==1.6.2
5-
google-cloud-pubsub==0.39.1
5+
google-cloud-iot==0.3.0
6+
google-cloud-pubsub==1.0.0
7+
grpc-google-iam-v1==0.12.3
68
pyjwt==1.7.1
79
requests==2.21.0

iot/api-client/manager/manager.py

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import sys
3636
import time
3737

38+
from google.cloud import iot_v1
3839
from google.cloud import pubsub
3940
from google.oauth2 import service_account
4041
from googleapiclient import discovery
@@ -236,29 +237,38 @@ def get_device(
236237
"""Retrieve the device with the given id."""
237238
# [START iot_get_device]
238239
print('Getting device')
239-
client = get_client(service_account_json)
240-
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
241-
project_id, cloud_region, registry_id)
240+
client = iot_v1.DeviceManagerClient()
241+
device_path = client.device_path(
242+
project_id, cloud_region, registry_id, device_id)
242243

243-
device_name = '{}/devices/{}'.format(registry_name, device_id)
244-
devices = client.projects().locations().registries().devices()
245-
device = devices.get(name=device_name).execute()
244+
device = client.get_device(device_path)
246245

247-
print('Id : {}'.format(device.get('id')))
248-
print('Name : {}'.format(device.get('name')))
246+
print('Id : {}'.format(device.id))
247+
print('Name : {}'.format(device.name))
249248
print('Credentials:')
250-
if device.get('credentials') is not None:
251-
for credential in device.get('credentials'):
252-
keyinfo = credential.get('publicKey')
253-
print('\tcertificate: \n{}'.format(keyinfo.get('key')))
254-
print('\tformat : {}'.format(keyinfo.get('format')))
255-
print('\texpiration: {}'.format(credential.get('expirationTime')))
249+
250+
if device.credentials is not None:
251+
for credential in device.credentials:
252+
keyinfo = credential.public_key
253+
print('\tcertificate: \n{}'.format(keyinfo.key))
254+
255+
if keyinfo.format == 4:
256+
keyformat = 'ES256_X509_PEM'
257+
elif keyinfo.format == 3:
258+
keyformat = 'RSA_PEM'
259+
elif keyinfo.format == 2:
260+
keyformat = 'ES256_PEM'
261+
elif keyinfo.format == 1:
262+
keyformat = 'RSA_X509_PEM'
263+
else:
264+
keyformat = 'UNSPECIFIED_PUBLIC_KEY_FORMAT'
265+
print('\tformat : {}'.format(keyformat))
266+
print('\texpiration: {}'.format(credential.expiration_time))
256267

257268
print('Config:')
258-
print('\tdata: {}'.format(device.get('config').get('data')))
259-
print('\tversion: {}'.format(device.get('config').get('version')))
260-
print('\tcloudUpdateTime: {}'.format(device.get('config').get(
261-
'cloudUpdateTime')))
269+
print('\tdata: {}'.format(device.config.binary_data))
270+
print('\tversion: {}'.format(device.config.version))
271+
print('\tcloudUpdateTime: {}'.format(device.config.cloud_update_time))
262272

263273
return device
264274
# [END iot_get_device]
@@ -269,17 +279,19 @@ def get_state(
269279
device_id):
270280
"""Retrieve a device's state blobs."""
271281
# [START iot_get_device_state]
272-
client = get_client(service_account_json)
273-
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
274-
project_id, cloud_region, registry_id)
282+
client = iot_v1.DeviceManagerClient()
283+
device_path = client.device_path(
284+
project_id, cloud_region, registry_id, device_id)
275285

276-
device_name = '{}/devices/{}'.format(registry_name, device_id)
277-
devices = client.projects().locations().registries().devices()
278-
state = devices.states().list(name=device_name, numStates=5).execute()
286+
device = client.get_device(device_path)
287+
print('Last state: {}'.format(device.state))
279288

280-
print('State: {}\n'.format(state))
289+
print('State history')
290+
states = client.list_device_states(device_path).device_states
291+
for state in states:
292+
print('State: {}'.format(state))
281293

282-
return state
294+
return states
283295
# [END iot_get_device_state]
284296

285297

@@ -288,16 +300,13 @@ def list_devices(
288300
"""List all devices in the registry."""
289301
# [START iot_list_devices]
290302
print('Listing devices')
291-
registry_path = 'projects/{}/locations/{}/registries/{}'.format(
292-
project_id, cloud_region, registry_id)
293-
client = get_client(service_account_json)
294-
devices = client.projects().locations().registries().devices(
295-
).list(parent=registry_path).execute().get('devices', [])
296303

304+
client = iot_v1.DeviceManagerClient()
305+
registry_path = client.registry_path(project_id, cloud_region, registry_id)
306+
307+
devices = list(client.list_devices(parent=registry_path))
297308
for device in devices:
298-
print('Device: {} : {}'.format(
299-
device.get('numId'),
300-
device.get('id')))
309+
print('Device: {} : {}'.format(device.num_id, device.id))
301310

302311
return devices
303312
# [END iot_list_devices]
@@ -307,16 +316,14 @@ def list_registries(service_account_json, project_id, cloud_region):
307316
"""List all registries in the project."""
308317
# [START iot_list_registries]
309318
print('Listing Registries')
310-
registry_path = 'projects/{}/locations/{}'.format(
311-
project_id, cloud_region)
312-
client = get_client(service_account_json)
313-
registries = client.projects().locations().registries().list(
314-
parent=registry_path).execute().get('deviceRegistries', [])
319+
client = iot_v1.DeviceManagerClient()
320+
parent = client.location_path(project_id, cloud_region)
315321

322+
registries = list(client.list_device_registries(parent))
316323
for registry in registries:
317324
print('id: {}\n\tname: {}'.format(
318-
registry.get('id'),
319-
registry.get('name')))
325+
registry.id,
326+
registry.name))
320327

321328
return registries
322329
# [END iot_list_registries]

iot/api-client/manager/manager_test.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343

4444
@pytest.fixture(scope="session", autouse=True)
4545
def clean_up_registries():
46-
all_registries = manager.list_registries(
47-
service_account_json, project_id, cloud_region)
46+
all_registries = list(manager.list_registries(
47+
service_account_json, project_id, cloud_region))
4848

4949
for registry in all_registries:
50-
registry_id = registry.get('id')
50+
registry_id = registry.id
5151
if registry_id.find('test-registry-') == 0:
5252
time_str = registry_id[
5353
registry_id.rfind('-') + 1: len(registry_id)]
@@ -61,19 +61,19 @@ def clean_up_registries():
6161
client = manager.get_client(service_account_json)
6262
gateways = client.projects().locations().registries().devices(
6363
).list(
64-
parent=registry.get('name'),
64+
parent=registry.name,
6565
fieldMask='config,gatewayConfig'
6666
).execute().get('devices', [])
6767
devices = client.projects().locations().registries().devices(
68-
).list(parent=registry.get('name')).execute().get(
68+
).list(parent=registry.name).execute().get(
6969
'devices', [])
7070

7171
# Unbind devices from each gateway and delete
7272
for gateway in gateways:
7373
gateway_id = gateway.get('id')
7474
bound = client.projects().locations().registries().devices(
7575
).list(
76-
parent=registry.get('name'),
76+
parent=registry.name,
7777
gatewayListOptions_associationsGatewayId=gateway_id
7878
).execute()
7979
if 'devices' in bound:
@@ -87,15 +87,15 @@ def clean_up_registries():
8787
parent=registry.get('name'),
8888
body=bind_request).execute()
8989
gateway_name = '{}/devices/{}'.format(
90-
registry.get('name'), gateway_id)
90+
registry.name, gateway_id)
9191
client.projects().locations().registries().devices(
9292
).delete(name=gateway_name).execute()
9393

9494
# Delete the devices
9595
# Assumption is that the devices are not bound to gateways
9696
for device in devices:
9797
device_name = '{}/devices/{}'.format(
98-
registry.get('name'), device.get('id'))
98+
registry.name, device.get('id'))
9999
print(device_name)
100100
remove_device = True
101101
try:
@@ -111,7 +111,7 @@ def clean_up_registries():
111111

112112
# Delete the old test registry
113113
client.projects().locations().registries().delete(
114-
name=registry.get('name')).execute()
114+
name=registry.name).execute()
115115

116116

117117
@pytest.fixture(scope='module')
@@ -193,6 +193,9 @@ def test_add_delete_unauth_device(test_topic, capsys):
193193
service_account_json, project_id, cloud_region, registry_id,
194194
device_id)
195195

196+
manager.delete_registry(
197+
service_account_json, project_id, cloud_region, registry_id)
198+
196199
out, _ = capsys.readouterr()
197200
assert 'UNAUTH' in out
198201

@@ -219,6 +222,9 @@ def test_add_config_unauth_device(test_topic, capsys):
219222
service_account_json, project_id, cloud_region, registry_id,
220223
device_id)
221224

225+
manager.delete_registry(
226+
service_account_json, project_id, cloud_region, registry_id)
227+
222228
out, _ = capsys.readouterr()
223229
assert 'Set device configuration' in out
224230
assert 'UNAUTH' in out
@@ -252,7 +258,6 @@ def test_add_delete_rs256_device(test_topic, capsys):
252258

253259
out, _ = capsys.readouterr()
254260
assert 'format : RSA_X509_PEM' in out
255-
assert 'State: {' in out
256261

257262

258263
def test_add_delete_es256_device(test_topic, capsys):
@@ -282,7 +287,6 @@ def test_add_delete_es256_device(test_topic, capsys):
282287

283288
out, _ = capsys.readouterr()
284289
assert 'format : ES256_PEM' in out
285-
assert 'State: {' in out
286290

287291

288292
def test_add_patch_delete_rs256(test_topic, capsys):

iot/api-client/manager/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ gcp-devrel-py-tools==0.0.15
44
google-api-python-client==1.7.8
55
google-auth-httplib2==0.0.3
66
google-auth==1.6.2
7-
google-cloud-pubsub==0.39.1
7+
google-cloud-iot==0.3.0
8+
google-cloud-pubsub==1.0.0
89
oauth2client==4.1.3
910
paho-mqtt==1.4.0
1011
pyjwt==1.7.1

iot/api-client/mqtt_example/cloudiot_mqtt_example_test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def test_state(test_topic, capsys):
139139

140140
out, _ = capsys.readouterr()
141141
assert 'on_publish' in out
142-
assert 'c3RhdGUgdGVzdA' in out
142+
assert 'binary_data: "state test"' in out
143143

144144

145145
def test_config(test_topic, capsys):
@@ -354,6 +354,12 @@ def trigger_error(client):
354354
service_account_json, project_id, cloud_region, registry_id,
355355
device_id, gateway_id, num_messages, rsa_private_path,
356356
'RS256', ca_cert_path, 'mqtt.googleapis.com', 443,
357+
20, 42, trigger_error)
358+
# Try to connect the gateway aagin on 8883
359+
cloudiot_mqtt_example.listen_for_messages(
360+
service_account_json, project_id, cloud_region, registry_id,
361+
device_id, gateway_id, num_messages, rsa_private_path,
362+
'RS256', ca_cert_path, 'mqtt.googleapis.com', 8883,
357363
20, 15, trigger_error)
358364

359365
# Clean up

iot/api-client/mqtt_example/requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ gcp-devrel-py-tools==0.0.15
44
google-api-python-client==1.7.8
55
google-auth-httplib2==0.0.3
66
google-auth==1.6.2
7-
google-cloud-pubsub==0.39.1
7+
google-cloud-pubsub==1.0.0
8+
google-cloud-iot==0.3.0
9+
grpc-google-iam-v1==0.12.3
810
pyjwt==1.7.1
911
paho-mqtt==1.4.0

0 commit comments

Comments
 (0)
0