From 7fffab2cb0af4cf2851ce9438ce07e55dc7acf3c Mon Sep 17 00:00:00 2001 From: noerog <32459203+noerog@users.noreply.github.com> Date: Thu, 11 Jan 2018 17:11:12 -0500 Subject: [PATCH] Add sample to get config versions for a device. [(#1310)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1310) --- samples/api-client/manager/manager.py | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/samples/api-client/manager/manager.py b/samples/api-client/manager/manager.py index b3658973..b1e6df6b 100644 --- a/samples/api-client/manager/manager.py +++ b/samples/api-client/manager/manager.py @@ -399,6 +399,29 @@ def set_config( name=device_path, body=config_body).execute() +def get_config_versions( + service_account_json, project_id, cloud_region, registry_id, + device_id): + """Lists versions of a device config in descending order (newest first).""" + client = get_client(service_account_json) + registry_name = 'projects/{}/locations/{}/registries/{}'.format( + project_id, cloud_region, registry_id) + + device_name = '{}/devices/{}'.format(registry_name, device_id) + devices = client.projects().locations().registries().devices() + configs = devices.configVersions().list( + name=device_name).execute().get( + 'deviceConfigs', []) + + for config in configs: + print('version: {}\n\tcloudUpdateTime: {}\n\t binaryData: {}'.format( + config.get('version'), + config.get('cloudUpdateTime'), + config.get('binaryData'))) + + return configs + + def parse_command_line_args(): """Parse command line arguments.""" default_registry = 'cloudiot_device_manager_example_registry_{}'.format( @@ -469,6 +492,7 @@ def parse_command_line_args(): 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__) + command.add_parser('get-config-versions', help=get_config_versions.__doc__) return parser.parse_args() @@ -571,6 +595,11 @@ def run_command(args): args.cloud_region, args.registry_id, args.device_id, args.version, args.config) + elif args.command == 'get-config-versions': + get_device( + args.service_account_json, args.project_id, + args.cloud_region, args.registry_id, args.device_id) + def main(): args = parse_command_line_args()