Skip to content

Commit

Permalink
Merge branch 'master' into bump-cfnspec/v53.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Jan 18, 2022
2 parents 1d549fa + c9f675c commit 065df78
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 5 deletions.
7 changes: 6 additions & 1 deletion allowed-breaking-changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,9 @@ removed:@aws-cdk/aws-lambda-event-sources.SelfManagedKafkaEventSourceProps.retry
removed:@aws-cdk/aws-lambda-event-sources.SelfManagedKafkaEventSourceProps.tumblingWindow
base-types:@aws-cdk/aws-lambda-event-sources.KafkaEventSourceProps
base-types:@aws-cdk/aws-lambda-event-sources.ManagedKafkaEventSourceProps
base-types:@aws-cdk/aws-lambda-event-sources.SelfManagedKafkaEventSourceProps
base-types:@aws-cdk/aws-lambda-event-sources.SelfManagedKafkaEventSourceProps

# fixed vpc property of BaseLoadBalancer so it correctly implements I(Application|Network)LoadBalancer (i.e: must be optional)
changed-type:@aws-cdk/aws-elasticloadbalancingv2.ApplicationLoadBalancer.vpc
changed-type:@aws-cdk/aws-elasticloadbalancingv2.BaseLoadBalancer.vpc
changed-type:@aws-cdk/aws-elasticloadbalancingv2.NetworkLoadBalancer.vpc
30 changes: 30 additions & 0 deletions packages/@aws-cdk/assertions/lib/private/parameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { filterLogicalId, formatFailure, matchSection } from './section';
import { Template } from './template';

export function findParameters(template: Template, logicalId: string, props: any = {}): { [key: string]: { [key: string]: any } } {
const section: { [key: string] : {} } = template.Parameters;
const result = matchSection(filterLogicalId(section, logicalId), props);

if (!result.match) {
return {};
}

return result.matches;
}

export function hasParameter(template: Template, logicalId: string, props: any): string | void {
const section: { [key: string] : {} } = template.Parameters;
const result = matchSection(filterLogicalId(section, logicalId), props);
if (result.match) {
return;
}

if (result.closestResult === undefined) {
return 'No parameters found in the template';
}

return [
`Template has ${result.analyzedCount} parameters, but none match as expected.`,
formatFailure(result.closestResult),
].join('\n');
}
10 changes: 8 additions & 2 deletions packages/@aws-cdk/assertions/lib/private/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
export type Template = {
Resources: { [logicalId: string]: Resource },
Outputs: { [logicalId: string]: Output },
Mappings: { [logicalId: string]: Mapping }
Mappings: { [logicalId: string]: Mapping },
Parameters: { [logicalId: string]: Parameter }
}

