UNPKG

@csermet/multiprovider

Version:

cloud-graph provider plugin for AWS used to fetch AWS cloud data.

110 lines (109 loc) 4.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const sdk_1 = __importDefault(require("@cloudgraph/sdk")); const iot_1 = __importDefault(require("aws-sdk/clients/iot")); const groupBy_1 = __importDefault(require("lodash/groupBy")); const isEmpty_1 = __importDefault(require("lodash/isEmpty")); const logger_1 = __importDefault(require("../../properties/logger")); const utils_1 = require("../../utils"); const errorLog_1 = __importDefault(require("../../utils/errorLog")); const lt = { ...logger_1.default }; const { logger } = sdk_1.default; const serviceName = 'IoT thing attribute'; const errorLog = new errorLog_1.default(serviceName); const endpoint = utils_1.initTestEndpoint(serviceName); const MAX_ITEMS = 250; const listThingsForRegion = async ({ iot, resolveRegion, }) => new Promise(resolve => { const thingAttributeList = []; const listThingsOpts = {}; const listAllThings = (token) => { listThingsOpts.maxResults = MAX_ITEMS; if (token) { listThingsOpts.nextToken = token; } try { iot.listThings(listThingsOpts, (err, data) => { const { nextToken, things } = data || {}; if (err) { errorLog.generateAwsErrorLog({ functionName: 'iot:listThings', err, }); } /** * No IoT things for this region */ if (isEmpty_1.default(data)) { return resolveRegion(); } thingAttributeList.push(...things); if (nextToken) { logger.debug(lt.foundMoreIoTThings(things.length)); listAllThings(nextToken); } else { resolve(thingAttributeList); } }); } catch (error) { resolve([]); } }; listAllThings(); }); exports.default = async ({ regions, config, }) => new Promise(async (resolve) => { const iotData = []; const regionPromises = []; const descriptionPromises = []; // get all things for all regions regions.split(',').map(region => { const iot = new iot_1.default({ ...config, region, endpoint }); const regionPromise = new Promise(async (resolveRegion) => { const thingAttributeList = await listThingsForRegion({ iot, resolveRegion, }); iotData.push(...thingAttributeList.map((thingAttribute) => ({ ...thingAttribute, region, }))); resolveRegion(); }); regionPromises.push(regionPromise); }); await Promise.all(regionPromises); // get all information for each thing iotData.map(({ thingName, region }, idx) => { const iot = new iot_1.default({ ...config, region, endpoint }); const descriptionPromise = new Promise(async (resolveDesc) => { const describeThingOpts = { thingName }; try { iot.describeThing(describeThingOpts, (err, data) => { if (err) { errorLog.generateAwsErrorLog({ functionName: 'iot:describeThing', err, }); } iotData[idx] = { ...iotData[idx], ...data, }; resolveDesc(); }); } catch (error) { resolveDesc(); } }); descriptionPromises.push(descriptionPromise); }); logger.debug(lt.gettingIoTThings); await Promise.all(descriptionPromises); errorLog.reset(); resolve(groupBy_1.default(iotData, 'region')); });