Skip to content

Commit

Permalink
Final additions in private beta
Browse files Browse the repository at this point in the history
  • Loading branch information
gguuss committed Sep 22, 2017
1 parent b9e2401 commit c275f57
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
34 changes: 32 additions & 2 deletions iot/api-client/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"""

import argparse
import base64
import io
import os
import sys
Expand Down Expand Up @@ -69,7 +70,7 @@ def get_client(service_account_json, api_key):
provided API key and creating a service object using the service account
credentials JSON."""
api_scopes = ['https://www.googleapis.com/auth/cloud-platform']
api_version = 'v1beta1'
api_version = 'v1'
discovery_api = 'https://cloudiot.googleapis.com/$discovery/rest'
service_name = 'cloudiotcore'

Expand Down Expand Up @@ -216,6 +217,23 @@ def get_device(
return device


def get_state(
service_account_json, api_key, project_id, cloud_region, registry_id,
device_id):
"""Retrieve a device's state blobs."""
client = get_client(service_account_json, api_key)
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()
state = devices.states().list(name=device_name, numStates=5).execute()

print('State: {}\n'.format(state))

return state


def list_devices(
service_account_json, api_key, project_id, cloud_region, registry_id):
"""List all devices in the registry."""
Expand Down Expand Up @@ -274,6 +292,8 @@ def create_registry(
print('Created registry')
return response
except HttpError:
print(dir(HttpError))
print('Error {}'.format(HttpError))
return ""


Expand Down Expand Up @@ -425,7 +445,8 @@ def parse_command_line_args():
command.add_parser('delete-device', help=delete_device.__doc__)
command.add_parser('delete-registry', help=delete_registry.__doc__)
command.add_parser('get', help=get_device.__doc__)
command.add_parser('get-registry', help=get_device.__doc__)
command.add_parser('get-state', help=get_state.__doc__)
command.add_parser('get-registry', help=get_registry.__doc__)
command.add_parser('list', help=list_devices.__doc__)
command.add_parser('list-registries', help=list_registries.__doc__)
command.add_parser('patch-es256', help=patch_es256_auth.__doc__)
Expand All @@ -436,6 +457,10 @@ def parse_command_line_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

if args.command == 'create-rsa256':
create_rs256_device(
args.service_account_json, args.api_key, args.project_id,
Expand Down Expand Up @@ -476,6 +501,11 @@ def run_command(args):
args.service_account_json, args.api_key, args.project_id,
args.cloud_region, args.registry_id, args.device_id)

elif args.command == 'get-state':
get_state(
args.service_account_json, args.api_key, args.project_id,
args.cloud_region, args.registry_id, args.device_id)

elif args.command == 'list':
list_devices(
args.service_account_json, args.api_key, args.project_id,
Expand Down
10 changes: 10 additions & 0 deletions iot/api-client/manager/manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def test_add_delete_rs256_device(test_topic, capsys):
service_account_json, api_key, project_id, cloud_region,
registry_id, device_id)

manager.get_state(
service_account_json, api_key, project_id, cloud_region,
registry_id, device_id)

manager.delete_device(
service_account_json, api_key, project_id, cloud_region,
registry_id, device_id)
Expand All @@ -111,6 +115,7 @@ def test_add_delete_rs256_device(test_topic, capsys):

out, _ = capsys.readouterr()
assert 'format : RSA_X509_PEM' in out
assert 'State : {}' in out


def test_add_delete_es256_device(test_topic, capsys):
Expand All @@ -127,6 +132,10 @@ def test_add_delete_es256_device(test_topic, capsys):
service_account_json, api_key, project_id, cloud_region,
registry_id, device_id)

manager.get_state(
service_account_json, api_key, project_id, cloud_region,
registry_id, device_id)

manager.delete_device(
service_account_json, api_key, project_id, cloud_region,
registry_id, device_id)
Expand All @@ -137,6 +146,7 @@ def test_add_delete_es256_device(test_topic, capsys):

out, _ = capsys.readouterr()
assert 'format : ES256_PEM' in out
assert 'State : {}' in out


def test_add_patch_delete_rs256(test_topic, capsys):
Expand Down

0 comments on commit c275f57

Please sign in to comment.