@@ -422,6 +422,42 @@ def get_config_versions(
422
422
return configs
423
423
424
424
425
+ def get_iam_permissions (
426
+ service_account_json , project_id , cloud_region , registry_id ):
427
+ """Retrieves IAM permissions for the given registry."""
428
+ client = get_client (service_account_json )
429
+ registry_path = 'projects/{}/locations/{}/registries/{}' .format (
430
+ project_id , cloud_region , registry_id )
431
+
432
+ policy = client .projects ().locations ().registries ().getIamPolicy (
433
+ resource = registry_path , body = {}).execute ()
434
+
435
+ return policy
436
+
437
+
438
+ def set_iam_permissions (
439
+ service_account_json , project_id , cloud_region , registry_id , role ,
440
+ member ):
441
+ """Retrieves IAM permissions for the given registry."""
442
+ client = get_client (service_account_json )
443
+
444
+ registry_path = 'projects/{}/locations/{}/registries/{}' .format (
445
+ project_id , cloud_region , registry_id )
446
+ body = {
447
+ "policy" :
448
+ {
449
+ "bindings" :
450
+ [{
451
+ "members" : [member ],
452
+ "role" : role
453
+ }]
454
+ }
455
+ }
456
+
457
+ return client .projects ().locations ().registries ().setIamPolicy (
458
+ resource = registry_path , body = body ).execute ()
459
+
460
+
425
461
def parse_command_line_args ():
426
462
"""Parse command line arguments."""
427
463
default_registry = 'cloudiot_device_manager_example_registry_{}' .format (
@@ -473,6 +509,14 @@ def parse_command_line_args():
473
509
'--version' ,
474
510
default = None ,
475
511
help = 'Version number for setting device configuration.' )
512
+ parser .add_argument (
513
+ '--member' ,
514
+ default = None ,
515
+ help = 'Member used for IAM commands.' )
516
+ parser .add_argument (
517
+ '--role' ,
518
+ default = None ,
519
+ help = 'Role used for IAM commands.' )
476
520
477
521
# Command subparser
478
522
command = parser .add_subparsers (dest = 'command' )
@@ -485,14 +529,16 @@ def parse_command_line_args():
485
529
command .add_parser ('delete-device' , help = delete_device .__doc__ )
486
530
command .add_parser ('delete-registry' , help = delete_registry .__doc__ )
487
531
command .add_parser ('get' , help = get_device .__doc__ )
532
+ command .add_parser ('get-config-versions' , help = get_config_versions .__doc__ )
533
+ command .add_parser ('get-iam-permissions' , help = get_iam_permissions .__doc__ )
488
534
command .add_parser ('get-registry' , help = get_registry .__doc__ )
489
535
command .add_parser ('get-state' , help = get_state .__doc__ )
490
536
command .add_parser ('list' , help = list_devices .__doc__ )
491
537
command .add_parser ('list-registries' , help = list_registries .__doc__ )
492
538
command .add_parser ('patch-es256' , help = patch_es256_auth .__doc__ )
493
539
command .add_parser ('patch-rs256' , help = patch_rsa256_auth .__doc__ )
494
540
command .add_parser ('set-config' , help = patch_rsa256_auth .__doc__ )
495
- command .add_parser ('get-config-versions ' , help = get_config_versions .__doc__ )
541
+ command .add_parser ('set-iam-permissions ' , help = set_iam_permissions .__doc__ )
496
542
497
543
return parser .parse_args ()
498
544
@@ -525,15 +571,45 @@ def run_create(args):
525
571
create_iot_topic (args .project_id , args .pubsub_topic )
526
572
527
573
574
+ def run_get (args ):
575
+ if args .command == 'get' :
576
+ get_device (
577
+ args .service_account_json , args .project_id ,
578
+ args .cloud_region , args .registry_id , args .device_id )
579
+
580
+ elif args .command == 'get-config-versions' :
581
+ get_device (
582
+ args .service_account_json , args .project_id ,
583
+ args .cloud_region , args .registry_id , args .device_id )
584
+
585
+ elif args .command == 'get-state' :
586
+ get_state (
587
+ args .service_account_json , args .project_id ,
588
+ args .cloud_region , args .registry_id , args .device_id )
589
+
590
+ elif args .command == 'get-iam-permissions' :
591
+ print (get_iam_permissions (
592
+ args .service_account_json , args .project_id ,
593
+ args .cloud_region , args .registry_id ))
594
+
595
+ elif args .command == 'get-registry' :
596
+ print (get_registry (
597
+ args .service_account_json , args .project_id ,
598
+ args .cloud_region , args .registry_id ))
599
+
600
+
528
601
def run_command (args ):
529
602
"""Calls the program using the specified command."""
530
603
if args .project_id is None :
531
604
print ('You must specify a project ID or set the environment variable.' )
532
605
return
533
606
534
- if args .command .startswith ('create' ):
607
+ elif args .command .startswith ('create' ):
535
608
run_create (args )
536
609
610
+ elif args .command .startswith ('get' ):
611
+ run_get (args )
612
+
537
613
elif args .command == 'delete-device' :
538
614
delete_device (
539
615
args .service_account_json , args .project_id ,
@@ -544,21 +620,6 @@ def run_command(args):
544
620
args .service_account_json , args .project_id ,
545
621
args .cloud_region , args .registry_id )
546
622
547
- elif args .command == 'get' :
548
- get_device (
549
- args .service_account_json , args .project_id ,
550
- args .cloud_region , args .registry_id , args .device_id )
551
-
552
- elif args .command == 'get-state' :
553
- get_state (
554
- args .service_account_json , args .project_id ,
555
- args .cloud_region , args .registry_id , args .device_id )
556
-
557
- elif args .command == 'get-registry' :
558
- print (get_registry (
559
- args .service_account_json , args .project_id ,
560
- args .cloud_region , args .registry_id ))
561
-
562
623
elif args .command == 'list' :
563
624
list_devices (
564
625
args .service_account_json , args .project_id ,
@@ -585,6 +646,15 @@ def run_command(args):
585
646
args .cloud_region , args .registry_id , args .device_id ,
586
647
args .rsa_certificate_file )
587
648
649
+ elif args .command == 'set-iam-permissions' :
650
+ if (args .member is None ):
651
+ sys .exit ('Error: specify --member' )
652
+ if (args .role is None ):
653
+ sys .exit ('Error: specify --role' )
654
+ set_iam_permissions (
655
+ args .service_account_json , args .project_id ,
656
+ args .cloud_region , args .registry_id , args .role , args .member )
657
+
588
658
elif args .command == 'set-config' :
589
659
if (args .config is None ):
590
660
sys .exit ('Error: specify --config' )
@@ -595,11 +665,6 @@ def run_command(args):
595
665
args .cloud_region , args .registry_id , args .device_id ,
596
666
args .version , args .config )
597
667
598
- elif args .command == 'get-config-versions' :
599
- get_device (
600
- args .service_account_json , args .project_id ,
601
- args .cloud_region , args .registry_id , args .device_id )
602
-
603
668
604
669
def main ():
605
670
args = parse_command_line_args ()
0 commit comments