UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

2 lines (1 loc) 3.65 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.addDependency=addDependency,exports.removeDependency=removeDependency,exports.obtainDependencies=obtainDependencies;var cfn_resource_1=()=>{var tmp=require("./cfn-resource");return cfn_resource_1=()=>tmp,tmp},errors_1=()=>{var tmp=require("./errors");return errors_1=()=>tmp,tmp},stack_1=()=>{var tmp=require("./stack");return stack_1=()=>tmp,tmp},stage_1=()=>{var tmp=require("./stage");return stage_1=()=>tmp,tmp},util_1=()=>{var tmp=require("./util");return util_1=()=>tmp,tmp};function addDependency(source,target,reason){operateOnDependency(DependencyOperation.ADD,source,target,reason)}function removeDependency(source,target){operateOnDependency(DependencyOperation.REMOVE,source,target)}var DependencyOperation;(function(DependencyOperation2){DependencyOperation2[DependencyOperation2.ADD=0]="ADD",DependencyOperation2[DependencyOperation2.REMOVE=1]="REMOVE"})(DependencyOperation||(DependencyOperation={}));function operateOnDependency(operation,source,target,description){if(source===target)return;const sourceStack=stack_1().Stack.of(source),targetStack=stack_1().Stack.of(target),sourceStage=stage_1().Stage.of(sourceStack),targetStage=stage_1().Stage.of(targetStack);if(sourceStage!==targetStage)throw new(errors_1()).UnscopedValidationError(`You cannot have a dependency from '${source.node.path}' (in ${describeStage(sourceStage)}) to '${target.node.path}' (in ${describeStage(targetStage)}): dependency cannot cross stage boundaries`);const sourcePath=(0,util_1().pathToTopLevelStack)(sourceStack),targetPath=(0,util_1().pathToTopLevelStack)(targetStack),commonStack=(0,util_1().findLastCommonElement)(sourcePath,targetPath);if(!commonStack){const topLevelSource=sourcePath[0],topLevelTarget=targetPath[0],reason={source,target,description};switch(operation){case DependencyOperation.ADD:{topLevelSource._addAssemblyDependency(topLevelTarget,reason);break}case DependencyOperation.REMOVE:{topLevelSource._removeAssemblyDependency(topLevelTarget,reason);break}default:throw new(errors_1()).AssumptionError(`Unsupported dependency operation: ${operation}`)}return}if(commonStack===source)return;if(commonStack===target)throw new(errors_1()).UnscopedValidationError(`Nested stack '${sourceStack.node.path}' cannot depend on a parent stack '${targetStack.node.path}'`);const sourceResource=resourceInCommonStackFor(source,commonStack),targetResource=resourceInCommonStackFor(target,commonStack);switch(operation){case DependencyOperation.ADD:{sourceResource._addResourceDependency(targetResource);break}case DependencyOperation.REMOVE:{sourceResource._removeResourceDependency(targetResource);break}default:throw new(errors_1()).AssumptionError(`Unsupported dependency operation: ${operation}`)}}function obtainDependencies(source){let dependencies=[];return source instanceof cfn_resource_1().CfnResource&&(dependencies=source.obtainResourceDependencies()),(0,util_1().pathToTopLevelStack)(stack_1().Stack.of(source)).forEach(stack=>{dependencies=[...dependencies,...stack._obtainAssemblyDependencies({source})]}),dependencies}function resourceInCommonStackFor(element,commonStack){const resource=stack_1().Stack.isStack(element)?element.nestedStackResource:element;if(!resource)throw new(errors_1()).AssumptionError(`Unexpected value for resource when looking at ${element}!`);const resourceStack=stack_1().Stack.of(resource);return commonStack===resourceStack?resource:resourceInCommonStackFor(resourceStack,commonStack)}function describeStage(assembly){return assembly?assembly.parentStage?`Stage '${assembly.node.path}'`:"the App":"an unrooted construct tree"}