-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdelete-sensors.js
97 lines (73 loc) · 2.99 KB
/
delete-sensors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const fs = require('fs').promises;
const {
IoTClient,
DetachThingPrincipalCommand,
DeleteThingCommand,
DetachPolicyCommand,
DeletePolicyCommand,
UpdateCertificateCommand,
DeleteCertificateCommand
} = require("@aws-sdk/client-iot");
const { ArgumentParser } = require('argparse');
//constants used in the app - do not change
const SENSORS_FILE = './sensors.json';
//open sensor definition file
var sensors = require(SENSORS_FILE);
async function deleteSensors(profile, region){
try {
const iotClient = new IoTClient({ profile: profile, region: region });
//iterate over all sensors and create policies, certs, and things
sensors.forEach(async (sensor) => {
//remove the iot core endpoint
sensor.settings.host = "";
//attach thing to certificate
var command = new DetachThingPrincipalCommand({thingName: sensor.settings.clientId, principal: sensor.settings.certificateArn})
await iotClient.send(command)
//delete the thing
command = new DeleteThingCommand({thingName: sensor.settings.clientId})
await iotClient.send(command)
//detach policy from certificate
var policyName = 'Policy-' + sensor.settings.clientId;
command = new DetachPolicyCommand({ policyName: policyName, target: sensor.settings.certificateArn})
await iotClient.send(command)
//delete the IOT policy
command = new DeletePolicyCommand({policyName: policyName})
await iotClient.send(command)
//delete the certificates
var certificateId = sensor.settings.certificateArn.split('/')[1];
command = new UpdateCertificateCommand({certificateId:certificateId, newStatus:"INACTIVE"})
await iotClient.send(command)
command = new DeleteCertificateCommand({certificateId:certificateId, forceDelete:true})
await iotClient.send(command)
sensor.settings.certificateArn = ""
//delete the certificate files
await fs.unlink(sensor.settings.keyPath);
sensor.settings.keyPath = "";
await fs.unlink(sensor.settings.certPath);
sensor.settings.certPath = "";
sensor.settings.caPath = "";
//save the updated settings file
let data = JSON.stringify(sensors, null, 2);
await fs.writeFile(SENSORS_FILE, data);
})
//display results
console.log('IoT Things removed: ' + sensors.length);
console.log('AWS Profile: ' + profile);
console.log('AWS Region: ' + region);
sensors.forEach((sensor) => {
console.log('Thing Name: ' + sensor.settings.clientId);
})
}
catch (err) {
console.log('Error deleting sensors');
console.log(err.message);
}
}
// parse for profile command line arguent
const parser = new ArgumentParser({
description: 'Deletes IoT Things for sensors defined in sensors.json'
});
parser.add_argument('--profile', {default: 'default'});
parser.add_argument('--region', {default: 'us-east-1'});
args = parser.parse_args()
deleteSensors(args.profile, args.region);