Skip to content

Commit f750195

Browse files
hongalexfhinkel
authored andcommitted
iot: update gateway samples for yargs (#1463)
1 parent c35c788 commit f750195

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

iot/manager/manager.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -942,14 +942,16 @@ const createGateway = async (
942942
registryId,
943943
gatewayId,
944944
publicKeyFormat,
945-
publicKeyFile
945+
publicKeyFile,
946+
gatewayAuthMethod
946947
) => {
947948
// [START iot_create_gateway]
948949
// const cloudRegion = 'us-central1';
949950
// const deviceId = 'my-unauth-device';
950951
// const gatewayId = 'my-gateway';
951952
// const projectId = 'adjective-noun-123';
952953
// const registryId = 'my-registry';
954+
// const gatewayAuthMethod = 'ASSOCIATION_ONLY';
953955
const parentName = `projects/${projectId}/locations/${cloudRegion}/registries/${registryId}`;
954956
console.log('Creating gateway:', gatewayId);
955957

@@ -974,7 +976,7 @@ const createGateway = async (
974976
credentials: credentials,
975977
gatewayConfig: {
976978
gatewayType: 'GATEWAY',
977-
gatewayAuthMethod: 'ASSOCIATION_ONLY',
979+
gatewayAuthMethod: gatewayAuthMethod,
978980
},
979981
},
980982
};
@@ -1669,22 +1671,49 @@ require(`yargs`) // eslint-disable-line
16691671
}
16701672
)
16711673
.command(
1672-
`createGateway <registryId> <gatewayId>`,
1674+
`createGateway`,
16731675
`Creates a gateway`,
16741676
{
1677+
registryId: {
1678+
description:
1679+
'Enter a permanent ID that starts with a lower case letter. Must end in a letter or number.',
1680+
requiresArg: true,
1681+
type: 'string',
1682+
},
1683+
gatewayId: {
1684+
description:
1685+
'Enter a permanent ID that starts with a lowercase letter. Must end in a letter or number',
1686+
requiresArg: true,
1687+
type: 'string',
1688+
},
16751689
publicKeyFormat: {
1690+
alias: 'format',
16761691
default: 'RSA_X509_PEM',
16771692
description: 'Public key format for devices.',
16781693
requiresArg: true,
16791694
choices: ['RSA_PEM', 'RSA_X509_PEM', 'ES256_PEM', 'ES256_X509_PEM'],
16801695
type: 'string',
16811696
},
16821697
publicKeyFile: {
1698+
alias: 'key',
16831699
description:
16841700
'Path to the public key file used for device authentication.',
16851701
requiresArg: true,
16861702
type: 'string',
16871703
},
1704+
gatewayAuthMethod: {
1705+
default: 'ASSOCIATION_ONLY',
1706+
description:
1707+
'Determines how Cloud IoT Core verifies and trusts devices associated with this gateway.',
1708+
requiresArg: true,
1709+
choices: [
1710+
'ASSOCIATION_ONLY',
1711+
'DEVICE_AUTH_TOKEN_ONLY',
1712+
'ASSOCIATION_AND_DEVICE_AUTH_TOKEN',
1713+
'GATEWAY_AUTH_METHOD_UNSPECIFIED',
1714+
],
1715+
type: 'string',
1716+
},
16881717
},
16891718
async opts => {
16901719
const client = await getClient(opts.serviceAccount);
@@ -1816,6 +1845,10 @@ require(`yargs`) // eslint-disable-line
18161845
.example(
18171846
`node $0 createRsa256Device my-rsa-device my-registry ../rsa_cert.pem`
18181847
)
1848+
.example(
1849+
`node $0 createGateway --registryId=my-registry --gatewayId=my-gateway\
1850+
--format=RS256_X509_PEM --key=./rsa_cert.pem`
1851+
)
18191852
.example(`node $0 createUnauthDevice my-device my-registry`)
18201853
.example(`node $0 deleteDevice my-device my-registry`)
18211854
.example(`node $0 deleteRegistry my-device my-registry`)

iot/manager/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"@google-cloud/iot": "^1.0.0",
2121
"@google-cloud/pubsub": "^0.28.0",
2222
"googleapis": "^42.0.0",
23-
"yargs": "^13.2.2"
23+
"yargs": "^14.0.0"
2424
},
2525
"devDependencies": {
2626
"@google-cloud/nodejs-repo-tools": "^3.3.0",

iot/manager/system-test/manager.test.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ it('should create and get an iam policy', async () => {
235235
});
236236

237237
it('should create and delete a registry', async () => {
238-
const createRegistryId = registryName + 'create';
238+
const createRegistryId = `${registryName}-create`;
239239

240240
let output = await tools.runAsync(`${cmd} setupIotTopic ${topicName}`, cwd);
241241
output = await tools.runAsync(
@@ -252,21 +252,23 @@ it('should create and delete a registry', async () => {
252252

253253
it('should send command message to device', async () => {
254254
const deviceId = 'test-device-command';
255-
const commandMessage = 'rotate 180 degrees';
255+
const commandMessage = 'rotate:180_degrees';
256256

257257
await tools.runAsync(
258258
`${cmd} createRsa256Device ${deviceId} ${registryName} ${rsaPublicCert}`,
259259
cwd
260260
);
261261

262262
tools.runAsync(
263-
`node cloudiot_mqtt_example_nodejs.js mqttDeviceDemo --deviceId=${deviceId} --registryId=${registryName} --privateKeyFile=${rsaPrivateKey} --algorithm=RS256 --numMessages=20 --mqttBridgePort=8883`,
263+
`node cloudiot_mqtt_example_nodejs.js mqttDeviceDemo --deviceId=${deviceId} --registryId=${registryName}\
264+
--privateKeyFile=${rsaPrivateKey} --algorithm=RS256 --numMessages=20 --mqttBridgePort=8883`,
264265
path.join(__dirname, '../../mqtt_example')
265266
);
266267

267268
const output = await tools.runAsync(
268269
`${cmd} sendCommand ${deviceId} ${registryName} ${commandMessage}`
269270
);
271+
console.log(output);
270272
assert.ok(output.includes('Sent command'));
271273

272274
await tools.runAsync(`${cmd} deleteDevice ${deviceId} ${registryName}`, cwd);
@@ -275,7 +277,8 @@ it('should send command message to device', async () => {
275277
it('should create a new gateway', async () => {
276278
const gatewayId = `nodejs-test-gateway-iot-${uuid.v4()}`;
277279
const gatewayOut = await tools.runAsync(
278-
`${cmd} createGateway ${registryName} ${gatewayId} RSA_X509_PEM ${rsaPublicCert}`
280+
`${cmd} createGateway --registryId=${registryName} --gatewayId=${gatewayId}\
281+
--format=RSA_X509_PEM --key=${rsaPublicCert}`
279282
);
280283

281284
// test no error on create gateway.
@@ -289,7 +292,8 @@ it('should create a new gateway', async () => {
289292
it('should list gateways', async () => {
290293
const gatewayId = `nodejs-test-gateway-iot-${uuid.v4()}`;
291294
await tools.runAsync(
292-
`${cmd} createGateway ${registryName} ${gatewayId} RSA_X509_PEM ${rsaPublicCert}`
295+
`${cmd} createGateway --registryId=${registryName} --gatewayId=${gatewayId}\
296+
--format=RSA_X509_PEM --key=${rsaPublicCert}`
293297
);
294298

295299
// look for output in list gateway
@@ -304,7 +308,8 @@ it('should list gateways', async () => {
304308
it('should bind existing device to gateway', async () => {
305309
const gatewayId = `nodejs-test-gateway-iot-${uuid.v4()}`;
306310
await tools.runAsync(
307-
`${cmd} createGateway ${registryName} ${gatewayId} RSA_X509_PEM ${rsaPublicCert}`
311+
`${cmd} createGateway --registryId=${registryName} --gatewayId=${gatewayId}\
312+
--format=RSA_X509_PEM --key=${rsaPublicCert}`
308313
);
309314

310315
// create device
@@ -342,7 +347,8 @@ it('should bind existing device to gateway', async () => {
342347
it('should list devices bound to gateway', async () => {
343348
const gatewayId = `nodejs-test-gateway-iot-${uuid.v4()}`;
344349
await tools.runAsync(
345-
`${cmd} createGateway ${registryName} ${gatewayId} RSA_X509_PEM ${rsaPublicCert}`
350+
`${cmd} createGateway --registryId=${registryName} --gatewayId=${gatewayId}\
351+
--format=RSA_X509_PEM --key=${rsaPublicCert}`
346352
);
347353

348354
const deviceId = `nodejs-test-device-iot-${uuid.v4()}`;
@@ -384,7 +390,8 @@ it('should list devices bound to gateway', async () => {
384390
it('should list gateways for bound device', async () => {
385391
const gatewayId = `nodejs-test-gateway-iot-${uuid.v4()}`;
386392
await tools.runAsync(
387-
`${cmd} createGateway ${registryName} ${gatewayId} RSA_X509_PEM ${rsaPublicCert}`
393+
`${cmd} createGateway --registryId=${registryName} --gatewayId=${gatewayId}\
394+
--format=RSA_X509_PEM --key=${rsaPublicCert}`
388395
);
389396

390397
// create device

0 commit comments

Comments
 (0)