-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathmesh.ts
208 lines (182 loc) · 5.01 KB
/
mesh.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
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnMesh } from './appmesh.generated';
import { VirtualGateway, VirtualGatewayBaseProps } from './virtual-gateway';
import { VirtualNode, VirtualNodeBaseProps } from './virtual-node';
import { VirtualRouter, VirtualRouterBaseProps } from './virtual-router';
import { VirtualService, VirtualServiceBaseProps } from './virtual-service';
/**
* A utility enum defined for the egressFilter type property, the default of DROP_ALL,
* allows traffic only to other resources inside the mesh, or API calls to amazon resources.
*
* @default DROP_ALL
*/
export enum MeshFilterType {
/**
* Allows all outbound traffic
*/
ALLOW_ALL = 'ALLOW_ALL',
/**
* Allows traffic only to other resources inside the mesh, or API calls to amazon resources
*/
DROP_ALL = 'DROP_ALL',
}
/**
* Interface wich all Mesh based classes MUST implement
*/
export interface IMesh extends cdk.IResource {
/**
* The name of the AppMesh mesh
*
* @attribute
*/
readonly meshName: string;
/**
* The Amazon Resource Name (ARN) of the AppMesh mesh
*
* @attribute
*/
readonly meshArn: string;
/**
* Adds a VirtualRouter to the Mesh with the given id and props
*/
addVirtualRouter(id: string, props?: VirtualRouterBaseProps): VirtualRouter;
/**
* Adds a VirtualService with the given id
*/
addVirtualService(id: string, props?: VirtualServiceBaseProps): VirtualService;
/**
* Adds a VirtualNode to the Mesh
*/
addVirtualNode(id: string, props?: VirtualNodeBaseProps): VirtualNode;
/**
* Adds a VirtualGateway to the Mesh
*/
addVirtualGateway(id: string, props?: VirtualGatewayBaseProps): VirtualGateway;
}
/**
* Represents a new or imported AppMesh mesh
*/
abstract class MeshBase extends cdk.Resource implements IMesh {
/**
* The name of the AppMesh mesh
*/
public abstract readonly meshName: string;
/**
* The Amazon Resource Name (ARN) of the AppMesh mesh
*/
public abstract readonly meshArn: string;
/**
* Adds a VirtualRouter to the Mesh with the given id and props
*/
public addVirtualRouter(id: string, props: VirtualRouterBaseProps = {}): VirtualRouter {
return new VirtualRouter(this, id, {
...props,
mesh: this,
});
}
/**
* Adds a VirtualService with the given id
*/
public addVirtualService(id: string, props: VirtualServiceBaseProps = {}): VirtualService {
return new VirtualService(this, id, {
...props,
mesh: this,
});
}
/**
* Adds a VirtualNode to the Mesh
*/
public addVirtualNode(id: string, props: VirtualNodeBaseProps = {}): VirtualNode {
return new VirtualNode(this, id, {
...props,
mesh: this,
});
}
/**
* Adds a VirtualGateway to the Mesh
*/
addVirtualGateway(id: string, props?: VirtualGatewayBaseProps): VirtualGateway {
return new VirtualGateway(this, id, {
...props,
mesh: this,
});
}
}
/**
* The set of properties used when creating a Mesh
*/
export interface MeshProps {
/**
* The name of the Mesh being defined
*
* @default - A name is autmoatically generated
*/
readonly meshName?: string;
/**
* Egress filter to be applied to the Mesh
*
* @default DROP_ALL
*/
readonly egressFilter?: MeshFilterType;
}
/**
* Define a new AppMesh mesh
*
* @see https://docs.aws.amazon.com/app-mesh/latest/userguide/meshes.html
*/
export class Mesh extends MeshBase {
/**
* Import an existing mesh by arn
*/
public static fromMeshArn(scope: Construct, id: string, meshArn: string): IMesh {
const parts = cdk.Stack.of(scope).parseArn(meshArn);
class Import extends MeshBase {
public meshName = parts.resourceName || '';
public meshArn = meshArn;
}
return new Import(scope, id);
}
/**
* Import an existing mesh by name
*/
public static fromMeshName(scope: Construct, id: string, meshName: string): IMesh {
const arn = cdk.Stack.of(scope).formatArn({
service: 'appmesh',
resource: 'mesh',
resourceName: meshName,
});
class Import extends MeshBase {
public meshName = meshName;
public meshArn = arn;
}
return new Import(scope, id);
}
/**
* The name of the AppMesh mesh
*/
public readonly meshName: string;
/**
* The Amazon Resource Name (ARN) of the AppMesh mesh
*/
public readonly meshArn: string;
constructor(scope: Construct, id: string, props: MeshProps = {}) {
super(scope, id, {
physicalName: props.meshName || cdk.Lazy.string({ produce: () => cdk.Names.uniqueId(this) }),
});
const mesh = new CfnMesh(this, 'Resource', {
meshName: this.physicalName,
spec: {
egressFilter: props.egressFilter ? {
type: props.egressFilter,
} : undefined,
},
});
this.meshName = this.getResourceNameAttribute(mesh.attrMeshName);
this.meshArn = this.getResourceArnAttribute(mesh.ref, {
service: 'appmesh',
resource: 'mesh',
resourceName: this.physicalName,
});
}
}