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)

176 lines 9.14 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 utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const path = require("path"); let color = (0, command_utils_1.getColor)("magenta"); const green = (0, command_utils_1.getColor)("green"); const red = (0, command_utils_1.getColor)("red"); exports.default = (program) => { program .command("policy") .alias("po") .option("-m, --mode [list|create|update|delete|template|info]", "list | create | update | delete | template | info", "list") .option("-f, --file <file>", ".mdsp.json file with policy definition", "policy.mdsp.json") .option("-n, --policy <policy>", "the policy name") .option("-i, --policyid <policyid>", "the policy id") .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("list, create or delete policies *")) .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 listPolicies(sdk, options); break; case "template": createTemplate(options, sdk.GetTenant()); console.log("Edit the file before submitting it to MindSphere."); break; case "delete": yield deletePolicy(options, sdk); break; case "update": yield updatePolicy(options, sdk); break; case "create": yield createPolicy(options, sdk); break; case "info": yield policyInfo(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 policy --mode list \t\t\t\tlist all policies`); (0, console_1.log)(` mdsp policy --mode template --policy <policy> \tcreate a template file for <policy>`); (0, console_1.log)(` mdsp policy --mode create --file <policy> \t\tcreate policy `); (0, console_1.log)(` mdsp policy --mode update --file <policy> --policyid <policyid> \t update policy `); (0, console_1.log)(` mdsp policy --mode info --policyid <policyid> \tpolicy info for specified id`); (0, console_1.log)(` mdsp policy --mode delete --policyid <policyid> \tdelete policy with specified id`); (0, command_utils_1.serviceCredentialLog)(); }); }; function createPolicy(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const policy = JSON.parse(file.toString()); const result = yield sdk.GetResourceManagementClient().PostPolicy(policy); console.log(`creted policy Id: ${color(result.id)} displayname: ${color(result.name)}`); }); } function updatePolicy(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const policy = JSON.parse(file.toString()); const oldPolicy = yield sdk.GetResourceManagementClient().GetPolicy(options.policyid); const result = yield sdk .GetResourceManagementClient() .PutPolicy(options.policyid, policy, { ifMatch: oldPolicy.eTag.toString() }); console.log(`updated policy Id: ${color(result.id)} displayname: ${color(result.name)}`); }); } function createTemplate(options, tenant) { const policy = { name: "Policy", description: "Created with MindSphere CLI", active: true, subjects: [`mdsp:core:identitymanagement:eu1:${tenant}:user:test@example.com`], rules: [ { name: "Rule1", comment: "see https://developer.mindsphere.io/apis/core-resourceaccessmanagement/api-resourceaccessmanagement-actions-list.html for actions", actions: ["mdsp:core:assetmanagement:asset:read"], resources: [`mdsp:core:assetmanagement:eu1:${tenant}:asset:dfb0d2961a224a259c44d8c3f76204fe`], propagationDepth: -1, }, ], }; (0, command_utils_1.verboseLog)(policy, options.verbose); writepolicyToFile(options, policy); } function writepolicyToFile(options, policy) { const fileName = options.file || `policy.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(fileName, JSON.stringify(policy, null, 2)); console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp policy --mode create --file ${fileName} \n\nto create the policy`); } function deletePolicy(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const id = options.policyid; yield (0, utils_1.retry)(options.retry, () => sdk.GetResourceManagementClient().DeletePolicy(id)); console.log(`policy with id ${color(id)} deleted.`); }); } function listPolicies(sdk, options) { return __awaiter(this, void 0, void 0, function* () { var _a; const policies = []; let startIndex = 0; let count = 500; let policyPage; do { policyPage = yield (0, utils_1.retry)(options.retry, () => sdk.GetResourceManagementClient().GetPolicies({ page: startIndex, size: count })); policies.push(...policyPage.policies); startIndex += count; } while (startIndex < (((_a = policyPage.page) === null || _a === void 0 ? void 0 : _a.totalElements) || 1)); policies.forEach((policy) => { console.log(`${color(policy.id)} ${color(policy.name)} [${policy.eTag}] ${policy.active ? green("active") : red("inactive")}`); }); console.log(`${color(policies.length)} policies listed.\n`); }); } function policyInfo(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const policy = (yield (0, utils_1.retry)(options.retry, () => sdk.GetResourceManagementClient().GetPolicy(options.policyid))); (0, command_utils_1.verboseLog)(JSON.stringify(policy, null, 2), options.verbose); (0, command_utils_1.printObjectInfo)("Policy", policy, options, ["owner", "id", "active"], color); }); } function checkRequiredParamaters(options) { options.mode === "create" && !options.file && (0, command_utils_1.errorLog)("you have to provide a file with policy definition to create an policy (see mdsp policy --help for more details)", true); options.mode === "delete" && !options.policyid && (0, command_utils_1.errorLog)("you have to provide the policyid to delete (see mdsp policy --help for more details)", true); options.mode === "delete" && !options.policyid && (0, command_utils_1.errorLog)("you have to provide the policyid to delete (see mdsp policy --help for more details)", true); options.mode === "info" && !options.policyid && (0, command_utils_1.errorLog)("you have to provide the policyid (see mdsp policy --help for more details)", true); } //# sourceMappingURL=policy.js.map