UNPKG

@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)

148 lines 7.73 kB
"use strict"; 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 fs = require("fs"); const uuid = require("uuid"); const __1 = require("../.."); const command_utils_1 = require("./command-utils"); const path = require("path"); let color = (0, command_utils_1.getColor)("magenta"); exports.default = (program) => { program .command("subtenants") .alias("st") .option("-m, --mode [list|create|delete|template | info]", "list | create | delete | template | info", "list") .option("-f, --file <file>", ".mdsp.json file with subtenant definition") .option("-n, --subtenant <subtenant>", "the subtenant name") .option("-i, --subtenantid <subtenantid>", "the subtenant id") .option("-k, --passkey <passkey>", "passkey") .option("-y, --retry <number>", "retry attempts before giving up", "3") .option("-v, --verbose", "verbose output") .description(color("list, create or delete subtenants *")) .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 listSubTenants(sdk, options); break; case "template": createTemplate(options); console.log("Edit the file before submitting it to MindSphere."); break; case "delete": yield deleteSubtenant(options, sdk); break; case "create": yield createSubtenant(options, sdk); break; case "info": yield subtenantInfo(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 subtenants --mode list \t\t\t\t\tlist all subtenants`); (0, console_1.log)(` mdsp subtenants --mode template --subtenant <subtenant> \tcreate a template file for <subtenant>`); (0, console_1.log)(` mdsp subtenants --mode create --file <subtenant> \t\tcreate subtenant `); (0, console_1.log)(` mdsp subtenants --mode info --subtenantid <subtenantid> \tsubtenant info for specified id`); (0, console_1.log)(` mdsp subtenants --mode delete --subtenantid <subtenantid> \tdelete subtenant with specified id`); (0, command_utils_1.serviceCredentialLog)(); }); }; function createSubtenant(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const subtenant = JSON.parse(file.toString()); const result = yield sdk.GetTenantManagementClient().PostSubtenant(subtenant); console.log(`creted subtenant Id: ${color(result.id)} displayname: ${color(result.displayName)}`); }); } function createTemplate(options) { const subTenant = { id: uuid.v4().toString(), displayName: `${options.subtenant}`, description: `${options.subtenant} created on ${new Date().toISOString()} with MindSphere CLI`, }; (0, command_utils_1.verboseLog)(subTenant, options.verbose); writesubtenantToFile(options, subTenant); } function writesubtenantToFile(options, subtenant) { const fileName = options.file || `${options.subtenant}.subtenant.mdsp.json`; fs.writeFileSync(fileName, JSON.stringify(subtenant, null, 2)); console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp subtenants --mode create --file ${fileName} \n\nto create the subtenant`); } function deleteSubtenant(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const id = options.subtenantid; yield sdk.GetTenantManagementClient().DeleteSubtenant(id); console.log(`subtenant with id ${color(id)} deleted.`); }); } function listSubTenants(sdk, options) { return __awaiter(this, void 0, void 0, function* () { const tenantManagement = sdk.GetTenantManagementClient(); let subtenantCount = 0; const subtenants = (yield (0, __1.retry)(options.retry, () => tenantManagement.GetSubtenants())); console.log(`id ${color("displayName")} description`); (subtenants.content || []).forEach((element) => { console.log(`${element.id} ${color(element.displayName)} ${element.description}`); subtenantCount++; }); console.log(`${color(subtenantCount)} subtenants listed.\n`); }); } function subtenantInfo(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const subtenant = yield sdk.GetTenantManagementClient().GetSubtenant(options.subtenantid); (0, command_utils_1.verboseLog)(JSON.stringify(subtenant, null, 2), options.verbose); console.log(`Subtenant Id: ${subtenant.id}`); console.log(`Display Name: ${color(subtenant.displayName)}`); console.log(`Description: ${subtenant.description}`); console.log(`ETag: ${subtenant.ETag}`); console.log(`EntityId: ${subtenant.entityId}`); console.log("Asset Manager:"); console.log("\t" + color(`${sdk .GetGateway() .replace("gateway", sdk.GetTenant() + "-assetmanager")}/entity/${subtenant.entityId}`)); }); } function checkRequiredParamaters(options) { options.mode === "template" && !options.subtenant && (0, command_utils_1.errorLog)("you have to provide subtenant to create a template (see mdsp subtenants --help for more details)", true); options.mode === "create" && !options.file && (0, command_utils_1.errorLog)("you have to provide a file with subtenant definition to create an subtenant (see mdsp subtenants --help for more details)", true); options.mode === "delete" && !options.subtenantid && (0, command_utils_1.errorLog)("you have to provide the subtenantid to delete (see mdsp subtenants --help for more details)", true); options.mode === "info" && !options.subtenantid && (0, command_utils_1.errorLog)("you have to provide the subtenantid (see mdsp subtenants --help for more details)", true); } //# sourceMappingURL=subtenant.js.map