Skip to content
This repository has been archived by the owner on Oct 29, 2023. It is now read-only.

Commit

Permalink
Final additions in private beta [(#1136)](GoogleCloudPlatform/python-…
Browse files Browse the repository at this point in the history
…docs-samples#1136)

* Final additions in private beta

* Adds HTTP client and state support

* Fixes rouge space

* Remove invalid message on error.
  • Loading branch information
gguuss authored and Jon Wayne Parrott committed Sep 26, 2017
1 parent a2f75ea commit 0ad6027
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
38 changes: 33 additions & 5 deletions samples/api-client/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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 @@ -173,7 +173,7 @@ def delete_device(


def delete_registry(
service_account_json, api_key, project_id, cloud_region, registry_id):
service_account_json, api_key, project_id, cloud_region, registry_id):
"""Deletes the specified registry."""
print('Delete registry')
client = get_client(service_account_json, api_key)
Expand Down Expand Up @@ -216,6 +216,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 @@ -261,9 +278,9 @@ def create_registry(
project_id,
cloud_region)
body = {
'eventNotificationConfig': {
'eventNotificationConfigs': [{
'pubsubTopicName': pubsub_topic
},
}],
'id': registry_id
}
request = client.projects().locations().registries().create(
Expand All @@ -274,6 +291,7 @@ def create_registry(
print('Created registry')
return response
except HttpError:
print('Error, registry not created')
return ""


Expand Down Expand Up @@ -425,7 +443,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-registry', help=get_registry.__doc__)
command.add_parser('get-state', help=get_state.__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 +455,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 +499,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 samples/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 0ad6027

Please sign in to comment.