35
35
import sys
36
36
import time
37
37
38
+ from google .api_core .exceptions import AlreadyExists
38
39
from google .cloud import iot_v1
39
40
from google .cloud import pubsub
40
41
from google .oauth2 import service_account
@@ -143,37 +144,29 @@ def create_device(
143
144
"""Create a device to bind to a gateway if it does not exist."""
144
145
# [START iot_create_device]
145
146
# Check that the device doesn't already exist
147
+ client = iot_v1 .DeviceManagerClient ()
148
+
146
149
exists = False
147
150
148
- client = get_client (service_account_json )
149
- registry_path = 'projects/{}/locations/{}/registries/{}' .format (
150
- project_id , cloud_region , registry_id )
151
+ parent = client .registry_path (project_id , cloud_region , registry_id )
151
152
152
- devices = client .projects ().locations ().registries ().devices (
153
- ).list (
154
- parent = registry_path , fieldMask = 'config,gatewayConfig'
155
- ).execute ().get ('devices' , [])
153
+ devices = list (client .list_devices (parent = parent ))
156
154
157
155
for device in devices :
158
- if device .get ( 'id' ) == device_id :
156
+ if device .id == device_id :
159
157
exists = True
160
158
161
159
# Create the device
162
- registry_name = 'projects/{}/locations/{}/registries/{}' .format (
163
- project_id , cloud_region , registry_id )
164
-
165
160
device_template = {
166
161
'id' : device_id ,
167
- 'gatewayConfig ' : {
168
- 'gatewayType ' : 'NON_GATEWAY' ,
169
- 'gatewayAuthMethod ' : 'ASSOCIATION_ONLY'
162
+ 'gateway_config ' : {
163
+ 'gateway_type ' : 'NON_GATEWAY' ,
164
+ 'gateway_auth_method ' : 'ASSOCIATION_ONLY'
170
165
}
171
166
}
172
- devices = client .projects ().locations ().registries ().devices ()
173
167
174
168
if not exists :
175
- res = devices .create (
176
- parent = registry_name , body = device_template ).execute ()
169
+ res = client .create_device (parent , device_template )
177
170
print ('Created Device {}' .format (res ))
178
171
else :
179
172
print ('Device exists, skipping' )
@@ -217,12 +210,17 @@ def delete_registry(
217
210
"""Deletes the specified registry."""
218
211
# [START iot_delete_registry]
219
212
print ('Delete registry' )
220
- client = get_client (service_account_json )
221
- registry_name = 'projects/{}/locations/{}/registries/{}' .format (
222
- project_id , cloud_region , registry_id )
223
213
224
- registries = client .projects ().locations ().registries ()
225
- return registries .delete (name = registry_name ).execute ()
214
+ client = iot_v1 .DeviceManagerClient ()
215
+ registry_path = client .registry_path (project_id , cloud_region , registry_id )
216
+
217
+ try :
218
+ response = client .delete_device_registry (registry_path )
219
+ print ('Deleted registry' )
220
+ return response
221
+ except HttpError :
222
+ print ('Error, registry not deleted' )
223
+ return ""
226
224
# [END iot_delete_registry]
227
225
228
226
@@ -330,40 +328,40 @@ def create_registry(
330
328
""" Creates a registry and returns the result. Returns an empty result if
331
329
the registry already exists."""
332
330
# [START iot_create_registry]
333
- client = get_client (service_account_json )
334
- registry_parent = 'projects/{}/locations/{}' .format (
335
- project_id ,
336
- cloud_region )
331
+ client = iot_v1 .DeviceManagerClient ()
332
+ parent = client .location_path (project_id , cloud_region )
333
+
334
+ if not pubsub_topic .startswith ('projects/' ):
335
+ pubsub_topic = 'projects/{}/topics/{}' .format (project_id , pubsub_topic )
336
+
337
337
body = {
338
- 'eventNotificationConfigs ' : [{
339
- 'pubsubTopicName ' : pubsub_topic
338
+ 'event_notification_configs ' : [{
339
+ 'pubsub_topic_name ' : pubsub_topic
340
340
}],
341
341
'id' : registry_id
342
342
}
343
- request = client .projects ().locations ().registries ().create (
344
- parent = registry_parent , body = body )
345
343
346
344
try :
347
- response = request . execute ( )
345
+ response = client . create_device_registry ( parent , body )
348
346
print ('Created registry' )
349
347
return response
350
348
except HttpError :
351
349
print ('Error, registry not created' )
352
350
return ""
351
+ except AlreadyExists :
352
+ print ('Error, registry already exists' )
353
+ return ""
353
354
# [END iot_create_registry]
354
355
355
356
356
357
def get_registry (
357
358
service_account_json , project_id , cloud_region , registry_id ):
358
359
""" Retrieves a device registry."""
359
360
# [START iot_get_registry]
360
- client = get_client (service_account_json )
361
- registry_parent = 'projects/{}/locations/{}' .format (
362
- project_id ,
363
- cloud_region )
364
- topic_name = '{}/registries/{}' .format (registry_parent , registry_id )
365
- request = client .projects ().locations ().registries ().get (name = topic_name )
366
- return request .execute ()
361
+ client = iot_v1 .DeviceManagerClient ()
362
+ registry_path = client .registry_path (project_id , cloud_region , registry_id )
363
+
364
+ return client .get_device_registry (registry_path )
367
365
# [END iot_get_registry]
368
366
369
367
@@ -377,7 +375,7 @@ def open_registry(
377
375
service_account_json , project_id , cloud_region ,
378
376
pubsub_topic , registry_id )
379
377
380
- if response == "" :
378
+ if response == '' :
381
379
# Device registry already exists
382
380
print (
383
381
'Registry {} already exists - looking it up instead.' .format (
@@ -386,7 +384,7 @@ def open_registry(
386
384
service_account_json , project_id , cloud_region ,
387
385
registry_id )
388
386
389
- print ('Registry {} opened: ' .format (response .get ( ' name' ) ))
387
+ print ('Registry {} opened: ' .format (response .name ))
390
388
print (response )
391
389
392
390
@@ -512,12 +510,11 @@ def get_iam_permissions(
512
510
service_account_json , project_id , cloud_region , registry_id ):
513
511
"""Retrieves IAM permissions for the given registry."""
514
512
# [START iot_get_iam_policy]
515
- client = get_client ( service_account_json )
516
- registry_path = 'projects/{}/locations/{}/registries/{}' . format (
517
- project_id , cloud_region , registry_id )
513
+ client = iot_v1 . DeviceManagerClient ( )
514
+
515
+ registry_path = client . registry_path ( project_id , cloud_region , registry_id )
518
516
519
- policy = client .projects ().locations ().registries ().getIamPolicy (
520
- resource = registry_path , body = {}).execute ()
517
+ policy = client .get_iam_policy (registry_path )
521
518
522
519
return policy
523
520
# [END iot_get_iam_policy]
@@ -528,23 +525,18 @@ def set_iam_permissions(
528
525
member ):
529
526
"""Sets IAM permissions for the given registry to a single role/member."""
530
527
# [START iot_set_iam_policy]
531
- client = get_client (service_account_json )
528
+ client = iot_v1 .DeviceManagerClient ()
529
+ registry_path = client .registry_path (project_id , cloud_region , registry_id )
532
530
533
- registry_path = 'projects/{}/locations/{}/registries/{}' .format (
534
- project_id , cloud_region , registry_id )
535
531
body = {
536
- "policy" :
537
- {
538
- "bindings" :
539
- [{
540
- "members" : [member ],
541
- "role" : role
542
- }]
543
- }
532
+ 'bindings' :
533
+ [{
534
+ 'members' : [member ],
535
+ 'role' : role
536
+ }]
544
537
}
545
538
546
- return client .projects ().locations ().registries ().setIamPolicy (
547
- resource = registry_path , body = body ).execute ()
539
+ return client .set_iam_policy (registry_path , body )
548
540
# [END iot_set_iam_policy]
549
541
550
542
@@ -577,17 +569,13 @@ def create_gateway(
577
569
# [START iot_create_gateway]
578
570
# Check that the gateway doesn't already exist
579
571
exists = False
580
- client = get_client (service_account_json )
581
- registry_path = 'projects/{}/locations/{}/registries/{}' .format (
582
- project_id , cloud_region , registry_id )
572
+ client = iot_v1 .DeviceManagerClient ()
583
573
584
- devices = client .projects ().locations ().registries ().devices (
585
- ).list (
586
- parent = registry_path , fieldMask = 'config,gatewayConfig'
587
- ).execute ().get ('devices' , [])
574
+ parent = client .registry_path (project_id , cloud_region , registry_id )
575
+ devices = list (client .list_devices (parent = parent ))
588
576
589
577
for device in devices :
590
- if device .get ( 'id' ) == gateway_id :
578
+ if device .id == gateway_id :
591
579
exists = True
592
580
print ('Device: {} : {} : {} : {}' .format (
593
581
device .get ('id' ),
@@ -596,10 +584,6 @@ def create_gateway(
596
584
device .get ('gatewayConfig' )
597
585
))
598
586
599
- # Create the gateway
600
- registry_name = 'projects/{}/locations/{}/registries/{}' .format (
601
- project_id , cloud_region , registry_id )
602
-
603
587
with io .open (certificate_file ) as f :
604
588
certificate = f .read ()
605
589
@@ -617,17 +601,15 @@ def create_gateway(
617
601
'key' : certificate
618
602
}
619
603
}],
620
- 'gatewayConfig ' : {
621
- 'gatewayType ' : 'GATEWAY' ,
622
- 'gatewayAuthMethod ' : 'ASSOCIATION_ONLY'
604
+ 'gateway_config ' : {
605
+ 'gateway_type ' : 'GATEWAY' ,
606
+ 'gateway_auth_method ' : 'ASSOCIATION_ONLY'
623
607
}
624
608
}
625
- devices = client .projects ().locations ().registries ().devices ()
626
609
627
610
if not exists :
628
- res = devices .create (
629
- parent = registry_name , body = device_template ).execute ()
630
- print ('Created gateway {}' .format (res ))
611
+ res = client .create_device (parent , device_template )
612
+ print ('Created Gateway {}' .format (res ))
631
613
else :
632
614
print ('Gateway exists, skipping' )
633
615
# [END iot_create_gateway]
@@ -638,21 +620,17 @@ def bind_device_to_gateway(
638
620
gateway_id ):
639
621
"""Binds a device to a gateway."""
640
622
# [START iot_bind_device_to_gateway]
641
- client = get_client ( service_account_json )
623
+ client = iot_v1 . DeviceManagerClient ( )
642
624
643
625
create_device (
644
626
service_account_json , project_id , cloud_region , registry_id ,
645
627
device_id )
646
628
647
- registry_name = 'projects/{}/locations/{}/registries/{}' .format (
648
- project_id , cloud_region , registry_id )
649
- bind_request = {
650
- 'deviceId' : device_id ,
651
- 'gatewayId' : gateway_id
652
- }
653
- client .projects ().locations ().registries ().bindDeviceToGateway (
654
- parent = registry_name , body = bind_request ).execute ()
655
- print ('Device Bound!' )
629
+ parent = client .registry_path (project_id , cloud_region , registry_id )
630
+
631
+ res = client .bind_device_to_gateway (parent , gateway_id , device_id )
632
+
633
+ print ('Device Bound! {}' .format (res ))
656
634
# [END iot_bind_device_to_gateway]
657
635
658
636
@@ -661,17 +639,12 @@ def unbind_device_from_gateway(
661
639
gateway_id ):
662
640
"""Unbinds a device to a gateway."""
663
641
# [START iot_unbind_device_from_gateway]
664
- client = get_client ( service_account_json )
642
+ client = iot_v1 . DeviceManagerClient ( )
665
643
666
- registry_name = 'projects/{}/locations/{}/registries/{}' .format (
667
- project_id , cloud_region , registry_id )
668
- bind_request = {
669
- 'deviceId' : device_id ,
670
- 'gatewayId' : gateway_id
671
- }
644
+ parent = client .registry_path (project_id , cloud_region , registry_id )
645
+
646
+ res = client .unbind_device_from_gateway (parent , gateway_id , device_id )
672
647
673
- res = client .projects ().locations ().registries ().unbindDeviceFromGateway (
674
- parent = registry_name , body = bind_request ).execute ()
675
648
print ('Device unbound: {}' .format (res ))
676
649
# [END iot_unbind_device_from_gateway]
677
650
@@ -680,19 +653,18 @@ def list_gateways(
680
653
service_account_json , project_id , cloud_region , registry_id ):
681
654
"""Lists gateways in a registry"""
682
655
# [START iot_list_gateways]
683
- client = get_client (service_account_json )
684
- registry_path = 'projects/{}/locations/{}/registries/{}' .format (
685
- project_id , cloud_region , registry_id )
656
+ client = iot_v1 .DeviceManagerClient ()
686
657
687
- devices = client .projects ().locations ().registries ().devices (
688
- ).list (
689
- parent = registry_path , fieldMask = 'config,gatewayConfig'
690
- ).execute ().get ('devices' , [])
658
+ path = client .registry_path (project_id , cloud_region , registry_id )
659
+ mask = iot_v1 .types .FieldMask ()
660
+ mask .paths .append ('config' )
661
+ mask .paths .append ('gateway_config' )
662
+ devices = list (client .list_devices (parent = path , field_mask = mask ))
691
663
692
664
for device in devices :
693
- if device .get ( 'gatewayConfig' ) is not None :
694
- if device .get ( 'gatewayConfig' ). get ( 'gatewayType' ) == 'GATEWAY' :
695
- print ('Gateway ID: {}\n \t {}' .format (device .get ( 'id' ) , device ))
665
+ if device .gateway_config is not None :
666
+ if device .gateway_config . gateway_type == 1 :
667
+ print ('Gateway ID: {}\n \t {}' .format (device .id , device ))
696
668
# [END iot_list_gateways]
697
669
698
670
@@ -701,23 +673,18 @@ def list_devices_for_gateway(
701
673
gateway_id ):
702
674
"""List devices bound to a gateway"""
703
675
# [START iot_list_devices_for_gateway]
704
- client = get_client ( service_account_json )
676
+ client = iot_v1 . DeviceManagerClient ( )
705
677
706
- registry_name = 'projects/{}/locations/{}/registries/{}' .format (
707
- project_id , cloud_region , registry_id )
678
+ path = client .registry_path (project_id , cloud_region , registry_id )
708
679
709
- devices = client .projects ().locations ().registries ().devices (
710
- ).list (
711
- parent = registry_name ,
712
- gatewayListOptions_associationsGatewayId = gateway_id
713
- ).execute ()
680
+ devices = list (client .list_devices (
681
+ parent = path ,
682
+ gateway_list_options = {'associations_gateway_id' : gateway_id }))
714
683
715
684
found = False
716
- for device in devices . get ( 'devices' , []) :
685
+ for device in devices :
717
686
found = True
718
- print ('Device: {} : {}' .format (
719
- device .get ('numId' ),
720
- device .get ('id' )))
687
+ print ('Device: {} : {}' .format (device .num_id , device .id ))
721
688
722
689
if not found :
723
690
print ('No devices bound to gateway {}' .format (gateway_id ))
0 commit comments