Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 900900b

Browse filesBrowse files
gguussandrewferlitsch
authored andcommitted
Updates create / delete device methods to use Cloud client library (GoogleCloudPlatform#2420)
* Updates create / delete device methods to use Cloud client library * Lint
1 parent 0e84529 commit 900900b
Copy full SHA for 900900b

File tree

Expand file treeCollapse file tree

1 file changed

+62
-54
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+62
-54
lines changed

‎iot/api-client/manager/manager.py

Copy file name to clipboardExpand all lines: iot/api-client/manager/manager.py
+62-54Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,25 @@ def create_rs256_device(
8787
"""Create a new device with the given id, using RS256 for
8888
authentication."""
8989
# [START iot_create_rsa_device]
90-
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
91-
project_id, cloud_region, registry_id)
90+
client = iot_v1.DeviceManagerClient()
91+
92+
parent = client.registry_path(project_id, cloud_region, registry_id)
9293

93-
client = get_client(service_account_json)
9494
with io.open(certificate_file) as f:
9595
certificate = f.read()
9696

9797
# Note: You can have multiple credentials associated with a device.
9898
device_template = {
9999
'id': device_id,
100100
'credentials': [{
101-
'publicKey': {
101+
'public_key': {
102102
'format': 'RSA_X509_PEM',
103103
'key': certificate
104104
}
105105
}]
106106
}
107107

108-
devices = client.projects().locations().registries().devices()
109-
return devices.create(parent=registry_name, body=device_template).execute()
108+
return client.create_device(parent, device_template)
110109
# [END iot_create_rsa_device]
111110

112111

@@ -116,26 +115,25 @@ def create_es256_device(
116115
"""Create a new device with the given id, using ES256 for
117116
authentication."""
118117
# [START iot_create_es_device]
119-
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
120-
project_id, cloud_region, registry_id)
118+
client = iot_v1.DeviceManagerClient()
119+
120+
parent = client.registry_path(project_id, cloud_region, registry_id)
121121

122-
client = get_client(service_account_json)
123122
with io.open(public_key_file) as f:
124123
public_key = f.read()
125124

126125
# Note: You can have multiple credentials associated with a device.
127126
device_template = {
128127
'id': device_id,
129128
'credentials': [{
130-
'publicKey': {
129+
'public_key': {
131130
'format': 'ES256_PEM',
132131
'key': public_key
133132
}
134133
}]
135134
}
136135

137-
devices = client.projects().locations().registries().devices()
138-
return devices.create(parent=registry_name, body=device_template).execute()
136+
return client.create_device(parent, device_template)
139137
# [END iot_create_es_device]
140138

141139

@@ -187,16 +185,15 @@ def create_unauth_device(
187185
device_id):
188186
"""Create a new device without authentication."""
189187
# [START iot_create_unauth_device]
190-
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
191-
project_id, cloud_region, registry_id)
188+
client = iot_v1.DeviceManagerClient()
189+
190+
parent = client.registry_path(project_id, cloud_region, registry_id)
192191

193-
client = get_client(service_account_json)
194192
device_template = {
195193
'id': device_id,
196194
}
197195

198-
devices = client.projects().locations().registries().devices()
199-
return devices.create(parent=registry_name, body=device_template).execute()
196+
return client.create_device(parent, device_template)
200197
# [END iot_create_unauth_device]
201198

202199

@@ -206,14 +203,12 @@ def delete_device(
206203
"""Delete the device with the given id."""
207204
# [START iot_delete_device]
208205
print('Delete device')
209-
client = get_client(service_account_json)
210-
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
211-
project_id, cloud_region, registry_id)
206+
client = iot_v1.DeviceManagerClient()
212207

213-
device_name = '{}/devices/{}'.format(registry_name, device_id)
208+
device_path = client.device_path(
209+
project_id, cloud_region, registry_id, device_id)
214210

215-
devices = client.projects().locations().registries().devices()
216-
return devices.delete(name=device_name).execute()
211+
return client.delete_device(device_path)
217212
# [END iot_delete_device]
218213

219214

@@ -401,26 +396,32 @@ def patch_es256_auth(
401396
"""Patch the device to add an ES256 public key to the device."""
402397
# [START iot_patch_es]
403398
print('Patch device with ES256 certificate')
404-
client = get_client(service_account_json)
405-
registry_path = 'projects/{}/locations/{}/registries/{}'.format(
406-
project_id, cloud_region, registry_id)
407399

400+
client = iot_v1.DeviceManagerClient()
401+
device_path = client.device_path(
402+
project_id, cloud_region, registry_id, device_id)
403+
404+
public_key_bytes = ''
408405
with io.open(public_key_file) as f:
409-
public_key = f.read()
406+
public_key_bytes = f.read()
410407

411-
patch = {
412-
'credentials': [{
413-
'publicKey': {
414-
'format': 'ES256_PEM',
415-
'key': public_key
416-
}
417-
}]
418-
}
408+
key = iot_v1.types.PublicKeyCredential(
409+
format='ES256_PEM',
410+
key=public_key_bytes)
411+
412+
cred = iot_v1.types.DeviceCredential(public_key=key)
413+
device = client.get_device(device_path)
414+
415+
device.id = b''
416+
device.num_id = 0
417+
device.credentials.append(cred)
419418

420-
device_name = '{}/devices/{}'.format(registry_path, device_id)
419+
mask = iot_v1.types.FieldMask()
420+
mask.paths.append('credentials')
421421

422-
return client.projects().locations().registries().devices().patch(
423-
name=device_name, updateMask='credentials', body=patch).execute()
422+
return client.update_device(
423+
device=device,
424+
update_mask=mask)
424425
# [END iot_patch_es]
425426

426427

@@ -430,26 +431,33 @@ def patch_rsa256_auth(
430431
"""Patch the device to add an RSA256 public key to the device."""
431432
# [START iot_patch_rsa]
432433
print('Patch device with RSA256 certificate')
433-
client = get_client(service_account_json)
434-
registry_path = 'projects/{}/locations/{}/registries/{}'.format(
435-
project_id, cloud_region, registry_id)
436434

435+
client = iot_v1.DeviceManagerClient()
436+
device_path = client.device_path(
437+
project_id, cloud_region, registry_id, device_id)
438+
439+
public_key_bytes = ''
437440
with io.open(public_key_file) as f:
438-
public_key = f.read()
441+
public_key_bytes = f.read()
439442

440-
patch = {
441-
'credentials': [{
442-
'publicKey': {
443-
'format': 'RSA_X509_PEM',
444-
'key': public_key
445-
}
446-
}]
447-
}
443+
key = iot_v1.types.PublicKeyCredential(
444+
format='RSA_X509_PEM',
445+
key=public_key_bytes)
446+
447+
cred = iot_v1.types.DeviceCredential(public_key=key)
448+
device = client.get_device(device_path)
449+
450+
device.id = b''
451+
device.num_id = 0
452+
device.credentials.append(cred)
453+
454+
mask = iot_v1.types.FieldMask()
455+
mask.paths.append('credentials')
448456

449-
device_name = '{}/devices/{}'.format(registry_path, device_id)
457+
return client.update_device(
458+
device=device,
459+
update_mask=mask)
450460

451-
return client.projects().locations().registries().devices().patch(
452-
name=device_name, updateMask='credentials', body=patch).execute()
453461
# [END iot_patch_rsa]
454462

455463

@@ -604,7 +612,7 @@ def create_gateway(
604612
device_template = {
605613
'id': gateway_id,
606614
'credentials': [{
607-
'publicKey': {
615+
'public_key': {
608616
'format': certificate_format,
609617
'key': certificate
610618
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.