Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds set-config to manager sample #1275

Merged
merged 1 commit into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 68 additions & 22 deletions iot/api-client/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"""

import argparse
import base64
import io
import os
import sys
Expand Down Expand Up @@ -378,6 +379,26 @@ def patch_rsa256_auth(
name=device_name, updateMask='credentials', body=patch).execute()


def set_config(
service_account_json, project_id, cloud_region, registry_id, device_id,
version, config):
print('Set device configuration')
client = get_client(service_account_json)
device_path = 'projects/{}/locations/{}/registries/{}/devices/{}'.format(
project_id, cloud_region, registry_id, device_id)

config_body = {
'versionToUpdate': version,
'binaryData': base64.urlsafe_b64encode(
config.encode('utf-8')).decode('ascii')
}

return client.projects(
).locations().registries(
).devices().modifyCloudToDeviceConfig(
name=device_path, body=config_body).execute()


def parse_command_line_args():
"""Parse command line arguments."""
default_registry = 'cloudiot_device_manager_example_registry_{}'.format(
Expand All @@ -396,31 +417,39 @@ def parse_command_line_args():

# Optional arguments
parser.add_argument(
'--project_id',
default=os.environ.get("GOOGLE_CLOUD_PROJECT"),
help='GCP cloud project name.')
'--cloud_region', default='us-central1', help='GCP cloud region')
parser.add_argument(
'--config',
default=None,
help='Configuration sent to a device.')
parser.add_argument(
'--device_id',
default=None,
help='Device id.')
parser.add_argument(
'--ec_public_key_file',
default=None,
help='Path to public ES256 key file.')
parser.add_argument(
'--project_id',
default=os.environ.get("GOOGLE_CLOUD_PROJECT"),
help='GCP cloud project name.')
parser.add_argument(
'--registry_id',
default=default_registry,
help='Registry id. If not set, a name will be generated.')
parser.add_argument(
'--rsa_certificate_file',
default=None,
help='Path to RS256 certificate file.')
parser.add_argument(
'--cloud_region', default='us-central1', help='GCP cloud region')
parser.add_argument(
'--service_account_json',
default=os.environ.get("GOOGLE_APPLICATION_CREDENTIALS"),
help='Path to service account json file.')
parser.add_argument(
'--registry_id',
default=default_registry,
help='Registry id. If not set, a name will be generated.')
parser.add_argument(
'--device_id',
'--version',
default=None,
help='Device id.')
help='Version number for setting device configuration.')

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

return parser.parse_args()


def run_command(args):
"""Calls the program using the specified command."""
if args.project_id is None:
print('You must specify a project ID or set the environment variable.')
return

def run_create(args):
"""Handles commands that create devices, registries, or topics."""
if args.command == 'create-rsa256':
create_rs256_device(
args.service_account_json, args.project_id,
Expand All @@ -474,6 +500,16 @@ def run_command(args):
elif args.command == 'create-topic':
create_iot_topic(args.project_id, args.pubsub_topic)


def run_command(args):
"""Calls the program using the specified command."""
if args.project_id is None:
print('You must specify a project ID or set the environment variable.')
return

if args.command.startswith('create'):
run_create(args)

elif args.command == 'delete-device':
delete_device(
args.service_account_json, args.project_id,
Expand All @@ -494,16 +530,16 @@ def run_command(args):
args.service_account_json, args.project_id,
args.cloud_region, args.registry_id, args.device_id)

elif args.command == 'list':
list_devices(
args.service_account_json, args.project_id,
args.cloud_region, args.registry_id)

elif args.command == 'get-registry':
print(get_registry(
args.service_account_json, args.project_id,
args.cloud_region, args.registry_id))

elif args.command == 'list':
list_devices(
args.service_account_json, args.project_id,
args.cloud_region, args.registry_id)

elif args.command == 'list-registries':
list_registries(
args.service_account_json, args.project_id,
Expand All @@ -525,6 +561,16 @@ def run_command(args):
args.cloud_region, args.registry_id, args.device_id,
args.rsa_certificate_file)

elif args.command == 'set-config':
if (args.config is None):
sys.exit('Error: specify --config')
if (args.version is None):
sys.exit('Error: specify --version')
set_config(
args.service_account_json, args.project_id,
args.cloud_region, args.registry_id, args.device_id,
args.version, args.config)


def main():
args = parse_command_line_args()
Expand Down
28 changes: 28 additions & 0 deletions iot/api-client/manager/manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,34 @@ def test_add_delete_unauth_device(test_topic, capsys):
assert 'UNAUTH' in out


def test_add_config_unauth_device(test_topic, capsys):
device_id = device_id_template.format('UNAUTH')
manager.open_registry(
service_account_json, project_id, cloud_region, pubsub_topic,
registry_id)

manager.create_unauth_device(
service_account_json, project_id, cloud_region, registry_id,
device_id)

manager.set_config(
service_account_json, project_id, cloud_region, registry_id,
device_id, 0, 'test')

manager.get_device(
service_account_json, project_id, cloud_region, registry_id,
device_id)

manager.delete_device(
service_account_json, project_id, cloud_region, registry_id,
device_id)

out, _ = capsys.readouterr()
assert 'Set device configuration' in out
assert 'UNAUTH' in out
assert 'version: 2' in out


def test_add_delete_rs256_device(test_topic, capsys):
device_id = device_id_template.format('RSA256')
manager.open_registry(
Expand Down