export type Resource = {
Expand All @@ -13,4 +14,9 @@ export type Resource = {

export type Output = { [key: string]: any };

export type Mapping = { [key: string]: any };
export type Mapping = { [key: string]: any };

export type Parameter = {
Type: string;
[key: string]: any;
}
25 changes: 25 additions & 0 deletions packages/@aws-cdk/assertions/lib/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Match } from './match';
import { Matcher } from './matcher';
import { findMappings, hasMapping } from './private/mappings';
import { findOutputs, hasOutput } from './private/outputs';
import { findParameters, hasParameter } from './private/parameters';
import { countResources, findResources, hasResource, hasResourceProperties } from './private/resources';
import { Template as TemplateType } from './private/template';

Expand Down Expand Up @@ -108,6 +109,30 @@ export class Template {
return findResources(this.template, type, props);
}

/**
* Assert that a Parameter with the given properties exists in the CloudFormation template.
* By default, performs partial matching on the parameter, via the `Match.objectLike()`.
* To configure different behavior, use other matchers in the `Match` class.
* @param logicalId the name of the parameter. Provide `'*'` to match all parameters in the template.
* @param props the parameter as should be expected in the template.
*/
public hasParameter(logicalId: string, props: any): void {
const matchError = hasParameter(this.template, logicalId, props);
if (matchError) {
throw new Error(matchError);
}
}

/**
* Get the set of matching Parameters that match the given properties in the CloudFormation template.
* @param logicalId the name of the parameter. Provide `'*'` to match all parameters in the template.
* @param props by default, matches all Parameters in the template.
* When a literal object is provided, performs a partial match via `Match.objectLike()`.
* Use the `Match` APIs to configure a different behaviour. */
public findParameters(logicalId: string, props: any = {}): { [key: string]: { [key: string]: any } } {
return findParameters(this.template, logicalId, props);
}

/**
* Assert that an Output with the given properties exists in the CloudFormation template.
* By default, performs partial matching on the resource, via the `Match.objectLike()`.
Expand Down
152 changes: 151 additions & 1 deletion packages/@aws-cdk/assertions/test/template.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, CfnMapping, CfnOutput, CfnResource, NestedStack, Stack } from '@aws-cdk/core';
import { App, CfnMapping, CfnOutput, CfnParameter, CfnResource, NestedStack, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { Capture, Match, Template } from '../lib';

Expand Down Expand Up @@ -708,6 +708,156 @@ describe('Template', () => {
});
});

describe('findParameters', () => {
test('matching', () => {
const stack = new Stack();
new CfnParameter(stack, 'p1', {
type: 'String',
description: 'string parameter',
});
new CfnParameter(stack, 'p2', {
type: 'Number',
description: 'number parameter',
});

const inspect = Template.fromStack(stack);
const result = inspect.findParameters('*', { Type: 'String' });
expect(result).toEqual({
p1: {
Description: 'string parameter',
Type: 'String',
},
});
});

test('not matching', () => {
const stack = new Stack();
new CfnParameter(stack, 'p1', {
type: 'String',
description: 'string parameter',
});

const inspect = Template.fromStack(stack);
const result = inspect.findParameters('*', { Type: 'Number' });
expect(Object.keys(result).length).toEqual(0);
});

test('matching with specific parameter name', () => {
const stack = new Stack();
new CfnParameter(stack, 'p1', {
type: 'String',
description: 'string parameter',
});
new CfnParameter(stack, 'p2', {
type: 'Number',
description: 'number parameter',
});

const inspect = Template.fromStack(stack);
const result = inspect.findParameters('p1', { Type: 'String' });
expect(result).toEqual({
p1: {
Description: 'string parameter',
Type: 'String',
},
});
});

test('not matching specific parameter name', () => {
const stack = new Stack();
new CfnParameter(stack, 'p1', {
type: 'String',
description: 'string parameter',
});
new CfnParameter(stack, 'p2', {
type: 'Number',
description: 'number parameter',
});

const inspect = Template.fromStack(stack);
const result = inspect.findParameters('p3', { Type: 'String' });
expect(Object.keys(result).length).toEqual(0);
});
});

describe('hasParameter', () => {
test('matching', () => {
const stack = new Stack();
new CfnParameter(stack, 'p1', {
type: 'String',
description: 'string parameter',
});
new CfnParameter(stack, 'p2', {
type: 'Number',
description: 'number parameter',
});

const inspect = Template.fromStack(stack);
expect(() => inspect.findParameters('p3', { Type: 'String' })).not.toThrow();
});

test('not matching', (done) => {
const stack = new Stack();
new CfnParameter(stack, 'p1', {
type: 'String',
description: 'string parameter',
});
new CfnParameter(stack, 'p2', {
type: 'Number',
description: 'number parameter',
});

const inspect = Template.fromStack(stack);
expectToThrow(
() => inspect.hasParameter('*', { Type: 'CommaDelimitedList' }),
[
/2 parameters/,
/Expected CommaDelimitedList but received String/,
],
done,
);
done();
});

test('matching specific parameter name', () => {
const stack = new Stack();
new CfnParameter(stack, 'p1', {
type: 'String',
description: 'string parameter',
});
new CfnParameter(stack, 'p2', {
type: 'Number',
description: 'number parameter',
});

const inspect = Template.fromStack(stack);
expect(() => inspect.findParameters('p1', { Type: 'String' })).not.toThrow();
});

test('not matching specific parameter name', (done) => {
const stack = new Stack();
new CfnParameter(stack, 'p1', {
type: 'String',
description: 'string parameter',
});
new CfnParameter(stack, 'p2', {
type: 'Number',
description: 'number parameter',
});

const inspect = Template.fromStack(stack);
expectToThrow(
() => inspect.hasParameter('p2', { Type: 'CommaDelimitedList' }),
[
/1 parameter/,
/Expected CommaDelimitedList but received Number/,
],
done,
);
done();
});
});

describe('findMappings', () => {
test('matching', () => {
const stack = new Stack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,11 @@ export abstract class BaseLoadBalancer extends Resource {

/**
* The VPC this load balancer has been created in.
*
* This property is always defined (not `null` or `undefined`) for sub-classes of `BaseLoadBalancer`.
*/
public readonly vpc: ec2.IVpc;
public readonly vpc?: ec2.IVpc;

/**
* Attributes set on this load balancer
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11448,6 +11448,7 @@
"AWS::EC2::Instance": {
"attributes": {
"AvailabilityZone": "The Availability Zone where the specified instance is launched. For example: `us-east-1b` .\n\nYou can retrieve a list of all Availability Zones for a Region by using the [Fn::GetAZs](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getavailabilityzones.html) intrinsic function.",
"PrivateDnsName": "The private DNS name of the specified instance. For example: `ip-10-24-34-0.ec2.internal` .",
"PrivateIp": "The private IP address of the specified instance. For example: `10.24.34.0` .",
"PublicDnsName": "The public DNS name of the specified instance. For example: `ec2-107-20-50-45.compute-1.amazonaws.com` .",
"PublicIp": "The public IP address of the specified instance. For example: `192.0.2.0` .",
Expand Down

0 comments on commit 065df78

Please sign in to comment.