-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
route.ts
157 lines (138 loc) · 4.23 KB
/
route.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
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnRoute } from './appmesh.generated';
import { IMesh } from './mesh';
import { renderMeshOwner } from './private/utils';
import { RouteSpec } from './route-spec';
import { IVirtualRouter, VirtualRouter } from './virtual-router';
/**
* Interface for which all Route based classes MUST implement
*/
export interface IRoute extends cdk.IResource {
/**
* The name of the route
*
* @attribute
*/
readonly routeName: string;
/**
* The Amazon Resource Name (ARN) for the route
*
* @attribute
*/
readonly routeArn: string;
/**
* The VirtualRouter the Route belongs to
*/
readonly virtualRouter: IVirtualRouter;
}
/**
* Base interface properties for all Routes
*/
export interface RouteBaseProps {
/**
* The name of the route
*
* @default - An automatically generated name
*/
readonly routeName?: string;
/**
* Protocol specific spec
*/
readonly routeSpec: RouteSpec;
}
/**
* Properties to define new Routes
*/
export interface RouteProps extends RouteBaseProps {
/**
* The service mesh to define the route in
*/
readonly mesh: IMesh;
/**
* The VirtualRouter the Route belongs to
*/
readonly virtualRouter: IVirtualRouter;
}
/**
* Route represents a new or existing route attached to a VirtualRouter and Mesh
*
* @see https://docs.aws.amazon.com/app-mesh/latest/userguide/routes.html
*/
export class Route extends cdk.Resource implements IRoute {
/**
* Import an existing Route given an ARN
*/
public static fromRouteArn(scope: Construct, id: string, routeArn: string): IRoute {
return new class extends cdk.Resource implements IRoute {
readonly routeArn = routeArn;
readonly virtualRouter = VirtualRouter.fromVirtualRouterArn(this, 'VirtualRouter', routeArn);
readonly routeName = cdk.Fn.select(4, cdk.Fn.split('/', cdk.Stack.of(scope).splitArn(routeArn, cdk.ArnFormat.SLASH_RESOURCE_NAME).resourceName!));
}(scope, id);
}
/**
* Import an existing Route given attributes
*/
public static fromRouteAttributes(scope: Construct, id: string, attrs: RouteAttributes): IRoute {
return new class extends cdk.Resource implements IRoute {
readonly routeName = attrs.routeName;
readonly virtualRouter = attrs.virtualRouter;
readonly routeArn = cdk.Stack.of(this).formatArn({
service: 'appmesh',
resource: `mesh/${attrs.virtualRouter.mesh.meshName}/virtualRouter/${attrs.virtualRouter.virtualRouterName}/route`,
resourceName: this.routeName,
});
}(scope, id);
}
/**
* The name of the Route
*/
public readonly routeName: string;
/**
* The Amazon Resource Name (ARN) for the route
*/
public readonly routeArn: string;
/**
* The VirtualRouter the Route belongs to
*/
public readonly virtualRouter: IVirtualRouter;
constructor(scope: Construct, id: string, props: RouteProps) {
super(scope, id, {
physicalName: props.routeName || cdk.Lazy.string({ produce: () => cdk.Names.uniqueId(this) }),
});
this.virtualRouter = props.virtualRouter;
const spec = props.routeSpec.bind(this);
const route = new CfnRoute(this, 'Resource', {
routeName: this.physicalName,
meshName: this.virtualRouter.mesh.meshName,
meshOwner: renderMeshOwner(this.env.account, this.virtualRouter.mesh.env.account),
virtualRouterName: this.virtualRouter.virtualRouterName,
spec: {
tcpRoute: spec.tcpRouteSpec,
httpRoute: spec.httpRouteSpec,
http2Route: spec.http2RouteSpec,
grpcRoute: spec.grpcRouteSpec,
priority: spec.priority,
},
});
this.routeName = this.getResourceNameAttribute(route.attrRouteName);
this.routeArn = this.getResourceArnAttribute(route.ref, {
service: 'appmesh',
resource: `mesh/${props.mesh.meshName}/virtualRouter/${props.virtualRouter.virtualRouterName}/route`,
resourceName: this.physicalName,
});
}
}
/**
* Interface with properties ncecessary to import a reusable Route
*/
export interface RouteAttributes {
/**
* The name of the Route
*/
readonly routeName: string;
/**
* The VirtualRouter the Route belongs to
*/
readonly virtualRouter: IVirtualRouter;
}