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

Commit

Permalink
Updates library versions and tests [(#1884)](GoogleCloudPlatform/pyth…
Browse files Browse the repository at this point in the history
…on-docs-samples#1884)

* Updates library versions and tests
  • Loading branch information
gguuss authored Dec 4, 2018
1 parent d90b81d commit 9b4205b
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 24 deletions.
39 changes: 34 additions & 5 deletions samples/api-client/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,28 @@ def set_iam_permissions(
# [END iot_set_iam_policy]


def send_command(
service_account_json, project_id, cloud_region, registry_id, device_id,
command):
"""Send a command to a device."""
# [START iot_send_command]
print('Sending command to device')
client = get_client(service_account_json)
device_path = 'projects/{}/locations/{}/registries/{}/devices/{}'.format(
project_id, cloud_region, registry_id, device_id)

config_body = {
'binaryData': base64.urlsafe_b64encode(
command.encode('utf-8')).decode('ascii')
}

return client.projects(
).locations().registries(
).devices().sendCommandToDevice(
name=device_path, body=config_body).execute()
# [END iot_send_command]


def parse_command_line_args():
"""Parse command line arguments."""
default_registry = 'cloudiot_device_manager_example_registry_{}'.format(
Expand Down Expand Up @@ -546,6 +568,10 @@ def parse_command_line_args():
'--role',
default=None,
help='Role used for IAM commands.')
parser.add_argument(
'--send_command',
default='1',
help='The command sent to the device')

# Command subparser
command = parser.add_subparsers(dest='command')
Expand All @@ -566,6 +592,7 @@ 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('send-command', help=send_command.__doc__)
command.add_parser('set-config', help=patch_rsa256_auth.__doc__)
command.add_parser('set-iam-permissions', help=set_iam_permissions.__doc__)

Expand Down Expand Up @@ -679,6 +706,12 @@ def run_command(args):
args.cloud_region, args.registry_id, args.device_id,
args.rsa_certificate_file)

elif args.command == 'send-command':
send_command(
args.service_account_json, args.project_id,
args.cloud_region, args.registry_id, args.device_id,
args.send_command)

elif args.command == 'set-iam-permissions':
if (args.member is None):
sys.exit('Error: specify --member')
Expand All @@ -699,10 +732,6 @@ def run_command(args):
args.version, args.config)


def main():
if __name__ == '__main__':
args = parse_command_line_args()
run_command(args)


if __name__ == '__main__':
main()
49 changes: 49 additions & 0 deletions samples/api-client/manager/manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@
# limitations under the License.

import os
import sys
import time

# Add command receiver for bootstrapping device registry / device for testing
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'mqtt_example')) # noqa
from google.cloud import pubsub
import pytest

import manager
import cloudiot_mqtt_example

cloud_region = 'us-central1'
device_id_template = 'test-device-{}'
ca_cert_path = '../mqtt_example/resources/roots.pem'
es_cert_path = 'resources/ec_public.pem'
rsa_cert_path = 'resources/rsa_cert.pem'
rsa_private_path = 'resources/rsa_private.pem' # Must match rsa_cert
topic_id = 'test-device-events-{}'.format(int(time.time()))

project_id = os.environ['GCLOUD_PROJECT']
Expand Down Expand Up @@ -270,3 +276,46 @@ def test_add_patch_delete_es256(test_topic, capsys):

manager.delete_registry(
service_account_json, project_id, cloud_region, registry_id)


def test_send_command(test_topic, capsys):
device_id = device_id_template.format('RSA256')
manager.create_registry(
service_account_json, project_id, cloud_region, pubsub_topic,
registry_id)
manager.create_rs256_device(
service_account_json, project_id, cloud_region, registry_id,
device_id, rsa_cert_path)

# Exercize the functionality
client = cloudiot_mqtt_example.get_client(
project_id, cloud_region, registry_id, device_id,
rsa_private_path, 'RS256', ca_cert_path,
'mqtt.googleapis.com', 443)
client.loop_start()
out, _ = capsys.readouterr()

# Pre-process commands
for i in range(1, 5):
client.loop()
time.sleep(1)

manager.send_command(
service_account_json, project_id, cloud_region, registry_id,
device_id, 'me want cookies')
out, _ = capsys.readouterr()

# Process commands
for i in range(1, 5):
client.loop()
time.sleep(1)

# Clean up
manager.delete_device(
service_account_json, project_id, cloud_region, registry_id,
device_id)
manager.delete_registry(
service_account_json, project_id, cloud_region, registry_id)

