Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iot: update samples for yargs v14.0.0 #1463

Merged
merged 1 commit into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions iot/manager/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,14 +942,16 @@ const createGateway = async (
registryId,
gatewayId,
publicKeyFormat,
publicKeyFile
publicKeyFile,
gatewayAuthMethod
) => {
// [START iot_create_gateway]
// const cloudRegion = 'us-central1';
// const deviceId = 'my-unauth-device';
// const gatewayId = 'my-gateway';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
// const gatewayAuthMethod = 'ASSOCIATION_ONLY';
const parentName = `projects/${projectId}/locations/${cloudRegion}/registries/${registryId}`;
console.log('Creating gateway:', gatewayId);

Expand All @@ -974,7 +976,7 @@ const createGateway = async (
credentials: credentials,
gatewayConfig: {
gatewayType: 'GATEWAY',
gatewayAuthMethod: 'ASSOCIATION_ONLY',
gatewayAuthMethod: gatewayAuthMethod,
},
},
};
Expand Down Expand Up @@ -1669,22 +1671,49 @@ require(`yargs`) // eslint-disable-line
}
)
.command(
`createGateway <registryId> <gatewayId>`,
`createGateway`,
`Creates a gateway`,
{
registryId: {
description:
'Enter a permanent ID that starts with a lower case letter. Must end in a letter or number.',
requiresArg: true,
type: 'string',
},
gatewayId: {
description:
'Enter a permanent ID that starts with a lowercase letter. Must end in a letter or number',
requiresArg: true,
type: 'string',
},
publicKeyFormat: {
alias: 'format',
default: 'RSA_X509_PEM',
description: 'Public key format for devices.',
requiresArg: true,
choices: ['RSA_PEM', 'RSA_X509_PEM', 'ES256_PEM', 'ES256_X509_PEM'],
type: 'string',
},
publicKeyFile: {
alias: 'key',
description:
'Path to the public key file used for device authentication.',
requiresArg: true,
type: 'string',
},
gatewayAuthMethod: {
default: 'ASSOCIATION_ONLY',
description:
'Determines how Cloud IoT Core verifies and trusts devices associated with this gateway.',
requiresArg: true,
choices: [
'ASSOCIATION_ONLY',
'DEVICE_AUTH_TOKEN_ONLY',
'ASSOCIATION_AND_DEVICE_AUTH_TOKEN',
'GATEWAY_AUTH_METHOD_UNSPECIFIED',
],
type: 'string',
},
},
async opts => {
const client = await getClient(opts.serviceAccount);
Expand Down Expand Up @@ -1816,6 +1845,10 @@ require(`yargs`) // eslint-disable-line
.example(
`node $0 createRsa256Device my-rsa-device my-registry ../rsa_cert.pem`
)
.example(
`node $0 createGateway --registryId=my-registry --gatewayId=my-gateway\
--format=RS256_X509_PEM --key=./rsa_cert.pem`
)
.example(`node $0 createUnauthDevice my-device my-registry`)
.example(`node $0 deleteDevice my-device my-registry`)
.example(`node $0 deleteRegistry my-device my-registry`)
Expand Down
2 changes: 1 addition & 1 deletion iot/manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@google-cloud/iot": "^1.0.0",
"@google-cloud/pubsub": "^0.28.0",
"googleapis": "^42.0.0",
"yargs": "^13.2.2"
"yargs": "^14.0.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.3.0",
Expand Down
23 changes: 15 additions & 8 deletions iot/manager/system-test/manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ it('should create and get an iam policy', async () => {
});

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

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

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

await tools.runAsync(
`${cmd} createRsa256Device ${deviceId} ${registryName} ${rsaPublicCert}`,
cwd
);

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

const output = await tools.runAsync(
`${cmd} sendCommand ${deviceId} ${registryName} ${commandMessage}`
);
console.log(output);
assert.ok(output.includes('Sent command'));

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

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

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

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

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

// create device
Expand Down