@uhuru/awsiot-thing-creator
Advanced tools
Comparing version 1.0.1-beta.27 to 1.0.1-beta.28
@@ -40,3 +40,4 @@ interface ConfigObject { | ||
private describeEndpoint; | ||
updateThingAttributes(): Promise<void>; | ||
updateThingAttributes(): Promise<import("@aws-sdk/client-iot").UpdateThingCommandOutput>; | ||
destroyThing(): Promise<import("@aws-sdk/client-iot").DeleteThingCommandOutput>; | ||
removeThingCerts(): Promise<void>; | ||
@@ -43,0 +44,0 @@ private existThing; |
@@ -54,3 +54,4 @@ "use strict"; | ||
clientCert: `./certs/${this.thingName}.crt.pem`, | ||
privateKey: `./certs/${this.thingName}-private.pem` | ||
privateKey: `./certs/${this.thingName}-private.pem`, | ||
enebularConfig: '.enebular-config.json' | ||
}; | ||
@@ -106,3 +107,3 @@ } | ||
}); | ||
yield this.iotClient.send(command).catch((error) => { | ||
return this.iotClient.send(command).catch((error) => { | ||
throw new Error(`Failed to update AWS IoT thing. Please check your AWS IoT configuration, reason: ${error.message}`); | ||
@@ -112,2 +113,10 @@ }); | ||
} | ||
destroyThing() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const command = new client_iot_1.DeleteThingCommand({ thingName: this.thingName }); | ||
return this.iotClient.send(command).catch((error) => { | ||
throw new Error(`Failed to delete AWS IoT thing. Please check your AWS IoT configuration, reason: ${error.message}`); | ||
}); | ||
}); | ||
} | ||
removeThingCerts() { | ||
@@ -124,13 +133,14 @@ return __awaiter(this, void 0, void 0, function* () { | ||
const principalsList = principalsResponse.principals || []; | ||
console.log(`~~~~~~~~~~~~~~ principalsResponse ~~~~~~~~~~~~~~`, JSON.stringify(principalsResponse, null, 2)); | ||
console.log(`~~~~~~~~~~~~~~ principalsList ~~~~~~~~~~~~~~`, JSON.stringify(principalsList, null, 2)); | ||
// const command = new DetachThingPrincipalCommand({ | ||
// principal: 'iot:Data-ATS', | ||
// thingName: this.thingName | ||
// }) | ||
// await this.iotClient.send(command).catch((error) => { | ||
// throw new Error( | ||
// `Failed to detach AWS IoT thing. Please check your AWS IoT configuration, reason: ${error.message}` | ||
// ) | ||
// }) | ||
if (principalsList.length === 0) { | ||
throw new Error(`No principals found for ${this.thingName}. Please check your AWS IoT configuration.`); | ||
} | ||
for (const principal of principalsList) { | ||
const command = new client_iot_1.DetachThingPrincipalCommand({ | ||
principal: principal, | ||
thingName: this.thingName | ||
}); | ||
yield this.iotClient.send(command).catch((error) => { | ||
throw new Error(`Failed to detach AWS IoT thing. Please check your AWS IoT configuration, reason: ${error.message}`); | ||
}); | ||
} | ||
}); | ||
@@ -168,3 +178,3 @@ } | ||
} | ||
writeFileAll({ configSavePath, thingName, certificatePem, privateKey, config, rootPem }) { | ||
writeFileAll({ configSavePath, thingName, certificatePem, privateKey, config, rootPem, enebularConfig }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -184,3 +194,3 @@ const configJsonPath = `${configSavePath}/config.json`; | ||
catch (err) { | ||
throw new Error('Make certs directory failed.'); | ||
throw new Error(`Make certs directory failed: ${err.message}`); | ||
} | ||
@@ -191,4 +201,4 @@ } | ||
.writeFile(path_1.default.resolve(configSavePath, this.envPaths.clientCert), certificatePem, 'utf8') | ||
.catch(() => { | ||
throw new Error('Failed to save certificate.'); | ||
.catch((err) => { | ||
throw new Error(`Failed to save certificate: ${err.message}`); | ||
}); | ||
@@ -198,4 +208,4 @@ // Save privateKey | ||
.writeFile(path_1.default.resolve(configSavePath, this.envPaths.privateKey), privateKey, 'utf8') | ||
.catch(() => { | ||
throw new Error('Failed to save private key.'); | ||
.catch((err) => { | ||
throw new Error(`Failed to save private key: ${err.message}`); | ||
}); | ||
@@ -207,4 +217,4 @@ // Download AWS certificate | ||
rootCertFile = yield this.downloadCertificate(this.awsCertificationAuthorityUrl); | ||
yield fs_extra_1.default.writeFile(rootCertPath, rootCertFile, 'utf8').catch(() => { | ||
throw new Error(`Failed to write certificate file to ${rootCertPath}`); | ||
yield fs_extra_1.default.writeFile(rootCertPath, rootCertFile, 'utf8').catch((err) => { | ||
throw new Error(`Failed to write certificate file to ${rootCertPath}: ${err.message}`); | ||
}); | ||
@@ -220,5 +230,14 @@ let configJson; | ||
// Save config json | ||
yield fs_extra_1.default.writeFile(configJsonPath, configJson, 'utf8').catch(() => { | ||
throw new Error('Failed to save config file.'); | ||
yield fs_extra_1.default.writeFile(configJsonPath, configJson, 'utf8').catch((err) => { | ||
throw new Error(`Failed to save config file: ${err.message}`); | ||
}); | ||
if (enebularConfig) { | ||
const enebularConfigPath = path_1.default.resolve(configSavePath, this.envPaths.enebularConfig); | ||
const enebularConfigJson = JSON.stringify(enebularConfig, null, 4); | ||
yield fs_extra_1.default | ||
.writeFile(enebularConfigPath, enebularConfigJson, 'utf8') | ||
.catch((err) => { | ||
throw new Error(`Failed to save enebular config file: ${err.message}`); | ||
}); | ||
} | ||
console.log(`Thing ${thingName} has been created successfully. This config has been saved to ${configJsonPath}`); | ||
@@ -369,3 +388,3 @@ }); | ||
const getAgentThingUrl = this.enebularApiUrl + `/get-thing-install-info/${this.enebularInstallId}`; | ||
const { thingName, clientPem, privateKey, config, rootPem } = yield axios_1.default | ||
const { thingName, clientPem, privateKey, config, rootPem, enebularConfig } = yield axios_1.default | ||
.get(getAgentThingUrl) | ||
@@ -385,3 +404,4 @@ .then((response) => response.data) | ||
rootPem, | ||
config | ||
config, | ||
enebularConfig | ||
}); | ||
@@ -388,0 +408,0 @@ } |
{ | ||
"name": "@uhuru/awsiot-thing-creator", | ||
"version": "1.0.1-beta.27", | ||
"version": "1.0.1-beta.28", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
52257
544