@shinkashi/insight-sdk
Version:
Solution Insight SDK for JavaScript
89 lines (88 loc) • 4.04 kB
JavaScript
import axios from 'axios';
import { InsightRepository } from './repository';
import { InsightResourceRepository } from './resource';
import { DashboardRepository } from './dashboard';
import { InsightTicketRepository } from './ticket';
import { InsightBucketRepository } from './bucket';
import * as yaml from 'js-yaml';
import { InsightACMPEnvService } from './acmpEnv';
export let fs;
export let os;
try {
fs = require('fs');
os = require('os');
}
catch (_a) {
console.log('fs/os module are not available, ignore');
}
export 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 InsightRepository(this, '/customers', 'PATCH');
this.user = new InsightRepository(this, '/identity/users', 'PATCH');
this.group = new InsightRepository(this, '/identity/groups', 'PATCH');
this.policy = new InsightRepository(this, '/policies', 'PUT');
this.application = new InsightRepository(this, '/applications', 'PATCH');
this.resource = new InsightResourceRepository(this, '/resources', 'PUT');
this.serviceDef = new InsightRepository(this, '/service_defs', 'PATCH');
this.approvalPlan = new InsightRepository(this, '/approval_plans', 'PATCH');
this.nflexModule = new InsightRepository(this, '/modules', 'PUT');
this.bucket = new InsightBucketRepository(this, '/storage/buckets', 'PUT');
this.globalCustomer = new InsightRepository(this, '/global-customers', 'PATCH');
this.dashboard = new DashboardRepository(this, '/v2/dashboards', 'PATCH');
this.ticket = new InsightTicketRepository(this);
this.env = new 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.create({ baseURL: this.url, headers: this.headers, auth });
}
static getCredentialFromFlexerYaml(regionName) {
var _a;
const file = fs.readFileSync(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
};
}
}