@hotglue/cli
Version:
hotglue CLI tools
191 lines (165 loc) • 5.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.handler = exports.desc = exports.command = exports.builder = void 0;
var _debug = _interopRequireDefault(require("../../helpers/debug"));
var _ora = _interopRequireDefault(require("ora"));
var _awsSdk = _interopRequireDefault(require("aws-sdk"));
var _cliTable = _interopRequireDefault(require("cli-table"));
var _descriptions = _interopRequireDefault(require("../../helpers/descriptions"));
var _print = require("../../helpers/print");
var _api = require("../../helpers/api");
var _utils = require("../../helpers/utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const debug = (0, _debug.default)('commands:tenants:custom-field-map');
const command = 'custom-field-map';
exports.command = command;
const desc = 'List tenants with custom field map in a specific flow';
exports.desc = desc;
const builder = async yargs => {
debug('builder', command);
return yargs.option('tenant', _descriptions.default.options['tenant'].config).option('flow', _descriptions.default.options['flow'].config).demandOption('flow', _descriptions.default.options['flow'].demandText);
};
exports.builder = builder;
const processTenant = async ({
debug,
baseUri,
apikey,
env,
flow,
tenant,
isV2Flow
}) => {
// Get all linked connectors/sources of the tuple (env, flow, tenant)
const linkedConnectors = isV2Flow ? await (0, _api.getLinkedConnectors)({
debug,
baseUri,
env,
flow,
tenant,
apikey
}) : await (0, _api.getLinkedSources)({
debug,
baseUri,
env,
flow,
tenant,
apikey
});
const connectors = []; // Generate AWS credentials in order to read the S3 tap folder
const {
accessKeyId,
secretAccessKey,
sessionToken
} = await (0, _api.genCredentialsOnClientApi)({
debug,
baseUri,
task: 'field-map-download',
env,
tenant,
flow,
apikey
});
const s3 = new _awsSdk.default.S3({
accessKeyId,
secretAccessKey,
sessionToken
});
for (const {
id,
tap
} of linkedConnectors) {
const entityLabel = isV2Flow ? 'connectors' : 'taps';
const params = {
Bucket: env,
Key: `${tenant}/flows/${flow}/${entityLabel}/${isV2Flow ? id : tap}/fieldMap.json`
};
try {
await s3.headObject(params).promise();
connectors.push(isV2Flow ? id : tap);
} catch (error) {// Does not have fieldMap.json
}
}
return {
tenant,
connectors
};
};
const handler = async argv => {
debug('handler', command, argv);
const {
hg,
json,
apikey,
env,
flow
} = argv;
let message;
let spinner = (0, _ora.default)();
try {
message = (0, _print.themed)(`Retrieving tenants for environment ${(0, _print.themed)(env, 'info')}`);
!json && spinner.start((0, _print.themed)(`In progress: ${message}...`, 'secondary'));
const [allTenants, supportedFlow] = await Promise.all([(0, _api.getTenants)({
debug,
baseUri: hg.clientApiBaseUri,
apikey,
env
}), (0, _api.getSupportedFlow)({
debug,
baseUri: hg.clientApiBaseUri,
apikey,
env,
flow
})]);
const isV2Flow = (supportedFlow === null || supportedFlow === void 0 ? void 0 : supportedFlow.version) === 2;
!json && spinner.succeed((0, _print.themed)(`Finished: ${message}.`, 'secondary'));
const tenantsWithCustomFieldMap = [];
message = (0, _print.themed)(`Querying for custom field maps for flow ${(0, _print.themed)(flow, 'info')}`);
!json && spinner.start((0, _print.themed)(`In progress: ${message}...`, 'secondary')); // Break the tenants array in to an array of arrays so we can make
// parallel calls and make it faster
for (const tenants of (0, _utils.buildChunks)(allTenants)) {
const maybeTenantsWithCustomFieldMap = await Promise.all(tenants.map(tenant => processTenant({
debug,
baseUri: hg.clientApiBaseUri,
apikey,
env,
flow,
tenant,
isV2Flow
})));
tenantsWithCustomFieldMap.push(...maybeTenantsWithCustomFieldMap.filter(({
connectors
}) => connectors.length > 0));
}
!json && spinner.succeed((0, _print.themed)(`Finished: ${message}.`, 'secondary'));
if (json) {
(0, _print.printJSON)(tenantsWithCustomFieldMap);
} else {
// generate results table
const table = new _cliTable.default({
head: ['Tenant ID', 'Connector IDs']
});
table.push(...tenantsWithCustomFieldMap.map(({
tenant,
connectors
}) => [tenant, connectors.join(', ')])); // print results
console.log(table.toString());
}
} catch (err) {
if (json) {
(0, _print.printJSON)({
status: 'error',
error: err
});
} else {
spinner.fail((0, _print.themed)(`Error: ${message}.`, 'secondary'));
(0, _print.pr)((0, _print.themed)(`Message: ${(0, _print.themed)(err.message)}`, 'secondary'));
debug(err);
if (err && err.response && err.response.data) {
debug('response', err.response.data);
}
}
}
};
exports.handler = handler;