@@ -87,26 +87,25 @@ def create_rs256_device(
87
87
"""Create a new device with the given id, using RS256 for
88
88
authentication."""
89
89
# [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 )
92
93
93
- client = get_client (service_account_json )
94
94
with io .open (certificate_file ) as f :
95
95
certificate = f .read ()
96
96
97
97
# Note: You can have multiple credentials associated with a device.
98
98
device_template = {
99
99
'id' : device_id ,
100
100
'credentials' : [{
101
- 'publicKey ' : {
101
+ 'public_key ' : {
102
102
'format' : 'RSA_X509_PEM' ,
103
103
'key' : certificate
104
104
}
105
105
}]
106
106
}
107
107
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 )
110
109
# [END iot_create_rsa_device]
111
110
112
111
@@ -116,26 +115,25 @@ def create_es256_device(
116
115
"""Create a new device with the given id, using ES256 for
117
116
authentication."""
118
117
# [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 )
121
121
122
- client = get_client (service_account_json )
123
122
with io .open (public_key_file ) as f :
124
123
public_key = f .read ()
125
124
126
125
# Note: You can have multiple credentials associated with a device.
127
126
device_template = {
128
127
'id' : device_id ,
129
128
'credentials' : [{
130
- 'publicKey ' : {
129
+ 'public_key ' : {
131
130
'format' : 'ES256_PEM' ,
132
131
'key' : public_key
133
132
}
134
133
}]
135
134
}
136
135
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 )
139
137
# [END iot_create_es_device]
140
138
141
139
@@ -187,16 +185,15 @@ def create_unauth_device(
187
185
device_id ):
188
186
"""Create a new device without authentication."""
189
187
# [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 )
192
191
193
- client = get_client (service_account_json )
194
192
device_template = {
195
193
'id' : device_id ,
196
194
}
197
195
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 )
200
197
# [END iot_create_unauth_device]
201
198
202
199
@@ -206,14 +203,12 @@ def delete_device(
206
203
"""Delete the device with the given id."""
207
204
# [START iot_delete_device]
208
205
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 ()
212
207
213
- device_name = '{}/devices/{}' .format (registry_name , device_id )
208
+ device_path = client .device_path (
209
+ project_id , cloud_region , registry_id , device_id )
214
210
215
- devices = client .projects ().locations ().registries ().devices ()
216
- return devices .delete (name = device_name ).execute ()
211
+ return client .delete_device (device_path )
217
212
# [END iot_delete_device]
218
213
219
214
@@ -401,26 +396,32 @@ def patch_es256_auth(
401
396
"""Patch the device to add an ES256 public key to the device."""
402
397
# [START iot_patch_es]
403
398
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 )
407
399
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 = ''
408
405
with io .open (public_key_file ) as f :
409
- public_key = f .read ()
406
+ public_key_bytes = f .read ()
410
407
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 )
419
418
420
- device_name = '{}/devices/{}' .format (registry_path , device_id )
419
+ mask = iot_v1 .types .FieldMask ()
420
+ mask .paths .append ('credentials' )
421
421
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 )
424
425
# [END iot_patch_es]
425
426
426
427
@@ -430,26 +431,33 @@ def patch_rsa256_auth(
430
431
"""Patch the device to add an RSA256 public key to the device."""
431
432
# [START iot_patch_rsa]
432
433
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 )
436
434
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 = ''
437
440
with io .open (public_key_file ) as f :
438
- public_key = f .read ()
441
+ public_key_bytes = f .read ()
439
442
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' )
448
456
449
- device_name = '{}/devices/{}' .format (registry_path , device_id )
457
+ return client .update_device (
458
+ device = device ,
459
+ update_mask = mask )
450
460
451
- return client .projects ().locations ().registries ().devices ().patch (
452
- name = device_name , updateMask = 'credentials' , body = patch ).execute ()
453
461
# [END iot_patch_rsa]
454
462
455
463
@@ -604,7 +612,7 @@ def create_gateway(
604
612
device_template = {
605
613
'id' : gateway_id ,
606
614
'credentials' : [{
607
- 'publicKey ' : {
615
+ 'public_key ' : {
608
616
'format' : certificate_format ,
609
617
'key' : certificate
610
618
}
0 commit comments