Skip to content

Commit

Permalink
chore(apigatewayv2): apiEndpoint is elevated to the IHttpApi interface (
Browse files Browse the repository at this point in the history
aws#11988)

This was originally a miss from a community contributed PR.
`apiEndpoint` is a CloudFormation attribute and should be elevated into
the `IHttpApi` interface.

BREAKING CHANGE: `HttpApi.fromApiId()` has been replaced with
`HttpApi.fromHttpApiAttributes()`.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Niranjan Jayakar authored Dec 15, 2020
1 parent bab21b3 commit bc5b9b6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
40 changes: 32 additions & 8 deletions packages/@aws-cdk/aws-apigatewayv2/lib/http/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export interface IHttpApi extends IResource {
*/
readonly httpApiId: string;

/**
* The default endpoint for an API
* @attribute
*/
readonly apiEndpoint: string;

/**
* The default stage
*/
Expand Down Expand Up @@ -184,6 +190,7 @@ export interface AddRoutesOptions extends BatchHttpRouteOptions {
abstract class HttpApiBase extends Resource implements IHttpApi { // note that this is not exported

public abstract readonly httpApiId: string;
public abstract readonly apiEndpoint: string;
private vpcLinks: Record<string, VpcLink> = {};

public metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
Expand Down Expand Up @@ -233,6 +240,21 @@ abstract class HttpApiBase extends Resource implements IHttpApi { // note that t
}
}

/**
* Attributes for importing an HttpApi into the CDK
*/
export interface HttpApiAttributes {
/**
* The identifier of the HttpApi
*/
readonly httpApiId: string;
/**
* The endpoint URL of the HttpApi
* @default - throws an error if apiEndpoint is accessed.
*/
readonly apiEndpoint?: string;
}

/**
* Create a new API Gateway HTTP API endpoint.
* @resource AWS::ApiGatewayV2::Api
Expand All @@ -241,9 +263,17 @@ export class HttpApi extends HttpApiBase {
/**
* Import an existing HTTP API into this CDK app.
*/
public static fromApiId(scope: Construct, id: string, httpApiId: string): IHttpApi {
public static fromHttpApiAttributes(scope: Construct, id: string, attrs: HttpApiAttributes): IHttpApi {
class Import extends HttpApiBase {
public readonly httpApiId = httpApiId;
public readonly httpApiId = attrs.httpApiId;
private readonly _apiEndpoint = attrs.apiEndpoint;

public get apiEndpoint(): string {
if (!this._apiEndpoint) {
throw new Error('apiEndpoint is not configured on the imported HttpApi.');
}
return this._apiEndpoint;
}
}
return new Import(scope, id);
}
Expand All @@ -252,13 +282,7 @@ export class HttpApi extends HttpApiBase {
* A human friendly name for this HTTP API. Note that this is different from `httpApiId`.
*/
public readonly httpApiName?: string;

public readonly httpApiId: string;

/**
* The default endpoint for an API
* @attribute
*/
public readonly apiEndpoint: string;

/**
Expand Down
16 changes: 11 additions & 5 deletions packages/@aws-cdk/aws-apigatewayv2/test/http/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ describe('HttpApi', () => {

test('import', () => {
const stack = new Stack();
const api = new HttpApi(stack, 'api', { apiName: 'customName' });
const imported = HttpApi.fromApiId(stack, 'imported', api.httpApiId );

expect(imported.httpApiId).toEqual(api.httpApiId);
const imported = HttpApi.fromHttpApiAttributes(stack, 'imported', { httpApiId: 'http-1234', apiEndpoint: 'api-endpoint' });

expect(imported.httpApiId).toEqual('http-1234');
expect(imported.apiEndpoint).toEqual('api-endpoint');
});

test('unsetting createDefaultStage', () => {
Expand Down Expand Up @@ -188,7 +187,7 @@ describe('HttpApi', () => {
// GIVEN
const stack = new Stack();
const apiId = 'importedId';
const api = HttpApi.fromApiId(stack, 'test-api', apiId);
const api = HttpApi.fromHttpApiAttributes(stack, 'test-api', { httpApiId: apiId });
const metricName = '4xxError';
const statistic = 'Sum';

Expand Down Expand Up @@ -261,6 +260,13 @@ describe('HttpApi', () => {

expect(api.apiEndpoint).toBeDefined();
});

test('apiEndpoint for imported', () => {
const stack = new Stack();
const api = HttpApi.fromHttpApiAttributes(stack, 'imported', { httpApiId: 'api-1234' });

expect(() => api.apiEndpoint).toThrow(/apiEndpoint is not configured/);
});
});

class DummyRouteIntegration implements IHttpRouteIntegration {
Expand Down

0 comments on commit bc5b9b6

Please sign in to comment.