assert 'Sending command to device' in out
assert '400' not in out
8 changes: 6 additions & 2 deletions samples/api-client/manager/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
google-api-python-client==1.7.4
cryptography==2.4.2
google-api-python-client==1.7.5
google-auth-httplib2==0.0.3
google-auth==1.6.1
google-cloud-pubsub==0.38.0
google-cloud-pubsub==0.39.0
oauth2client==4.1.3
paho-mqtt==1.4.0
pyjwt==1.6.4
33 changes: 16 additions & 17 deletions samples/api-client/manager/resources/rsa_cert.pem
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
-----BEGIN CERTIFICATE-----
MIIDFzCCAf+gAwIBAgIJALsqlqk6FkVjMA0GCSqGSIb3DQEBBQUAMBExDzANBgNV
BAMTBnVudXNlZDAeFw0xNzEyMDYyMjQwNDRaFw0yNzEyMDQyMjQwNDRaMBExDzAN
BgNVBAMTBnVudXNlZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALZg
rgM97eLsTl3ul4m07pVHE/g9f0VP6/MDQcAjqj9kRmg5XMo/E6eRbNcIVwKEGHwC
SuSvGO/j7reYN4cbjvBqimc8asecg5rtXjaDBm66feB/ktOjASSfGfuN79JOJf+r
/BNcCrs8qWa1FSPEnQO7VTTUstkDKB8uvBFPOiPNVw438KM2lId2G/i63soMes0m
9RjPdXbRqJI39WdMVDYXIqLSVIX11xOXvroetwDPkHIi4Tjzus1T6KMjwKD3f3sm
DeUJeu4f1ZV4LPhhW62O9fkKeGy55LglfqOmWusySdOoNgEocN4V3iDQmJ7QOEXw
yPX+ZhhPHuptDwBTWJcCAwEAAaNyMHAwHQYDVR0OBBYEFD7D1XyWbYOiMWDgDyBs
po+JmlViMEEGA1UdIwQ6MDiAFD7D1XyWbYOiMWDgDyBspo+JmlVioRWkEzARMQ8w
DQYDVQQDEwZ1bnVzZWSCCQC7KpapOhZFYzAMBgNVHRMEBTADAQH/MA0GCSqGSIb3
DQEBBQUAA4IBAQAl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sRPXJbj2KoM07UUXMgUe57daYm72wDfKZpvG5qwybkkym+NnahJY+C
u9FX1dTBjM/TqPWKI817mDp5W31a+q6DXdggG+Yf6pz0dMXGGzRtSTEpLKsKtXqe
Y9f7266JCx9V5QFK14SmpIdBF38G0bcNPEvRJ6uKaVKBnU4+7o4YlQLuDczT29Tp
CXL0egUViNT9kv03Pj9iSPR6EGcmOjnSZe1SFVg9OeCauF1wuuFCxUuCkWSEkEm7
laNke9PHHTe9BoBxKMsTFEPivhVaAf9fUp+NQxZNmdgbux9AAlJgiyU0sFg=
-----END CERTIFICATE-----
28 changes: 28 additions & 0 deletions samples/api-client/manager/resources/rsa_private.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDqwG/uVzCIMMpe
vgpHSZCiqeH/toRwDqEqkK6I9jEf4uNWMrFGygpPCnUEmakwbSL62HX+7Osw1d85
otywQR1+jBp8/9Y+WCF5bowYbJ0E0W07lI4w8feopNgtq0dcO6JAOzJgbHVc0XvI
OaXzUBnVdJdeVGmSvqhZS4NMc2rDzypmBRVGyl3lj2iiZrcPXf5emvI2/O38CNy0
1o2a+gZOy+GX9Uw5Uw66DLzZZFvWfcUpHIAu+xIzLQu0WMcW3pWsXaiPSHcSKZhM
YOqSjCRLyTQNdRmIU5QMZCTYe4HdFpWL0JcAyOw+jST2wppuGnNT9gANTBR1YMaC
gBZP/Fd/AgMBAAECggEBAOevf4kColJ3nPM+qlRLJaV09yjyUOlrduLUon1oRXmL
6wUCyPXtp5j04CLnKRUzUUezZVlxKHotSr/OnfKSgXKJAgeGVEN5paf8U+YzJBFC
RIV+C4wA84WNFBKWrbo43Nx50DFcOcSet4UYaFGoJ6cFB/PAaeW7p9lhbreAXcnb
g2z4SBsGGO2ZKZkSOcXg39GKX5S0bsEhHzIOFvPBdDQpaVAx/Bq76iBzVz1bRwGp
A3SjJn0g4V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