UNPKG

cloudworker-proxy

Version:
274 lines (219 loc) 8.94 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /* * Deployment */ var _ramda = require('ramda'); var _ramda2 = _interopRequireDefault(_ramda); var _package = require('../../package.json'); var _config = require('../config'); var _config2 = _interopRequireDefault(_config); var _accessKeys = require('../accessKeys'); var _fetch = require('../fetch'); var _fetch2 = _interopRequireDefault(_fetch); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var _class = function () { function _class() { _classCallCheck(this, _class); this.data = { /* * Versions */ versionFramework: null, versionEnterprisePlugin: null, versionSDK: _package.version, /* * Service Data * - Standard service data */ serverlessFile: null, serverlessFileName: null, tenantUid: null, appUid: null, tenantName: null, appName: null, serviceName: null, stageName: null, regionName: null, // the arn generated for fetching constructed logs logsRoleArn: null, status: null, // success OR errror error: null, // IF ARCHIVED... everything below this will be null archived: false, /* * App Data * - Provider, functions, subscriptions, resources, etc... * - Function-defaults in `provider` will be replicated across each function */ provider: { type: 'aws' }, functions: {}, subscriptions: [], resources: {}, layers: {}, plugins: [], safeguards: [], secrets: [], outputs: {}, custom: {} }; } /* * Get */ _createClass(_class, [{ key: 'get', value: function get() { return this.data; } /* * Set */ }, { key: 'set', value: function set(data) { // TODO: Validate this.data = _ramda2.default.mergeDeepRight(this.data, data); return this.data; } /* * Set Function */ }, { key: 'setFunction', value: function setFunction(data) { if (!data.name) { throw new Error(`function 'name' is required`); } var fn = { // Non-provider-specific data goes here name: null, description: null, type: 'awsLambda', timeout: null, // Provider-specific data goes here custom: { handler: null, memorySize: null, runtime: null, role: null, onError: null, awsKmsKeyArn: null, tags: {}, vpc: { securityGroupIds: [], subnetIds: [] }, layers: [] } }; this.data.functions[data.name] = _ramda2.default.mergeDeepRight(fn, data); return this.data.functions[data.name]; } /* * Set Subscription */ }, { key: 'setSubscription', value: function setSubscription(data) { if (!data.type) { throw new Error(`subscription 'type' is required`); } if (!data.function) { throw new Error(`subscription 'function' is required`); } if (!this.data.functions[data.function]) { throw new Error(`subscription 'function' must be added to the deployment before subscriptions`); } var sub = { // Non-provider-specific data goes here type: null, function: null, // Provider-specific data goes here custom: {} // Add custom subscription properties per event type };switch (data.type) { case 'aws.apigateway.http': sub.custom.path = null; sub.custom.method = null; sub.custom.restApiId = null; sub.custom.cors = false; break; default: break; } sub = _ramda2.default.mergeDeepRight(sub, data); this.data.subscriptions.push(sub); return sub; } /* * Save */ }, { key: 'save', value: function () { var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { var dashboardApi, dashboardUrl, accessKey, response; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: // Create backend & frontend urls dashboardApi = _config2.default.backendUrl; dashboardApi += `tenants/${this.data.tenantName}/`; dashboardApi += `applications/${this.data.appName}/`; dashboardApi += `services/${this.data.serviceName}/`; dashboardApi += `stages/${this.data.stageName}/`; dashboardApi += `regions/${this.data.regionName}/`; dashboardApi += `deployments`; dashboardUrl = _config2.default.frontendUrl; dashboardUrl += `tenants/${this.data.tenantName}/`; dashboardUrl += `applications/${this.data.appName}/`; dashboardUrl += `services/${this.data.serviceName}/`; dashboardUrl += `stage/${this.data.stageName}/`; dashboardUrl += `region/${this.data.regionName}`; // Fetch access key _context.next = 15; return (0, _accessKeys.getAccessKeyForTenant)(this.data.tenantName); case 15: accessKey = _context.sent; _context.next = 18; return (0, _fetch2.default)(dashboardApi, { // eslint-disable-line method: 'POST', body: JSON.stringify(this.data), headers: { Authorization: `bearer ${accessKey}` } }); case 18: response = _context.sent; _context.next = 21; return response.json(); case 21: _context.t0 = _context.sent; _context.t1 = dashboardUrl; return _context.abrupt('return', { deployment: _context.t0, dashboardUrl: _context.t1 }); case 24: case 'end': return _context.stop(); } } }, _callee, this); })); function save() { return _ref.apply(this, arguments); } return save; }() }]); return _class; }(); exports.default = _class; //# sourceMappingURL=index.js.map