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 f59dffd

Browse filesBrowse files
gguussJon Wayne Parrott
authored andcommitted
Adds set-config to manager sample (GoogleCloudPlatform#1275)
1 parent 9ef0556 commit f59dffd
Copy full SHA for f59dffd

File tree

Expand file treeCollapse file tree

2 files changed

+96
-22
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+96
-22
lines changed

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

Copy file name to clipboardExpand all lines: iot/api-client/manager/manager.py
+68-22Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"""
3232

3333
import argparse
34+
import base64
3435
import io
3536
import os
3637
import sys
@@ -378,6 +379,26 @@ def patch_rsa256_auth(
378379
name=device_name, updateMask='credentials', body=patch).execute()
379380

380381

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+
381402
def parse_command_line_args():
382403
"""Parse command line arguments."""
383404
default_registry = 'cloudiot_device_manager_example_registry_{}'.format(
@@ -396,31 +417,39 @@ def parse_command_line_args():
396417

397418
# Optional arguments
398419
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.')
402429
parser.add_argument(
403430
'--ec_public_key_file',
404431
default=None,
405432
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.')
406441
parser.add_argument(
407442
'--rsa_certificate_file',
408443
default=None,
409444
help='Path to RS256 certificate file.')
410-
parser.add_argument(
411-
'--cloud_region', default='us-central1', help='GCP cloud region')
412445
parser.add_argument(
413446
'--service_account_json',
414447
default=os.environ.get("GOOGLE_APPLICATION_CREDENTIALS"),
415448
help='Path to service account json file.')
416449
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',
422451
default=None,
423-
help='Device id.')
452+
help='Version number for setting device configuration.')
424453

425454
# Command subparser
426455
command = parser.add_subparsers(dest='command')
@@ -439,16 +468,13 @@ def parse_command_line_args():
439468
command.add_parser('list-registries', help=list_registries.__doc__)
440469
command.add_parser('patch-es256', help=patch_es256_auth.__doc__)
441470
command.add_parser('patch-rs256', help=patch_rsa256_auth.__doc__)
471+
command.add_parser('set-config', help=patch_rsa256_auth.__doc__)
442472

443473
return parser.parse_args()
444474

445475

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."""
452478
if args.command == 'create-rsa256':
453479
create_rs256_device(
454480
args.service_account_json, args.project_id,
@@ -474,6 +500,16 @@ def run_command(args):
474500
elif args.command == 'create-topic':
475501
create_iot_topic(args.project_id, args.pubsub_topic)
476502

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+
477513
elif args.command == 'delete-device':
478514
delete_device(
479515
args.service_account_json, args.project_id,
@@ -494,16 +530,16 @@ def run_command(args):
494530
args.service_account_json, args.project_id,
495531
args.cloud_region, args.registry_id, args.device_id)
496532

497-
elif args.command == 'list':
498-
list_devices(
499-
args.service_account_json, args.project_id,
500-
args.cloud_region, args.registry_id)
501-
502533
elif args.command == 'get-registry':
503534
print(get_registry(
504535
args.service_account_json, args.project_id,
505536
args.cloud_region, args.registry_id))
506537

538+
elif args.command == 'list':
539+
list_devices(
540+
args.service_account_json, args.project_id,
541+
args.cloud_region, args.registry_id)
542+
507543
elif args.command == 'list-registries':
508544
list_registries(
509545
args.service_account_json, args.project_id,
@@ -525,6 +561,16 @@ def run_command(args):
525561
args.cloud_region, args.registry_id, args.device_id,
526562
args.rsa_certificate_file)
527563

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+
528574

529575
def main():
530576
args = parse_command_line_args()

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

Copy file name to clipboardExpand all lines: iot/api-client/manager/manager_test.py
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,34 @@ def test_add_delete_unauth_device(test_topic, capsys):
8585
assert 'UNAUTH' in out
8686

8787

88+
def test_add_config_unauth_device(test_topic, capsys):
89+
device_id = device_id_template.format('UNAUTH')
90+
manager.open_registry(
91+
service_account_json, project_id, cloud_region, pubsub_topic,
92+
registry_id)
93+
94+
manager.create_unauth_device(
95+
service_account_json, project_id, cloud_region, registry_id,
96+
device_id)
97+
98+
manager.set_config(
99+
service_account_json, project_id, cloud_region, registry_id,
100+
device_id, 0, 'test')
101+
102+
manager.get_device(
103+
service_account_json, project_id, cloud_region, registry_id,
104+
device_id)
105+
106+
manager.delete_device(
107+
service_account_json, project_id, cloud_region, registry_id,
108+
device_id)
109+
110+
out, _ = capsys.readouterr()
111+
assert 'Set device configuration' in out
112+
assert 'UNAUTH' in out
113+
assert 'version: 2' in out
114+
115+
88116
def test_add_delete_rs256_device(test_topic, capsys):
89117
device_id = device_id_template.format('RSA256')
90118
manager.open_registry(

0 commit comments

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