Skip to content

Commit

Permalink
allow the virtualServiceName field for virtual node backends to be su…
Browse files Browse the repository at this point in the history
…pplied as a string
  • Loading branch information
wplucinsky committed Nov 26, 2021
1 parent efaaaf5 commit dd01a40
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/@aws-cdk/aws-appmesh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ declare const mesh: appmesh.Mesh;
declare const router: appmesh.VirtualRouter;
declare const service: cloudmap.Service;

const virtualServiceName = 'service1.domain.local';
const node = new appmesh.VirtualNode(this, 'node', {
mesh,
serviceDiscovery: appmesh.ServiceDiscovery.cloudMap(service),
Expand All @@ -218,21 +219,20 @@ const node = new appmesh.VirtualNode(this, 'node', {
timeout: {
idle: cdk.Duration.seconds(5),
},
backends: [appmesh.Backend.virtualServiceName(virtualServiceName)]
})],
accessLog: appmesh.AccessLog.fromFilePath('/dev/stdout'),
});

const virtualService = new appmesh.VirtualService(this, 'service-1', {
virtualServiceProvider: appmesh.VirtualServiceProvider.virtualRouter(router),
virtualServiceName: 'service1.domain.local',
virtualServiceName,
});

node.addBackend(appmesh.Backend.virtualService(virtualService));
```

The `listeners` property can be left blank and added later with the `node.addListener()` method. The `serviceDiscovery` property must be specified when specifying a listener.

The `backends` property can be added with `node.addBackend()`. In the example, we define a virtual service and add it to the virtual node to allow egress traffic to other nodes.
The `backends` property can be added in the constructor or with `node.addBackend()`. In the example, we define a virtual service and add it to the virtual node to allow egress traffic to other nodes.

The `backendDefaults` property is added to the node while creating the virtual node. These are the virtual node's default settings for all backends.

Expand Down
36 changes: 36 additions & 0 deletions packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ export abstract class Backend {
return new VirtualServiceBackend(virtualService, props.tlsClientPolicy);
}

/**
* Construct a Virtual Service backend via a Virtual Service name
*/
public static virtualServiceName(virtualServiceName: string, props: VirtualServiceBackendOptions = {}): Backend {
return new VirtualServiceNameBackend(virtualServiceName, props.tlsClientPolicy);
}

/**
* Return backend config
*/
Expand Down Expand Up @@ -250,6 +257,35 @@ class VirtualServiceBackend extends Backend {
}
}

/**
* Represents the properties needed to define a Virtual Service backend via a Virtual Service name
*/
class VirtualServiceNameBackend extends Backend {

constructor (private readonly virtualServiceName: string,
private readonly tlsClientPolicy: TlsClientPolicy | undefined) {
super();
}

/**
* Return config for a Virtual Service backend
*/
public bind(scope: Construct): BackendConfig {
return {
virtualServiceBackend: {
virtualService: {
virtualServiceName: this.virtualServiceName,
clientPolicy: this.tlsClientPolicy
? {
tls: renderTlsClientPolicy(scope, this.tlsClientPolicy),
}
: undefined,
},
},
};
}
}

/**
* Connection pool properties for HTTP listeners
*/
Expand Down
45 changes: 45 additions & 0 deletions packages/@aws-cdk/aws-appmesh/test/virtual-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,53 @@ describe('virtual node', () => {
},
MeshOwner: ABSENT,
});
});

test('should add resource with service backends via virtual service names', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
const mesh = new appmesh.Mesh(stack, 'mesh', {
meshName: 'test-mesh',
});

const service1Name = 'service1.domain.local';
new appmesh.VirtualService(stack, 'service-1', {
virtualServiceName: service1Name,
virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh),
});
const service2Name = 'service2.domain.local';
new appmesh.VirtualService(stack, 'service-2', {
virtualServiceName: service2Name,
virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh),
});

const node = new appmesh.VirtualNode(stack, 'test-node', {
mesh,
serviceDiscovery: appmesh.ServiceDiscovery.dns('test'),
backends: [appmesh.Backend.virtualServiceName(service1Name)],
});

node.addBackend(appmesh.Backend.virtualServiceName(service2Name));

// THEN
expect(stack).toHaveResourceLike('AWS::AppMesh::VirtualNode', {
Spec: {
Backends: [
{
VirtualService: {
VirtualServiceName: service1Name,
},
},
{
VirtualService: {
VirtualServiceName: service2Name,
},
},
],
},
});
});
});

Expand Down

0 comments on commit dd01a40

Please sign in to comment.