UNPKG

@shinkashi/insight-sdk

Version:

Solution Insight SDK for JavaScript

117 lines (116 loc) 5.52 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InsightClient = exports.os = exports.fs = void 0; const axios_1 = __importDefault(require("axios")); const repository_1 = require("./repository"); const resource_1 = require("./resource"); const dashboard_1 = require("./dashboard"); const ticket_1 = require("./ticket"); const bucket_1 = require("./bucket"); const yaml = __importStar(require("js-yaml")); const acmpEnv_1 = require("./acmpEnv"); try { exports.fs = require('fs'); exports.os = require('os'); } catch (_a) { console.log('fs/os module are not available, ignore'); } class InsightClient { constructor(credential = undefined, prefix = '') { this.credential = credential; this.prefix = prefix; this.url = '/cmp/api'; // default for GUI authentication this.apiKey = ''; this.apiSecret = ''; this.headers = {}; this.customer = new repository_1.InsightRepository(this, '/customers', 'PATCH'); this.user = new repository_1.InsightRepository(this, '/identity/users', 'PATCH'); this.group = new repository_1.InsightRepository(this, '/identity/groups', 'PATCH'); this.policy = new repository_1.InsightRepository(this, '/policies', 'PUT'); this.application = new repository_1.InsightRepository(this, '/applications', 'PATCH'); this.resource = new resource_1.InsightResourceRepository(this, '/resources', 'PUT'); this.serviceDef = new repository_1.InsightRepository(this, '/service_defs', 'PATCH'); this.approvalPlan = new repository_1.InsightRepository(this, '/approval_plans', 'PATCH'); this.nflexModule = new repository_1.InsightRepository(this, '/modules', 'PUT'); this.bucket = new bucket_1.InsightBucketRepository(this, '/storage/buckets', 'PUT'); this.globalCustomer = new repository_1.InsightRepository(this, '/global-customers', 'PATCH'); this.dashboard = new dashboard_1.DashboardRepository(this, '/v2/dashboards', 'PATCH'); this.ticket = new ticket_1.InsightTicketRepository(this); this.env = new acmpEnv_1.InsightACMPEnvService(this); if (typeof credential === 'string') { credential = InsightClient.getCredentialFromFlexerYaml(credential); } if (credential) { this.url = credential.url; this.apiKey = credential.apiKey; this.apiSecret = credential.apiSecret; this.headers = credential.headers; } // Use basic auth only when API key is specified const auth = this.apiKey ? { username: this.apiKey, password: this.apiSecret } : undefined; // Set default Content-Type. // Axios could automatically add 'application/json;charset=utf-8' but // the charset part cannot be understood by Insight, unfortunately. this.headers['Content-Type'] = 'application/json'; this.ax = axios_1.default.create({ baseURL: this.url, headers: this.headers, auth }); } static getCredentialFromFlexerYaml(regionName) { var _a; const file = exports.fs.readFileSync(exports.os.homedir() + '/.flexer.yaml', 'utf-8'); const config = yaml.load(file); if (typeof config === 'undefined') { throw new Error('Cannot read .flexer.yaml file'); } if (typeof config === 'string') { throw new Error('.flexer.yaml is not JSON'); } const regionConfig = (_a = config === null || config === void 0 ? void 0 : config.regions) === null || _a === void 0 ? void 0 : _a[regionName]; if (regionConfig === undefined) { throw new Error(`flexerRegion ${regionName} is not found in flexer.yaml`); } // Build headers const headers = {}; for (let k in regionConfig) { const v = regionConfig[k]; if (k.startsWith('x_')) { k = k.replace(/_/g, '-') .replace(/\w+/g, x => x.charAt(0).toUpperCase() + x.substr(1).toLowerCase()); headers[k] = v; } } return { url: regionConfig['cmp_url'], apiKey: regionConfig['cmp_api_key'], apiSecret: regionConfig['cmp_api_secret'], headers: headers }; } } exports.InsightClient = InsightClient;