Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Niranjan Jayakar committed Oct 8, 2020
1 parent e1608d7 commit 1e87d57
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
14 changes: 10 additions & 4 deletions packages/@aws-cdk/aws-apigateway/lib/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export interface IntegrationOptions {

/**
* The type of network connection to the integration endpoint.
* @default ConnectionType.Internet
* @default - ConnectionType.VPC_LINK if `vpcLink` property is configured; ConnectionType.Internet otherwise.
*/
readonly connectionType?: ConnectionType;

Expand Down Expand Up @@ -201,10 +201,13 @@ export class Integration {
*/
public bind(_method: Method): IntegrationConfig {
let uri = this.props.uri;
if (this.props.options?.connectionType === ConnectionType.VPC_LINK && uri === undefined) {
const options = this.props.options;

if (options?.connectionType === ConnectionType.VPC_LINK && uri === undefined) {
uri = Lazy.stringValue({
// needs to be a lazy since the targets can be added to the VpcLink construct after initialization.
produce: () => {
const vpcLink = this.props.options?.vpcLink;
const vpcLink = options.vpcLink;
if (vpcLink instanceof VpcLink) {
const targets = vpcLink._targetDnsNames;
if (targets.length > 1) {
Expand All @@ -219,7 +222,10 @@ export class Integration {
});
}
return {
options: this.props.options,
options: {
...options,
connectionType: options?.vpcLink ? ConnectionType.VPC_LINK : options?.connectionType,
},
type: this.props.type,
uri,
integrationHttpMethod: this.props.integrationHttpMethod,
Expand Down
5 changes: 4 additions & 1 deletion packages/@aws-cdk/aws-apigateway/lib/vpc-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ export class VpcLink extends Resource implements IVpcLink {
this._targets.push(...targets);
}

/** @internal */
/**
* Return the list of DNS names from the target NLBs.
* @internal
* */
public get _targetDnsNames(): string[] {
return this._targets.map(t => t.loadBalancerDnsName);
}
Expand Down
58 changes: 57 additions & 1 deletion packages/@aws-cdk/aws-apigateway/test/test.integration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, haveResourceLike } from '@aws-cdk/assert';
import { ABSENT, expect, haveResourceLike } from '@aws-cdk/assert';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2';
import * as iam from '@aws-cdk/aws-iam';
Expand Down Expand Up @@ -139,4 +139,60 @@ export = {
}), /cannot set 'vpcLink' where 'connectionType' is INTERNET/);
test.done();
},

'connectionType is ABSENT when vpcLink is not specified'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const api = new apigw.RestApi(stack, 'restapi');

// WHEN
const integration = new apigw.Integration({
type: apigw.IntegrationType.HTTP_PROXY,
integrationHttpMethod: 'ANY',
});
api.root.addMethod('ANY', integration);

// THEN
expect(stack).to(haveResourceLike('AWS::ApiGateway::Method', {
HttpMethod: 'ANY',
Integration: {
ConnectionType: ABSENT,
},
}));

test.done();
},

'connectionType defaults to VPC_LINK if vpcLink is configured'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const nlb = new elbv2.NetworkLoadBalancer(stack, 'NLB', {
vpc,
});
const link = new apigw.VpcLink(stack, 'link', {
targets: [nlb],
});
const api = new apigw.RestApi(stack, 'restapi');

// WHEN
const integration = new apigw.Integration({
type: apigw.IntegrationType.HTTP_PROXY,
integrationHttpMethod: 'ANY',
options: {
vpcLink: link,
},
});
api.root.addMethod('ANY', integration);

// THEN
expect(stack).to(haveResourceLike('AWS::ApiGateway::Method', {
HttpMethod: 'ANY',
Integration: {
ConnectionType: 'VPC_LINK',
},
}));

test.done();
},
};

0 comments on commit 1e87d57

Please sign in to comment.