Skip to content

Commit df3afad

Browse files
authored
* Adds gateway control plane examples for IoT Core
1 parent 42bc3cb commit df3afad

11 files changed

+602
-30
lines changed

iot/README.md

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,33 @@ IOT API from PHP. These samples are best seen in the context of the
3131
5. Run `php iot.php`. The following commands are available:
3232

3333
```
34-
create-es-device Create a new device with the given id, using ES256 for authentication.
35-
create-registry Creates a registry and returns the result.
36-
create-rsa-device Create a new device with the given id, using RS256 for authentication.
37-
create-unauth-device Create a new device without authentication.
38-
delete-device Delete the device with the given id.
39-
delete-registry Deletes the specified registry.
40-
get-device Retrieve the device with the given id.
41-
get-device-configs Lists versions of a device config in descending order (newest first).
42-
get-device-state Retrieve a device's state blobs.
43-
get-iam-policy Retrieves IAM permissions for the given registry.
44-
get-registry Retrieves a device registry.
45-
help Displays help for a command
46-
list Lists commands
47-
list-devices List all devices in the registry.
48-
list-registries List all registries in the project.
49-
patch-es-device Patch device with ES256 public key.
50-
patch-rsa-device Patch device with RSA256 certificate.
51-
set-device-config Patch device with RSA256 certificate.
52-
set-device-state Sets the state of a device.
53-
set-iam-policy Sets IAM permissions for the given registry to a single role/member. ```
34+
bind-device-to-gateway (Beta feature) Bind a device to a gateway.
35+
create-es-device Create a new device with the given id, using ES256 for authentication.
36+
create-gateway (Beta feature) Create a new gateway with the given id.
37+
create-registry Creates a registry and returns the result.
38+
create-rsa-device Create a new device with the given id, using RS256 for authentication.
39+
create-unauth-device Create a new device without authentication.
40+
delete-device Delete the device with the given id.
41+
delete-gateway (Beta feature) Delete the gateway with the given id.
42+
delete-registry Deletes the specified registry.
43+
get-device Retrieve the device with the given id.
44+
get-device-configs Lists versions of a device config in descending order (newest first).
45+
get-device-state Retrieve a device's state blobs.
46+
get-iam-policy Retrieves IAM permissions for the given registry.
47+
get-registry Retrieves a device registry.
48+
help Displays help for a command
49+
list Lists commands
50+
list-devices List all devices in the registry.
51+
list-devices-for-gateway List devices for the given gateway.
52+
list-gateways List gateways for the given registry.
53+
list-registries List all registries in the project.
54+
patch-es-device Patch device with ES256 public key.
55+
patch-rsa-device Patch device with RSA256 certificate.
56+
send-command-to-device Sends a command to a device.
57+
set-device-config Set a device's configuration.
58+
set-device-state Sets the state of a device.
59+
set-iam-policy Sets IAM permissions for the given registry to a single role/member.
60+
unbind-device-from-gateway (Beta feature) Unbind a device from a gateway.
5461
5562
Example:
5663

iot/composer.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,40 @@
22
"name": "google/iot-sample",
33
"type": "project",
44
"require": {
5-
"google/cloud-iot": "^0.5.0",
5+
"google/cloud-iot": "^0.6.0",
66
"symfony/console": "^3.3"
77
},
88
"autoload": {
99
"files": [
10+
"src/bind_device_to_gateway.php",
1011
"src/create_es_device.php",
12+
"src/create_gateway.php",
1113
"src/create_registry.php",
1214
"src/create_rsa_device.php",
1315
"src/create_unauth_device.php",
1416
"src/delete_device.php",
17+
"src/delete_gateway.php",
1518
"src/delete_registry.php",
1619
"src/get_iam_policy.php",
1720
"src/get_device.php",
1821
"src/get_device_state.php",
1922
"src/get_device_configs.php",
2023
"src/get_registry.php",
2124
"src/list_devices.php",
25+
"src/list_devices_for_gateway.php",
26+
"src/list_gateways.php",
2227
"src/list_registries.php",
2328
"src/patch_es.php",
2429
"src/patch_rsa.php",
2530
"src/send_command_to_device.php",
2631
"src/set_device_config.php",
2732
"src/set_device_state.php",
28-
"src/set_iam_policy.php"
33+
"src/set_iam_policy.php",
34+
"src/unbind_device_from_gateway.php"
2935
]
3036
},
3137
"require-dev": {
32-
"phpunit/phpunit": "^5"
38+
"phpunit/phpunit": "^5",
39+
"google/cloud-tools": "^0.8.5"
3340
}
3441
}

iot/iot.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,103 @@
319319
);
320320
});
321321

