UNPKG

@altostra/core

Version:

Core library for shared types and logic

1 lines • 9.02 kB
"use strict";var _BlueprintHelper_instances,_BlueprintHelper_connectionsCache,_BlueprintHelper_resourcesCache,_BlueprintHelper_suppressParamSupportCheck,_BlueprintHelper_runMigration,_BlueprintHelper_migrate,__awaiter=this&&this.__awaiter||function(e,r,t,i){return new(t||(t=Promise))((function(n,o){function s(e){try{l(i.next(e))}catch(e){o(e)}}function a(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var r;e.done?n(e.value):(r=e.value,r instanceof t?r:new t((function(e){e(r)}))).then(s,a)}l((i=i.apply(e,r||[])).next())}))},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(e,r,t,i){if("a"===t&&!i)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof r?e!==r||!i:!r.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===t?i:"a"===t?i.call(e):i?i.value:r.get(e)},__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(e,r,t,i,n){if("m"===i)throw new TypeError("Private method is not writable");if("a"===i&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof r?e!==r||!n:!r.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===i?n.call(e,t):n?n.value=t:r.set(e,t),t};Object.defineProperty(exports,"__esModule",{value:!0}),exports.BlueprintHelper=void 0;const util_1=require("util"),EnvironmentConfigurations_1=require("../../bizops/ServiceClients/EnvironmentConfigurations"),Errors_1=require("../../common/Errors"),AltoError_1=require("../../common/Errors/AltoError"),Object_1=require("../../common/Utils/Object"),type_validations_1=require("@altostra/type-validations"),from_1=require("@reactivex/ix-es2015-cjs/iterable/from"),filter_1=require("@reactivex/ix-es2015-cjs/iterable/operators/filter"),flatmap_1=require("@reactivex/ix-es2015-cjs/iterable/operators/flatmap"),groupby_1=require("@reactivex/ix-es2015-cjs/iterable/operators/groupby"),map_1=require("@reactivex/ix-es2015-cjs/iterable/operators/map"),some_1=require("@reactivex/ix-es2015-cjs/iterable/some"),Blueprint_1=require("../Blueprint"),Blueprint_2=require("../Blueprint/Blueprint"),ConnectionDefinitions_1=require("../Connections/Factory/ConnectionDefinitions"),ConnectionsFactory_1=require("../Connections/Factory/ConnectionsFactory"),ResourcesIdsManager_1=require("../ResourcesIdsManager"),IntegrityCheck_1=require("../Utils/IntegrityCheck"),BluePrintGlobalVariablesHelper_1=require("./BluePrintGlobalVariablesHelper"),ConnectionCollection_1=require("./ConnectionCollection"),ParametersHelper_1=require("./ParametersHelper"),RequiredFiles_1=require("./RequiredFiles"),ResourceCollection_1=require("./ResourceCollection");class BlueprintHelper{constructor(e,{suppressParamSupportCheck:r,runMigration:t=!0,suppressIntegrityCheck:i}={}){if(this.blueprint=e,_BlueprintHelper_instances.add(this),this.connectionsFactory=new ConnectionsFactory_1.ConnectionsFactory(ConnectionDefinitions_1.standardDefinitions),_BlueprintHelper_connectionsCache.set(this,new WeakMap),_BlueprintHelper_resourcesCache.set(this,new WeakMap),_BlueprintHelper_suppressParamSupportCheck.set(this,void 0),_BlueprintHelper_runMigration.set(this,void 0),!e)throw new Blueprint_1.BlueprintError("Blueprint cannot be null or undefined");if(e.version!==Blueprint_2.SUPPORTED_VERSION)throw new Blueprint_1.BlueprintError("Invalid blueprint version");if(!i){const r=(0,IntegrityCheck_1.integrityCheck)(e);if(r.length>0)throw Errors_1.UserFacingError.create("Blueprint is corrupted. Please review the problems and fix them",{category:"user-fault",type:"blueprint-integrity",data:{problems:r}})}this.resources=new ResourceCollection_1.ResourceCollection(__classPrivateFieldGet(this,_BlueprintHelper_resourcesCache,"f"),__classPrivateFieldGet(this,_BlueprintHelper_connectionsCache,"f"),this),this.connections=new ConnectionCollection_1.BlueprintConnectionCollection(__classPrivateFieldGet(this,_BlueprintHelper_connectionsCache,"f"),this),this.idsManager=new ResourcesIdsManager_1.ResourceIdsManager(e.resources),setUnsafeProp(this),__classPrivateFieldSet(this,_BlueprintHelper_runMigration,t,"f"),__classPrivateFieldSet(this,_BlueprintHelper_suppressParamSupportCheck,!!r,"f"),this.parameters=new ParametersHelper_1.BlueprintParametersHelper(this,__classPrivateFieldGet(this,_BlueprintHelper_suppressParamSupportCheck,"f")),this.globalEnvironmentVariables=new BluePrintGlobalVariablesHelper_1.BluePrintGlobalVariablesHelper(this),t&&__classPrivateFieldGet(this,_BlueprintHelper_instances,"m",_BlueprintHelper_migrate).call(this)}get unsafe(){return!!this.blueprint.unsafe}getRequiredFiles(){return __awaiter(this,void 0,void 0,(function*(){const e=yield Promise.all(this.resources.pipe((0,map_1.map)(RequiredFiles_1.getRequiredFiles)));return(0,from_1.from)(e).pipe((0,flatmap_1.flatMap)(e=>e),(0,groupby_1.groupBy)(({path:e})=>e),(0,map_1.map)(e=>({path:e.key,requiringResources:[...e.pipe((0,flatmap_1.flatMap)(({requiringResources:e})=>e))]})))}))}applyConfiguration(e,r){var t;const{paramMapping:i,expectFullConfiguration:n}=Object.assign(Object.assign({},defaultApplyConfigurationOptions),r),o=[];if(!this.parameters.parameters)return o;for(const[r,s]of Object.entries(this.parameters.parameters)){if(!s||0===s.paths.length)continue;const l=null!==(t=null==i?void 0:i[r])&&void 0!==t?t:r;if(!Object.prototype.hasOwnProperty.call(e,l)){const t=this.parameters.getParameterUsage(l),i=t.resources.map(e=>e.id),s=t.connections.map(e=>({from:e.sourceId,to:e.targetId}));if(n){const n=a(l,t);throw Errors_1.UserFacingError.create(n,{category:"user-fault",type:"config-application",data:{config:e,key:l,paramName:r,resources:i,connections:s}})}o.push({type:"missing-params",key:l,paramName:r,resources:i,connections:s});continue}const c=e[l];for(const e of s.paths){const t=this.parameters.getParamTypeAt(e);if(!t)throw AltoError_1.AltoError.create(`Parameter [${r}] defined on an unsupported path: [${e}]`,{data:{paramName:r,configName:l,spec:s,path:e}});const i=t.parse(c);if(t.isValid&&!t.isValid(i))throw AltoError_1.AltoError.create(`Parameter [${r}] value was parsed to invalid value.`,{data:{paramName:r,configName:l,spec:s,path:e,configValue:c,blueprintValue:i,propertyType:t}});const{parent:n,key:o}=this.parameters.getParamParent(e);n[o]=i}}const s=(0,type_validations_1.createRejectionsCollector)();if(!(0,Blueprint_2.isBlueprint)(this.blueprint,s))throw AltoError_1.AltoError.create("Blueprint configured, but has invalid values",{data:{config:e,validationErrors:s}});function a(e,r){let t="";if(t=(0,EnvironmentConfigurations_1.isReservedKey)(e)?`Missing value for default ${function(e){switch(e){case EnvironmentConfigurations_1.VPCDataKeys.VPCId:return"VPC Id";case EnvironmentConfigurations_1.VPCDataKeys.PublicSubnets:return"Public Subnets";case EnvironmentConfigurations_1.VPCDataKeys.PrivateSubnets:return"Private Subnets";case EnvironmentConfigurations_1.VPCDataKeys.Subnets:return"Subnets"}}(e)}\nYou can provide the value in the environment aws parameters configuration in the Web Console`:`Missing value for parameter [${e}]\nYou can provide the value in the environment parameters configuration in the Web Console, or by using the CLI deploy command’s --config-set option.`,r.resources.length>0){t+="\nUsed by resources:\n "+r.resources.map(printResource).join("\n ")}if(r.connections.length>0){t+="\nUsed by connections:\n "+r.connections.map(({source:e,target:r})=>`${printResource(e)}] -> ${printResource(r)}]`).join("\n ")}return t}return o}clone(){return new BlueprintHelper((0,Object_1.deepClone)(this.blueprint),{suppressParamSupportCheck:__classPrivateFieldGet(this,_BlueprintHelper_suppressParamSupportCheck,"f"),runMigration:__classPrivateFieldGet(this,_BlueprintHelper_runMigration,"f")})}toJSON(){return this.blueprint}[(_BlueprintHelper_connectionsCache=new WeakMap,_BlueprintHelper_resourcesCache=new WeakMap,_BlueprintHelper_suppressParamSupportCheck=new WeakMap,_BlueprintHelper_runMigration=new WeakMap,_BlueprintHelper_instances=new WeakSet,util_1.inspect.custom)](){return this.blueprint}}function printResource({id:e,name:r}){return`${r} [${e}]`}exports.BlueprintHelper=BlueprintHelper,_BlueprintHelper_migrate=function(){const e=[...this.connections.ofType("connection.trigger").pipe((0,filter_1.filter)(e=>"resource.notification-topic"===e.source.actualType))];for(const r of e){const{source:e,target:t}=r;this.connections.delete(r),this.connections.add({type:"connection.topic-subscription",from:e.id,to:t.id})}};const defaultApplyConfigurationOptions={expectFullConfiguration:!0},haveUnsafeProp=new WeakSet;function setUnsafeProp(e){var r;if(haveUnsafeProp.has(e.blueprint))return;haveUnsafeProp.add(e.blueprint);const t=Object.getOwnPropertyDescriptor(e.blueprint,"unsafe");(null===(r=null==t?void 0:t.configurable)||void 0===r||r)&&Object.defineProperty(e.blueprint,"unsafe",{configurable:!1,enumerable:!0,get:()=>(0,some_1.some)(e.resources,e=>e.is("resource.cloud-formation.custom-resources"))})}