UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

4 lines (3 loc) 3.19 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.countResourcesProperties=exports.countResources=exports.hasResourceProperties=exports.hasResource=exports.allResourcesProperties=exports.allResources=exports.findResources=void 0;const absent_1=require("./matchers/absent"),section_1=require("./section"),__1=require("..");function findResources(template,type,props={}){const section=template.Resources??{},result=section_1.matchSection(filterType(section,type),props);return result.match?result.matches:{}}exports.findResources=findResources;function allResources(template,type,props){const section=template.Resources??{},result=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.`,section_1.formatAllMismatches(result.analyzed,result.matches)].join(` `)}else return[`Template has ${result.analyzedCount} resource(s) with type ${type}, but none match as expected.`,section_1.formatAllMismatches(result.analyzed)].join(` `)}exports.allResources=allResources;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}))}exports.allResourcesProperties=allResourcesProperties;function hasResource(template,type,props){const section=template.Resources??{},result=section_1.matchSection(filterType(section,type),props);if(!result.match)return section_1.formatSectionMatchFailure(`resources with type ${type}`,result)}exports.hasResource=hasResource;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}))}exports.hasResourceProperties=hasResourceProperties;function countResources(template,type){const section=template.Resources??{},types=filterType(section,type);return Object.entries(types).length}exports.countResources=countResources;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=section_1.matchSection(filterType(section,type),__1.Match.objectLike({Properties:props}));return result.match?Object.keys(result.matches).length:0}exports.countResourcesProperties=countResourcesProperties;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}),{})}