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)

205 lines 10.3 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 path = require("path"); const uuid = require("uuid"); const utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const mime = require("mime-types"); let color = (0, command_utils_1.getColor)("magenta"); exports.default = (program) => { program .command("tenant") .alias("ti") .option("-m, --mode [info|create|delete|upload|template]", "mode [info | create | delete | upload | template]", "info") .option("-f, --file <file>", ".mdsp.json file with legal configuration or logo") .option("-k, --passkey <passkey>", "passkey") .option("-y, --retry <number>", "retry attempts before giving up", "3") .option("-v, --verbose", "verbose output") .description(color(`create or delete tenant legal configuration and logo *`)) .action((options) => { (() => __awaiter(void 0, void 0, void 0, function* () { try { checkRequiredParameters(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 "info": yield tenantInfo(sdk, options); break; case "upload": yield uploadFile(sdk, options); break; case "create": yield createLegal(sdk, options); break; case "template": writeTemplate(sdk, options); break; case "delete": deleteLegal(sdk, options); 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 tenant \t\t\t\t\t prints out the tenant information`); (0, console_1.log)(` mdsp tenant --mode info \t\t\t prints out the tenant information`); (0, console_1.log)(` mdsp tenant --mode template \t\t\t creates template file with legal configuration`); (0, console_1.log)(` mdsp tenant --mode delete \t\t\t deletes legal configuration`); (0, console_1.log)(` mdsp tenant --mode create --file <filename> \t creates legal configuration`); (0, console_1.log)(` mdsp tenant --mode upload --file <filename> \t uploads the company logo`); (0, command_utils_1.serviceCredentialLog)(); }); }; function uploadFile(sdk, options) { return __awaiter(this, void 0, void 0, function* () { const tenantManagement = sdk.GetTenantManagementClient(); const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const mimeType = mime.lookup(options.file); yield tenantManagement.PostTenantInfoLogo(file, path.basename(filePath), mimeType); console.log("File successfully uploaded."); console.log(`Logo: ${color(sdk.GetGateway().replace("gateway", sdk.GetTenant()) + "/api/tenantmanagement/v4/tenantInfo/logo")}`); }); } function deleteLegal(sdk, options) { return __awaiter(this, void 0, void 0, function* () { const tenantManagement = sdk.GetTenantManagementClient(); yield tenantManagement.DeleteLegalConfigRegions(); console.log("Legal configuration successfully deleted."); console.log("Settings application (not available in start for free tenants):"); console.log(color(`\t${sdk.GetGateway().replace("gateway", sdk.GetTenant() + "-settings")}/provider/`)); }); } function createLegal(sdk, options) { return __awaiter(this, void 0, void 0, function* () { const tenantManagement = sdk.GetTenantManagementClient(); const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const fileAsJson = JSON.parse(file.toString()); yield tenantManagement.PostLegalConfigRegions(fileAsJson); console.log("Legal configuration successfully created."); console.log("Settings application (not available in start for free tenants):"); console.log(color(`\t${sdk.GetGateway().replace("gateway", sdk.GetTenant() + "-settings")}/provider/`)); }); } function writeTemplate(sdk, options) { const fileName = options.file || `${sdk.GetTenant()}.tenant.mdsp.json`; fs.writeFileSync(fileName, JSON.stringify(createTemplate(), null, 2)); console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp tenant --mode create --file ${fileName} \n\nto create the legal information of the tenant`); } function tenantInfo(sdk, options) { return __awaiter(this, void 0, void 0, function* () { var _a; const tenantManagement = sdk.GetTenantManagementClient(); const tenantInfo = (yield (0, utils_1.retry)(options.retry, () => tenantManagement.GetTenantInfo())); console.log("Tenant Info:"); console.log(`\tName: ${color(tenantInfo.name)}`); console.log(`\tPrefix: ${tenantInfo.prefix}`); console.log(`\tDisplay Name: ${tenantInfo.displayName}`); console.log(`\tCountry: ${tenantInfo.country}`); console.log(`\tType: ${color(tenantInfo.type)}`); console.log(`\tCompany Name: ${color(tenantInfo.companyName)}`); console.log(`\tAllowed to create subtenants: ${color(tenantInfo.allowedToCreateSubtenant)}`); (0, command_utils_1.verboseLog)(JSON.stringify(tenantInfo, null, 2), options.verbose); const tenantLegalConfig = (yield (0, utils_1.retry)(options.retry, () => tenantManagement.GetLegalConfigRegions())); console.log("Logo:"); try { const logoMetaData = yield tenantManagement.GetTenantInfoLogoMetaData(); console.log(`\tName: ${color(logoMetaData.name)}`); console.log(`\tSize: ${logoMetaData.size}`); console.log(`\tURL: ${color(sdk.GetGateway().replace("gateway", sdk.GetTenant()) + "/api/tenantmanagement/v4/tenantInfo/logo")}`); } catch (_b) { console.log("\tNo logo found"); } console.log("Regions:"); for (const x of tenantLegalConfig.regions) { console.log(`\t${color(x.regionId)} ${x.regionName} - Countries: ${JSON.stringify(x.regionCountries)}`); console.log("Links:"); (_a = x.links) === null || _a === void 0 ? void 0 : _a.forEach((link) => { for (const language of Object.keys(link.languages)) { console.log(`\t[${link.type}] [${language}] ${link.languages[language].name} -> ${color(link.languages[language].value)}`); } }); } (0, command_utils_1.verboseLog)(JSON.stringify(tenantLegalConfig, null, 2), options.verbose); console.log("Settings application (not available in start for free tenants):"); console.log(color(`\t${sdk.GetGateway().replace("gateway", sdk.GetTenant() + "-settings")}/provider/`)); }); } function createTemplate() { return { regions: [ { regionId: "global", regionName: "global", regionCountries: [], links: [ { id: uuid.v4().toString(), type: "www", sorting: 1, languages: { default: { name: "Connectivity Playground", value: "https://playground.mindconnect.rocks", }, }, }, { id: uuid.v4().toString(), type: "www", sorting: 2, languages: { default: { name: "Open Source Software", value: "https://developer.siemens.com/industrial-iot-open-source/index.html", }, }, }, { id: "a785dd65-5470-4c1f-9e56-6a17ef73ceaf", type: "www", sorting: 3, languages: { default: { name: "MindSphere GitHub", value: "https://github.com/mindsphere/", }, }, }, ], }, ], }; } function checkRequiredParameters(options) { options.mode === "upload" && !options.file && (0, command_utils_1.errorLog)("You have to specify the file to upload in --file parameter", true); options.mode === "create" && !options.file && (0, command_utils_1.errorLog)("You have to specify the file with legal configuration in --file parameter", true); } //# sourceMappingURL=tenant.js.map