@mindconnect/mindconnect-nodejs
Version:
NodeJS Library for Siemens Insights Hub Connectivity - TypeScript SDK for Insights Hub and Industrial IoT - Command Line Interface - Insights Hub Development Proxy (Siemens Insights Hub was formerly known as MindSphere)
158 lines • 8.42 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const console_1 = require("console");
const utils_1 = require("../../api/utils");
const command_utils_1 = require("./command-utils");
const fs = require("fs");
const path = require("path");
let color = (0, command_utils_1.getColor)("magenta");
exports.default = (program) => {
program
.command("sdi-data-lakes")
.alias("sdl")
.option("-m, --mode [list|create|update|template|info|delete]", "list | create | update | template | info | delete", "list")
.option("-d, --datalake <datalake>", "data lake file with definition for --mode create or update command")
.option("-i, --datalakeid <datalakeid>", "the datalake id for --mode info, update or delete command")
.option("-o, --overwrite", "overwrite template file if it already exists")
.option("-k, --passkey <passkey>", "passkey")
.option("-y, --retry <number>", "retry attempts before giving up", "3")
.option("-v, --verbose", "verbose output")
.description(color("manage data lakes for SDI *"))
.action((options) => {
(() => __awaiter(void 0, void 0, void 0, function* () {
try {
checkRequiredParamaters(options);
const sdk = (0, command_utils_1.getSdk)(options);
color = (0, command_utils_1.adjustColor)(color, options);
(0, command_utils_1.homeDirLog)(options.verbose, color);
(0, command_utils_1.proxyLog)(options.verbose, color);
switch (options.mode) {
case "list":
yield listDataLakes(sdk, options);
break;
case "info":
yield dataLakeInfo(sdk, options);
break;
case "template":
createTemplate(options);
console.log("Edit the file before submitting it to MindSphere.");
break;
case "update":
yield updateDataLake(options, sdk);
break;
case "create":
yield createDataLake(options, sdk);
break;
case "delete":
yield deleteDataLake(options, sdk);
break;
default:
throw Error(`no such option: ${options.mode}`);
}
}
catch (err) {
(0, command_utils_1.errorLog)(err, options.verbose);
}
}))();
})
.on("--help", () => {
(0, console_1.log)("\n Examples:\n");
(0, console_1.log)(` mdsp sdi-data-lakes --mode list \t\t list all sdi datalakes`);
(0, console_1.log)(` mdsp sdi-data-lakes --mode template \t\t create template file`);
(0, console_1.log)(` mdsp sdi-data-lakes --mode create --datalake <datalakefile> \t\t create sdi data lake`);
(0, console_1.log)(` mdsp sdi-data-lakes --mode update --datalake <datalakefile> --datalakeid <datalakeid> \
\t\t update sdi data lake`);
(0, console_1.log)(` mdsp sdi-data-lakes --mode info --datalakeid <datalakeid> \t\t get sdi data lake info`);
(0, console_1.log)(` mdsp sdi-data-lakes --mode delete --datalakeid <datalakeid> \t\t delete sdi data lake`);
(0, command_utils_1.serviceCredentialLog)();
});
};
function checkRequiredParamaters(options) {
options.mode === "create" &&
!options.datalake &&
(0, command_utils_1.errorLog)("you have to provide a datalake template file to create a sdi datalake (see mdsp sdi-data-lakes --help for more details)", true);
options.mode === "update" &&
!options.datalakeid &&
(0, command_utils_1.errorLog)("you have to provide the datalakeid of the datalake you want to update (see mdsp sdi-data-lakes --help for more details)", true);
options.mode === "info" &&
!options.datalakeid &&
(0, command_utils_1.errorLog)("you have to provide the datalakeid to get infos about the sdi data lake (see mdsp sdi-data-lakes --help for more details)", true);
options.mode === "delete" &&
!options.datalakeid &&
(0, command_utils_1.errorLog)("you have to provide the datalakeid to delete the sdi data lake (see mdsp sdi-data-lakes --help for more details)", true);
}
function listDataLakes(sdk, options) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const sdiClient = sdk.GetSemanticDataInterConnectClient();
const dataLakes = yield sdiClient.GetDataLakes();
console.log(`${color("Id")} Name Type ${color("basePath")}`);
(_a = dataLakes.dataLakes) === null || _a === void 0 ? void 0 : _a.forEach((element) => {
console.log(`${color(element.id)} ${element.name} ${element.type} ${color(element.basePath)}`);
});
(0, command_utils_1.verboseLog)(JSON.stringify(dataLakes, null, 2), options.verbose);
});
}
function dataLakeInfo(sdk, options) {
return __awaiter(this, void 0, void 0, function* () {
const sdiClient = sdk.GetSemanticDataInterConnectClient();
const dataLake = yield sdiClient.GetDataLake(`${options.datalakeid}`);
printDataLakeInfos(dataLake, options);
});
}
function printDataLakeInfos(dataLake, options) {
(0, command_utils_1.printObjectInfo)("Data Lake:", dataLake, options, ["id", "name", "basePath"], color);
}
function createTemplate(options) {
const templateType = {
name: "customDataLake",
type: "Custom",
basePath: "your/custom/path/to/datalake",
};
(0, command_utils_1.verboseLog)(JSON.stringify(templateType, null, 2), options.verbose);
writeToFile(options, templateType);
}
function writeToFile(options, dataLake) {
const fileName = options.file || `sdi.datalake.mdsp.json`;
const filePath = path.resolve(fileName);
fs.existsSync(filePath) &&
!options.overwrite &&
(0, utils_1.throwError)(`The ${filePath} already exists. (use --overwrite to overwrite) `);
fs.writeFileSync(filePath, JSON.stringify(dataLake, null, 2));
console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp sdi-data-lakes --mode create --datalake ${fileName} \n\nto create the sdi data lake`);
}
function createDataLake(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = path.resolve(options.datalake);
const file = fs.readFileSync(filePath);
const dataLake = JSON.parse(file.toString());
const result = yield sdk.GetSemanticDataInterConnectClient().PostDataLake(dataLake);
printDataLakeInfos(result, options);
});
}
function updateDataLake(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = path.resolve(options.datalake);
const file = fs.readFileSync(filePath);
const dataLake = JSON.parse(file.toString());
const result = yield sdk.GetSemanticDataInterConnectClient().PatchDataLake(`${options.datalakeid}`, dataLake);
printDataLakeInfos(result, options);
});
}
function deleteDataLake(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const sdiClient = sdk.GetSemanticDataInterConnectClient();
yield sdiClient.DeleteDataLake(`${options.datalakeid}`);
console.log(`The sdi data lake with id : ${color(options.datalakeid)} was deleted.`);
});
}
//# sourceMappingURL=sdi-datalakes.js.map