-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
cloudformation-template.ts
122 lines (107 loc) · 3.23 KB
/
cloudformation-template.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
import { IBucket } from '@aws-cdk/aws-s3';
import * as s3_assets from '@aws-cdk/aws-s3-assets';
import { Construct } from 'constructs';
import { hashValues } from './private/util';
import { ProductStack } from './product-stack';
/**
* Represents the Product Provisioning Artifact Template.
*/
export abstract class CloudFormationTemplate {
/**
* Template from URL
* @param url The url that points to the provisioning artifacts template
*/
public static fromUrl(url: string): CloudFormationTemplate {
return new CloudFormationUrlTemplate(url);
}
/**
* Loads the provisioning artifacts template from a local disk path.
*
* @param path A file containing the provisioning artifacts
*/
public static fromAsset(path: string, options?: s3_assets.AssetOptions): CloudFormationTemplate {
return new CloudFormationAssetTemplate(path, options);
}
/**
* Creates a product with the resources defined in the given product stack.
*/
public static fromProductStack(productStack: ProductStack): CloudFormationTemplate {
return new CloudFormationProductStackTemplate(productStack);
}
/**
* Called when the product is initialized to allow this object to bind
* to the stack, add resources and have fun.
*
* @param scope The binding scope. Don't be smart about trying to down-cast or
* assume it's initialized. You may just use it as a construct scope.
*/
public abstract bind(scope: Construct): CloudFormationTemplateConfig;
}
/**
* Result of binding `Template` into a `Product`.
*/
export interface CloudFormationTemplateConfig {
/**
* The http url of the template in S3.
*/
readonly httpUrl: string;
/**
* The S3 bucket containing product stack assets.
* @default - None - no assets are used in this product
*/
readonly assetBucket?: IBucket;
}
/**
* Template code from a Url.
*/
class CloudFormationUrlTemplate extends CloudFormationTemplate {
constructor(private readonly url: string) {
super();
}
public bind(_scope: Construct): CloudFormationTemplateConfig {
return {
httpUrl: this.url,
};
}
}
/**
* Template from a local file.
*/
class CloudFormationAssetTemplate extends CloudFormationTemplate {
private asset?: s3_assets.Asset;
/**
* @param path The path to the asset file.
*/
constructor(public readonly path: string, private readonly options: s3_assets.AssetOptions = { }) {
super();
}
public bind(scope: Construct): CloudFormationTemplateConfig {
// If the same AssetCode is used multiple times, retain only the first instantiation.
if (!this.asset) {
this.asset = new s3_assets.Asset(scope, `Template${hashValues(this.path)}`, {
path: this.path,
...this.options,
});
}
return {
httpUrl: this.asset.httpUrl,
};
}
}
/**
* Template from a CDK defined product stack.
*/
class CloudFormationProductStackTemplate extends CloudFormationTemplate {
/**
* @param productStack A service catalog product stack.
*/
constructor(public readonly productStack: ProductStack) {
super();
}
public bind(_scope: Construct): CloudFormationTemplateConfig {
return {
httpUrl: this.productStack._getTemplateUrl(),
assetBucket: this.productStack._getAssetBucket(),
};
}
}