Closed michanto closed 1 week ago
Example usage: Find all stacks (including sub stacks) in the construct tree that are deployed to eu-west-1:
function printFoundUnder<T extends Construct>(
search: ConstructTreeSearch<T>, scope: Construct, stopCondition?: IStopCondition) {
let found = search.searchDown(scope, stopCondition)
console.log(`Found ${found.length} under '${
scope.node.path == undefined || scope.node.path.length == 0 ? "App" : scope.node.path}'`)
found.forEach(c => console.log(" " + c.node.path))
}
export function isStackForRegion(region: string) {
return (x: IConstruct ): x is Stack =>
Stack.isStack(x) && x.region == region
}
let dubStacksSearch = ConstructTreeSearch.for(isStackForRegion("eu-west-1"))
printFoundUnder(dubStacksSearch, app)
Do you for-see any usage for this directly in a CDK application?
This will be available in the cdk-orchestration package.
The Construct Tree in the CDK is an underused asset of the framework. The CDK contains lots of code for dealing with RunTimeTypeInfo, searching the construct tree, storing data in the construct tree, etc. Much of this is hidden from the CDK user, and should be abstracted out into reusable classes that expose the full suite of techniques pioneered by the CDK team.
A good start is ConstructTreeSearch, which allows a user to navigate the construct tree the same way the CDK does.
Tests for this class will ensue that Stack, CfnElement, and Resource classes can be found in the Construct tree using predicates and exercise the searchSelf, searchDown, and searchUp methods. They will also exercise the stopCondition for restricting searchUp to the current stack, and searchDown to exclude sub stacks.