31
31
"""
32
32
33
33
import argparse
34
+ import base64
34
35
import io
35
36
import os
36
37
import sys
@@ -378,6 +379,26 @@ def patch_rsa256_auth(
378
379
name = device_name , updateMask = 'credentials' , body = patch ).execute ()
379
380
380
381
382
+ def set_config (
383
+ service_account_json , project_id , cloud_region , registry_id , device_id ,
384
+ version , config ):
385
+ print ('Set device configuration' )
386
+ client = get_client (service_account_json )
387
+ device_path = 'projects/{}/locations/{}/registries/{}/devices/{}' .format (
388
+ project_id , cloud_region , registry_id , device_id )
389
+
390
+ config_body = {
391
+ 'versionToUpdate' : version ,
392
+ 'binaryData' : base64 .urlsafe_b64encode (
393
+ config .encode ('utf-8' )).decode ('ascii' )
394
+ }
395
+
396
+ return client .projects (
397
+ ).locations ().registries (
398
+ ).devices ().modifyCloudToDeviceConfig (
399
+ name = device_path , body = config_body ).execute ()
400
+
401
+
381
402
def parse_command_line_args ():
382
403
"""Parse command line arguments."""
383
404
default_registry = 'cloudiot_device_manager_example_registry_{}' .format (
@@ -396,31 +417,39 @@ def parse_command_line_args():
396
417
397
418
# Optional arguments
398
419
parser .add_argument (
399
- '--project_id' ,
400
- default = os .environ .get ("GOOGLE_CLOUD_PROJECT" ),
401
- help = 'GCP cloud project name.' )
420
+ '--cloud_region' , default = 'us-central1' , help = 'GCP cloud region' )
421
+ parser .add_argument (
422
+ '--config' ,
423
+ default = None ,
424
+ help = 'Configuration sent to a device.' )
425
+ parser .add_argument (
426
+ '--device_id' ,
427
+ default = None ,
428
+ help = 'Device id.' )
402
429
parser .add_argument (
403
430
'--ec_public_key_file' ,
404
431
default = None ,
405
432
help = 'Path to public ES256 key file.' )
433
+ parser .add_argument (
434
+ '--project_id' ,
435
+ default = os .environ .get ("GOOGLE_CLOUD_PROJECT" ),
436
+ help = 'GCP cloud project name.' )
437
+ parser .add_argument (
438
+ '--registry_id' ,
439
+ default = default_registry ,
440
+ help = 'Registry id. If not set, a name will be generated.' )
406
441
parser .add_argument (
407
442
'--rsa_certificate_file' ,
408
443
default = None ,
409
444
help = 'Path to RS256 certificate file.' )
410
- parser .add_argument (
411
- '--cloud_region' , default = 'us-central1' , help = 'GCP cloud region' )
412
445
parser .add_argument (
413
446
'--service_account_json' ,
414
447
default = os .environ .get ("GOOGLE_APPLICATION_CREDENTIALS" ),
415
448
help = 'Path to service account json file.' )
416
449
parser .add_argument (
417
- '--registry_id' ,
418
- default = default_registry ,
419
- help = 'Registry id. If not set, a name will be generated.' )
420
- parser .add_argument (
421
- '--device_id' ,
450
+ '--version' ,
422
451
default = None ,
423
- help = 'Device id .' )
452
+ help = 'Version number for setting device configuration .' )
424
453
425
454
# Command subparser
426
455
command = parser .add_subparsers (dest = 'command' )
@@ -439,16 +468,13 @@ def parse_command_line_args():
439
468
command .add_parser ('list-registries' , help = list_registries .__doc__ )
440
469
command .add_parser ('patch-es256' , help = patch_es256_auth .__doc__ )
441
470
command .add_parser ('patch-rs256' , help = patch_rsa256_auth .__doc__ )
471
+ command .add_parser ('set-config' , help = patch_rsa256_auth .__doc__ )
442
472
443
473
return parser .parse_args ()
444
474
445
475
446
- def run_command (args ):
447
- """Calls the program using the specified command."""
448
- if args .project_id is None :
449
- print ('You must specify a project ID or set the environment variable.' )
450
- return
451
-
476
+ def run_create (args ):
477
+ """Handles commands that create devices, registries, or topics."""
452
478
if args .command == 'create-rsa256' :
453
479
create_rs256_device (
454
480
args .service_account_json , args .project_id ,
@@ -474,6 +500,16 @@ def run_command(args):
474
500
elif args .command == 'create-topic' :
475
501
create_iot_topic (args .project_id , args .pubsub_topic )
476
502
503
+
504
+ def run_command (args ):
505
+ """Calls the program using the specified command."""
506
+ if args .project_id is None :
507
+ print ('You must specify a project ID or set the environment variable.' )
508
+ return
509
+
510
+ if args .command .startswith ('create' ):
511
+ run_create (args )
512
+
477
513
elif args .command == 'delete-device' :
478
514
delete_device (
479
515
args .service_account_json , args .project_id ,
@@ -494,16 +530,16 @@ def run_command(args):
494
530
args .service_account_json , args .project_id ,
495
531
args .cloud_region , args .registry_id , args .device_id )
496
532
497
- elif args .command == 'list' :
498
- list_devices (
499
- args .service_account_json , args .project_id ,
500
- args .cloud_region , args .registry_id )
501
-
502
533
elif args .command == 'get-registry' :
503
534
print (get_registry (
504
535
args .service_account_json , args .project_id ,
505
536
args .cloud_region , args .registry_id ))
506
537
538
+ elif args .command == 'list' :
539
+ list_devices (
540
+ args .service_account_json , args .project_id ,
541
+ args .cloud_region , args .registry_id )
542
+
507
543
elif args .command == 'list-registries' :
508
544
list_registries (
509
545
args .service_account_json , args .project_id ,
@@ -525,6 +561,16 @@ def run_command(args):
525
561
args .cloud_region , args .registry_id , args .device_id ,
526
562
args .rsa_certificate_file )
527
563
564
+ elif args .command == 'set-config' :
565
+ if (args .config is None ):
566
+ sys .exit ('Error: specify --config' )
567
+ if (args .version is None ):
568
+ sys .exit ('Error: specify --version' )
569
+ set_config (
570
+ args .service_account_json , args .project_id ,
571
+ args .cloud_region , args .registry_id , args .device_id ,
572
+ args .version , args .config )
573
+
528
574
529
575
def main ():
530
576
args = parse_command_line_args ()
0 commit comments