Skip to content

Commit ed3cb0f

Browse files
committed
remove cmdutil from fleet provisioning samples
1 parent 6a84279 commit ed3cb0f

File tree

4 files changed

+169
-79
lines changed

4 files changed

+169
-79
lines changed

samples/node/service_clients/fleet_provisioning/basic/index.ts

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,73 @@
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
55

6-
import { iotidentity } from 'aws-iot-device-sdk-v2';
6+
import { iotidentity, mqtt5, iot } from 'aws-iot-device-sdk-v2';
77
import { once } from "events"
8-
9-
type Args = { [index: string]: any };
10-
const yargs = require('yargs');
11-
12-
// The relative path is '../../../util/cli_args' from here, but the compiled javascript file gets put one level
13-
// deeper inside the 'dist' folder
14-
const common_args = require('../../../../util/cli_args');
15-
16-
yargs.command('*', false, (yargs: any) => {
17-
common_args.add_direct_connection_establishment_arguments(yargs);
18-
yargs
19-
.option('template_name', {
20-
alias: 't',
21-
description: 'Template Name.',
22-
type: 'string',
23-
required: true
24-
})
25-
.option('template_parameters', {
26-
alias: 'tp',
27-
description: '<json>: Template parameters json.',
28-
type: 'string',
29-
required: false
30-
})
31-
}, main).parse();
32-
33-
async function main(argv: Args) {
34-
common_args.apply_sample_arguments(argv);
35-
8+
import { v4 as uuidv4 } from 'uuid';
9+
10+
const TIMEOUT = 100000;
11+
12+
// --------------------------------- ARGUMENT PARSING -----------------------------------------
13+
const args = require('yargs')
14+
.option('endpoint', {
15+
alias: 'e',
16+
description: 'IoT endpoint hostname',
17+
type: 'string',
18+
required: true
19+
})
20+
.option('cert', {
21+
alias: 'c',
22+
description: 'Path to the certificate file to use during mTLS connection establishment',
23+
type: 'string',
24+
required: true
25+
})
26+
.option('key', {
27+
alias: 'k',
28+
description: 'Path to the private key file to use during mTLS connection establishment',
29+
type: 'string',
30+
required: true
31+
})
32+
.option('client_id', {
33+
alias: 'C',
34+
description: 'Client ID',
35+
type: 'string',
36+
default: `fleet-provisioning-${uuidv4().substring(0, 8)}`
37+
})
38+
.option('template_name', {
39+
alias: 't',
40+
description: 'Template Name',
41+
type: 'string',
42+
required: true
43+
})
44+
.option('template_parameters', {
45+
alias: 'tp',
46+
description: '<json>: Template parameters json',
47+
type: 'string',
48+
required: false
49+
})
50+
.help()
51+
.argv;
52+
53+
// --------------------------------- ARGUMENT PARSING END -----------------------------------------
54+
55+
async function main() {
3656
console.log("Connecting...");
37-
let protocolClient = common_args.build_mqtt5_client_from_cli_args(argv);
57+
58+
// Create MQTT5 client using mutual TLS via X509 Certificate and Private Key
59+
const builder = iot.AwsIotMqtt5ClientConfigBuilder.newDirectMqttBuilderWithMtlsFromPath(
60+
args.endpoint,
61+
args.cert,
62+
args.key
63+
);
64+
65+
builder.withConnectProperties({
66+
clientId: args.client_id,
67+
keepAliveIntervalSeconds: 1200
68+
});
69+
70+
const config = builder.build();
71+
const protocolClient = new mqtt5.Mqtt5Client(config);
72+
3873
let identityClient = iotidentity.IotIdentityClientv2.newFromMqtt5(protocolClient, {
3974
maxRequestResponseSubscriptions: 2,
4075
maxStreamingSubscriptions: 0,
@@ -44,19 +79,22 @@ async function main(argv: Args) {
4479
const connectionSuccess = once(protocolClient, "connectionSuccess");
4580
protocolClient.start();
4681

47-
await connectionSuccess;
82+
await Promise.race([
83+
connectionSuccess,
84+
new Promise((_, reject) => setTimeout(() => reject(new Error("Connection timeout")), TIMEOUT))
85+
]);
4886
console.log("Connected!");
4987

5088
let createKeysResponse = await identityClient.createKeysAndCertificate({});
5189
console.log(`CreateKeysAndCertificate Response: ${JSON.stringify(createKeysResponse)}`);
5290

5391
let registerThingRequest : iotidentity.model.RegisterThingRequest = {
54-
templateName: argv.template_name,
92+
templateName: args.template_name,
5593
certificateOwnershipToken: createKeysResponse.certificateOwnershipToken,
5694
};
5795

58-
if (argv.template_parameters) {
59-
registerThingRequest.parameters = JSON.parse(argv.template_parameters);
96+
if (args.template_parameters) {
97+
registerThingRequest.parameters = JSON.parse(args.template_parameters);
6098
}
6199

62100
let registerThingResponse = await identityClient.registerThing(registerThingRequest);
@@ -73,3 +111,8 @@ async function main(argv: Args) {
73111
console.log("Disconnected");
74112
process.exit(0);
75113
}
114+
115+
main().catch((error) => {
116+
console.error(error);
117+
process.exit(1);
118+
});

samples/node/service_clients/fleet_provisioning/basic/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
"prepare": "npm run tsc"
1818
},
1919
"devDependencies": {
20-
"@types/node": "^10.17.50",
20+
"@types/node": "^14.18.63",
21+
"@types/uuid": "^8.3.4",
2122
"typescript": "^4.7.4"
2223
},
2324
"dependencies": {
2425
"aws-iot-device-sdk-v2": "file:../../../../..",
25-
"yargs": "^16.2.0"
26+
"yargs": "^16.2.0",
27+
"uuid": "^8.3.2"
2628
}
2729
}

samples/node/service_clients/fleet_provisioning/csr/index.ts

Lines changed: 84 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,82 @@
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
55

6-
import { iotidentity } from 'aws-iot-device-sdk-v2';
6+
import { iotidentity, mqtt5, iot } from 'aws-iot-device-sdk-v2';
77
import { once } from "events"
88
import fs from "fs";
9-
10-
type Args = { [index: string]: any };
11-
const yargs = require('yargs');
12-
13-
// The relative path is '../../../util/cli_args' from here, but the compiled javascript file gets put one level
14-
// deeper inside the 'dist' folder
15-
const common_args = require('../../../../util/cli_args');
16-
17-
yargs.command('*', false, (yargs: any) => {
18-
common_args.add_direct_connection_establishment_arguments(yargs);
19-
yargs
20-
.option('csr_file', {
21-
alias: 'csr',
22-
description: '<path>: Path to a CSR file in PEM format.',
23-
type: 'string',
24-
required: true
25-
})
26-
.option('template_name', {
27-
alias: 't',
28-
description: 'Template Name.',
29-
type: 'string',
30-
required: true
31-
})
32-
.option('template_parameters', {
33-
alias: 'tp',
34-
description: '<json>: Template parameters json.',
35-
type: 'string',
36-
required: false
37-
})
38-
}, main).parse();
39-
40-
async function main(argv: Args) {
41-
common_args.apply_sample_arguments(argv);
42-
43-
const csr: string = fs.readFileSync(argv.csr_file, 'utf8');
9+
import { v4 as uuidv4 } from 'uuid';
10+
11+
const TIMEOUT = 100000;
12+
13+
// --------------------------------- ARGUMENT PARSING -----------------------------------------
14+
const args = require('yargs')
15+
.option('endpoint', {
16+
alias: 'e',
17+
description: 'IoT endpoint hostname',
18+
type: 'string',
19+
required: true
20+
})
21+
.option('cert', {
22+
alias: 'c',
23+
description: 'Path to the certificate file to use during mTLS connection establishment',
24+
type: 'string',
25+
required: true
26+
})
27+
.option('key', {
28+
alias: 'k',
29+
description: 'Path to the private key file to use during mTLS connection establishment',
30+
type: 'string',
31+
required: true
32+
})
33+
.option('client_id', {
34+
alias: 'C',
35+
description: 'Client ID',
36+
type: 'string',
37+
default: `fleet-provisioning-csr-${uuidv4().substring(0, 8)}`
38+
})
39+
.option('csr_file', {
40+
alias: 'csr',
41+
description: '<path>: Path to a CSR file in PEM format',
42+
type: 'string',
43+
required: true
44+
})
45+
.option('template_name', {
46+
alias: 't',
47+
description: 'Template Name',
48+
type: 'string',
49+
required: true
50+
})
51+
.option('template_parameters', {
52+
alias: 'tp',
53+
description: '<json>: Template parameters json',
54+
type: 'string',
55+
required: false
56+
})
57+
.help()
58+
.argv;
59+
60+
// --------------------------------- ARGUMENT PARSING END -----------------------------------------
61+
62+
async function main() {
63+
const csr: string = fs.readFileSync(args.csr_file, 'utf8');
4464

4565
console.log("Connecting...");
46-
let protocolClient = common_args.build_mqtt5_client_from_cli_args(argv);
66+
67+
// Create MQTT5 client using mutual TLS via X509 Certificate and Private Key
68+
const builder = iot.AwsIotMqtt5ClientConfigBuilder.newDirectMqttBuilderWithMtlsFromPath(
69+
args.endpoint,
70+
args.cert,
71+
args.key
72+
);
73+
74+
builder.withConnectProperties({
75+
clientId: args.client_id,
76+
keepAliveIntervalSeconds: 1200
77+
});
78+
79+
const config = builder.build();
80+
const protocolClient = new mqtt5.Mqtt5Client(config);
81+
4782
let identityClient = iotidentity.IotIdentityClientv2.newFromMqtt5(protocolClient, {
4883
maxRequestResponseSubscriptions: 2,
4984
maxStreamingSubscriptions: 0,
@@ -53,7 +88,10 @@ async function main(argv: Args) {
5388
const connectionSuccess = once(protocolClient, "connectionSuccess");
5489
protocolClient.start();
5590

56-
await connectionSuccess;
91+
await Promise.race([
92+
connectionSuccess,
93+
new Promise((_, reject) => setTimeout(() => reject(new Error("Connection timeout")), TIMEOUT))
94+
]);
5795
console.log("Connected!");
5896

5997
let createCertificateFromCsrResponse = await identityClient.createCertificateFromCsr({
@@ -62,12 +100,12 @@ async function main(argv: Args) {
62100
console.log(`CreateCertificateFromCsr Response: ${JSON.stringify(createCertificateFromCsrResponse)}`);
63101

64102
let registerThingRequest : iotidentity.model.RegisterThingRequest = {
65-
templateName: argv.template_name,
103+
templateName: args.template_name,
66104
certificateOwnershipToken: createCertificateFromCsrResponse.certificateOwnershipToken,
67105
};
68106

69-
if (argv.template_parameters) {
70-
registerThingRequest.parameters = JSON.parse(argv.template_parameters);
107+
if (args.template_parameters) {
108+
registerThingRequest.parameters = JSON.parse(args.template_parameters);
71109
}
72110

73111
let registerThingResponse = await identityClient.registerThing(registerThingRequest);
@@ -84,3 +122,8 @@ async function main(argv: Args) {
84122
console.log("Disconnected");
85123
process.exit(0);
86124
}
125+
126+
main().catch((error) => {
127+
console.error(error);
128+
process.exit(1);
129+
});

samples/node/service_clients/fleet_provisioning/csr/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
"prepare": "npm run tsc"
1818
},
1919
"devDependencies": {
20-
"@types/node": "^10.17.50",
20+
"@types/node": "^14.18.63",
21+
"@types/uuid": "^8.3.4",
2122
"typescript": "^4.7.4"
2223
},
2324
"dependencies": {
2425
"aws-iot-device-sdk-v2": "file:../../../../..",
25-
"yargs": "^16.2.0"
26+
"yargs": "^16.2.0",
27+
"uuid": "^8.3.2"
2628
}
2729
}

0 commit comments

Comments
 (0)