UNPKG

deep-package-manager

Version:
146 lines (120 loc) 3.42 kB
/** * Created by AlexanderC on 11/24/15. */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.AbstractDriver = undefined; var _deepCore = require('deep-core'); var _deepCore2 = _interopRequireDefault(_deepCore); var _AbstractService = require('../Service/AbstractService'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class AbstractDriver extends _deepCore2.default.OOP.Interface { /** * @param {Object} awsService * @param {String|RegExp|Function} baseHash * @param {String} env * @param {Object|null} deployCfg * * @todo: rename base hash */ constructor(awsService, baseHash, env = null, deployCfg = null) { super(['list']); this._awsService = awsService; this._baseHash = baseHash; this._env = env; this._deployCfg = deployCfg; this._stack = {}; } /** * @returns {string} */ static get UNKNOWN_HASH() { return 'unknown'; } static get AVAILABLE_REGIONS() { throw new Error(`AVAILABLE_REGIONS method should be implemented into child class.`); } /** * @param {String} resourceToMatch * @param {String} resourceId * @param {Object} rawData * @private */ _checkPushStack(resourceToMatch, resourceId, rawData = {}) { let resourceHash = _AbstractService.AbstractService.extractBaseHashFromResourceName(resourceToMatch) || AbstractDriver.UNKNOWN_HASH; if (this._matchResource(resourceToMatch, rawData)) { this._stack[resourceHash] = this._stack[resourceHash] || {}; this._stack[resourceHash][resourceId] = rawData; } } /** * @returns {Object} */ get extractResetStack() { let stack = this._stack; this._stack = {}; return stack; } /** * @returns {String|RegExp} */ get baseHash() { return this._baseHash; } /** * @returns {Object} */ get awsService() { return this._awsService; } /** * @param {String} resource * @param {Object} rawData * @returns {Boolean} * @private */ _matchResource(resource, rawData = {}) { if (!this._matchResourceEnv(resource)) { return false; } if (typeof this.baseHash === 'function') { return this.baseHash.bind(this)(resource); } else if (this.baseHash instanceof RegExp) { return this.baseHash.test(resource); } return _AbstractService.AbstractService.extractBaseHashFromResourceName(resource) === this.baseHash; } /** * @param {String} resource * @returns {Boolean} * @private */ _matchResourceEnv(resource) { if (this._env) { let resourceEnv = _AbstractService.AbstractService.extractEnvFromResourceName(resource); // do we need to check env only for typeof hash = string ? if (resourceEnv && resourceEnv !== this._env) { return false; } } return true; } /** * @param {AWS.Request|Object} request * @param {String[]} retryableCodes * @param {Number} delay * @returns {AWS.Request|Object} */ _retryableRequest(request, retryableCodes = ['ResourceInUseException', 'Throttling'], delay = 5000) { request.on('retry', response => { if (retryableCodes.indexOf(response.error.code) !== -1) { response.error.retryable = true; response.error.retryDelay = delay; } }); return request; } } exports.AbstractDriver = AbstractDriver;