Skip to content

Commit 384d887

Browse files
authored
Merge branch 'master' into epolon/self-managed-cluster-sg
2 parents 204b325 + bc5b9b6 commit 384d887

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

packages/@aws-cdk/aws-apigatewayv2/lib/http/api.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export interface IHttpApi extends IResource {
1818
*/
1919
readonly httpApiId: string;
2020

21+
/**
22+
* The default endpoint for an API
23+
* @attribute
24+
*/
25+
readonly apiEndpoint: string;
26+
2127
/**
2228
* The default stage
2329
*/
@@ -184,6 +190,7 @@ export interface AddRoutesOptions extends BatchHttpRouteOptions {
184190
abstract class HttpApiBase extends Resource implements IHttpApi { // note that this is not exported
185191

186192
public abstract readonly httpApiId: string;
193+
public abstract readonly apiEndpoint: string;
187194
private vpcLinks: Record<string, VpcLink> = {};
188195

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

243+
/**
244+
* Attributes for importing an HttpApi into the CDK
245+
*/
246+
export interface HttpApiAttributes {
247+
/**
248+
* The identifier of the HttpApi
249+
*/
250+
readonly httpApiId: string;
251+
/**
252+
* The endpoint URL of the HttpApi
253+
* @default - throws an error if apiEndpoint is accessed.
254+
*/
255+
readonly apiEndpoint?: string;
256+
}
257+
236258
/**
237259
* Create a new API Gateway HTTP API endpoint.
238260
* @resource AWS::ApiGatewayV2::Api
@@ -241,9 +263,17 @@ export class HttpApi extends HttpApiBase {
241263
/**
242264
* Import an existing HTTP API into this CDK app.
243265
*/
244-
public static fromApiId(scope: Construct, id: string, httpApiId: string): IHttpApi {
266+
public static fromHttpApiAttributes(scope: Construct, id: string, attrs: HttpApiAttributes): IHttpApi {
245267
class Import extends HttpApiBase {
246-
public readonly httpApiId = httpApiId;
268+
public readonly httpApiId = attrs.httpApiId;
269+
private readonly _apiEndpoint = attrs.apiEndpoint;
270+
271+
public get apiEndpoint(): string {
272+
if (!this._apiEndpoint) {
273+
throw new Error('apiEndpoint is not configured on the imported HttpApi.');
274+
}
275+
return this._apiEndpoint;
276+
}
247277
}
248278
return new Import(scope, id);
249279
}
@@ -252,13 +282,7 @@ export class HttpApi extends HttpApiBase {
252282
* A human friendly name for this HTTP API. Note that this is different from `httpApiId`.
253283
*/
254284
public readonly httpApiName?: string;
255-
256285
public readonly httpApiId: string;
257-
258-
/**
259-
* The default endpoint for an API
260-
* @attribute
261-
*/
262286
public readonly apiEndpoint: string;
263287

264288
/**

packages/@aws-cdk/aws-apigatewayv2/test/http/api.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ describe('HttpApi', () => {
2929

3030
test('import', () => {
3131
const stack = new Stack();
32-
const api = new HttpApi(stack, 'api', { apiName: 'customName' });
33-
const imported = HttpApi.fromApiId(stack, 'imported', api.httpApiId );
34-
35-
expect(imported.httpApiId).toEqual(api.httpApiId);
32+
const imported = HttpApi.fromHttpApiAttributes(stack, 'imported', { httpApiId: 'http-1234', apiEndpoint: 'api-endpoint' });
3633

34+
expect(imported.httpApiId).toEqual('http-1234');
35+
expect(imported.apiEndpoint).toEqual('api-endpoint');
3736
});
3837

3938
test('unsetting createDefaultStage', () => {
@@ -188,7 +187,7 @@ describe('HttpApi', () => {
188187
// GIVEN
189188
const stack = new Stack();
190189
const apiId = 'importedId';
191-
const api = HttpApi.fromApiId(stack, 'test-api', apiId);
190+
const api = HttpApi.fromHttpApiAttributes(stack, 'test-api', { httpApiId: apiId });
192191
const metricName = '4xxError';
193192
const statistic = 'Sum';
194193

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

262261
expect(api.apiEndpoint).toBeDefined();
263262
});
263+
264+
test('apiEndpoint for imported', () => {
265+
const stack = new Stack();
266+
const api = HttpApi.fromHttpApiAttributes(stack, 'imported', { httpApiId: 'api-1234' });
267+
268+
expect(() => api.apiEndpoint).toThrow(/apiEndpoint is not configured/);
269+
});
264270
});
265271

266272
class DummyRouteIntegration implements IHttpRouteIntegration {

0 commit comments

Comments
 (0)