generated from szikszail/npm-package
/
cloudfront.ts
163 lines (153 loc) · 5.17 KB
/
cloudfront.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { Match } from "aws-cdk-lib/assertions";
import { CfnDistribution, CfnFunction, FunctionEventType, HttpVersion, OriginProtocolPolicy, OriginSslPolicy } from "aws-cdk-lib/aws-cloudfront";
import { AdvancedMatcher } from "./advanced-matcher";
import { AdvancedTemplate } from "./advanced-template";
import { RemovableResource, Resource } from "./resource";
import { S3Bucket } from "./s3";
import { WafV2WebACL } from "./wafv2";
/**
* A test construct for a CloudFront Function resource
* @see {@link https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.CfnFunction.html}
*/
export class CloudFrontFunction extends RemovableResource {
constructor(template: AdvancedTemplate, props?: any) {
super(CfnFunction.CFN_RESOURCE_TYPE_NAME, template, props);
}
/**
* Sets a matching function code for the function
* @param code The whole of a part of the function code
* @returns
*/
public withCode(code: string) {
this.withProperty('FunctionCode', Match.stringLikeRegexp(code));
return this;
}
}
/**
* A test construct for a CloudFront Distribution
* @see {@link https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.CfnDistribution.html}
*/
export class CloudFrontDistribution extends RemovableResource {
constructor(template: AdvancedTemplate, props?: any) {
super(CfnDistribution.CFN_RESOURCE_TYPE_NAME, template, props);
this.withProperty('DistributionConfig', {});
}
/**
* Sets matching aliases for the distribution
* @param aliases The whole or a partial alias array
* @returns
*/
public withAliases(aliases: string[]) {
this.propertiesMatcher.DistributionConfig.Aliases = Match.arrayWith(aliases);
return this;
}
/**
* Sets a matching CloudFront Function association
* @param fn The CloudFront Function test construct resource
* @param eventType The association event type
* @returns
*/
public withFunctionAssociation(fn: CloudFrontFunction, eventType?: FunctionEventType) {
this.propertiesMatcher.DistributionConfig.DefaultCacheBehavior = Match.objectLike({
FunctionAssociations: Match.arrayWith([
Match.objectLike({
EventType: eventType || FunctionEventType.VIEWER_REQUEST,
FunctionARN: AdvancedMatcher.arn(fn, "FunctionARN"),
})
])
});
return this;
}
/**
* Sets a matching certificate
* @param requestorResource The certificate requestor (or certificate) resource
* @returns
*/
public withCertificate(requestorResource: Resource) {
this.propertiesMatcher.DistributionConfig.ViewerCertificate = Match.objectLike({
AcmCertificateArn: requestorResource.arn,
});
return this;
}
/**
* Sets a matching WafV2 WebACL ID
* @param webACLId The WebACL ID
* @returns
*/
public withWebACL(webACLId: any) {
if (webACLId instanceof WafV2WebACL) {
this.propertiesMatcher.DistributionConfig.WebACLId = AdvancedMatcher.fnGetAtt(webACLId.id, "Id");
} else {
this.propertiesMatcher.DistributionConfig.WebACLId = webACLId;
}
return this;
}
/**
* Sets a matching origin for the passed S3 Bucket resource
* @param s3Bucket The S3 Bucket test construct resource
* @returns
*/
public withPublicS3BucketOrigin(s3Bucket: S3Bucket) {
this.propertiesMatcher.DistributionConfig.Origins = [
Match.objectLike({
DomainName: s3Bucket.websiteUrl,
})
];
return this;
}
/**
* Sets a matching HTTP version for the resource
* @param version The matching HTTP version
* @returns
*/
public withHttpVersion(version: HttpVersion) {
this.propertiesMatcher.DistributionConfig.HttpVersion = version;
return this;
}
/**
* Sets a matching Origin configuration
* @param originConfig The matching origin configuration
* @returns
*/
public withOrigin(originConfig: {
protocolPolicy?: OriginProtocolPolicy,
sslProtocol?: OriginSslPolicy,
domain?: any,
id?: string,
path?: string,
} = {}) {
const origin: any = {};
if (originConfig.protocolPolicy) {
origin.CustomOriginConfig = origin.CustomOriginConfig || {};
origin.CustomOriginConfig.OriginProtocolPolicy = originConfig.protocolPolicy;
}
if (originConfig.sslProtocol) {
origin.CustomOriginConfig = origin.CustomOriginConfig || {};
origin.CustomOriginConfig.OriginSSLProtocols = Match.arrayWith([
originConfig.sslProtocol
]);
}
if (originConfig.path) {
origin.CustomOriginConfig = origin.CustomOriginConfig || {};
origin.CustomOriginConfig.OriginPath = Match.stringLikeRegexp(originConfig.path);
}
if (originConfig.domain) {
if (typeof originConfig.domain === 'string') {
origin.DomainName = Match.stringLikeRegexp(originConfig.domain);
} else {
origin.DomainName = originConfig.domain;
}
}
if (originConfig.id) {
origin.Id = Match.stringLikeRegexp(originConfig.id);
}
if (Object.keys(origin).length) {
this.propertiesMatcher.DistributionConfig.Origins = Match.arrayWith([
Match.objectLike(origin)
]);
} else {
this.propertiesMatcher.DistributionConfig.Origins = Match.anyValue();
}
return this;
}
}