UNPKG

deep-package-manager

Version:
121 lines (99 loc) 3.05 kB
/** * Created by AlexanderC on 11/24/15. */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.CloudFrontDriver = undefined; var _deepCore = require('deep-core'); var _deepCore2 = _interopRequireDefault(_deepCore); var _AbstractTaggingDriver = require('./AbstractTaggingDriver'); var _CloudFrontService = require('../Service/CloudFrontService'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class CloudFrontDriver extends _AbstractTaggingDriver.AbstractTaggingDriver { /** * @param {*} args */ constructor(...args) { super(...args); } /** * @returns {String} */ region() { // used this region to avoid "Invocation of TagResources for this resource is not supported in this region" return _deepCore2.default.AWS.Region.US_EAST_VIRGINIA; } /** * @returns {String[]} */ static get AVAILABLE_REGIONS() { return _CloudFrontService.CloudFrontService.AVAILABLE_REGIONS; } /** * @param {Function} cb */ list(cb) { Promise.all([this.listFilteredResources(), this._listDistributions()]).then(responses => { let resourcesToPush = responses[0]; let distributions = responses[1]; let resourcesToPushMap = resourcesToPush.reduce((map, resource) => { map[resource.ResourceARN] = resource.Tags; return map; }, {}); let arnsToPush = Object.keys(resourcesToPushMap); distributions.forEach(distribution => { if (arnsToPush.indexOf(distribution.ARN) !== -1) { // @todo: refactor deepify list to extract resource id directly from tags distribution.DeepResourceId = this._generateResourceIdFromTags(resourcesToPushMap[distribution.ARN]); this._checkPushStack(distribution.DeepResourceId, distribution.Id, distribution); } }); }).catch(e => { console.warn('Error while listing cloudfront distributions: ', e.stack); }).then(() => { // avoid callback synchronous errors setImmediate(() => { cb(); }); }); } /** * @returns {String} */ resourceType() { return 'cloudfront:distribution'; } /** * @param {String|null} nextMarker * @returns {Promise} * @private */ _listDistributions(nextMarker = null) { let payload = { MaxItems: CloudFrontDriver.MAX_ITEMS }; if (nextMarker) { payload.Marker = nextMarker; } return this._awsService.listDistributions(payload).promise().then(data => { let distributions = data.DistributionList.Items || []; if (data.DistributionList.IsTruncated) { let marker = data.DistributionList.NextMarker; return this._listDistributions(marker).then(childDistributions => { return distributions.concat(childDistributions); }); } else { return distributions; } }); } /** * @returns {String} */ static get MAX_ITEMS() { return '100'; } } exports.CloudFrontDriver = CloudFrontDriver;