From b83a8eed702957c84f34dfeaf368559d601addc8 Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Mon, 2 Nov 2020 20:30:10 -0800 Subject: [PATCH 1/6] refactor(appmesh): remove from*Name() methods and replace with from*Attributes() --- .../@aws-cdk/aws-appmesh/lib/gateway-route.ts | 49 ++++++----- packages/@aws-cdk/aws-appmesh/lib/route.ts | 73 ++++++++-------- .../aws-appmesh/lib/virtual-gateway.ts | 56 ++++++------- .../@aws-cdk/aws-appmesh/lib/virtual-node.ts | 72 +++++++++------- .../aws-appmesh/lib/virtual-router.ts | 84 ++++++------------- .../aws-appmesh/lib/virtual-service.ts | 72 ++++++++-------- .../aws-appmesh/test/test.gateway-route.ts | 28 +++++-- .../@aws-cdk/aws-appmesh/test/test.route.ts | 68 +++++---------- .../aws-appmesh/test/test.virtual-gateway.ts | 21 +++-- .../aws-appmesh/test/test.virtual-node.ts | 58 ++++--------- .../aws-appmesh/test/test.virtual-router.ts | 20 ++++- .../aws-appmesh/test/test.virtual-service.ts | 39 ++++----- 12 files changed, 306 insertions(+), 334 deletions(-) diff --git a/packages/@aws-cdk/aws-appmesh/lib/gateway-route.ts b/packages/@aws-cdk/aws-appmesh/lib/gateway-route.ts index cc00c0f632ac3..aeded032426c6 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/gateway-route.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/gateway-route.ts @@ -65,7 +65,14 @@ export class GatewayRoute extends cdk.Resource implements IGatewayRoute { * Import an existing GatewayRoute given an ARN */ public static fromGatewayRouteArn(scope: Construct, id: string, gatewayRouteArn: string): IGatewayRoute { - return new ImportedGatewayRoute(scope, id, { gatewayRouteArn }); + return new ImportedGatewayRoute(scope, id, undefined, gatewayRouteArn); + } + + /** + * Import an existing GatewayRoute given attributes + */ + public static fromGatewayRouteAttributes(scope: Construct, id: string, attrs: GatewayRouteAttributes): IGatewayRoute { + return new ImportedGatewayRoute(scope, id, attrs); } /** @@ -114,26 +121,16 @@ export class GatewayRoute extends cdk.Resource implements IGatewayRoute { /** * Interface with properties necessary to import a reusable GatewayRoute */ -interface GatewayRouteAttributes { +export interface GatewayRouteAttributes { /** * The name of the GatewayRoute */ - readonly gatewayRouteName?: string; - - /** - * The Amazon Resource Name (ARN) for the GatewayRoute - */ - readonly gatewayRouteArn?: string; + readonly gatewayRouteName: string; /** - * The name of the mesh this GatewayRoute is associated with + * The name of the VirtualGateway this GatewayRoute is associated with */ - readonly meshName?: string; - - /** - * The name of the Virtual Gateway this GatewayRoute is associated with - */ - readonly virtualGateway?: IVirtualGateway; + readonly virtualGateway: IVirtualGateway; } /** @@ -151,18 +148,26 @@ class ImportedGatewayRoute extends cdk.Resource implements IGatewayRoute { public gatewayRouteArn: string; /** - * The VirtualGateway the GatewayRoute belongs to + * The VirtualGateway the GatewayRoute is associated with */ public virtualGateway: IVirtualGateway; - constructor(scope: Construct, id: string, props: GatewayRouteAttributes) { + constructor(scope: Construct, id: string, attrs?: GatewayRouteAttributes, gatewayRouteArn?: string) { super(scope, id); - if (props.gatewayRouteArn) { - this.gatewayRouteArn = props.gatewayRouteArn; - this.gatewayRouteName = cdk.Fn.select(4, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(props.gatewayRouteArn).resourceName!)); - this.virtualGateway = VirtualGateway.fromVirtualGatewayArn(this, 'virtualGateway', props.gatewayRouteArn); + if (gatewayRouteArn) { + this.gatewayRouteArn = gatewayRouteArn; + this.gatewayRouteName = cdk.Fn.select(4, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(gatewayRouteArn).resourceName!)); + this.virtualGateway = VirtualGateway.fromVirtualGatewayArn(this, 'virtualGateway', gatewayRouteArn); + } else if (attrs) { + this.gatewayRouteName = attrs.gatewayRouteName; + this.virtualGateway = attrs.virtualGateway; + this.gatewayRouteArn = cdk.Stack.of(this).formatArn({ + service: 'appmesh', + resource: `mesh/${attrs.virtualGateway.mesh.meshName}/virtualGateway/${attrs.virtualGateway.virtualGatewayName}/gatewayRoute`, + resourceName: this.gatewayRouteName, + }); } else { - throw new Error('Need gatewayRouteArn'); + throw new Error('Need gatewayRouteArn or gatewayRouteName and mesh'); } } } diff --git a/packages/@aws-cdk/aws-appmesh/lib/route.ts b/packages/@aws-cdk/aws-appmesh/lib/route.ts index 0dfeac897375a..d933372776a9b 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/route.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/route.ts @@ -1,9 +1,9 @@ import * as cdk from '@aws-cdk/core'; import { Construct } from 'constructs'; import { CfnRoute } from './appmesh.generated'; -import { IMesh } from './mesh'; +import { IMesh, Mesh } from './mesh'; import { IVirtualNode } from './virtual-node'; -import { IVirtualRouter } from './virtual-router'; +import { IVirtualRouter, VirtualRouter } from './virtual-router'; /** * Interface for which all Route based classes MUST implement @@ -22,6 +22,11 @@ export interface IRoute extends cdk.IResource { * @attribute */ readonly routeArn: string; + + /** + * The VirtualRouter the Route belongs to + */ + readonly virtualRouter: IVirtualRouter; } /** @@ -99,7 +104,7 @@ export interface RouteProps extends RouteBaseProps { readonly mesh: IMesh; /** - * The virtual router in which to define the route + * The VirtualRouter the Route belongs to */ readonly virtualRouter: IVirtualRouter; } @@ -111,21 +116,21 @@ export interface RouteProps extends RouteBaseProps { */ export class Route extends cdk.Resource implements IRoute { /** - * Import an existing route given an ARN + * Import an existing Route given an ARN */ public static fromRouteArn(scope: Construct, id: string, routeArn: string): IRoute { - return new ImportedRoute(scope, id, { routeArn }); + return new ImportedRoute(scope, id, undefined, routeArn); } /** - * Import an existing route given its name + * Import an existing Route given attributes */ - public static fromRouteName(scope: Construct, id: string, meshName: string, virtualRouterName: string, routeName: string): IRoute { - return new ImportedRoute(scope, id, { meshName, virtualRouterName, routeName }); + public static fromRouteAttributes(scope: Construct, id: string, attrs: RouteAttributes): IRoute { + return new ImportedRoute(scope, id, attrs); } /** - * The name of the route + * The name of the Route */ public readonly routeName: string; @@ -135,7 +140,7 @@ export class Route extends cdk.Resource implements IRoute { public readonly routeArn: string; /** - * The virtual router this route is a part of + * The VirtualRouter the Route belongs to */ public readonly virtualRouter: IVirtualRouter; @@ -215,26 +220,16 @@ export class Route extends cdk.Resource implements IRoute { /** * Interface with properties ncecessary to import a reusable Route */ -interface RouteAttributes { - /** - * The name of the route - */ - readonly routeName?: string; - - /** - * The Amazon Resource Name (ARN) for the route - */ - readonly routeArn?: string; - +export interface RouteAttributes { /** - * The name of the mesh this route is associated with + * The name of the Route */ - readonly meshName?: string; + readonly routeName: string; /** - * The name of the virtual router this route is associated with + * The VirtualRouter the Route belongs to */ - readonly virtualRouterName?: string; + readonly virtualRouter: IVirtualRouter; } /** @@ -242,26 +237,36 @@ interface RouteAttributes { */ class ImportedRoute extends cdk.Resource implements IRoute { /** - * The name of the route + * The name of the Route */ public readonly routeName: string; /** - * The Amazon Resource Name (ARN) for the route + * The Amazon Resource Name (ARN) for the Route */ public readonly routeArn: string; - constructor(scope: Construct, id: string, props: RouteAttributes) { + /** + * The VirtualRouter the Route belongs to + */ + public readonly virtualRouter: IVirtualRouter; + + constructor(scope: Construct, id: string, attrs?: RouteAttributes, routeArn?: string) { super(scope, id); - if (props.routeArn) { - this.routeArn = props.routeArn; - this.routeName = cdk.Fn.select(4, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(props.routeArn).resourceName!)); - } else if (props.routeName && props.meshName && props.virtualRouterName) { - this.routeName = props.routeName; + if (routeArn) { + this.routeArn = routeArn; + const meshName = cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(routeArn).resourceName!)); + const mesh = Mesh.fromMeshName(this, 'Mesh', meshName); + const virtualRouterName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(routeArn).resourceName!)); + this.virtualRouter = VirtualRouter.fromVirtualRouterAttributes(this, 'VirtualROuter', { mesh, virtualRouterName }); + this.routeName = cdk.Fn.select(4, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(routeArn).resourceName!)); + } else if (attrs) { + this.routeName = attrs.routeName; + this.virtualRouter = attrs.virtualRouter; this.routeArn = cdk.Stack.of(this).formatArn({ service: 'appmesh', - resource: `mesh/${props.meshName}/virtualRouter/${props.virtualRouterName}/route`, + resource: `mesh/${attrs.virtualRouter.mesh.meshName}/virtualRouter/${attrs.virtualRouter.virtualRouterName}/route`, resourceName: this.routeName, }); } else { diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts index 51950f82dbb13..04c147e9ad890 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts @@ -26,14 +26,9 @@ export interface IVirtualGateway extends cdk.IResource { readonly virtualGatewayArn: string; /** - * The mesh which the VirtualGateway belongs to + * The Mesh which the VirtualGateway belongs to */ readonly mesh: IMesh; - - /** - * Utility method to add a new GatewayRoute to the VirtualGateway - */ - addGatewayRoute(id: string, route: GatewayRouteBaseProps): GatewayRoute; } /** @@ -67,7 +62,7 @@ export interface VirtualGatewayBaseProps { */ export interface VirtualGatewayProps extends VirtualGatewayBaseProps { /** - * The mesh which the VirtualGateway belongs to + * The Mesh which the VirtualGateway belongs to */ readonly mesh: IMesh; } @@ -84,7 +79,7 @@ abstract class VirtualGatewayBase extends cdk.Resource implements IVirtualGatewa public abstract readonly virtualGatewayArn: string; /** - * The name of the mesh which the VirtualGateway belongs to + * The Mesh which the VirtualGateway belongs to */ public abstract readonly mesh: IMesh; @@ -112,7 +107,14 @@ export class VirtualGateway extends VirtualGatewayBase { * Import an existing VirtualGateway given an ARN */ public static fromVirtualGatewayArn(scope: Construct, id: string, virtualGatewayArn: string): IVirtualGateway { - return new ImportedVirtualGateway(scope, id, { virtualGatewayArn }); + return new ImportedVirtualGateway(scope, id, undefined, virtualGatewayArn); + } + + /** + * Import an existing VirtualGateway given its attributes + */ + public static fromVirtualGatewayAttributes(scope: Construct, id: string, attrs: VirtualGatewayAttributes): IVirtualGateway { + return new ImportedVirtualGateway(scope, id, attrs); } /** @@ -171,26 +173,16 @@ export class VirtualGateway extends VirtualGatewayBase { /** * Unterface with properties necessary to import a reusable VirtualGateway */ -interface VirtualGatewayAttributes { +export interface VirtualGatewayAttributes { /** * The name of the VirtualGateway */ - readonly virtualGatewayName?: string; - - /** - * The Amazon Resource Name (ARN) belonging to the VirtualGateway - */ - readonly virtualGatewayArn?: string; + readonly virtualGatewayName: string; /** * The Mesh that the VirtualGateway belongs to */ - readonly mesh?: IMesh; - - /** - * The name of the mesh that the VirtualGateway belongs to - */ - readonly meshName?: string; + readonly mesh: IMesh; } /** @@ -212,15 +204,23 @@ class ImportedVirtualGateway extends VirtualGatewayBase { */ public readonly mesh: IMesh; - constructor(scope: Construct, id: string, props: VirtualGatewayAttributes) { + constructor(scope: Construct, id: string, attrs?: VirtualGatewayAttributes, virtualGatewayArn?: string) { super(scope, id); - if (props.virtualGatewayArn) { - const meshName = cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(props.virtualGatewayArn).resourceName!)); + if (virtualGatewayArn) { + const meshName = cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualGatewayArn).resourceName!)); this.mesh = Mesh.fromMeshName(this, 'Mesh', meshName); - this.virtualGatewayArn = props.virtualGatewayArn; - this.virtualGatewayName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(props.virtualGatewayArn).resourceName!)); + this.virtualGatewayArn = virtualGatewayArn; + this.virtualGatewayName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualGatewayArn).resourceName!)); + } else if (attrs) { + this.mesh = attrs.mesh; + this.virtualGatewayName = attrs.virtualGatewayName; + this.virtualGatewayArn = cdk.Stack.of(this).formatArn({ + service: 'appmesh', + resource: `mesh/${attrs.mesh.meshName}/virtualGateway`, + resourceName: this.virtualGatewayName, + }); } else { - throw new Error('Need virtualGatewayArn'); + throw new Error('Need either virtualGatewayArn or virtualGatewayName and mesh'); } } } diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts index bfad388fa91ee..478b31495b8e6 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts @@ -2,7 +2,7 @@ import * as cloudmap from '@aws-cdk/aws-servicediscovery'; import * as cdk from '@aws-cdk/core'; import { Construct } from 'constructs'; import { CfnVirtualNode } from './appmesh.generated'; -import { IMesh } from './mesh'; +import { IMesh, Mesh } from './mesh'; import { validateHealthChecks } from './private/utils'; import { AccessLog, HealthCheck, PortMapping, Protocol, VirtualNodeListener } from './shared-interfaces'; import { IVirtualService } from './virtual-service'; @@ -19,7 +19,7 @@ export interface IVirtualNode extends cdk.IResource { readonly virtualNodeName: string; /** - * The Amazon Resource Name belonging to the VirtualNdoe + * The Amazon Resource Name belonging to the VirtualNode * * Set this value as the APPMESH_VIRTUAL_NODE_NAME environment variable for * your task group's Envoy proxy container in your task definition or pod @@ -29,6 +29,11 @@ export interface IVirtualNode extends cdk.IResource { */ readonly virtualNodeArn: string; + /** + * The Mesh which the VirtualNode belongs to + */ + readonly mesh: IMesh; + /** * Utility method to add backends for existing or new VirtualNodes */ @@ -105,7 +110,7 @@ export interface VirtualNodeBaseProps { */ export interface VirtualNodeProps extends VirtualNodeBaseProps { /** - * The name of the AppMesh which the virtual node belongs to + * The Mesh which the VirtualNode belongs to */ readonly mesh: IMesh; } @@ -117,15 +122,20 @@ abstract class VirtualNodeBase extends cdk.Resource implements IVirtualNode { public abstract readonly virtualNodeName: string; /** - * The Amazon Resource Name belonging to the VirtualNdoe + * The Amazon Resource Name belonging to the VirtualNode */ public abstract readonly virtualNodeArn: string; + /** + * The Mesh which the VirtualNode belongs to + */ + public abstract readonly mesh: IMesh; + protected readonly backends = new Array(); protected readonly listeners = new Array(); /** - * Add a Virtual Services that this node is expected to send outbound traffic to + * Add a VirtualServices that this node is expected to send outbound traffic to */ public addBackends(...props: IVirtualService[]) { for (const s of props) { @@ -197,17 +207,14 @@ export class VirtualNode extends VirtualNodeBase { * Import an existing VirtualNode given an ARN */ public static fromVirtualNodeArn(scope: Construct, id: string, virtualNodeArn: string): IVirtualNode { - return new ImportedVirtualNode(scope, id, { virtualNodeArn }); + return new ImportedVirtualNode(scope, id, undefined, virtualNodeArn); } /** * Import an existing VirtualNode given its name */ - public static fromVirtualNodeName(scope: Construct, id: string, meshName: string, virtualNodeName: string): IVirtualNode { - return new ImportedVirtualNode(scope, id, { - meshName, - virtualNodeName, - }); + public static fromVirtualNodeAttributes(scope: Construct, id: string, attrs: VirtualNodeAttributes): IVirtualNode { + return new ImportedVirtualNode(scope, id, attrs); } /** @@ -221,7 +228,7 @@ export class VirtualNode extends VirtualNodeBase { public readonly virtualNodeArn: string; /** - * The service mesh that the virtual node resides in + * The Mesh which the VirtualNode belongs to */ public readonly mesh: IMesh; @@ -271,23 +278,18 @@ function renderAttributes(attrs?: {[key: string]: string}) { } /** - * Interface with properties ncecessary to import a reusable VirtualNode + * Interface with properties necessary to import a reusable VirtualNode */ -interface VirtualNodeAttributes { +export interface VirtualNodeAttributes { /** * The name of the VirtualNode */ - readonly virtualNodeName?: string; - - /** - * The Amazon Resource Name belonging to the VirtualNdoe - */ - readonly virtualNodeArn?: string; + readonly virtualNodeName: string; /** - * The service mesh that the virtual node resides in + * The Mesh that the VirtualNode belongs to */ - readonly meshName?: string; + readonly mesh: IMesh; } /** @@ -300,25 +302,33 @@ class ImportedVirtualNode extends VirtualNodeBase { public readonly virtualNodeName: string; /** - * The Amazon Resource Name belonging to the VirtualNdoe + * The Amazon Resource Name belonging to the VirtualNode */ public readonly virtualNodeArn: string; - constructor(scope: Construct, id: string, props: VirtualNodeAttributes) { + /** + * The Mesh which the VirtualNode belongs to + */ + public readonly mesh: IMesh; + + constructor(scope: Construct, id: string, attrs?: VirtualNodeAttributes, virtualNodeArn?: string) { super(scope, id); - if (props.virtualNodeArn) { - this.virtualNodeArn = props.virtualNodeArn; - this.virtualNodeName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(props.virtualNodeArn).resourceName!)); - } else if (props.virtualNodeName && props.meshName) { - this.virtualNodeName = props.virtualNodeName; + if (virtualNodeArn) { + this.virtualNodeArn = virtualNodeArn; + const meshName = cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualNodeArn).resourceName!)); + this.mesh = Mesh.fromMeshName(this, 'Mesh', meshName); + this.virtualNodeName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualNodeArn).resourceName!)); + } else if (attrs) { + this.mesh = attrs.mesh; + this.virtualNodeName = attrs.virtualNodeName; this.virtualNodeArn = cdk.Stack.of(this).formatArn({ service: 'appmesh', - resource: `mesh/${props.meshName}/virtualNode`, + resource: `mesh/${attrs.mesh.meshName}/virtualNode`, resourceName: this.virtualNodeName, }); } else { - throw new Error('Need either arn or both names'); + throw new Error('Need either virtualNodeArn or virtualNodeName and mesh'); } } } diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts index 05fcbb29d5f71..537ba2b1a5813 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts @@ -24,7 +24,7 @@ export interface IVirtualRouter extends cdk.IResource { readonly virtualRouterArn: string; /** - * The service mesh that the virtual router resides in + * The Mesh which the VirtualRouter belongs to */ readonly mesh: IMesh; @@ -39,7 +39,7 @@ export interface IVirtualRouter extends cdk.IResource { */ export interface VirtualRouterBaseProps { /** - * Listener specification for the virtual router + * Listener specification for the VirtualRouter * * @default - A listener on HTTP port 8080 */ @@ -58,7 +58,7 @@ export interface VirtualRouterBaseProps { */ export interface Listener { /** - * Listener port for the virtual router + * Listener port for the VirtualRouter */ readonly portMapping: PortMapping; } @@ -75,7 +75,7 @@ abstract class VirtualRouterBase extends cdk.Resource implements IVirtualRouter public abstract readonly virtualRouterArn: string; /** - * The AppMesh mesh the VirtualRouter belongs to + * The Mesh which the VirtualRouter belongs to */ public abstract readonly mesh: IMesh; @@ -95,11 +95,11 @@ abstract class VirtualRouterBase extends cdk.Resource implements IVirtualRouter } /** - * The properties used when creating a new VritualRouter + * The properties used when creating a new VirtualRouter */ export interface VirtualRouterProps extends VirtualRouterBaseProps { /** - * The AppMesh mesh the VirtualRouter belongs to + * The Mesh which the VirtualRouter belongs to */ readonly mesh: IMesh; } @@ -109,18 +109,11 @@ export class VirtualRouter extends VirtualRouterBase { * Import an existing VirtualRouter given an ARN */ public static fromVirtualRouterArn(scope: Construct, id: string, virtualRouterArn: string): IVirtualRouter { - return new ImportedVirtualRouter(scope, id, { virtualRouterArn }); + return new ImportedVirtualRouter(scope, id, undefined, virtualRouterArn); } /** - * Import an existing VirtualRouter given names - */ - public static fromVirtualRouterName(scope: Construct, id: string, meshName: string, virtualRouterName: string): IVirtualRouter { - return new ImportedVirtualRouter(scope, id, { meshName, virtualRouterName }); - } - - /** - * Import an existing virtual router given attributes + * Import an existing VirtualRouter given attributes */ public static fromVirtualRouterAttributes(scope: Construct, id: string, attrs: VirtualRouterAttributes): IVirtualRouter { return new ImportedVirtualRouter(scope, id, attrs); @@ -137,7 +130,7 @@ export class VirtualRouter extends VirtualRouterBase { public readonly virtualRouterArn: string; /** - * The AppMesh mesh the VirtualRouter belongs to + * The Mesh which the VirtualRouter belongs to */ public readonly mesh: IMesh; @@ -185,22 +178,12 @@ export interface VirtualRouterAttributes { /** * The name of the VirtualRouter */ - readonly virtualRouterName?: string; - - /** - * The Amazon Resource Name (ARN) for the VirtualRouter - */ - readonly virtualRouterArn?: string; - - /** - * The AppMesh mesh the VirtualRouter belongs to - */ - readonly mesh?: IMesh; + readonly virtualRouterName: string; /** - * The name of the AppMesh mesh the VirtualRouter belongs to + * The Mesh which the VirtualRouter belongs to */ - readonly meshName?: string; + readonly mesh: IMesh; } /** @@ -217,43 +200,28 @@ class ImportedVirtualRouter extends VirtualRouterBase { */ public readonly virtualRouterArn: string; - private _mesh?: IMesh; + /** + * The Mesh which the VirtualRouter belongs to + */ + public readonly mesh: IMesh; - constructor(scope: Construct, id: string, props: VirtualRouterAttributes) { + constructor(scope: Construct, id: string, attrs?: VirtualRouterAttributes, virtualRouterArn?: string) { super(scope, id); - - if (props.mesh) { - this._mesh = props.mesh; - } - if (props.meshName) { - if (props.mesh) { - throw new Error('Supply either \'mesh\' or \'meshName\', but not both'); - } - this._mesh = Mesh.fromMeshName(this, 'Mesh', props.meshName); - } - - if (props.virtualRouterArn) { - this.virtualRouterArn = props.virtualRouterArn; - this.virtualRouterName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(props.virtualRouterArn).resourceName!)); - } else if (props.virtualRouterName && props.meshName) { - this.virtualRouterName = props.virtualRouterName; + if (virtualRouterArn) { + this.virtualRouterArn = virtualRouterArn; + this.virtualRouterName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualRouterArn).resourceName!)); + const meshName = cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualRouterArn).resourceName!)); + this.mesh = Mesh.fromMeshName(this, 'Mesh', meshName); + } else if (attrs) { + this.virtualRouterName = attrs.virtualRouterName; + this.mesh = attrs.mesh; this.virtualRouterArn = cdk.Stack.of(this).formatArn({ service: 'appmesh', - resource: `mesh/${props.meshName}/virtualRouter`, + resource: `mesh/${attrs.mesh.meshName}/virtualRouter`, resourceName: this.virtualRouterName, }); } else { throw new Error('Need either arn or both names'); } } - - /** - * The AppMesh mesh the VirtualRouter belongs to - */ - public get mesh(): IMesh { - if (!this._mesh) { - throw new Error('Please supply either \'mesh\' or \'meshName\' when calling \'fromVirtualRouterAttributes\''); - } - return this._mesh; - } } diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts index 00b48f2d39d80..c36cbca062bee 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts @@ -1,7 +1,7 @@ import * as cdk from '@aws-cdk/core'; import { Construct } from 'constructs'; import { CfnVirtualService } from './appmesh.generated'; -import { IMesh } from './mesh'; +import { IMesh, Mesh } from './mesh'; import { IVirtualNode } from './virtual-node'; import { IVirtualRouter } from './virtual-router'; @@ -22,6 +22,11 @@ export interface IVirtualService extends cdk.IResource { * @attribute */ readonly virtualServiceArn: string; + + /** + * The Mesh which the VirtualService belongs to + */ + readonly mesh: IMesh; } /** @@ -59,7 +64,7 @@ export interface VirtualServiceBaseProps { */ export interface VirtualServiceProps extends VirtualServiceBaseProps { /** - * The AppMesh mesh name for which the VirtualService belongs to + * The Mesh which the VirtualService belongs to */ readonly mesh: IMesh; } @@ -76,19 +81,14 @@ export class VirtualService extends cdk.Resource implements IVirtualService { * Import an existing VirtualService given an ARN */ public static fromVirtualServiceArn(scope: Construct, id: string, virtualServiceArn: string): IVirtualService { - return new ImportedVirtualService(scope, id, { - virtualServiceArn, - }); + return new ImportedVirtualService(scope, id, undefined, virtualServiceArn); } /** - * Import an existing VirtualService given mesh and service names + * Import an existing VirtualService given its attributes */ - public static fromVirtualServiceName(scope: Construct, id: string, meshName: string, virtualServiceName: string): IVirtualService { - return new ImportedVirtualService(scope, id, { - meshName, - virtualServiceName, - }); + public static fromVirtualServiceAttributes(scope: Construct, id: string, attrs: VirtualServiceAttributes): IVirtualService { + return new ImportedVirtualService(scope, id, attrs); } /** @@ -101,8 +101,12 @@ export class VirtualService extends cdk.Resource implements IVirtualService { */ public readonly virtualServiceArn: string; + /** + * The Mesh which the VirtualService belongs to + */ + public readonly mesh: IMesh; + private readonly virtualServiceProvider?: CfnVirtualService.VirtualServiceProviderProperty; - private readonly mesh: IMesh; constructor(scope: Construct, id: string, props: VirtualServiceProps) { super(scope, id, { @@ -159,29 +163,16 @@ export class VirtualService extends cdk.Resource implements IVirtualService { /** * Interface with properties ncecessary to import a reusable VirtualService */ -interface VirtualServiceAttributes { - /** - * The Amazon Resource Name (ARN) for the virtual service - * - * @default - Required if virtualServiceName and virtualMeshName are not supplied. - */ - readonly virtualServiceArn?: string; - +export interface VirtualServiceAttributes { /** * The name of the VirtualService, it is recommended this follows the fully-qualified domain name format. - * - * @default - Required if virtualServiceArn is not supplied. */ - readonly virtualServiceName?: string; + readonly virtualServiceName: string; /** - * The name of the service mesh that the virtual service resides in - * - * Used to derive ARN from mesh name if ARN not provided - * - * @default - Required if virtualServiceArn is not supplied. + * The Mesh which the VirtualService belongs to */ - readonly meshName?: string; + readonly mesh: IMesh; } /** @@ -198,17 +189,24 @@ class ImportedVirtualService extends cdk.Resource implements IVirtualService { */ public readonly virtualServiceArn: string; - constructor(scope: Construct, id: string, props: VirtualServiceAttributes) { - super(scope, id); + /** + * The Mesh which the VirtualService belongs to + */ + public readonly mesh: IMesh; - if (props.virtualServiceArn) { - this.virtualServiceArn = props.virtualServiceArn; - this.virtualServiceName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(props.virtualServiceArn).resourceName!)); - } else if (props.virtualServiceName && props.meshName) { - this.virtualServiceName = props.virtualServiceName; + constructor(scope: Construct, id: string, attrs?: VirtualServiceAttributes, virtualServiceArn?: string) { + super(scope, id); + if (virtualServiceArn) { + this.virtualServiceArn = virtualServiceArn; + this.virtualServiceName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualServiceArn).resourceName!)); + const meshName = cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualServiceArn).resourceName!)); + this.mesh = Mesh.fromMeshName(this, 'Mesh', meshName); + } else if (attrs) { + this.virtualServiceName = attrs.virtualServiceName; + this.mesh = attrs.mesh; this.virtualServiceArn = cdk.Stack.of(this).formatArn({ service: 'appmesh', - resource: `mesh/${props.meshName}/virtualService`, + resource: `mesh/${attrs.mesh.meshName}/virtualService`, resourceName: this.virtualServiceName, }); } else { diff --git a/packages/@aws-cdk/aws-appmesh/test/test.gateway-route.ts b/packages/@aws-cdk/aws-appmesh/test/test.gateway-route.ts index 842e553dfa20c..75fb112ec87e1 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.gateway-route.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.gateway-route.ts @@ -133,20 +133,36 @@ export = { }, }, - 'Can export and import GatewayRoutes and perform actions'(test: Test) { + 'Can import Gateway Routes using ARN and attributes'(test: Test) { const app = new cdk.App(); // GIVEN const stack = new cdk.Stack(app, 'Imports', { env: { account: '123456789012', region: 'us-east-1' }, }); + const meshName = 'test-mesh'; + const virtualGatewayName = 'test-gateway'; + const gatewayRouteName = 'test-gateway-route'; + const arn = `arn:aws:appmesh:us-east-1:123456789012:mesh/${meshName}/virtualGateway/${virtualGatewayName}/gatewayRoute/${gatewayRouteName}`; // WHEN - const gatewayRoute2 = appmesh.GatewayRoute.fromGatewayRouteArn( - stack, 'importedGatewayRoute2', 'arn:aws:appmesh:us-east-1:123456789012:mesh/test-mesh/virtualGateway/test-gateway/gatewayRoute/test-gateway-route'); + const mesh = appmesh.Mesh.fromMeshName(stack, 'Mesh', meshName); + const gateway = mesh.addVirtualGateway('VirtualGateway', { + virtualGatewayName: virtualGatewayName, + }); + const gatewayRoute1 = appmesh.GatewayRoute.fromGatewayRouteAttributes(stack, 'importedGatewayRoute1', { + gatewayRouteName: gatewayRouteName, + virtualGateway: gateway, + }); + // THEN + test.equal(gatewayRoute1.gatewayRouteName, gatewayRouteName); + // test.equal(gatewayRoute1.virtualGateway.virtualGatewayName, virtualGatewayName); + test.equal(gatewayRoute1.virtualGateway.mesh.meshName, meshName); + // WHEN + const gatewayRoute2 = appmesh.GatewayRoute.fromGatewayRouteArn(stack, 'importedGatewayRoute2', arn); // THEN - test.equal(gatewayRoute2.gatewayRouteName, 'test-gateway-route'); - test.equal(gatewayRoute2.virtualGateway.virtualGatewayName, 'test-gateway'); - test.equal(gatewayRoute2.virtualGateway.mesh.meshName, 'test-mesh'); + test.equal(gatewayRoute2.gatewayRouteName, gatewayRouteName); + test.equal(gatewayRoute2.virtualGateway.virtualGatewayName, virtualGatewayName); + test.equal(gatewayRoute2.virtualGateway.mesh.meshName, meshName); test.done(); }, }; \ No newline at end of file diff --git a/packages/@aws-cdk/aws-appmesh/test/test.route.ts b/packages/@aws-cdk/aws-appmesh/test/test.route.ts index 706a29a3552fc..869409dba45ee 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.route.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.route.ts @@ -4,55 +4,33 @@ import { Test } from 'nodeunit'; import * as appmesh from '../lib'; export = { - 'Can export existing route and re-import'(test: Test) { + 'Can import Routes using ARN and attributes'(test: Test) { + const app = new cdk.App(); // GIVEN - const stack = new cdk.Stack(); - - // WHEN - const mesh = new appmesh.Mesh(stack, 'mesh', { - meshName: 'test-mesh', - }); - - const router = new appmesh.VirtualRouter(stack, 'router', { - mesh, + const stack = new cdk.Stack(app, 'Imports', { + env: { account: '123456789012', region: 'us-east-1' }, }); + const meshName = 'test-mesh'; + const virtualRouterName = 'test-virtual-router'; + const routeName = 'test-route'; + const arn = `arn:aws:appmesh:us-east-1:123456789012:mesh/${meshName}/virtualRouter/${virtualRouterName}/gatewayRoute/${routeName}`; - const service1 = new appmesh.VirtualService(stack, 'service-1', { - virtualServiceName: 'service1.domain.local', - mesh, - }); - - const node = mesh.addVirtualNode('test-node', { - dnsHostName: 'test', - listener: { - portMapping: - { - port: 8080, - protocol: appmesh.Protocol.HTTP, - }, - }, - backends: [ - service1, - ], - }); - - const route = new appmesh.Route(stack, 'route-1', { - mesh, - virtualRouter: router, - routeTargets: [ - { - virtualNode: node, - weight: 50, - }, - ], - prefix: '/', + // WHEN + const mesh = appmesh.Mesh.fromMeshName(stack, 'Mesh', meshName); + const virtualRouter = mesh.addVirtualRouter('VirtualGateway', { + virtualRouterName: virtualRouterName, }); - - const stack2 = new cdk.Stack(); - appmesh.Route.fromRouteName(stack2, 'imported-route', mesh.meshName, router.virtualRouterName, route.routeName); - - // Nothing to do with imported route yet - + const route1 = appmesh.Route.fromRouteAttributes(stack, 'importedRoute1', { routeName, virtualRouter }); + // THEN + test.equal(route1.routeName, routeName); + // test.equal(route1.virtualRouter.virtualRouterName, virtualRouterName); + test.equal(route1.virtualRouter.mesh.meshName, meshName); + // WHEN + const route2 = appmesh.Route.fromRouteArn(stack, 'importedRoute2', arn); + // THEN + test.equal(route2.routeName, routeName); + test.equal(route2.virtualRouter.virtualRouterName, virtualRouterName); + test.equal(route2.virtualRouter.mesh.meshName, meshName); test.done(); }, }; diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts index 1dd75b938ab04..a2166f2a71565 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts @@ -239,20 +239,31 @@ export = { }, }, - 'Can export and import VirtualGateway and perform actions'(test: Test) { + 'Can import VirtualGateways using ARN and attributes'(test: Test) { const app = new cdk.App(); // GIVEN const stack = new cdk.Stack(app, 'Imports', { env: { account: '123456789012', region: 'us-east-1' }, }); + const meshName = 'testMesh'; + const virtualGatewayName = 'test-gateway'; + const arn = `arn:aws:appmesh:us-east-1:123456789012:mesh/${meshName}/virtualGateway/${virtualGatewayName}`; // WHEN - const virtualGateway2 = appmesh.VirtualGateway.fromVirtualGatewayArn( - stack, 'importedGateway2', 'arn:aws:appmesh:us-east-1:123456789012:mesh/testMesh/virtualGateway/test-gateway'); + const virtualGateway1 = appmesh.VirtualGateway.fromVirtualGatewayAttributes(stack, 'importedGateway1', { + mesh: appmesh.Mesh.fromMeshName(stack, 'Mesh', meshName), + virtualGatewayName: virtualGatewayName, + }); + // THEN + test.equal(virtualGateway1.mesh.meshName, meshName); + test.equal(virtualGateway1.virtualGatewayName, virtualGatewayName); + // WHEN + const virtualGateway2 = appmesh.VirtualGateway.fromVirtualGatewayArn( + stack, 'importedGateway2', arn); // THEN - test.equal(virtualGateway2.mesh.meshName, 'testMesh'); - test.equal(virtualGateway2.virtualGatewayName, 'test-gateway'); + test.equal(virtualGateway2.mesh.meshName, meshName); + test.equal(virtualGateway2.virtualGatewayName, virtualGatewayName); test.done(); }, }; \ No newline at end of file diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts index 82589e2c52ac5..29b0107384497 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts @@ -102,55 +102,29 @@ export = { }, }, }, - 'Can export and import VirtualNode and perform actions'(test: Test) { + 'Can import Virtual Nodes using ARN and attributes'(test: Test) { // GIVEN const stack = new cdk.Stack(); - // WHEN - const mesh = new appmesh.Mesh(stack, 'mesh', { - meshName: 'test-mesh', - }); - - const node = mesh.addVirtualNode('test-node', { - dnsHostName: 'test.domain.local', - listener: {}, - }); + const meshName = 'testMesh'; + const virtualNodeName = 'test-node'; + const arn = `arn:aws:appmesh:us-east-1:123456789012:mesh/${meshName}/virtualNode/${virtualNodeName}`; - const stack2 = new cdk.Stack(); - - const node2 = appmesh.VirtualNode.fromVirtualNodeName(stack2, 'imported-node', mesh.meshName, node.virtualNodeName); - - node2.addListeners({ - portMapping: { - port: 8081, - protocol: appmesh.Protocol.TCP, - }, + // WHEN + const virtualNode1 = appmesh.VirtualNode.fromVirtualNodeAttributes(stack, 'importedVirtualNode1', { + mesh: appmesh.Mesh.fromMeshName(stack, 'Mesh', meshName), + virtualNodeName: virtualNodeName, }); + // THEN + test.equal(virtualNode1.mesh.meshName, meshName); + test.equal(virtualNode1.virtualNodeName, virtualNodeName); + // WHEN + const virtualNode2 = appmesh.VirtualNode.fromVirtualNodeArn( + stack, 'importedVirtualNode2', arn); // THEN - expect(stack).to( - haveResourceLike('AWS::AppMesh::VirtualNode', { - MeshName: { - 'Fn::GetAtt': ['meshACDFE68E', 'MeshName'], - }, - Spec: { - Listeners: [ - { - PortMapping: { - Port: 8080, - Protocol: 'http', - }, - }, - ], - ServiceDiscovery: { - DNS: { - Hostname: 'test.domain.local', - }, - }, - }, - VirtualNodeName: 'meshtestnode428A9479', - }), - ); + test.equal(virtualNode2.mesh.meshName, meshName); + test.equal(virtualNode2.virtualNodeName, virtualNodeName); test.done(); }, 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..d118f69d73a14 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts @@ -304,15 +304,29 @@ export = { }, }, - 'can import a virtual router'(test: Test) { + 'Can import Virtual Routers using ARN and attributes'(test: Test) { // GIVEN const stack = new cdk.Stack(); + const meshName = 'testMesh'; + const virtualRouterName = 'virtual-router'; + const arn = `arn:aws:appmesh:us-east-1:123456789012:mesh/${meshName}/virtualRouter/${virtualRouterName}`; + // WHEN - const vr = appmesh.VirtualRouter.fromVirtualRouterName(stack, 'Router', 'MyMesh', 'MyRouter'); + const virtualRouter1 = appmesh.VirtualRouter.fromVirtualRouterAttributes(stack, 'importVirtualRouter1', { + mesh: appmesh.Mesh.fromMeshName(stack, 'Mesh', meshName), + virtualRouterName: virtualRouterName, + }); + // THEN + test.equal(virtualRouter1.mesh.meshName, meshName); + test.equal(virtualRouter1.virtualRouterName, virtualRouterName); + // WHEN + const virtualRouter2 = appmesh.VirtualRouter.fromVirtualRouterArn( + stack, 'importedVirtualRouter2', arn); // THEN - test.ok(vr.mesh !== undefined); + test.equal(virtualRouter2.mesh.meshName, meshName); + test.equal(virtualRouter2.virtualRouterName, virtualRouterName); test.done(); }, diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-service.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-service.ts index aa582c9376f19..3665635d01b9e 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-service.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-service.ts @@ -1,38 +1,31 @@ -import * as ec2 from '@aws-cdk/aws-ec2'; -import * as cloudmap from '@aws-cdk/aws-servicediscovery'; import * as cdk from '@aws-cdk/core'; import { Test } from 'nodeunit'; import * as appmesh from '../lib'; export = { - 'Can export existing virtual-service and re-import'(test: Test) { + 'Can import Virtual Services using ARN and attributes'(test: Test) { // GIVEN const stack = new cdk.Stack(); - // WHEN - const mesh = new appmesh.Mesh(stack, 'mesh', { - meshName: 'test-mesh', - }); - - const router = new appmesh.VirtualRouter(stack, 'router', { mesh }); - - const vpc = new ec2.Vpc(stack, 'vpc'); - const namespace = new cloudmap.PrivateDnsNamespace(stack, 'test-namespace', { - vpc, - name: 'domain.local', - }); + const meshName = 'testMesh'; + const virtualServiceName = 'virtual-service'; + const arn = `arn:aws:appmesh:us-east-1:123456789012:mesh/${meshName}/virtualService/${virtualServiceName}`; - const service = new appmesh.VirtualService(stack, 'service-1', { - mesh, - virtualServiceName: `service.${namespace.namespaceName}`, - virtualRouter: router, + // WHEN + const virtualService1 = appmesh.VirtualService.fromVirtualServiceAttributes(stack, 'importedVirtualService1', { + mesh: appmesh.Mesh.fromMeshName(stack, 'Mesh', meshName), + virtualServiceName: virtualServiceName, }); + // THEN + test.equal(virtualService1.mesh.meshName, meshName); + test.equal(virtualService1.virtualServiceName, virtualServiceName); - const stack2 = new cdk.Stack(); - appmesh.VirtualService.fromVirtualServiceName(stack2, 'imported-virtual-service', mesh.meshName, service.virtualServiceName); - - // Nothing to do with imported virtual service yet + // WHEN + const virtualRouter2 = appmesh.VirtualRouter.fromVirtualRouterArn(stack, 'importedVirtualRouter2', arn); + // THEN + test.equal(virtualRouter2.mesh.meshName, meshName); + test.equal(virtualRouter2.virtualRouterName, virtualServiceName); test.done(); }, From f53354c88bde4a855c76acc5c750db8f07390257 Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Tue, 3 Nov 2020 12:51:19 -0800 Subject: [PATCH 2/6] remove the import classes and replace with inline class declarations --- .../@aws-cdk/aws-appmesh/lib/gateway-route.ts | 55 +++++----------- packages/@aws-cdk/aws-appmesh/lib/route.ts | 63 ++++++------------- .../aws-appmesh/lib/virtual-gateway.ts | 56 +++++------------ .../@aws-cdk/aws-appmesh/lib/virtual-node.ts | 57 +++++------------ .../aws-appmesh/lib/virtual-router.ts | 56 +++++------------ .../aws-appmesh/lib/virtual-service.ts | 56 +++++------------ .../aws-appmesh/test/test.gateway-route.ts | 33 ++++++---- .../@aws-cdk/aws-appmesh/test/test.route.ts | 34 ++++++---- .../aws-appmesh/test/test.virtual-gateway.ts | 32 ++++++---- .../aws-appmesh/test/test.virtual-node.ts | 29 +++++---- .../aws-appmesh/test/test.virtual-router.ts | 27 +++++--- .../aws-appmesh/test/test.virtual-service.ts | 29 +++++---- 12 files changed, 208 insertions(+), 319 deletions(-) diff --git a/packages/@aws-cdk/aws-appmesh/lib/gateway-route.ts b/packages/@aws-cdk/aws-appmesh/lib/gateway-route.ts index aeded032426c6..9eedb66b525e4 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/gateway-route.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/gateway-route.ts @@ -65,14 +65,26 @@ export class GatewayRoute extends cdk.Resource implements IGatewayRoute { * Import an existing GatewayRoute given an ARN */ public static fromGatewayRouteArn(scope: Construct, id: string, gatewayRouteArn: string): IGatewayRoute { - return new ImportedGatewayRoute(scope, id, undefined, gatewayRouteArn); + return new class extends cdk.Resource implements IGatewayRoute { + readonly gatewayRouteArn = gatewayRouteArn; + readonly gatewayRouteName = cdk.Fn.select(4, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(gatewayRouteArn).resourceName!)); + readonly virtualGateway = VirtualGateway.fromVirtualGatewayArn(this, 'virtualGateway', gatewayRouteArn); + }(scope, id); } /** * Import an existing GatewayRoute given attributes */ public static fromGatewayRouteAttributes(scope: Construct, id: string, attrs: GatewayRouteAttributes): IGatewayRoute { - return new ImportedGatewayRoute(scope, id, attrs); + return new class extends cdk.Resource implements IGatewayRoute { + readonly gatewayRouteName = attrs.gatewayRouteName; + readonly gatewayRouteArn = cdk.Stack.of(scope).formatArn({ + service: 'appmesh', + resource: `mesh/${attrs.virtualGateway.mesh.meshName}/virtualGateway/${attrs.virtualGateway.virtualGatewayName}/gatewayRoute`, + resourceName: this.gatewayRouteName, + }); + readonly virtualGateway = attrs.virtualGateway; + }(scope, id); } /** @@ -132,42 +144,3 @@ export interface GatewayRouteAttributes { */ readonly virtualGateway: IVirtualGateway; } - -/** - * Represents an imported IGatewayRoute - */ -class ImportedGatewayRoute extends cdk.Resource implements IGatewayRoute { - /** - * The name of the GatewayRoute - */ - public gatewayRouteName: string; - - /** - * The Amazon Resource Name (ARN) for the GatewayRoute - */ - public gatewayRouteArn: string; - - /** - * The VirtualGateway the GatewayRoute is associated with - */ - public virtualGateway: IVirtualGateway; - - constructor(scope: Construct, id: string, attrs?: GatewayRouteAttributes, gatewayRouteArn?: string) { - super(scope, id); - if (gatewayRouteArn) { - this.gatewayRouteArn = gatewayRouteArn; - this.gatewayRouteName = cdk.Fn.select(4, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(gatewayRouteArn).resourceName!)); - this.virtualGateway = VirtualGateway.fromVirtualGatewayArn(this, 'virtualGateway', gatewayRouteArn); - } else if (attrs) { - this.gatewayRouteName = attrs.gatewayRouteName; - this.virtualGateway = attrs.virtualGateway; - this.gatewayRouteArn = cdk.Stack.of(this).formatArn({ - service: 'appmesh', - resource: `mesh/${attrs.virtualGateway.mesh.meshName}/virtualGateway/${attrs.virtualGateway.virtualGatewayName}/gatewayRoute`, - resourceName: this.gatewayRouteName, - }); - } else { - throw new Error('Need gatewayRouteArn or gatewayRouteName and mesh'); - } - } -} diff --git a/packages/@aws-cdk/aws-appmesh/lib/route.ts b/packages/@aws-cdk/aws-appmesh/lib/route.ts index d933372776a9b..d071863d55103 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/route.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/route.ts @@ -119,14 +119,30 @@ 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 ImportedRoute(scope, id, undefined, routeArn); + return new class extends cdk.Resource implements IRoute { + readonly routeArn = routeArn; + readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(routeArn).resourceName!))); + readonly virtualRouter = VirtualRouter.fromVirtualRouterAttributes(this, 'VirtualRouter', { + mesh: this.mesh, + virtualRouterName: cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(routeArn).resourceName!)), + }); + readonly routeName = cdk.Fn.select(4, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(routeArn).resourceName!)); + }(scope, id); } /** * Import an existing Route given attributes */ public static fromRouteAttributes(scope: Construct, id: string, attrs: RouteAttributes): IRoute { - return new ImportedRoute(scope, id, attrs); + 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); } /** @@ -231,46 +247,3 @@ export interface RouteAttributes { */ readonly virtualRouter: IVirtualRouter; } - -/** - * Represents and imported IRoute - */ -class ImportedRoute extends cdk.Resource implements IRoute { - /** - * 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, attrs?: RouteAttributes, routeArn?: string) { - super(scope, id); - - if (routeArn) { - this.routeArn = routeArn; - const meshName = cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(routeArn).resourceName!)); - const mesh = Mesh.fromMeshName(this, 'Mesh', meshName); - const virtualRouterName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(routeArn).resourceName!)); - this.virtualRouter = VirtualRouter.fromVirtualRouterAttributes(this, 'VirtualROuter', { mesh, virtualRouterName }); - this.routeName = cdk.Fn.select(4, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(routeArn).resourceName!)); - } else if (attrs) { - this.routeName = attrs.routeName; - this.virtualRouter = attrs.virtualRouter; - this.routeArn = cdk.Stack.of(this).formatArn({ - service: 'appmesh', - resource: `mesh/${attrs.virtualRouter.mesh.meshName}/virtualRouter/${attrs.virtualRouter.virtualRouterName}/route`, - resourceName: this.routeName, - }); - } else { - throw new Error('Need either arn or three names'); - } - } -} diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts index 04c147e9ad890..5d76a14a7554c 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts @@ -107,14 +107,26 @@ export class VirtualGateway extends VirtualGatewayBase { * Import an existing VirtualGateway given an ARN */ public static fromVirtualGatewayArn(scope: Construct, id: string, virtualGatewayArn: string): IVirtualGateway { - return new ImportedVirtualGateway(scope, id, undefined, virtualGatewayArn); + return new class extends cdk.Resource implements IVirtualGateway { + readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualGatewayArn).resourceName!))); + readonly virtualGatewayArn = virtualGatewayArn; + readonly virtualGatewayName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualGatewayArn).resourceName!)); + }(scope, id); } /** * Import an existing VirtualGateway given its attributes */ public static fromVirtualGatewayAttributes(scope: Construct, id: string, attrs: VirtualGatewayAttributes): IVirtualGateway { - return new ImportedVirtualGateway(scope, id, attrs); + return new class extends cdk.Resource implements IVirtualGateway { + readonly mesh = attrs.mesh; + readonly virtualGatewayName = attrs.virtualGatewayName; + readonly virtualGatewayArn = cdk.Stack.of(this).formatArn({ + service: 'appmesh', + resource: `mesh/${attrs.mesh.meshName}/virtualGateway`, + resourceName: this.virtualGatewayName, + }); + }(scope, id); } /** @@ -184,43 +196,3 @@ export interface VirtualGatewayAttributes { */ readonly mesh: IMesh; } - -/** - * Used to import a VirtualGateway and read its properties - */ -class ImportedVirtualGateway extends VirtualGatewayBase { - /** - * The name of the VirtualGateway - */ - public readonly virtualGatewayName: string; - - /** - * The Amazon Resource Name (ARN) belonging to the VirtualGateway - */ - public readonly virtualGatewayArn: string; - - /** - * The Mesh that the VirtualGateway belongs to - */ - public readonly mesh: IMesh; - - constructor(scope: Construct, id: string, attrs?: VirtualGatewayAttributes, virtualGatewayArn?: string) { - super(scope, id); - if (virtualGatewayArn) { - const meshName = cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualGatewayArn).resourceName!)); - this.mesh = Mesh.fromMeshName(this, 'Mesh', meshName); - this.virtualGatewayArn = virtualGatewayArn; - this.virtualGatewayName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualGatewayArn).resourceName!)); - } else if (attrs) { - this.mesh = attrs.mesh; - this.virtualGatewayName = attrs.virtualGatewayName; - this.virtualGatewayArn = cdk.Stack.of(this).formatArn({ - service: 'appmesh', - resource: `mesh/${attrs.mesh.meshName}/virtualGateway`, - resourceName: this.virtualGatewayName, - }); - } else { - throw new Error('Need either virtualGatewayArn or virtualGatewayName and mesh'); - } - } -} diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts index 478b31495b8e6..c06c98b2973d1 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts @@ -207,14 +207,26 @@ export class VirtualNode extends VirtualNodeBase { * Import an existing VirtualNode given an ARN */ public static fromVirtualNodeArn(scope: Construct, id: string, virtualNodeArn: string): IVirtualNode { - return new ImportedVirtualNode(scope, id, undefined, virtualNodeArn); + return new class extends VirtualNodeBase { + readonly virtualNodeArn = virtualNodeArn; + readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualNodeArn).resourceName!))); + readonly virtualNodeName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualNodeArn).resourceName!)); + }(scope, id); } /** * Import an existing VirtualNode given its name */ public static fromVirtualNodeAttributes(scope: Construct, id: string, attrs: VirtualNodeAttributes): IVirtualNode { - return new ImportedVirtualNode(scope, id, attrs); + return new class extends VirtualNodeBase { + readonly mesh = attrs.mesh; + readonly virtualNodeName = attrs.virtualNodeName; + readonly virtualNodeArn = cdk.Stack.of(this).formatArn({ + service: 'appmesh', + resource: `mesh/${attrs.mesh.meshName}/virtualNode`, + resourceName: this.virtualNodeName, + }); + }(scope, id); } /** @@ -291,44 +303,3 @@ export interface VirtualNodeAttributes { */ readonly mesh: IMesh; } - -/** - * Used to import a VirtualNode and read its properties - */ -class ImportedVirtualNode extends VirtualNodeBase { - /** - * The name of the VirtualNode - */ - public readonly virtualNodeName: string; - - /** - * The Amazon Resource Name belonging to the VirtualNode - */ - public readonly virtualNodeArn: string; - - /** - * The Mesh which the VirtualNode belongs to - */ - public readonly mesh: IMesh; - - constructor(scope: Construct, id: string, attrs?: VirtualNodeAttributes, virtualNodeArn?: string) { - super(scope, id); - - if (virtualNodeArn) { - this.virtualNodeArn = virtualNodeArn; - const meshName = cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualNodeArn).resourceName!)); - this.mesh = Mesh.fromMeshName(this, 'Mesh', meshName); - this.virtualNodeName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualNodeArn).resourceName!)); - } else if (attrs) { - this.mesh = attrs.mesh; - this.virtualNodeName = attrs.virtualNodeName; - this.virtualNodeArn = cdk.Stack.of(this).formatArn({ - service: 'appmesh', - resource: `mesh/${attrs.mesh.meshName}/virtualNode`, - resourceName: this.virtualNodeName, - }); - } else { - throw new Error('Need either virtualNodeArn or virtualNodeName and mesh'); - } - } -} diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts index 537ba2b1a5813..3ee77868d2426 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts @@ -109,14 +109,26 @@ export class VirtualRouter extends VirtualRouterBase { * Import an existing VirtualRouter given an ARN */ public static fromVirtualRouterArn(scope: Construct, id: string, virtualRouterArn: string): IVirtualRouter { - return new ImportedVirtualRouter(scope, id, undefined, virtualRouterArn); + return new class extends VirtualRouterBase { + readonly virtualRouterArn = virtualRouterArn; + readonly virtualRouterName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualRouterArn).resourceName!)); + readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualRouterArn).resourceName!))); + }(scope, id); } /** * Import an existing VirtualRouter given attributes */ public static fromVirtualRouterAttributes(scope: Construct, id: string, attrs: VirtualRouterAttributes): IVirtualRouter { - return new ImportedVirtualRouter(scope, id, attrs); + return new class extends VirtualRouterBase { + readonly virtualRouterName = attrs.virtualRouterName; + readonly mesh = attrs.mesh; + readonly virtualRouterArn = cdk.Stack.of(this).formatArn({ + service: 'appmesh', + resource: `mesh/${attrs.mesh.meshName}/virtualRouter`, + resourceName: this.virtualRouterName, + }); + }(scope, id); } /** @@ -185,43 +197,3 @@ export interface VirtualRouterAttributes { */ readonly mesh: IMesh; } - -/** - * Used to import a VirtualRouter and perform actions or read properties from - */ -class ImportedVirtualRouter extends VirtualRouterBase { - /** - * The name of the VirtualRouter - */ - public readonly virtualRouterName: string; - - /** - * The Amazon Resource Name (ARN) for the VirtualRouter - */ - public readonly virtualRouterArn: string; - - /** - * The Mesh which the VirtualRouter belongs to - */ - public readonly mesh: IMesh; - - constructor(scope: Construct, id: string, attrs?: VirtualRouterAttributes, virtualRouterArn?: string) { - super(scope, id); - if (virtualRouterArn) { - this.virtualRouterArn = virtualRouterArn; - this.virtualRouterName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualRouterArn).resourceName!)); - const meshName = cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualRouterArn).resourceName!)); - this.mesh = Mesh.fromMeshName(this, 'Mesh', meshName); - } else if (attrs) { - this.virtualRouterName = attrs.virtualRouterName; - this.mesh = attrs.mesh; - this.virtualRouterArn = cdk.Stack.of(this).formatArn({ - service: 'appmesh', - resource: `mesh/${attrs.mesh.meshName}/virtualRouter`, - resourceName: this.virtualRouterName, - }); - } else { - throw new Error('Need either arn or both names'); - } - } -} diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts index c36cbca062bee..161578ceb2222 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts @@ -81,14 +81,26 @@ export class VirtualService extends cdk.Resource implements IVirtualService { * Import an existing VirtualService given an ARN */ public static fromVirtualServiceArn(scope: Construct, id: string, virtualServiceArn: string): IVirtualService { - return new ImportedVirtualService(scope, id, undefined, virtualServiceArn); + return new class extends cdk.Resource implements IVirtualService { + readonly virtualServiceArn = virtualServiceArn; + readonly virtualServiceName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualServiceArn).resourceName!)); + readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualServiceArn).resourceName!))); + }(scope, id); } /** * Import an existing VirtualService given its attributes */ public static fromVirtualServiceAttributes(scope: Construct, id: string, attrs: VirtualServiceAttributes): IVirtualService { - return new ImportedVirtualService(scope, id, attrs); + return new class extends cdk.Resource implements IVirtualService { + readonly virtualServiceName = attrs.virtualServiceName; + readonly mesh = attrs.mesh; + readonly virtualServiceArn = cdk.Stack.of(this).formatArn({ + service: 'appmesh', + resource: `mesh/${attrs.mesh.meshName}/virtualService`, + resourceName: this.virtualServiceName, + }); + }(scope, id); } /** @@ -174,43 +186,3 @@ export interface VirtualServiceAttributes { */ readonly mesh: IMesh; } - -/** - * Returns properties that allows a VirtualService to be imported - */ -class ImportedVirtualService extends cdk.Resource implements IVirtualService { - /** - * The name of the VirtualService, it is recommended this follows the fully-qualified domain name format. - */ - public readonly virtualServiceName: string; - - /** - * The Amazon Resource Name (ARN) for the virtual service - */ - public readonly virtualServiceArn: string; - - /** - * The Mesh which the VirtualService belongs to - */ - public readonly mesh: IMesh; - - constructor(scope: Construct, id: string, attrs?: VirtualServiceAttributes, virtualServiceArn?: string) { - super(scope, id); - if (virtualServiceArn) { - this.virtualServiceArn = virtualServiceArn; - this.virtualServiceName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualServiceArn).resourceName!)); - const meshName = cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualServiceArn).resourceName!)); - this.mesh = Mesh.fromMeshName(this, 'Mesh', meshName); - } else if (attrs) { - this.virtualServiceName = attrs.virtualServiceName; - this.mesh = attrs.mesh; - this.virtualServiceArn = cdk.Stack.of(this).formatArn({ - service: 'appmesh', - resource: `mesh/${attrs.mesh.meshName}/virtualService`, - resourceName: this.virtualServiceName, - }); - } else { - throw new Error('Need either arn or both names'); - } - } -} diff --git a/packages/@aws-cdk/aws-appmesh/test/test.gateway-route.ts b/packages/@aws-cdk/aws-appmesh/test/test.gateway-route.ts index 75fb112ec87e1..2f08e58bb873a 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.gateway-route.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.gateway-route.ts @@ -133,7 +133,7 @@ export = { }, }, - 'Can import Gateway Routes using ARN and attributes'(test: Test) { + 'Can import Gateway Routes using an ARN'(test: Test) { const app = new cdk.App(); // GIVEN const stack = new cdk.Stack(app, 'Imports', { @@ -144,25 +144,36 @@ export = { const gatewayRouteName = 'test-gateway-route'; const arn = `arn:aws:appmesh:us-east-1:123456789012:mesh/${meshName}/virtualGateway/${virtualGatewayName}/gatewayRoute/${gatewayRouteName}`; + // WHEN + const gatewayRoute = appmesh.GatewayRoute.fromGatewayRouteArn(stack, 'importedGatewayRoute', arn); + // THEN + test.equal(gatewayRoute.gatewayRouteName, gatewayRouteName); + test.equal(gatewayRoute.virtualGateway.virtualGatewayName, virtualGatewayName); + test.equal(gatewayRoute.virtualGateway.mesh.meshName, meshName); + test.done(); + }, + 'Can import Gateway Routes using attributes'(test: Test) { + const app = new cdk.App(); + // GIVEN + const stack = new cdk.Stack(app, 'Imports', { + env: { account: '123456789012', region: 'us-east-1' }, + }); + const meshName = 'test-mesh'; + const virtualGatewayName = 'test-gateway'; + const gatewayRouteName = 'test-gateway-route'; + // WHEN const mesh = appmesh.Mesh.fromMeshName(stack, 'Mesh', meshName); const gateway = mesh.addVirtualGateway('VirtualGateway', { virtualGatewayName: virtualGatewayName, }); - const gatewayRoute1 = appmesh.GatewayRoute.fromGatewayRouteAttributes(stack, 'importedGatewayRoute1', { + const gatewayRoute = appmesh.GatewayRoute.fromGatewayRouteAttributes(stack, 'importedGatewayRoute', { gatewayRouteName: gatewayRouteName, virtualGateway: gateway, }); // THEN - test.equal(gatewayRoute1.gatewayRouteName, gatewayRouteName); - // test.equal(gatewayRoute1.virtualGateway.virtualGatewayName, virtualGatewayName); - test.equal(gatewayRoute1.virtualGateway.mesh.meshName, meshName); - // WHEN - const gatewayRoute2 = appmesh.GatewayRoute.fromGatewayRouteArn(stack, 'importedGatewayRoute2', arn); - // THEN - test.equal(gatewayRoute2.gatewayRouteName, gatewayRouteName); - test.equal(gatewayRoute2.virtualGateway.virtualGatewayName, virtualGatewayName); - test.equal(gatewayRoute2.virtualGateway.mesh.meshName, meshName); + test.equal(gatewayRoute.gatewayRouteName, gatewayRouteName); + test.equal(gatewayRoute.virtualGateway.mesh.meshName, meshName); test.done(); }, }; \ No newline at end of file diff --git a/packages/@aws-cdk/aws-appmesh/test/test.route.ts b/packages/@aws-cdk/aws-appmesh/test/test.route.ts index 869409dba45ee..3f24bdf4be470 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.route.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.route.ts @@ -4,7 +4,7 @@ import { Test } from 'nodeunit'; import * as appmesh from '../lib'; export = { - 'Can import Routes using ARN and attributes'(test: Test) { + 'Can import Routes using an ARN'(test: Test) { const app = new cdk.App(); // GIVEN const stack = new cdk.Stack(app, 'Imports', { @@ -15,22 +15,34 @@ export = { const routeName = 'test-route'; const arn = `arn:aws:appmesh:us-east-1:123456789012:mesh/${meshName}/virtualRouter/${virtualRouterName}/gatewayRoute/${routeName}`; + // WHEN + const route = appmesh.Route.fromRouteArn(stack, 'importedRoute', arn); + // THEN + test.equal(route.routeName, routeName); + test.equal(route.virtualRouter.virtualRouterName, virtualRouterName); + test.equal(route.virtualRouter.mesh.meshName, meshName); + test.done(); + }, + 'Can import Routes using ARN and attributes'(test: Test) { + const app = new cdk.App(); + // GIVEN + const stack = new cdk.Stack(app, 'Imports', { + env: { account: '123456789012', region: 'us-east-1' }, + }); + const meshName = 'test-mesh'; + const virtualRouterName = 'test-virtual-router'; + const routeName = 'test-route'; + // WHEN const mesh = appmesh.Mesh.fromMeshName(stack, 'Mesh', meshName); const virtualRouter = mesh.addVirtualRouter('VirtualGateway', { virtualRouterName: virtualRouterName, }); - const route1 = appmesh.Route.fromRouteAttributes(stack, 'importedRoute1', { routeName, virtualRouter }); - // THEN - test.equal(route1.routeName, routeName); - // test.equal(route1.virtualRouter.virtualRouterName, virtualRouterName); - test.equal(route1.virtualRouter.mesh.meshName, meshName); - // WHEN - const route2 = appmesh.Route.fromRouteArn(stack, 'importedRoute2', arn); + const route = appmesh.Route.fromRouteAttributes(stack, 'importedRoute', { routeName, virtualRouter }); // THEN - test.equal(route2.routeName, routeName); - test.equal(route2.virtualRouter.virtualRouterName, virtualRouterName); - test.equal(route2.virtualRouter.mesh.meshName, meshName); + test.equal(route.routeName, routeName); + test.equal(route.virtualRouter.mesh.meshName, meshName); + test.done(); }, }; diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts index a2166f2a71565..f5c020426c9b9 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts @@ -238,8 +238,7 @@ export = { test.done(); }, }, - - 'Can import VirtualGateways using ARN and attributes'(test: Test) { + 'Can import VirtualGateways using an ARN'(test: Test) { const app = new cdk.App(); // GIVEN const stack = new cdk.Stack(app, 'Imports', { @@ -250,20 +249,31 @@ export = { const arn = `arn:aws:appmesh:us-east-1:123456789012:mesh/${meshName}/virtualGateway/${virtualGatewayName}`; // WHEN - const virtualGateway1 = appmesh.VirtualGateway.fromVirtualGatewayAttributes(stack, 'importedGateway1', { + const virtualGateway = appmesh.VirtualGateway.fromVirtualGatewayArn( + stack, 'importedGateway', arn); + // THEN + test.equal(virtualGateway.mesh.meshName, meshName); + test.equal(virtualGateway.virtualGatewayName, virtualGatewayName); + test.done(); + }, + 'Can import VirtualGateways using attributes'(test: Test) { + const app = new cdk.App(); + // GIVEN + const stack = new cdk.Stack(app, 'Imports', { + env: { account: '123456789012', region: 'us-east-1' }, + }); + const meshName = 'testMesh'; + const virtualGatewayName = 'test-gateway'; + + // WHEN + const virtualGateway = appmesh.VirtualGateway.fromVirtualGatewayAttributes(stack, 'importedGateway', { mesh: appmesh.Mesh.fromMeshName(stack, 'Mesh', meshName), virtualGatewayName: virtualGatewayName, }); // THEN - test.equal(virtualGateway1.mesh.meshName, meshName); - test.equal(virtualGateway1.virtualGatewayName, virtualGatewayName); + test.equal(virtualGateway.mesh.meshName, meshName); + test.equal(virtualGateway.virtualGatewayName, virtualGatewayName); - // WHEN - const virtualGateway2 = appmesh.VirtualGateway.fromVirtualGatewayArn( - stack, 'importedGateway2', arn); - // THEN - test.equal(virtualGateway2.mesh.meshName, meshName); - test.equal(virtualGateway2.virtualGatewayName, virtualGatewayName); test.done(); }, }; \ No newline at end of file diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts index 29b0107384497..06928a4a25351 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts @@ -102,7 +102,7 @@ export = { }, }, }, - 'Can import Virtual Nodes using ARN and attributes'(test: Test) { + 'Can import Virtual Nodes using an ARN'(test: Test) { // GIVEN const stack = new cdk.Stack(); @@ -111,20 +111,27 @@ export = { const arn = `arn:aws:appmesh:us-east-1:123456789012:mesh/${meshName}/virtualNode/${virtualNodeName}`; // WHEN - const virtualNode1 = appmesh.VirtualNode.fromVirtualNodeAttributes(stack, 'importedVirtualNode1', { - mesh: appmesh.Mesh.fromMeshName(stack, 'Mesh', meshName), - virtualNodeName: virtualNodeName, - }); + const virtualNode = appmesh.VirtualNode.fromVirtualNodeArn( + stack, 'importedVirtualNode', arn); // THEN - test.equal(virtualNode1.mesh.meshName, meshName); - test.equal(virtualNode1.virtualNodeName, virtualNodeName); + test.equal(virtualNode.mesh.meshName, meshName); + test.equal(virtualNode.virtualNodeName, virtualNodeName); + test.done(); + }, + 'Can import Virtual Nodes using attributes'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const meshName = 'testMesh'; + const virtualNodeName = 'test-node'; // WHEN - const virtualNode2 = appmesh.VirtualNode.fromVirtualNodeArn( - stack, 'importedVirtualNode2', arn); + const virtualNode = appmesh.VirtualNode.fromVirtualNodeAttributes(stack, 'importedVirtualNode', { + mesh: appmesh.Mesh.fromMeshName(stack, 'Mesh', meshName), + virtualNodeName: virtualNodeName, + }); // THEN - test.equal(virtualNode2.mesh.meshName, meshName); - test.equal(virtualNode2.virtualNodeName, virtualNodeName); + test.equal(virtualNode.mesh.meshName, meshName); + test.equal(virtualNode.virtualNodeName, virtualNodeName); test.done(); }, 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 d118f69d73a14..dff7e4321f1f1 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts @@ -304,7 +304,7 @@ export = { }, }, - 'Can import Virtual Routers using ARN and attributes'(test: Test) { + 'Can import Virtual Routers using an ARN'(test: Test) { // GIVEN const stack = new cdk.Stack(); @@ -313,7 +313,23 @@ export = { const arn = `arn:aws:appmesh:us-east-1:123456789012:mesh/${meshName}/virtualRouter/${virtualRouterName}`; // WHEN - const virtualRouter1 = appmesh.VirtualRouter.fromVirtualRouterAttributes(stack, 'importVirtualRouter1', { + const virtualRouter = appmesh.VirtualRouter.fromVirtualRouterArn( + stack, 'importedVirtualRouter', arn); + // THEN + test.equal(virtualRouter.mesh.meshName, meshName); + test.equal(virtualRouter.virtualRouterName, virtualRouterName); + + test.done(); + }, + 'Can import Virtual Routers using attributes'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + const meshName = 'testMesh'; + const virtualRouterName = 'virtual-router'; + + // WHEN + const virtualRouter1 = appmesh.VirtualRouter.fromVirtualRouterAttributes(stack, 'importVirtualRouter', { mesh: appmesh.Mesh.fromMeshName(stack, 'Mesh', meshName), virtualRouterName: virtualRouterName, }); @@ -321,13 +337,6 @@ export = { test.equal(virtualRouter1.mesh.meshName, meshName); test.equal(virtualRouter1.virtualRouterName, virtualRouterName); - // WHEN - const virtualRouter2 = appmesh.VirtualRouter.fromVirtualRouterArn( - stack, 'importedVirtualRouter2', arn); - // THEN - test.equal(virtualRouter2.mesh.meshName, meshName); - test.equal(virtualRouter2.virtualRouterName, virtualRouterName); - test.done(); }, }; diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-service.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-service.ts index 3665635d01b9e..c09c156ac75ea 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-service.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-service.ts @@ -4,29 +4,36 @@ import { Test } from 'nodeunit'; import * as appmesh from '../lib'; export = { - 'Can import Virtual Services using ARN and attributes'(test: Test) { + 'Can import Virtual Services using an ARN'(test: Test) { // GIVEN const stack = new cdk.Stack(); const meshName = 'testMesh'; const virtualServiceName = 'virtual-service'; const arn = `arn:aws:appmesh:us-east-1:123456789012:mesh/${meshName}/virtualService/${virtualServiceName}`; + // WHEN + const virtualRouter = appmesh.VirtualRouter.fromVirtualRouterArn(stack, 'importedVirtualRouter', arn); + // THEN + test.equal(virtualRouter.mesh.meshName, meshName); + test.equal(virtualRouter.virtualRouterName, virtualServiceName); + + test.done(); + }, + 'Can import Virtual Services using attributes'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + const meshName = 'testMesh'; + const virtualServiceName = 'virtual-service'; // WHEN - const virtualService1 = appmesh.VirtualService.fromVirtualServiceAttributes(stack, 'importedVirtualService1', { + const virtualService = appmesh.VirtualService.fromVirtualServiceAttributes(stack, 'importedVirtualService', { mesh: appmesh.Mesh.fromMeshName(stack, 'Mesh', meshName), virtualServiceName: virtualServiceName, }); // THEN - test.equal(virtualService1.mesh.meshName, meshName); - test.equal(virtualService1.virtualServiceName, virtualServiceName); - - // WHEN - const virtualRouter2 = appmesh.VirtualRouter.fromVirtualRouterArn(stack, 'importedVirtualRouter2', arn); - // THEN - test.equal(virtualRouter2.mesh.meshName, meshName); - test.equal(virtualRouter2.virtualRouterName, virtualServiceName); - + test.equal(virtualService.mesh.meshName, meshName); + test.equal(virtualService.virtualServiceName, virtualServiceName); test.done(); }, }; From 2b60aabafd099b197f08962d36881ff499cf112d Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Tue, 3 Nov 2020 12:53:58 -0800 Subject: [PATCH 3/6] add addGatewayRoute back to IVirtualGateway --- packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts index 5d76a14a7554c..35aa90736cc15 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts @@ -29,6 +29,11 @@ export interface IVirtualGateway extends cdk.IResource { * The Mesh which the VirtualGateway belongs to */ readonly mesh: IMesh; + + /** + * Utility method to add a new GatewayRoute to the VirtualGateway + */ + addGatewayRoute(id: string, route: GatewayRouteBaseProps): GatewayRoute; } /** @@ -107,7 +112,7 @@ export class VirtualGateway extends VirtualGatewayBase { * Import an existing VirtualGateway given an ARN */ public static fromVirtualGatewayArn(scope: Construct, id: string, virtualGatewayArn: string): IVirtualGateway { - return new class extends cdk.Resource implements IVirtualGateway { + return new class extends VirtualGatewayBase { readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualGatewayArn).resourceName!))); readonly virtualGatewayArn = virtualGatewayArn; readonly virtualGatewayName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualGatewayArn).resourceName!)); @@ -118,7 +123,7 @@ export class VirtualGateway extends VirtualGatewayBase { * Import an existing VirtualGateway given its attributes */ public static fromVirtualGatewayAttributes(scope: Construct, id: string, attrs: VirtualGatewayAttributes): IVirtualGateway { - return new class extends cdk.Resource implements IVirtualGateway { + return new class extends VirtualGatewayBase { readonly mesh = attrs.mesh; readonly virtualGatewayName = attrs.virtualGatewayName; readonly virtualGatewayArn = cdk.Stack.of(this).formatArn({ From 0d225a8cff927d381012051fcd2079731a96816f Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Tue, 3 Nov 2020 14:21:01 -0800 Subject: [PATCH 4/6] Update comment on GatewayRouteAttributes.virtualGateway --- packages/@aws-cdk/aws-appmesh/lib/gateway-route.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-appmesh/lib/gateway-route.ts b/packages/@aws-cdk/aws-appmesh/lib/gateway-route.ts index 9eedb66b525e4..5bfb3b0ca21cd 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/gateway-route.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/gateway-route.ts @@ -140,7 +140,7 @@ export interface GatewayRouteAttributes { readonly gatewayRouteName: string; /** - * The name of the VirtualGateway this GatewayRoute is associated with + * The VirtualGateway this GatewayRoute is associated with. */ readonly virtualGateway: IVirtualGateway; } From f84669b74d06bc5ecd4b2f62c7d038f41213727f Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Wed, 4 Nov 2020 16:15:30 -0800 Subject: [PATCH 5/6] update README to contain information about importing resources --- packages/@aws-cdk/aws-appmesh/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/@aws-cdk/aws-appmesh/README.md b/packages/@aws-cdk/aws-appmesh/README.md index e38a8ea49a9e9..9e2a150fc9849 100644 --- a/packages/@aws-cdk/aws-appmesh/README.md +++ b/packages/@aws-cdk/aws-appmesh/README.md @@ -307,3 +307,21 @@ gateway.addGatewayRoute('gateway-route-grpc', { }), }); ``` + +## Importing Resources + +Each resource comes with two static methods for importing a reference to an existing App Mesh resource. +There are two static methods, `fromArn` and `fromAttributes` where the `` is replaced with the resource name. +These imported resources can be used as references for other resources in your mesh. + +```typescript +const arn = "arn:aws:appmesh:us-east-1:123456789012:mesh/testMesh/virtualNode/testNode"; +appmesh.VirtualNode.fromVirtualNodeArn(stack, 'importedVirtualNode', arn); +``` + +```typescript +appmesh.VirtualNode.fromVirtualNodeAttributes(stack, 'importedVirtualNode', { + mesh: appmesh.Mesh.fromMeshName(stack, 'Mesh', 'testMesh'), + virtualNodeName: virtualNodeName, +}); +``` From 30f27044e81c90fb8c5a2361dc774880a3e1b9f2 Mon Sep 17 00:00:00 2001 From: Dominic Fezzie Date: Wed, 4 Nov 2020 21:14:59 -0800 Subject: [PATCH 6/6] addresses feedback --- packages/@aws-cdk/aws-appmesh/README.md | 17 ++++++++++++++--- packages/@aws-cdk/aws-appmesh/lib/route.ts | 8 ++------ .../@aws-cdk/aws-appmesh/lib/virtual-gateway.ts | 5 +++-- .../@aws-cdk/aws-appmesh/lib/virtual-node.ts | 5 +++-- .../@aws-cdk/aws-appmesh/lib/virtual-router.ts | 5 +++-- .../@aws-cdk/aws-appmesh/lib/virtual-service.ts | 5 +++-- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/packages/@aws-cdk/aws-appmesh/README.md b/packages/@aws-cdk/aws-appmesh/README.md index 9e2a150fc9849..00d70bd8b0888 100644 --- a/packages/@aws-cdk/aws-appmesh/README.md +++ b/packages/@aws-cdk/aws-appmesh/README.md @@ -310,9 +310,9 @@ gateway.addGatewayRoute('gateway-route-grpc', { ## Importing Resources -Each resource comes with two static methods for importing a reference to an existing App Mesh resource. -There are two static methods, `fromArn` and `fromAttributes` where the `` is replaced with the resource name. +Each mesh resource comes with two static methods for importing a reference to an existing App Mesh resource. These imported resources can be used as references for other resources in your mesh. +There are two static methods, `fromArn` and `fromAttributes` where the `` is replaced with the resource name. ```typescript const arn = "arn:aws:appmesh:us-east-1:123456789012:mesh/testMesh/virtualNode/testNode"; @@ -320,8 +320,19 @@ appmesh.VirtualNode.fromVirtualNodeArn(stack, 'importedVirtualNode', arn); ``` ```typescript -appmesh.VirtualNode.fromVirtualNodeAttributes(stack, 'importedVirtualNode', { +appmesh.VirtualNode.fromVirtualNodeAttributes(stack, 'imported-virtual-node', { mesh: appmesh.Mesh.fromMeshName(stack, 'Mesh', 'testMesh'), virtualNodeName: virtualNodeName, }); ``` + +To import a mesh, there are two static methods, `fromMeshArn` and `fromMeshName`. + +```typescript +const arn = 'arn:aws:appmesh:us-east-1:123456789012:mesh/testMesh'; +appmesh.Mesh.fromMeshArn(stack, 'imported-mesh', arn); +``` + +```typescript +appmesh.Mesh.fromMeshName(stack, 'imported-mesh', 'abc'); +``` diff --git a/packages/@aws-cdk/aws-appmesh/lib/route.ts b/packages/@aws-cdk/aws-appmesh/lib/route.ts index bc47bce2615a1..cfa0e8e6d54c1 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/route.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/route.ts @@ -1,7 +1,7 @@ import * as cdk from '@aws-cdk/core'; import { Construct } from 'constructs'; import { CfnRoute } from './appmesh.generated'; -import { IMesh, Mesh } from './mesh'; +import { IMesh } from './mesh'; import { IVirtualNode } from './virtual-node'; import { IVirtualRouter, VirtualRouter } from './virtual-router'; @@ -121,11 +121,7 @@ export class Route extends cdk.Resource implements IRoute { public static fromRouteArn(scope: Construct, id: string, routeArn: string): IRoute { return new class extends cdk.Resource implements IRoute { readonly routeArn = routeArn; - readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(routeArn).resourceName!))); - readonly virtualRouter = VirtualRouter.fromVirtualRouterAttributes(this, 'VirtualRouter', { - mesh: this.mesh, - virtualRouterName: cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(routeArn).resourceName!)), - }); + readonly virtualRouter = VirtualRouter.fromVirtualRouterArn(this, 'VirtualRouter', routeArn); readonly routeName = cdk.Fn.select(4, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(routeArn).resourceName!)); }(scope, id); } diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts index 35aa90736cc15..337807c9153da 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts @@ -113,9 +113,10 @@ export class VirtualGateway extends VirtualGatewayBase { */ public static fromVirtualGatewayArn(scope: Construct, id: string, virtualGatewayArn: string): IVirtualGateway { return new class extends VirtualGatewayBase { - readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualGatewayArn).resourceName!))); + private readonly parsedArn = cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualGatewayArn).resourceName!); + readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, this.parsedArn)); readonly virtualGatewayArn = virtualGatewayArn; - readonly virtualGatewayName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualGatewayArn).resourceName!)); + readonly virtualGatewayName = cdk.Fn.select(2, this.parsedArn); }(scope, id); } diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts index 29933ef968ef9..b8b9da2026715 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts @@ -209,8 +209,9 @@ export class VirtualNode extends VirtualNodeBase { public static fromVirtualNodeArn(scope: Construct, id: string, virtualNodeArn: string): IVirtualNode { return new class extends VirtualNodeBase { readonly virtualNodeArn = virtualNodeArn; - readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualNodeArn).resourceName!))); - readonly virtualNodeName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualNodeArn).resourceName!)); + private readonly parsedArn = cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualNodeArn).resourceName!); + readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, this.parsedArn)); + readonly virtualNodeName = cdk.Fn.select(2, this.parsedArn); }(scope, id); } diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts index d6c7de8d2fb85..2b416427ae389 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-router.ts @@ -111,8 +111,9 @@ export class VirtualRouter extends VirtualRouterBase { public static fromVirtualRouterArn(scope: Construct, id: string, virtualRouterArn: string): IVirtualRouter { return new class extends VirtualRouterBase { readonly virtualRouterArn = virtualRouterArn; - readonly virtualRouterName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualRouterArn).resourceName!)); - readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualRouterArn).resourceName!))); + private readonly parsedArn = cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualRouterArn).resourceName!); + readonly virtualRouterName = cdk.Fn.select(2, this.parsedArn); + readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, this.parsedArn)); }(scope, id); } diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts index 16e13a12e79c0..374d342040784 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts @@ -83,8 +83,9 @@ export class VirtualService extends cdk.Resource implements IVirtualService { public static fromVirtualServiceArn(scope: Construct, id: string, virtualServiceArn: string): IVirtualService { return new class extends cdk.Resource implements IVirtualService { readonly virtualServiceArn = virtualServiceArn; - readonly virtualServiceName = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualServiceArn).resourceName!)); - readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualServiceArn).resourceName!))); + private readonly parsedArn = cdk.Fn.split('/', cdk.Stack.of(scope).parseArn(virtualServiceArn).resourceName!); + readonly virtualServiceName = cdk.Fn.select(2, this.parsedArn); + readonly mesh = Mesh.fromMeshName(this, 'Mesh', cdk.Fn.select(0, this.parsedArn)); }(scope, id); }