Skip to content

Commit

Permalink
feat(AWS API Gateway): Error on tracing or logs set for external API
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Enabling logs or tracing for imported API Gateway will
now result in an error instead of warning
  • Loading branch information
pgrzesik authored and medikoo committed Jan 27, 2022
1 parent 1f44227 commit 64ea6e5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 33 deletions.
30 changes: 0 additions & 30 deletions lib/plugins/aws/package/compile/events/apiGateway/index.js
Expand Up @@ -236,36 +236,6 @@ class AwsCompileApigEvents {
this.createRequestValidator = memoize(this.createRequestValidator.bind(this));

this.hooks = {
'initialize': () => {
if (
this.serverless.service.provider.name === 'aws' &&
this.serverless.service.provider.apiGateway &&
this.serverless.service.provider.apiGateway.restApiId &&
this.serverless.service.provider.tracing &&
this.serverless.service.provider.tracing.apiGateway != null
) {
this.serverless._logDeprecation(
'AWS_API_GATEWAY_NON_APPLICABLE_SETTINGS',
'When external API Gateway resource is imported via ' +
'`provider.apiGateway.restApiId`, property ' +
'"provider.tracing.apiGateway" will be ignored.'
);
}
if (
this.serverless.service.provider.name === 'aws' &&
this.serverless.service.provider.apiGateway &&
this.serverless.service.provider.apiGateway.restApiId &&
this.serverless.service.provider.logs &&
this.serverless.service.provider.logs.restApi != null
) {
this.serverless._logDeprecation(
'AWS_API_GATEWAY_NON_APPLICABLE_SETTINGS',
'When external API Gateway resource is imported via ' +
'`provider.apiGateway.restApiId`, property ' +
'"provider.logs.restApi" will be ignored.'
);
}
},
'package:compileEvents': async () => {
this.validated = this.validate();

Expand Down
24 changes: 24 additions & 0 deletions lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js
Expand Up @@ -50,6 +50,30 @@ module.exports = {
);
}

if (
this.serverless.service.provider.apiGateway &&
this.serverless.service.provider.apiGateway.restApiId &&
this.serverless.service.provider.tracing &&
this.serverless.service.provider.tracing.apiGateway != null
) {
throw new ServerlessError(
'When external API Gateway resource is imported via "provider.apiGateway.restApiId", property "provider.tracing.apiGateway" is ineffective.',
'API_GATEWAY_EXTERNAL_API_TRACING'
);
}

if (
this.serverless.service.provider.apiGateway &&
this.serverless.service.provider.apiGateway.restApiId &&
this.serverless.service.provider.logs &&
this.serverless.service.provider.logs.restApi != null
) {
throw new ServerlessError(
'When external API Gateway resource is imported via "provider.apiGateway.restApiId", property "provider.logs.restApi" is ineffective.',
'API_GATEWAY_EXTERNAL_API_LOGS'
);
}

Object.entries(this.serverless.service.functions).forEach(([functionName, functionObject]) => {
(functionObject.events || []).forEach((event) => {
if (event.http) {
Expand Down
@@ -1,15 +1,17 @@
'use strict';

const expect = require('chai').expect;
const chai = require('chai');
const sinon = require('sinon');
const runServerless = require('../../../../../../../../../utils/run-serverless');
const AwsCompileApigEvents = require('../../../../../../../../../../lib/plugins/aws/package/compile/events/apiGateway/index');
const Serverless = require('../../../../../../../../../../lib/Serverless');
const AwsProvider = require('../../../../../../../../../../lib/plugins/aws/provider');
const ServerlessError = require('../../../../../../../../../../lib/serverless-error');

const chai = require('chai');
chai.use(require('chai-as-promised'));
chai.use(require('sinon-chai'));

const expect = chai.expect;

describe('#validate()', () => {
let serverless;
Expand Down Expand Up @@ -1529,7 +1531,6 @@ describe('test/unit/lib/plugins/aws/package/compile/events/apiGateway/lib/valida
'API_GATEWAY_MISSING_REST_API_ROOT_RESOURCE_ID'
);
});

it('should throw when using a CUSTOM authorizer without an authorizer id', async () => {
await expect(
runServerless({
Expand Down Expand Up @@ -1592,4 +1593,44 @@ describe('test/unit/lib/plugins/aws/package/compile/events/apiGateway/lib/valida
const resource = getApiGatewayMethod('/custom-authorizer', 'POST');
expect(resource.Properties.AuthorizationType).to.equal('CUSTOM');
});

it('Should error when using external API Gateway and enabling tracing', async () => {
await expect(
runServerless({
fixture: 'apiGateway',
command: 'package',
configExt: {
provider: {
apiGateway: {
restApiId: 'xxx',
restApiRootResourceId: 'yyy',
},
tracing: {
apiGateway: true,
},
},
},
})
).to.be.eventually.rejected.and.have.property('code', 'API_GATEWAY_EXTERNAL_API_TRACING');
});

it('Should error when using external API Gateway and enabling logs', async () => {
await expect(
runServerless({
fixture: 'apiGateway',
command: 'package',
configExt: {
provider: {
apiGateway: {
restApiId: 'xxx',
restApiRootResourceId: 'yyy',
},
logs: {
restApi: true,
},
},
},
})
).to.be.eventually.rejected.and.have.property('code', 'API_GATEWAY_EXTERNAL_API_LOGS');
});
});

0 comments on commit 64ea6e5

Please sign in to comment.