forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.ts
443 lines (352 loc) · 11.4 KB
/
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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { ArnFormat, ContextProvider, Resource, IResource, Stack, Tag } from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct, IConstruct } from 'constructs';
import { Analysis, IAnalysis } from './analysis';
import { DataSet, IDataSet } from './data-set';
import { CfnTemplate } from './quicksight.generated';
/**
* A QuickSight Template
*/
export interface ITemplate extends IResource {
// Common QuickSight Resource properties
/**
* An ID for the Template that you want to create. This ID is unique per AWS Region for each AWS account.
*
* @attribute
*/
readonly resourceId: string;
/**
* The display name for the Template.
*
* @attribute
*/
readonly templateName?: string;
/**
* A list of Tags on this Template.
*
* @attribute
*/
readonly tags?: Tag[];
/**
* A list of resource permissions on the Template.
*
* @attribute
*/
readonly permissions?: CfnTemplate.ResourcePermissionProperty[];
/**
* Dataset placeholder.
*
* @attribute
*/
readonly placeholders: IDataSet[];
/**
* The arn of this Template.
*
* @attribute
*/
readonly templateArn: string;
// Template Specific properties
/**
* The entity that you are using as a source when you create the template.
*/
readonly sourceEntity: ITemplate | IAnalysis;
/**
* A description of the current template version being created.
*/
readonly versionDescription?: string;
}
abstract class TemplateBase extends Resource implements ITemplate {
// Common QuickSight Resource Properties
public abstract readonly resourceId: string;
public abstract readonly templateName?: string;
public abstract readonly tags?: Tag[];
public abstract readonly permissions?: CfnTemplate.ResourcePermissionProperty[];
public abstract readonly templateArn: string;
public abstract readonly placeholders: IDataSet[];
// Template specific properties
public abstract readonly sourceEntity: ITemplate | IAnalysis;
public abstract readonly versionDescription?: string;
}
/**
* Properties for a Template.
*/
export interface TemplateProps {
// Common QuickSight Resource properties
/**
* An ID for the template that you want to create. This ID is unique per AWS Region for each AWS account.
*
* @default - A new UUID
*/
readonly resourceId: string;
/**
* The display name for the template.
*
* @default - An automatically generated name.
*/
readonly templateName: string;
/**
* A list of Tags on this template.
*
* @default - No tags.
*/
readonly tags?: Tag[];
/**
* A list of resource permissions on the template.
*
* @default - Empty permissions.
*/
readonly permissions?: CfnTemplate.ResourcePermissionProperty[];
// Template Specific properties
/**
* The Template that you are using as a source when you create the template.
* Either this or sourceAnalysis must be used.
*
* @default - undefined
*/
readonly sourceTemplate?: ITemplate;
/**
* The Analysis that you are using as a source when you create the template.
* Either this or sourceTemplate must be used.
*
* @default - undefined
*/
readonly sourceAnalysis?: IAnalysis;
/**
* A description of the current template version being created.
*
* @default - Empty string.
*/
readonly versionDescription?: string;
}
/**
* Define a QuickSight Template.
*/
export class Template extends Resource {
/**
* Imports a Template based on its Id.
*/
public static fromId(scope: Construct, id: string, templateId: string): ITemplate {
// placeholders
class Import extends TemplateBase {
private _template: cxapi.QuickSightContextResponse.Template | undefined;
private get template(): cxapi.QuickSightContextResponse.Template {
if (!this._template) {
let contextProps: cxschema.QuickSightTemplateContextQuery = {
account: Stack.of(scope).account,
region: Stack.of(scope).region,
templateId: templateId,
};
this._template = ContextProvider.getValue(scope, {
provider: cxschema.ContextProvider.QUICKSIGHT_ANALYSIS_PROVIDER,
props: contextProps,
dummyValue: undefined,
}).value;
if (!this._template) {
throw Error(`No Template found in account ${contextProps.account} and region ${contextProps.region} with id ${contextProps.templateId}`);
}
}
return this._template;
}
private _templatePermissions: cxapi.QuickSightContextResponse.ResourcePermissionList | undefined;
private get templatePermissions(): cxapi.QuickSightContextResponse.ResourcePermissionList {
if (!this._templatePermissions) {
let contextProps: cxschema.QuickSightTemplateContextQuery = {
account: Stack.of(scope).account,
region: Stack.of(scope).region,
templateId: templateId,
};
this._templatePermissions = ContextProvider.getValue(scope, {
provider: cxschema.ContextProvider.QUICKSIGHT_ANALYSIS_PERMISSIONS_PROVIDER,
props: contextProps,
dummyValue: [],
}).value;
if (!this._templatePermissions) {
this._templatePermissions = [];
}
}
return this._templatePermissions;
}
private _templateTags: cxapi.QuickSightContextResponse.TagList | undefined;
private get templateTags(): cxapi.QuickSightContextResponse.TagList {
if (!this._templateTags) {
let contextProps: cxschema.QuickSightTagsContextQuery = {
account: Stack.of(scope).account,
region: Stack.of(scope).region,
resourceArn: this.templateArn,
};
this._templateTags = ContextProvider.getValue(scope, {
provider: cxschema.ContextProvider.QUICKSIGHT_TAGS_PROVIDER,
props: contextProps,
dummyValue: [],
}).value;
if (!this._templateTags) {
this._templateTags = [];
}
}
return this._templateTags;
}
public get templateName() {
return this.template.name;
}
public get permissions() {
return this.templatePermissions;
}
public get tags() {
let tags: Tag[] = [];
if (this.templateTags) {
this.templateTags.forEach(function(value: any) {
if (value.Key && value.Value) {
tags.push(new Tag(value.Key, value.Value));
}
});
}
return tags;
}
public get templateArn() {
return this.template.arn ?? '';
}
public get resourceId(): string {
return this.template.templateId ?? '';
}
public get versionDescription() {
return this.template.version?.description;
};
private _sourceEntity: ITemplate | IAnalysis | undefined;
public get sourceEntity() {
if (!this._sourceEntity) {
let splitArn = Stack.of(scope).splitArn(this.template.version?.sourceEntityArn ?? '', ArnFormat.SLASH_RESOURCE_NAME);
let sourceEntityId: string = this.template.version?.sourceEntityArn?.split('/')[1] ?? '';
if (splitArn.resource == 'analysis') {
this._sourceEntity = Analysis.fromId(scope, sourceEntityId, sourceEntityId);
} else {
this._sourceEntity = Template.fromId(scope, sourceEntityId, sourceEntityId);
}
}
return this._sourceEntity;
}
private _placeholders: IDataSet[] | undefined;
public get placeholders(): IDataSet[] {
if (!this._placeholders) {
let placeholders: IDataSet[] = [];
this.template.version?.dataSetConfigurations?.forEach(function(value: any) {
if (value.Placeholder) {
let placeholderId: string = value.Placeholder.split('/')[1];
placeholders.push(DataSet.fromId(scope, placeholderId, placeholderId));
}
});
this._placeholders = placeholders;
}
return this._placeholders;
}
}
return new Import(scope, id, {
account: Stack.of(scope).account,
region: Stack.of(scope).region,
});
}
// Common QuickSight Resource properties
/**
* An ID for the template that you want to create. This ID is unique per AWS Region for each AWS account.
*/
public readonly resourceId: string;
/**
* The display name for the template.
*
* @attribute
*/
public readonly templateName?: string;
/**
* A list of Tags on this template.
*
* @default - No tags.
*/
public readonly tags?: Tag[];
/**
* A list of resource permissions on the template.
*
* @default - Empty permissions.
*/
public readonly permissions?: CfnTemplate.ResourcePermissionProperty[];
/**
* Dataset placeholder.
*
* @attribute
*/
public readonly placeholders: IDataSet[];
/**
* The arn of this template.
*
* @attribute
*/
public get templateArn(): string {
return Stack.of(this.scope).formatArn({
service: 'quicksight',
resource: 'dataset',
arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
resourceName: this.resourceId,
});
};
// Template specific properties
/**
* The entity that you are using as a source when you create the template.
*/
public readonly sourceEntity: ITemplate | IAnalysis;
/**
* A description of the current template version being created.
*
* @default - Empty string.
*/
public readonly versionDescription?: string;
private scope: IConstruct;
constructor(scope: Construct, id: string, props: TemplateProps) {
super(scope, id);
this.resourceId = props.resourceId;
this.scope = scope;
this.templateName = props.templateName;
this.tags = props.tags;
this.permissions = props.permissions;
this.versionDescription = props.versionDescription;
let sourceEntity: CfnTemplate.TemplateSourceEntityProperty;
if (props.sourceAnalysis && !props.sourceTemplate) {
this.sourceEntity = props.sourceAnalysis;
let dataSetReferences: CfnTemplate.DataSetReferenceProperty[] = [];
let _this = this;
this.placeholders = [];
this.sourceEntity.dataSets?.forEach(function(value) {
dataSetReferences.push({
dataSetArn: value.dataSetArn,
dataSetPlaceholder: value.dataSetArn,
});
_this.placeholders?.push(value);
});
sourceEntity = {
sourceAnalysis: {
arn: this.sourceEntity.analysisArn,
dataSetReferences: dataSetReferences,
},
};
} else if (props.sourceTemplate && !props.sourceAnalysis) {
this.sourceEntity = props.sourceTemplate;
sourceEntity = {
sourceTemplate: {
arn: this.sourceEntity.templateArn,
},
};
this.placeholders = this.sourceEntity.placeholders;
} else {
throw new Error('There must be either a sourceTemplate or sourceAnalysis');
}
//const resource: CfnTemplate =
new CfnTemplate(this, 'Resource', {
awsAccountId: Stack.of(scope).account,
sourceEntity: sourceEntity,
templateId: props.resourceId,
// the properties below are optional
name: this.templateName,
permissions: this.permissions,
tags: this.tags,
versionDescription: this.versionDescription,
});
}
}