-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathapplication-associator.ts
74 lines (66 loc) · 2.42 KB
/
application-associator.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
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { IApplication } from './application';
import { CheckedStageStackAssociator } from './aspects/stack-associator';
import { TargetApplication } from './target-application';
/**
* Properties for Service Catalog AppRegistry Application Associator
*/
export interface ApplicationAssociatorProps {
/**
* Application associator properties.
*
* @default - Empty array.
*/
readonly applications: TargetApplication[];
}
/**
* An AppRegistry construct to automatically create an application with the given name and description.
*
* The application name must be unique at the account level and it's immutable.
* This construct will automatically associate all stacks in the given scope, however
* in case of a `Pipeline` stack, stage underneath the pipeline will not automatically be associated and
* needs to be associated separately.
*
* If cross account stack is detected, then this construct will automatically share the application to consumer accounts.
* Cross account feature will only work for non environment agnostic stacks.
*/
export class ApplicationAssociator extends Construct {
/**
* Created or imported application.
*/
private readonly application: IApplication;
private readonly associatedStages: Set<cdk.Stage> = new Set();
constructor(scope: cdk.App, id: string, props: ApplicationAssociatorProps) {
super(scope, id);
if (props.applications.length != 1) {
throw new Error('Please pass exactly 1 instance of TargetApplication.createApplicationStack() or TargetApplication.existingApplicationFromArn() into the "applications" property');
}
const targetApplication = props.applications[0];
this.application = targetApplication.bind(this).application;
cdk.Aspects.of(scope).add(new CheckedStageStackAssociator(this));
}
/**
* Associate this application with the given stage.
*
*/
public associateStage(stage: cdk.Stage): cdk.Stage {
this.associatedStages.add(stage);
cdk.Aspects.of(stage).add(new CheckedStageStackAssociator(this));
return stage;
}
/**
* Validates if a stage is already associated to the application.
*
*/
public isStageAssociated(stage: cdk.Stage): boolean {
return this.associatedStages.has(stage);
}
/**
* Get the AppRegistry application.
*
*/
public appRegistryApplication(): IApplication {
return this.application;
}
}