forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.ts
290 lines (256 loc) · 8.67 KB
/
product.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { Construct } from 'constructs';
import { CloudFormationTemplate } from './cloudformation-template';
import { MessageLanguage } from './common';
import { AssociationManager } from './private/association-manager';
import { InputValidator } from './private/validation';
import { CfnCloudFormationProduct } from './servicecatalog.generated';
import { TagOptions } from './tag-options';
import { IBucket } from '../../aws-s3';
import { ArnFormat, IResource, Resource, Stack } from '../../core';
import { addConstructMetadata } from '../../core/lib/metadata-resource';
/**
* The type of product
*/
export enum ProductType {
/**
* CloudFormation template
*/
CLOUD_FORMATION_TEMPLATE = 'CLOUD_FORMATION_TEMPLATE',
/**
* created by Amazon Web Services Marketplace
*/
MARKETPLACE = 'MARKETPLACE',
/**
* Terraform Open Source configuration file
*/
TERRAFORM_OPEN_SOURCE = 'TERRAFORM_OPEN_SOURCE',
/**
* Terraform Cloud configuration file
*/
TERRAFORM_CLOUD = 'TERRAFORM_CLOUD',
/**
* External configuration file
*/
EXTERNAL = 'EXTERNAL',
}
/**
* A Service Catalog product, currently only supports type CloudFormationProduct
*/
export interface IProduct extends IResource {
/**
* The ARN of the product.
* @attribute
*/
readonly productArn: string;
/**
* The id of the product
* @attribute
*/
readonly productId: string;
/**
* The asset buckets of a product created via product stack.
* @attribute
*/
readonly assetBuckets: IBucket[];
/**
* Associate Tag Options.
* A TagOption is a key-value pair managed in AWS Service Catalog.
* It is not an AWS tag, but serves as a template for creating an AWS tag based on the TagOption.
*/
associateTagOptions(tagOptions: TagOptions): void;
}
abstract class ProductBase extends Resource implements IProduct {
public abstract readonly productArn: string;
public abstract readonly productId: string;
public abstract readonly assetBuckets: IBucket[];
public associateTagOptions(tagOptions: TagOptions) {
AssociationManager.associateTagOptions(this, this.productId, tagOptions);
}
}
/**
* Properties of product version (also known as a provisioning artifact).
*/
export interface CloudFormationProductVersion {
/**
* The description of the product version
* @default - No description provided
*/
readonly description?: string;
/**
* Whether the specified product template will be validated by CloudFormation.
* If turned off, an invalid template configuration can be stored.
* @default true
*/
readonly validateTemplate?: boolean;
/**
* The S3 template that points to the provisioning version template
*/
readonly cloudFormationTemplate: CloudFormationTemplate;
/**
* The name of the product version.
* @default - No product version name provided
*/
readonly productVersionName?: string;
}
/**
* Properties for a Cloudformation Product
*/
export interface CloudFormationProductProps {
/**
* The owner of the product.
*/
readonly owner: string;
/**
* The name of the product.
*/
readonly productName: string;
/**
* The configuration of the product version.
*/
readonly productVersions: CloudFormationProductVersion[];
/**
* The language code.
* Controls language for logging and errors.
*
* @default - English
*/
readonly messageLanguage?: MessageLanguage;
/**
* The description of the product.
* @default - No description provided
*/
readonly description?: string;
/**
* The distributor of the product.
* @default - No distributor provided
*/
readonly distributor?: string;
/**
* The type of the product.
* @default - No type provided
*/
readonly productType?: ProductType;
/**
* Whether to give provisioning artifacts a new unique identifier when the product attributes or provisioning artifacts is updated
* @default false
*/
readonly replaceProductVersionIds?: boolean;
/**
* The support information about the product
* @default - No support description provided
*/
readonly supportDescription?: string;
/**
* The contact email for product support.
* @default - No support email provided
*/
readonly supportEmail?: string;
/**
* The contact URL for product support.
* @default - No support URL provided
*/
readonly supportUrl?: string;
/**
* TagOptions associated directly to a product.
*
* @default - No tagOptions provided
*/
readonly tagOptions?: TagOptions;
}
/**
* Abstract class for Service Catalog Product.
*/
export abstract class Product extends ProductBase {
/**
* Creates a Product construct that represents an external product.
* @param scope The parent creating construct (usually `this`).
* @param id The construct's name.
* @param productArn Product Arn
*/
public static fromProductArn(scope: Construct, id: string, productArn: string): IProduct {
const arn = Stack.of(scope).splitArn(productArn, ArnFormat.SLASH_RESOURCE_NAME);
const productId = arn.resourceName;
if (!productId) {
throw new Error('Missing required Portfolio ID from Portfolio ARN: ' + productArn);
}
return new class extends ProductBase {
public readonly productId = productId!;
public readonly productArn = productArn;
public readonly assetBuckets = [];
}(scope, id);
}
}
/**
* A Service Catalog Cloudformation Product.
*/
export class CloudFormationProduct extends Product {
public readonly productArn: string;
public readonly productId: string;
/**
* The asset bucket of a product created via product stack.
* @default - Empty - no assets are used in this product
*/
public readonly assetBuckets = new Array<IBucket>();
constructor(scope: Construct, id: string, props: CloudFormationProductProps) {
super(scope, id);
// Enhanced CDK Analytics Telemetry
addConstructMetadata(this, props);
this.validateProductProps(props);
const product = new CfnCloudFormationProduct(this, 'Resource', {
acceptLanguage: props.messageLanguage,
description: props.description,
distributor: props.distributor,
name: props.productName,
owner: props.owner,
provisioningArtifactParameters: this.renderProvisioningArtifacts(props),
productType: props.productType,
replaceProvisioningArtifacts: props.replaceProductVersionIds,
supportDescription: props.supportDescription,
supportEmail: props.supportEmail,
supportUrl: props.supportUrl,
});
this.productId = product.ref;
this.productArn = Stack.of(this).formatArn({
service: 'catalog',
resource: 'product',
resourceName: product.ref,
});
if (props.tagOptions !== undefined) {
this.associateTagOptions(props.tagOptions);
}
}
private renderProvisioningArtifacts(
props: CloudFormationProductProps): CfnCloudFormationProduct.ProvisioningArtifactPropertiesProperty[] {
return props.productVersions.map(productVersion => {
const template = productVersion.cloudFormationTemplate.bind(this);
if (template.assetBucket) {
this.assetBuckets.push(template.assetBucket);
}
InputValidator.validateUrl(this.node.path, 'provisioning template url', template.httpUrl);
return {
name: productVersion.productVersionName,
description: productVersion.description,
disableTemplateValidation: productVersion.validateTemplate === false ? true : false,
info: {
LoadTemplateFromURL: template.httpUrl,
},
};
});
}
private validateProductProps(props: CloudFormationProductProps) {
InputValidator.validateLength(this.node.path, 'product product name', 1, 100, props.productName);
InputValidator.validateLength(this.node.path, 'product owner', 1, 8191, props.owner);
InputValidator.validateLength(this.node.path, 'product description', 0, 8191, props.description);
InputValidator.validateLength(this.node.path, 'product distributor', 0, 8191, props.distributor);
InputValidator.validateEmail(this.node.path, 'support email', props.supportEmail);
InputValidator.validateUrl(this.node.path, 'support url', props.supportUrl);
InputValidator.validateLength(this.node.path, 'support description', 0, 8191, props.supportDescription);
if (props.productVersions.length == 0) {
throw new Error(`Invalid product versions for resource ${this.node.path}, must contain at least 1 product version`);
}
props.productVersions.forEach(productVersion => {
InputValidator.validateLength(this.node.path, 'provisioning artifact name', 0, 100, productVersion.productVersionName);
InputValidator.validateLength(this.node.path, 'provisioning artifact description', 0, 8191, productVersion.description);
});
}
}