UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

5 lines (4 loc) 3.73 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.findResources=findResources,exports.getResourceId=getResourceId,exports.allResources=allResources,exports.allResourcesProperties=allResourcesProperties,exports.hasResource=hasResource,exports.hasResourceProperties=hasResourceProperties,exports.countResources=countResources,exports.countResourcesProperties=countResourcesProperties;var __1=()=>{var tmp=require("..");return __1=()=>tmp,tmp},absent_1=()=>{var tmp=require("./matchers/absent");return absent_1=()=>tmp,tmp},section_1=()=>{var tmp=require("./section");return section_1=()=>tmp,tmp};function findResources(template,type,props={}){const section=template.Resources??{},result=(0,section_1().matchSection)(filterType(section,type),props);return result.match?result.matches:{}}function getResourceId(template,type,props={}){const section=template.Resources??{},result=(0,section_1().matchSection)(filterType(section,type),props);if(!result.match)return{matchError:(0,section_1().formatSectionMatchFailure)(`resources with type ${type}`,result)};const resourceIds=Object.keys(result.matches);return resourceIds.length!==1?{matchError:[`Template has ${resourceIds.length} matches, expected only one.`,(0,section_1().formatAllMatches)(result.matches)].join(` `)}:{resourceId:resourceIds[0]}}function allResources(template,type,props){const section=template.Resources??{},result=(0,section_1().matchSection)(filterType(section,type),props);if(result.match){const matchCount=Object.keys(result.matches).length;if(result.analyzedCount>matchCount)return[`Template has ${result.analyzedCount} resource(s) with type ${type}, but only ${matchCount} match as expected.`,(0,section_1().formatAllMismatches)(result.analyzed,result.matches)].join(` `)}else return[`Template has ${result.analyzedCount} resource(s) with type ${type}, but none match as expected.`,(0,section_1().formatAllMismatches)(result.analyzed)].join(` `)}function allResourcesProperties(template,type,props){let amended=template;return(!__1().Matcher.isMatcher(props)||!(props instanceof absent_1().AbsentMatch))&&(amended=JSON.parse(JSON.stringify(template)),amended=addEmptyProperties(amended)),allResources(amended,type,__1().Match.objectLike({Properties:props}))}function hasResource(template,type,props){const section=template.Resources??{},result=(0,section_1().matchSection)(filterType(section,type),props);if(!result.match)return(0,section_1().formatSectionMatchFailure)(`resources with type ${type}`,result)}function hasResourceProperties(template,type,props){let amended=template;return(!__1().Matcher.isMatcher(props)||!(props instanceof absent_1().AbsentMatch))&&(amended=JSON.parse(JSON.stringify(template)),amended=addEmptyProperties(amended)),hasResource(amended,type,__1().Match.objectLike({Properties:props}))}function countResources(template,type){const section=template.Resources??{},types=filterType(section,type);return Object.entries(types).length}function countResourcesProperties(template,type,props){let amended=template;(!__1().Matcher.isMatcher(props)||!(props instanceof absent_1().AbsentMatch))&&(amended=JSON.parse(JSON.stringify(template)),amended=addEmptyProperties(amended));const section=amended.Resources??{},result=(0,section_1().matchSection)(filterType(section,type),__1().Match.objectLike({Properties:props}));return result.match?Object.keys(result.matches).length:0}function addEmptyProperties(template){let section=template.Resources??{};return Object.keys(section).map(key=>{section[key].hasOwnProperty("Properties")||(section[key].Properties={})}),template}function filterType(section,type){return Object.entries(section??{}).filter(([_,v])=>v.Type===type).reduce((agg,[k,v])=>({...agg,[k]:v}),{})}