322+
// Beta features
323+
$application->add(new Command('create-gateway'))
324+
->addOption('project', '', InputOption::VALUE_REQUIRED, 'The Google Cloud project ID', getenv('GCLOUD_PROJECT'))
325+
->addOption('location', '', InputOption::VALUE_REQUIRED, 'The location of your device registries', 'us-central1')
326+
->addArgument('registry', InputArgument::REQUIRED, 'the registry ID')
327+
->addArgument('gateway', InputArgument::REQUIRED, 'the gateway ID')
328+
->addArgument('certificate-file', InputArgument::REQUIRED, 'Path to public key file')
329+
->addArgument('algorithm', InputArgument::REQUIRED, 'The algorithm (RS256|ES256) used for the public key')
330+
->setDescription('(Beta feature) Create a new gateway with the given id.')
331+
->setCode(function ($input, $output) {
332+
create_gateway(
333+
$input->getOption('project'),
334+
$input->getOption('location'),
335+
$input->getArgument('registry'),
336+
$input->getArgument('gateway'),
337+
$input->getArgument('certificate-file'),
338+
$input->getArgument('algorithm')
339+
);
340+
});
341+
342+
$application->add(new Command('delete-gateway'))
343+
->addOption('project', '', InputOption::VALUE_REQUIRED, 'The Google Cloud project ID', getenv('GCLOUD_PROJECT'))
344+
->addOption('location', '', InputOption::VALUE_REQUIRED, 'The location of your device registries', 'us-central1')
345+
->addArgument('registry', InputArgument::REQUIRED, 'the registry ID')
346+
->addArgument('gateway', InputArgument::REQUIRED, 'the gateway ID')
347+
->setDescription('(Beta feature) Delete the gateway with the given id.')
348+
->setCode(function ($input, $output) {
349+
delete_gateway(
350+
$input->getOption('project'),
351+
$input->getOption('location'),
352+
$input->getArgument('registry'),
353+
$input->getArgument('gateway')
354+
);
355+
});
356+
357+
$application->add(new Command('list-gateways'))
358+
->addArgument('registry', InputArgument::REQUIRED, 'the registry ID')
359+
->addOption('project', '', InputOption::VALUE_REQUIRED, 'The Google Cloud project ID', getenv('GCLOUD_PROJECT'))
360+
->addOption('location', '', InputOption::VALUE_REQUIRED, 'The location of your device registries', 'us-central1')
361+
->setDescription('(Beta feature) List gateways for the given registry.')
362+
->setCode(function ($input, $output) {
363+
list_gateways(
364+
$input->getOption('project'),
365+
$input->getOption('location'),
366+
$input->getArgument('registry')
367+
);
368+
});
369+
370+
$application->add(new Command('list-devices-for-gateway'))
371+
->addOption('project', '', InputOption::VALUE_REQUIRED, 'The Google Cloud project ID', getenv('GCLOUD_PROJECT'))
372+
->addOption('location', '', InputOption::VALUE_REQUIRED, 'The location of your device registries', 'us-central1')
373+
->addArgument('registry', InputArgument::REQUIRED, 'the registry ID')
374+
->addArgument('gateway', InputArgument::REQUIRED, 'the gateway ID')
375+
->setDescription('(Beta feature) List devices for the given gateway.')
376+
->setCode(function ($input, $output) {
377+
list_devices_for_gateway(
378+
$input->getOption('project'),
379+
$input->getOption('location'),
380+
$input->getArgument('registry'),
381+
$input->getArgument('gateway')
382+
);
383+
});
384+
385+
$application->add(new Command('bind-device-to-gateway'))
386+
->addOption('project', '', InputOption::VALUE_REQUIRED, 'The Google Cloud project ID', getenv('GCLOUD_PROJECT'))
387+
->addOption('location', '', InputOption::VALUE_REQUIRED, 'The location of your device registries', 'us-central1')
388+
->addArgument('registry', InputArgument::REQUIRED, 'the registry ID')
389+
->addArgument('device', InputArgument::REQUIRED, 'the device ID')
390+
->addArgument('gateway', InputArgument::REQUIRED, 'the gateway ID')
391+
->setDescription('(Beta feature) Bind a device to a gateway.')
392+
->setCode(function ($input, $output) {
393+
bind_device_to_gateway(
394+
$input->getOption('project'),
395+
$input->getOption('location'),
396+
$input->getArgument('registry'),
397+
$input->getArgument('gateway'),
398+
$input->getArgument('device')
399+
);
400+
});
401+
402+
$application->add(new Command('unbind-device-from-gateway'))
403+
->addArgument('registry', InputArgument::REQUIRED, 'the registry ID')
404+
->addArgument('device', InputArgument::REQUIRED, 'the device ID')
405+
->addArgument('gateway', InputArgument::REQUIRED, 'the gateway ID')
406+
->addOption('project', '', InputOption::VALUE_REQUIRED, 'The Google Cloud project ID', getenv('GCLOUD_PROJECT'))
407+
->addOption('location', '', InputOption::VALUE_REQUIRED, 'The location of your device registries', 'us-central1')
408+
->setDescription('(Beta feature) Unbind a device from a gateway.')
409+
->setCode(function ($input, $output) {
410+
unbind_device_from_gateway(
411+
$input->getOption('project'),
412+
$input->getOption('location'),
413+
$input->getArgument('registry'),
414+
$input->getArgument('gateway'),
415+
$input->getArgument('device')
416+
);
417+
});
418+
322419
// for testing
323420
if (getenv('PHPUNIT_TESTS') === '1') {
324421
return $application;

iot/phpunit.xml.dist

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
3-
Copyright 2018 Google Inc.
3+
Copyright 2019 Google Inc.
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -14,8 +14,7 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
-->
17-
<phpunit bootstrap="./vendor/autoload.php"
18-
convertNoticesToExceptions="false">
17+
<phpunit bootstrap="./vendor/autoload.php">
1918
<testsuites>
2019
<testsuite name="PHP IOT test">
2120
<directory>test</directory>

iot/src/bind_device_to_gateway.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2019 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
namespace Google\Cloud\Samples\Iot;
19+
20+
# [START iot_bind_device_to_gateway]
21+
use Google\Cloud\Iot\V1\DeviceManagerClient;
22+
23+
/**
24+
* Binds a device to a gateway.
25+
*
26+
* @param string $projectId Google Cloud project ID
27+
* @param string $location Google Cloud region
28+
* @param string $registryId IOT Device Registry ID
29+
* @param string $deviceId the device ID to bind
30+
* @param string $gatewayId the ID for the gateway to bind to
31+
*/
32+
function bind_device_to_gateway(
33+
$projectId,
34+
$location = 'us-central1',
35+
$registryId,
36+
$gatewayId,
37+
$deviceId
38+
) {
39+
print('Binding Device to Gateway' . PHP_EOL);
40+
41+
// Instantiate a client.
42+
$deviceManager = new DeviceManagerClient();
43+
$registryName = $deviceManager->registryName($projectId, $location, $registryId);
44+
45+
$result = $deviceManager->bindDeviceToGateway($registryName, $gatewayId, $deviceId);
46+
47+
print('Device bound');
48+
}
49+
# [END iot_bind_device_to_gateway]

iot/src/create_gateway.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2019 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
namespace Google\Cloud\Samples\Iot;
19+
20+
# [START iot_create_gateway]
21+
use Google\Cloud\Iot\V1\DeviceManagerClient;
22+
use Google\Cloud\Iot\V1\Device;
23+
use Google\Cloud\Iot\V1\DeviceCredential;
24+
use Google\Cloud\Iot\V1\GatewayAuthMethod;
25+
use Google\Cloud\Iot\V1\GatewayConfig;
26+
use Google\Cloud\Iot\V1\GatewayType;
27+
use Google\Cloud\Iot\V1\PublicKeyCredential;
28+
use Google\Cloud\Iot\V1\PublicKeyFormat;
29+
30+
/**
31+
* Create a new gateway with the given id and certificate file.
32+
*
33+
* @param string $projectId (optional) Google Cloud project ID
34+
* @param string $location (Optional) Google Cloud region
35+
* @param string $registryId IOT Gateway Registry ID
36+
* @param string $gatewayId IOT Gateway ID
37+
* @param string $certificateFile Path to certificate file.
38+
* @param string $algorithm the algorithm used for JWT (ES256 or RS256).
39+
*/
40+
function create_gateway(
41+
$projectId,
42+
$location = 'us-central1',
43+
$registryId,
44+
$gatewayId,
45+
$certificateFile,
46+
$algorithm
47+
) {
48+
print('Creating new Gateway' . PHP_EOL);
49+
50+
// Instantiate a client.
51+
$deviceManager = new DeviceManagerClient();
52+
$registryName = $deviceManager->registryName($projectId, $location, $registryId);
53+
54+
$publicKeyFormat = PublicKeyFormat::ES256_PEM;
55+
if ($algorithm == 'RS256') {
56+
$publicKeyFormat = PublicKeyFormat::RSA_X509_PEM;
57+
}
58+
59+
$gatewayConfig = (new GatewayConfig())
60+
->setGatewayType(GatewayType::GATEWAY)
61+
->setGatewayAuthMethod(GatewayAuthMethod::ASSOCIATION_ONLY);
62+
63+
$publicKey = (new PublicKeyCredential())
64+
->setFormat($publicKeyFormat)
65+
->setKey(file_get_contents($certificateFile));
66+
67+
$credential = (new DeviceCredential())
68+
->setPublicKey($publicKey);
69+
70+
$device = (new Device())
71+
->setId($gatewayId)
72+
->setGatewayConfig($gatewayConfig)
73+
->setCredentials([$credential]);
74+
75+
$gateway = $deviceManager->createDevice($registryName, $device);
76+
77+
printf('Gateway: %s : %s' . PHP_EOL,
78+
$gateway->getNumId(),
79+
$gateway->getId());
80+
}
81+
# [END iot_create_gateway]

0 commit comments

Comments
 (0)