From c0aed38f37a3998e29edacec888e0386e4adca5a Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Mon, 2 Nov 2020 22:18:21 -0800 Subject: [PATCH 01/12] refactor(appmesh): changes VirtualRouter to have static listener methods --- packages/@aws-cdk/aws-appmesh/lib/index.ts | 1 + .../lib/virtual-router-listener.ts | 122 ++++++++++++++++++ .../aws-appmesh/lib/virtual-router.ts | 33 ++--- .../@aws-cdk/aws-appmesh/test/integ.mesh.ts | 9 +- .../@aws-cdk/aws-appmesh/test/test.mesh.ts | 27 ++-- .../aws-appmesh/test/test.virtual-router.ts | 57 ++++++++ 6 files changed, 202 insertions(+), 47 deletions(-) create mode 100644 packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts diff --git a/packages/@aws-cdk/aws-appmesh/lib/index.ts b/packages/@aws-cdk/aws-appmesh/lib/index.ts index 69cdfd8d2e3c6..d95c017c2071e 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/index.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/index.ts @@ -5,6 +5,7 @@ export * from './route'; export * from './shared-interfaces'; export * from './virtual-node'; export * from './virtual-router'; +export * from './virtual-router-listener'; export * from './virtual-service'; export * from './virtual-gateway'; export * from './virtual-gateway-listener'; diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts new file mode 100644 index 0000000000000..33b46482180b3 --- /dev/null +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts @@ -0,0 +1,122 @@ +import * as cdk from '@aws-cdk/core'; +import { CfnVirtualRouter } from './appmesh.generated'; +import { Protocol } from './shared-interfaces'; + +/** + * Represents the properties needed to define Listeners for a VirtualRouter + */ +export interface RouterListenerProps { + /** + * Port to listen for connections on + * + * @default - 8080 + */ + readonly port?: number +} + +/** + * Properties for a VirtualRouter listener + */ +export interface VirtualRouterListenerConfig { + /** + * Single listener config for a VirtualRouter + */ + readonly listener: CfnVirtualRouter.VirtualRouterListenerProperty; +} + +/** + * Represents the properties needed to define listeners for a VirtualRouter + */ +export abstract class VirtualRouterListener { + /** + * Returns an HTTP Listener for a VirtualRouter + */ + public static httpVirtualRouterListener(props: RouterListenerProps = {}): VirtualRouterListener { + return new HttpVirtualRouterListener(props); + } + + /** + * Returns an HTTP2 Listener for a VirtualRouter + */ + public static http2GatewayListener(props: RouterListenerProps = {}): VirtualRouterListener { + return new Http2VirtualRouterListener(props); + } + + /** + * Returns a GRPC Listener for a VirtualRouter + */ + public static grpcGatewayListener(props: RouterListenerProps = {}): VirtualRouterListener { + return new GrpcVirtualRouterListener(props); + } + + /** + * Returns a TCP Listener for a VirtualRouter + */ + public static tcpGatewayListener(props: RouterListenerProps = {}): VirtualRouterListener { + return new TcpVirtualRouterListener(props); + } + + /** + * Protocol the listener implements + */ + protected abstract protocol: Protocol; + + /** + * Port to listen for connections on + */ + protected abstract port: number; + + /** + * Called when the VirtualRouterListener type is initialized. Can be used to enforce + * mutual exclusivity + */ + public abstract bind(scope: cdk.Construct): VirtualRouterListenerConfig; +} + +class HttpVirtualRouterListener extends VirtualRouterListener { + /** + * Protocol the listener implements + */ + protected protocol: Protocol = Protocol.HTTP; + /** + * Port to listen for connections on + */ + protected port: number; + + constructor(props: RouterListenerProps = {}) { + super(); + this.port = props.port ? props.port : 8080; + } + + bind(_scope: cdk.Construct): VirtualRouterListenerConfig { + return { + listener: { + portMapping: { + port: this.port, + protocol: this.protocol, + }, + }, + }; + } +} + +class Http2VirtualRouterListener extends HttpVirtualRouterListener { + constructor(props: RouterListenerProps = {}) { + super(props); + this.protocol = Protocol.HTTP2; + } +} + +class GrpcVirtualRouterListener extends HttpVirtualRouterListener { + constructor(props: RouterListenerProps = {}) { + super(props); + this.protocol = Protocol.GRPC; + } +} + +class TcpVirtualRouterListener extends HttpVirtualRouterListener { + constructor(props: RouterListenerProps = {}) { + super(props); + this.protocol = Protocol.TCP; + } +} diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts index 05fcbb29d5f71..107e23d6c6335 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts @@ -3,7 +3,7 @@ import { Construct } from 'constructs'; import { CfnVirtualRouter } from './appmesh.generated'; import { IMesh, Mesh } from './mesh'; import { Route, RouteBaseProps } from './route'; -import { PortMapping, Protocol } from './shared-interfaces'; +import { VirtualRouterListener } from './virtual-router-listener'; /** * Interface which all VirtualRouter based classes MUST implement @@ -27,11 +27,6 @@ export interface IVirtualRouter extends cdk.IResource { * The service mesh that the virtual router resides in */ readonly mesh: IMesh; - - /** - * Add a single route to the router - */ - addRoute(id: string, props: RouteBaseProps): Route; } /** @@ -43,7 +38,7 @@ export interface VirtualRouterBaseProps { * * @default - A listener on HTTP port 8080 */ - readonly listener?: Listener; + readonly listeners?: VirtualRouterListener[]; /** * The name of the VirtualRouter @@ -53,16 +48,6 @@ export interface VirtualRouterBaseProps { readonly virtualRouterName?: string; } -/** - * A single listener for - */ -export interface Listener { - /** - * Listener port for the virtual router - */ - readonly portMapping: PortMapping; -} - abstract class VirtualRouterBase extends cdk.Resource implements IVirtualRouter { /** * The name of the VirtualRouter @@ -149,8 +134,12 @@ export class VirtualRouter extends VirtualRouterBase { }); this.mesh = props.mesh; - - this.addListener(props.listener || { portMapping: { port: 8080, protocol: Protocol.HTTP } }); + // this.addListener(listeners || VirtualRouterListener.httpVirtualRouterListener().); + if (props.listeners) { + props.listeners.forEach(listener => this.addListener(listener)); + } else { + this.addListener(VirtualRouterListener.httpVirtualRouterListener()); + } const router = new CfnVirtualRouter(this, 'Resource', { virtualRouterName: this.physicalName, @@ -171,10 +160,8 @@ export class VirtualRouter extends VirtualRouterBase { /** * Add port mappings to the router */ - private addListener(listener: Listener) { - this.listeners.push({ - portMapping: listener.portMapping, - }); + private addListener(listener: VirtualRouterListener) { + this.listeners.push(listener.bind(this).listener); } } diff --git a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts index 322ae1cc608d6..bdb903f37ab88 100644 --- a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts @@ -19,12 +19,9 @@ const namespace = new cloudmap.PrivateDnsNamespace(stack, 'test-namespace', { const mesh = new appmesh.Mesh(stack, 'mesh'); const router = mesh.addVirtualRouter('router', { - listener: { - portMapping: { - port: 8080, - protocol: appmesh.Protocol.HTTP, - }, - }, + listeners: [ + appmesh.VirtualRouterListener.httpVirtualRouterListener(), + ], }); const virtualService = mesh.addVirtualService('service', { diff --git a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts index f8e6ebcbb716d..06be0486d4827 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts @@ -67,12 +67,9 @@ export = { }); mesh.addVirtualRouter('router', { - listener: { - portMapping: { - port: 8080, - protocol: appmesh.Protocol.HTTP, - }, - }, + listeners: [ + appmesh.VirtualRouterListener.httpVirtualRouterListener(), + ], }); // THEN @@ -147,12 +144,9 @@ export = { }); const testRouter = mesh.addVirtualRouter('router', { - listener: { - portMapping: { - port: 8080, - protocol: appmesh.Protocol.HTTP, - }, - }, + listeners: [ + appmesh.VirtualRouterListener.httpVirtualRouterListener(), + ], }); // THEN @@ -178,12 +172,9 @@ export = { }); const testRouter = mesh.addVirtualRouter('test-router', { - listener: { - portMapping: { - port: 8080, - protocol: appmesh.Protocol.HTTP, - }, - }, + listeners: [ + appmesh.VirtualRouterListener.httpVirtualRouterListener(), + ], }); mesh.addVirtualService('service', { diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts index 47b59e0e39962..519282c9e6fde 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts @@ -5,6 +5,63 @@ import { Test } from 'nodeunit'; import * as appmesh from '../lib'; export = { + 'When creating a VirtualRouter': { + 'should have protocol variant listeners'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const mesh = new appmesh.Mesh(stack, 'mesh', { + meshName: 'test-mesh', + }); + // WHEN + mesh.addVirtualRouter('http-router-listener', { + listeners: [ + appmesh.VirtualRouterListener.httpVirtualRouterListener(), + ], + virtualRouterName: 'http-router-listener', + }); + + mesh.addVirtualRouter('http2-router-listener', { + listeners: [ + appmesh.VirtualRouterListener.http2GatewayListener(), + ], + virtualRouterName: 'http2-router-listener', + }); + + mesh.addVirtualRouter('grpc-router-listener', { + listeners: [ + appmesh.VirtualRouterListener.grpcGatewayListener(), + ], + virtualRouterName: 'grpc-router-listener', + }); + + mesh.addVirtualRouter('tcp-router-listener', { + listeners: [ + appmesh.VirtualRouterListener.tcpGatewayListener(), + ], + virtualRouterName: 'tcp-router-listener', + }); + + // THEN + const expectedPorts = [appmesh.Protocol.HTTP, appmesh.Protocol.HTTP2, appmesh.Protocol.GRPC, appmesh.Protocol.TCP]; + expectedPorts.forEach(protocol => { + expect(stack).to(haveResourceLike('AWS::AppMesh::VirtualRouter', { + VirtualRouterName: `${protocol}-router-listener`, + Spec: { + Listeners: [ + { + PortMapping: { + Port: 8080, + Protocol: protocol, + }, + }, + ], + }, + })); + }); + + test.done(); + }, + }, 'When adding route to existing VirtualRouter': { 'should create route resource'(test: Test) { // GIVEN From 53d5122c130a4f7eee37d37d483fb32253556b16 Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Tue, 3 Nov 2020 14:28:39 -0800 Subject: [PATCH 02/12] remove comment and check length on listener props --- packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts index 107e23d6c6335..2bcfec5b0ef66 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts @@ -134,8 +134,7 @@ export class VirtualRouter extends VirtualRouterBase { }); this.mesh = props.mesh; - // this.addListener(listeners || VirtualRouterListener.httpVirtualRouterListener().); - if (props.listeners) { + if (props.listeners && props.listeners.length) { props.listeners.forEach(listener => this.addListener(listener)); } else { this.addListener(VirtualRouterListener.httpVirtualRouterListener()); From ed0474eb6e50467130837efbe55197cb4e91768c Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Wed, 4 Nov 2020 21:40:30 -0800 Subject: [PATCH 03/12] renames virtual router listener static methods --- .../@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts | 8 ++++---- packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts | 2 +- packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts | 2 +- packages/@aws-cdk/aws-appmesh/test/test.mesh.ts | 6 +++--- packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts | 8 ++++---- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts index 33b46482180b3..8a30f6e2117b6 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts @@ -31,28 +31,28 @@ export abstract class VirtualRouterListener { /** * Returns an HTTP Listener for a VirtualRouter */ - public static httpVirtualRouterListener(props: RouterListenerProps = {}): VirtualRouterListener { + public static http(props: RouterListenerProps = {}): VirtualRouterListener { return new HttpVirtualRouterListener(props); } /** * Returns an HTTP2 Listener for a VirtualRouter */ - public static http2GatewayListener(props: RouterListenerProps = {}): VirtualRouterListener { + public static http2(props: RouterListenerProps = {}): VirtualRouterListener { return new Http2VirtualRouterListener(props); } /** * Returns a GRPC Listener for a VirtualRouter */ - public static grpcGatewayListener(props: RouterListenerProps = {}): VirtualRouterListener { + public static grpc(props: RouterListenerProps = {}): VirtualRouterListener { return new GrpcVirtualRouterListener(props); } /** * Returns a TCP Listener for a VirtualRouter */ - public static tcpGatewayListener(props: RouterListenerProps = {}): VirtualRouterListener { + public static tcp(props: RouterListenerProps = {}): VirtualRouterListener { return new TcpVirtualRouterListener(props); } diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts index b89f27fa60e2d..e5f263e497572 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts @@ -137,7 +137,7 @@ export class VirtualRouter extends VirtualRouterBase { if (props.listeners && props.listeners.length) { props.listeners.forEach(listener => this.addListener(listener)); } else { - this.addListener(VirtualRouterListener.httpVirtualRouterListener()); + this.addListener(VirtualRouterListener.http()); } const router = new CfnVirtualRouter(this, 'Resource', { diff --git a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts index bdb903f37ab88..3de6eb20c77d2 100644 --- a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts @@ -20,7 +20,7 @@ const namespace = new cloudmap.PrivateDnsNamespace(stack, 'test-namespace', { const mesh = new appmesh.Mesh(stack, 'mesh'); const router = mesh.addVirtualRouter('router', { listeners: [ - appmesh.VirtualRouterListener.httpVirtualRouterListener(), + appmesh.VirtualRouterListener.http(), ], }); diff --git a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts index 06be0486d4827..ad0c7b298706d 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts @@ -68,7 +68,7 @@ export = { mesh.addVirtualRouter('router', { listeners: [ - appmesh.VirtualRouterListener.httpVirtualRouterListener(), + appmesh.VirtualRouterListener.http(), ], }); @@ -145,7 +145,7 @@ export = { const testRouter = mesh.addVirtualRouter('router', { listeners: [ - appmesh.VirtualRouterListener.httpVirtualRouterListener(), + appmesh.VirtualRouterListener.http(), ], }); @@ -173,7 +173,7 @@ export = { const testRouter = mesh.addVirtualRouter('test-router', { listeners: [ - appmesh.VirtualRouterListener.httpVirtualRouterListener(), + appmesh.VirtualRouterListener.http(), ], }); diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts index 519282c9e6fde..21146635cf0cd 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts @@ -15,28 +15,28 @@ export = { // WHEN mesh.addVirtualRouter('http-router-listener', { listeners: [ - appmesh.VirtualRouterListener.httpVirtualRouterListener(), + appmesh.VirtualRouterListener.http(), ], virtualRouterName: 'http-router-listener', }); mesh.addVirtualRouter('http2-router-listener', { listeners: [ - appmesh.VirtualRouterListener.http2GatewayListener(), + appmesh.VirtualRouterListener.http2(), ], virtualRouterName: 'http2-router-listener', }); mesh.addVirtualRouter('grpc-router-listener', { listeners: [ - appmesh.VirtualRouterListener.grpcGatewayListener(), + appmesh.VirtualRouterListener.grpc(), ], virtualRouterName: 'grpc-router-listener', }); mesh.addVirtualRouter('tcp-router-listener', { listeners: [ - appmesh.VirtualRouterListener.tcpGatewayListener(), + appmesh.VirtualRouterListener.tcp(), ], virtualRouterName: 'tcp-router-listener', }); From 6ac1fe927096abe85da6dceee2828e5778558706 Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Wed, 4 Nov 2020 21:59:53 -0800 Subject: [PATCH 04/12] refactors the static methods to accept port instead of props --- .../lib/virtual-router-listener.ts | 67 ++++++------------- 1 file changed, 19 insertions(+), 48 deletions(-) diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts index 8a30f6e2117b6..672cef61be7b8 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts @@ -2,18 +2,6 @@ import * as cdk from '@aws-cdk/core'; import { CfnVirtualRouter } from './appmesh.generated'; import { Protocol } from './shared-interfaces'; -/** - * Represents the properties needed to define Listeners for a VirtualRouter - */ -export interface RouterListenerProps { - /** - * Port to listen for connections on - * - * @default - 8080 - */ - readonly port?: number -} - /** * Properties for a VirtualRouter listener */ @@ -31,40 +19,42 @@ export abstract class VirtualRouterListener { /** * Returns an HTTP Listener for a VirtualRouter */ - public static http(props: RouterListenerProps = {}): VirtualRouterListener { - return new HttpVirtualRouterListener(props); + public static http(port?: number): VirtualRouterListener { + return new VirtualRouterListenerImpl(Protocol.HTTP, port); } /** * Returns an HTTP2 Listener for a VirtualRouter */ - public static http2(props: RouterListenerProps = {}): VirtualRouterListener { - return new Http2VirtualRouterListener(props); + public static http2(port?: number): VirtualRouterListener { + return new VirtualRouterListenerImpl(Protocol.HTTP2, port); } /** * Returns a GRPC Listener for a VirtualRouter */ - public static grpc(props: RouterListenerProps = {}): VirtualRouterListener { - return new GrpcVirtualRouterListener(props); + public static grpc(port?: number): VirtualRouterListener { + return new VirtualRouterListenerImpl(Protocol.GRPC, port); } /** * Returns a TCP Listener for a VirtualRouter */ - public static tcp(props: RouterListenerProps = {}): VirtualRouterListener { - return new TcpVirtualRouterListener(props); + public static tcp(port?: number): VirtualRouterListener { + return new VirtualRouterListenerImpl(Protocol.TCP, port); } /** * Protocol the listener implements */ - protected abstract protocol: Protocol; + public abstract readonly protocol: Protocol; /** * Port to listen for connections on + * + * @default - defaults to port 8080 */ - protected abstract port: number; + public abstract readonly port: number; /** * Called when the VirtualRouterListener type is initialized. Can be used to enforce @@ -73,19 +63,21 @@ export abstract class VirtualRouterListener { public abstract bind(scope: cdk.Construct): VirtualRouterListenerConfig; } -class HttpVirtualRouterListener extends VirtualRouterListener { +class VirtualRouterListenerImpl extends VirtualRouterListener { /** * Protocol the listener implements */ - protected protocol: Protocol = Protocol.HTTP; + public readonly protocol: Protocol; + /** * Port to listen for connections on */ - protected port: number; + public readonly port: number; - constructor(props: RouterListenerProps = {}) { + constructor(protocol: Protocol, port?: number) { super(); - this.port = props.port ? props.port : 8080; + this.protocol = protocol; + this.port = port ? port : 8080; } bind(_scope: cdk.Construct): VirtualRouterListenerConfig { @@ -99,24 +91,3 @@ class HttpVirtualRouterListener extends VirtualRouterListener { }; } } - -class Http2VirtualRouterListener extends HttpVirtualRouterListener { - constructor(props: RouterListenerProps = {}) { - super(props); - this.protocol = Protocol.HTTP2; - } -} - -class GrpcVirtualRouterListener extends HttpVirtualRouterListener { - constructor(props: RouterListenerProps = {}) { - super(props); - this.protocol = Protocol.GRPC; - } -} - -class TcpVirtualRouterListener extends HttpVirtualRouterListener { - constructor(props: RouterListenerProps = {}) { - super(props); - this.protocol = Protocol.TCP; - } -} From 526035fb6f707a965d3a6ce13b35047d6c31f54b Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Wed, 4 Nov 2020 22:06:45 -0800 Subject: [PATCH 05/12] adds addRoute method back to IVirtualRouter --- packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts index e5f263e497572..cadc727783432 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts @@ -27,6 +27,11 @@ export interface IVirtualRouter extends cdk.IResource { * The service mesh that the virtual router resides in */ readonly mesh: IMesh; + + /** + * Add a single route to the router + */ + addRoute(id: string, props: RouteBaseProps): Route; } /** From c50357c417e0a45e83738c4c3173f366c65668ec Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Wed, 4 Nov 2020 22:12:01 -0800 Subject: [PATCH 06/12] add a test for default configuration --- .../aws-appmesh/test/test.virtual-router.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts index 21146635cf0cd..1307b6473c02c 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts @@ -6,6 +6,30 @@ import * as appmesh from '../lib'; export = { 'When creating a VirtualRouter': { + 'should have appropriate defaults'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const mesh = new appmesh.Mesh(stack, 'mesh', { + meshName: 'test-mesh', + }); + // WHEN + mesh.addVirtualRouter('http-router-listener'); + + expect(stack).to(haveResourceLike('AWS::AppMesh::VirtualRouter', { + VirtualRouterName: 'meshhttprouterlistenerF57BCB2F', + Spec: { + Listeners: [ + { + PortMapping: { + Port: 8080, + Protocol: appmesh.Protocol.HTTP, + }, + }, + ], + }, + })); + test.done(); + }, 'should have protocol variant listeners'(test: Test) { // GIVEN const stack = new cdk.Stack(); From 060a86d4acbf4253511aca332193e7ae2a0b704f Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Thu, 5 Nov 2020 13:14:24 -0800 Subject: [PATCH 07/12] addresses pr feedback --- packages/@aws-cdk/aws-appmesh/README.md | 30 ++++++++----------- .../lib/virtual-router-listener.ts | 8 +++++ .../@aws-cdk/aws-appmesh/test/test.mesh.ts | 6 +--- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/packages/@aws-cdk/aws-appmesh/README.md b/packages/@aws-cdk/aws-appmesh/README.md index e38a8ea49a9e9..efd082f1b0251 100644 --- a/packages/@aws-cdk/aws-appmesh/README.md +++ b/packages/@aws-cdk/aws-appmesh/README.md @@ -53,22 +53,21 @@ const mesh = new Mesh(stack, 'AppMesh', { ## Adding VirtualRouters -The `Mesh` needs `VirtualRouters` as logical units to route to `VirtualNodes`. +The _Mesh_ needs _VirtualRouters_ as logical units to route requests to _VirtualNodes_. -Virtual routers handle traffic for one or more virtual services within your mesh. After you create a virtual router, you can create and associate routes for your virtual router that direct incoming requests to different virtual nodes. +Virtual routers handle traffic for one or more virtual services within your mesh. +After you create a virtual router, you can create and associate routes to your virtual router that direct incoming requests to different virtual nodes. ```typescript const router = mesh.addVirtualRouter('router', { - listener: { - portMapping: { - port: 8081, - protocol: Protocol.HTTP, - } - } + listeners: [ appmesh.VirtualRouterListener.http(8080) ], }); ``` -The router can also be created using the constructor and passing in the mesh instead of calling the addVirtualRouter() method for the mesh. +The router can also be created using the constructor and passing in the mesh instead of calling the `addVirtualRouter()` method for the mesh. +The same pattern applies to all constructs within the appmesh library, for any mesh.addXZY method, a new constuctor can also be used. +This is particularly useful for cross stack resources are required. +Where creating the `mesh` as part of an infrastructure stack and creating the `resources` such as `nodes` is more useful to keep in the application stack. ```typescript const mesh = new Mesh(stack, 'AppMesh', { @@ -78,18 +77,15 @@ const mesh = new Mesh(stack, 'AppMesh', { const router = new VirtualRouter(stack, 'router', { mesh, // notice that mesh is a required property when creating a router with a new statement - listener: { - portMapping: { - port: 8081, - protocol: Protocol.HTTP, - } + listeners: [ appmesh.VirtualRouterListener.http(8081) ] } }); ``` -The listener protocol can be either `HTTP` or `TCP`. - -The same pattern applies to all constructs within the appmesh library, for any mesh.addXZY method, a new constuctor can also be used. This is particularly useful for cross stack resources are required. Where creating the `mesh` as part of an infrastructure stack and creating the `resources` such as `nodes` is more useful to keep in the application stack. +The _VirtualRouterListener_ class provides an easy interface for defining new protocol specific listeners. +The `http()`, `http2()`, `grpc()` and `tcp()` methods are available for use. +They accept a single port parameter, that is used to define what port to match requests on. +The port parameter can be omitted, and it will default to port 8080. ## Adding VirtualService diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts index 672cef61be7b8..2bbfc223958ae 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts @@ -18,6 +18,8 @@ export interface VirtualRouterListenerConfig { export abstract class VirtualRouterListener { /** * Returns an HTTP Listener for a VirtualRouter + * + * @param port the optional port of the listener, 8080 by default */ public static http(port?: number): VirtualRouterListener { return new VirtualRouterListenerImpl(Protocol.HTTP, port); @@ -25,6 +27,8 @@ export abstract class VirtualRouterListener { /** * Returns an HTTP2 Listener for a VirtualRouter + * + * @param port the optional port of the listener, 8080 by default */ public static http2(port?: number): VirtualRouterListener { return new VirtualRouterListenerImpl(Protocol.HTTP2, port); @@ -32,6 +36,8 @@ export abstract class VirtualRouterListener { /** * Returns a GRPC Listener for a VirtualRouter + * + * @param port the optional port of the listener, 8080 by default */ public static grpc(port?: number): VirtualRouterListener { return new VirtualRouterListenerImpl(Protocol.GRPC, port); @@ -39,6 +45,8 @@ export abstract class VirtualRouterListener { /** * Returns a TCP Listener for a VirtualRouter + * + * @param port the optional port of the listener, 8080 by default */ public static tcp(port?: number): VirtualRouterListener { return new VirtualRouterListenerImpl(Protocol.TCP, port); diff --git a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts index ad0c7b298706d..690dce4a08ddc 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts @@ -66,11 +66,7 @@ export = { meshName: 'test-mesh', }); - mesh.addVirtualRouter('router', { - listeners: [ - appmesh.VirtualRouterListener.http(), - ], - }); + mesh.addVirtualRouter('router'); // THEN expect(stack).to( From c170649aa4e2be1dc9b09566eef1189cde8aaf17 Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Thu, 5 Nov 2020 13:24:15 -0800 Subject: [PATCH 08/12] fixes whitespace issue in tests --- packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts index 1307b6473c02c..ef2a23a7541e3 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts @@ -86,6 +86,7 @@ export = { test.done(); }, }, + 'When adding route to existing VirtualRouter': { 'should create route resource'(test: Test) { // GIVEN From cc00cb4486ee7e277f98233dd79470ea3fa4d194 Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Fri, 6 Nov 2020 15:23:19 -0800 Subject: [PATCH 09/12] fixes ecs service extension to use new listener class --- .../lib/extensions/appmesh.ts | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts index 0ca41490969d8..8e519aed5ef7a 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts @@ -272,16 +272,30 @@ export class AppMeshExtension extends ServiceExtension { }, }); + var virtualRouterBaseListener: appmesh.VirtualRouterListener; + switch (this.protocol) { + case appmesh.Protocol.HTTP: + virtualRouterBaseListener = appmesh.VirtualRouterListener.http(containerextension.trafficPort); + break; + case appmesh.Protocol.HTTP2: + virtualRouterBaseListener = appmesh.VirtualRouterListener.http2(containerextension.trafficPort); + break; + case appmesh.Protocol.GRPC: + virtualRouterBaseListener = appmesh.VirtualRouterListener.grpc(containerextension.trafficPort); + break; + case appmesh.Protocol.TCP: + virtualRouterBaseListener= appmesh.VirtualRouterListener.tcp(containerextension.trafficPort); + break; + default: + throw new Error(`Protocol ${this.protocol} not supported by App Mesh Extension`); + } // Create a virtual router for this service. This allows for retries // and other similar behaviors. this.virtualRouter = new appmesh.VirtualRouter(this.scope, `${this.parentService.id}-virtual-router`, { mesh: this.mesh, - listener: { - portMapping: { - port: containerextension.trafficPort, - protocol: this.protocol, - }, - }, + listeners: [ + virtualRouterBaseListener, + ], virtualRouterName: `${this.parentService.id}`, }); From 4aede9519ac18371aa9b62a1f09082975512c596 Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Fri, 6 Nov 2020 15:44:31 -0800 Subject: [PATCH 10/12] rename variable --- .../ecs-service-extensions/lib/extensions/appmesh.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts index 8e519aed5ef7a..ed1694e6a38eb 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts @@ -272,19 +272,19 @@ export class AppMeshExtension extends ServiceExtension { }, }); - var virtualRouterBaseListener: appmesh.VirtualRouterListener; + var virtualRouterListener: appmesh.VirtualRouterListener; switch (this.protocol) { case appmesh.Protocol.HTTP: - virtualRouterBaseListener = appmesh.VirtualRouterListener.http(containerextension.trafficPort); + virtualRouterListener = appmesh.VirtualRouterListener.http(containerextension.trafficPort); break; case appmesh.Protocol.HTTP2: - virtualRouterBaseListener = appmesh.VirtualRouterListener.http2(containerextension.trafficPort); + virtualRouterListener = appmesh.VirtualRouterListener.http2(containerextension.trafficPort); break; case appmesh.Protocol.GRPC: - virtualRouterBaseListener = appmesh.VirtualRouterListener.grpc(containerextension.trafficPort); + virtualRouterListener = appmesh.VirtualRouterListener.grpc(containerextension.trafficPort); break; case appmesh.Protocol.TCP: - virtualRouterBaseListener= appmesh.VirtualRouterListener.tcp(containerextension.trafficPort); + virtualRouterListener= appmesh.VirtualRouterListener.tcp(containerextension.trafficPort); break; default: throw new Error(`Protocol ${this.protocol} not supported by App Mesh Extension`); @@ -294,7 +294,7 @@ export class AppMeshExtension extends ServiceExtension { this.virtualRouter = new appmesh.VirtualRouter(this.scope, `${this.parentService.id}-virtual-router`, { mesh: this.mesh, listeners: [ - virtualRouterBaseListener, + virtualRouterListener, ], virtualRouterName: `${this.parentService.id}`, }); From ddbdc9d3a1b8dcbee000d5c09b6d27c1d3a9e00a Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Fri, 6 Nov 2020 16:02:54 -0800 Subject: [PATCH 11/12] move switch to a private method --- .../lib/extensions/appmesh.ts | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts index ed1694e6a38eb..583ca06435c09 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts @@ -272,29 +272,12 @@ export class AppMeshExtension extends ServiceExtension { }, }); - var virtualRouterListener: appmesh.VirtualRouterListener; - switch (this.protocol) { - case appmesh.Protocol.HTTP: - virtualRouterListener = appmesh.VirtualRouterListener.http(containerextension.trafficPort); - break; - case appmesh.Protocol.HTTP2: - virtualRouterListener = appmesh.VirtualRouterListener.http2(containerextension.trafficPort); - break; - case appmesh.Protocol.GRPC: - virtualRouterListener = appmesh.VirtualRouterListener.grpc(containerextension.trafficPort); - break; - case appmesh.Protocol.TCP: - virtualRouterListener= appmesh.VirtualRouterListener.tcp(containerextension.trafficPort); - break; - default: - throw new Error(`Protocol ${this.protocol} not supported by App Mesh Extension`); - } // Create a virtual router for this service. This allows for retries // and other similar behaviors. this.virtualRouter = new appmesh.VirtualRouter(this.scope, `${this.parentService.id}-virtual-router`, { mesh: this.mesh, listeners: [ - virtualRouterListener, + this.virtualRouterListener(containerextension.trafficPort), ], virtualRouterName: `${this.parentService.id}`, }); @@ -345,4 +328,13 @@ export class AppMeshExtension extends ServiceExtension { // nodes from the other service. this.virtualNode.addBackends(otherAppMesh.virtualService); } + + private virtualRouterListener(port: number): appmesh.VirtualRouterListener { + switch (this.protocol) { + case appmesh.Protocol.HTTP: return appmesh.VirtualRouterListener.http(port); + case appmesh.Protocol.HTTP2: return appmesh.VirtualRouterListener.http2(port); + case appmesh.Protocol.GRPC: return appmesh.VirtualRouterListener.grpc(port); + case appmesh.Protocol.TCP: return appmesh.VirtualRouterListener.tcp(port); + } + } } From 41a525e1df3eeae2a0181489e678e7b760bc3b11 Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Fri, 6 Nov 2020 16:11:19 -0800 Subject: [PATCH 12/12] Remove unnecessary public fields from VirtualRouterListener --- .../lib/virtual-router-listener.ts | 25 +++---------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts index 2bbfc223958ae..7a5e867d0b7c9 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-router-listener.ts @@ -52,18 +52,6 @@ export abstract class VirtualRouterListener { return new VirtualRouterListenerImpl(Protocol.TCP, port); } - /** - * Protocol the listener implements - */ - public abstract readonly protocol: Protocol; - - /** - * Port to listen for connections on - * - * @default - defaults to port 8080 - */ - public abstract readonly port: number; - /** * Called when the VirtualRouterListener type is initialized. Can be used to enforce * mutual exclusivity @@ -72,20 +60,13 @@ export abstract class VirtualRouterListener { } class VirtualRouterListenerImpl extends VirtualRouterListener { - /** - * Protocol the listener implements - */ - public readonly protocol: Protocol; - - /** - * Port to listen for connections on - */ - public readonly port: number; + private readonly protocol: Protocol; + private readonly port: number; constructor(protocol: Protocol, port?: number) { super(); this.protocol = protocol; - this.port = port ? port : 8080; + this.port = port ?? 8080; } bind(_scope: cdk.Construct): VirtualRouterListenerConfig {