Skip to content

Commit

Permalink
feat(Config Schema): Schema for provider props of AWS http event
Browse files Browse the repository at this point in the history
(PR #8383)
  • Loading branch information
Oz Weiss committed Oct 14, 2020
1 parent 4e49473 commit e51e0f2
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 165 deletions.
5 changes: 1 addition & 4 deletions lib/plugins/aws/lib/naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,7 @@ module.exports = {

// API Gateway
getApiGatewayName() {
if (
this.provider.serverless.service.provider.apiName &&
typeof this.provider.serverless.service.provider.apiName === 'string'
) {
if (this.provider.serverless.service.provider.apiName) {
return `${this.provider.serverless.service.provider.apiName}`;
}
return `${this.provider.getStage()}-${this.provider.serverless.service.service}`;
Expand Down
21 changes: 0 additions & 21 deletions lib/plugins/aws/package/compile/events/apiGateway/lib/apiKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,8 @@ function createApiKeyResource(that, apiKey) {
}

module.exports = {
validateApiKeyInput(apiKey) {
if (_.isObject(apiKey) && (apiKey.name != null || apiKey.value != null)) {
return true;
} else if (typeof apiKey !== 'string') {
return false;
}
return true;
},
compileApiKeys() {
if (this.serverless.service.provider.apiKeys) {
if (!Array.isArray(this.serverless.service.provider.apiKeys)) {
throw new this.serverless.classes.Error('apiKeys property must be an array');
}
const resources = this.serverless.service.provider.compiledCloudFormationTemplate.Resources;
let keyNumber = 0;
this.serverless.service.provider.apiKeys.forEach(apiKeyDefinition => {
Expand All @@ -57,11 +46,6 @@ module.exports = {
) {
keyNumber = 0;
apiKeyDefinition[name].forEach(key => {
if (!this.validateApiKeyInput(key)) {
throw new this.serverless.classes.Error(
'API Key must be a string or an object which contains name and/or value'
);
}
keyNumber += 1;
const apiKeyLogicalId = this.provider.naming.getApiKeyLogicalId(keyNumber, name);
const resourceTemplate = createApiKeyResource(this, key);
Expand All @@ -71,11 +55,6 @@ module.exports = {
});
} else {
keyNumber += 1;
if (!this.validateApiKeyInput(apiKeyDefinition)) {
throw new this.serverless.classes.Error(
'API Key must be a string or an object which contains name and/or value'
);
}
const apiKeyLogicalId = this.provider.naming.getApiKeyLogicalId(keyNumber);
const resourceTemplate = createApiKeyResource(this, apiKeyDefinition);
_.merge(resources, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,4 @@ describe('#compileApiKeys()', () => {
});
});
});

it('throw error if an apiKey is not a valid object', () => {
awsCompileApigEvents.serverless.service.provider.apiKeys = [
{
named: 'invalid',
},
];
expect(() => awsCompileApigEvents.compileApiKeys()).to.throw(Error);
});
});
34 changes: 1 addition & 33 deletions lib/plugins/aws/package/compile/events/apiGateway/lib/restApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,7 @@ module.exports = {
}

if (this.serverless.service.provider.endpointType) {
const validEndpointTypes = ['REGIONAL', 'EDGE', 'PRIVATE'];
endpointType = this.serverless.service.provider.endpointType;

if (typeof endpointType !== 'string') {
throw new this.serverless.classes.Error('endpointType must be a string');
}

if (!validEndpointTypes.includes(endpointType.toUpperCase())) {
const message =
'endpointType must be one of "REGIONAL" or "EDGE" or "PRIVATE". ' +
`You provided ${endpointType}.`;
throw new this.serverless.classes.Error(message);
}
endpointType = endpointType.toUpperCase();
endpointType = this.serverless.service.provider.endpointType.toUpperCase();

if (this.serverless.service.provider.vpcEndpointIds) {
vpcEndpointIds = this.serverless.service.provider.vpcEndpointIds;
Expand All @@ -45,10 +32,6 @@ module.exports = {
'VPC endpoint IDs are only available for private APIs'
);
}

if (!Array.isArray(vpcEndpointIds)) {
throw new this.serverless.classes.Error('vpcEndpointIds must be an array');
}
}
}

Expand Down Expand Up @@ -100,14 +83,6 @@ module.exports = {

if (apiGateway.apiKeySourceType) {
const apiKeySourceType = apiGateway.apiKeySourceType.toUpperCase();
const validApiKeySourceType = ['HEADER', 'AUTHORIZER'];

if (!validApiKeySourceType.includes(apiKeySourceType)) {
const message =
'apiKeySourceType must be one of "HEADER" or "AUTHORIZER". ' +
`You provided ${apiKeySourceType}.`;
return BbPromise.reject(new this.serverless.classes.Error(message));
}

_.merge(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources[
Expand All @@ -120,13 +95,6 @@ module.exports = {
if (apiGateway.minimumCompressionSize) {
const minimumCompressionSize = apiGateway.minimumCompressionSize;

if (minimumCompressionSize < 0 || minimumCompressionSize > 10485760) {
const message =
'minimumCompressionSize must be between 0 and 10485760. ' +
`You provided ${minimumCompressionSize}.`;
return BbPromise.reject(new this.serverless.classes.Error(message));
}

_.merge(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources[
this.apiGatewayRestApiLogicalId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,79 +154,9 @@ describe('#compileRestApi()', () => {
});
});

it('throw error if endpointType property is not a string', () => {
awsCompileApigEvents.serverless.service.provider.endpointType = ['EDGE'];
expect(() => awsCompileApigEvents.compileRestApi()).to.throw(Error);
});

it('should compile if endpointType property is REGIONAL', () => {
awsCompileApigEvents.serverless.service.provider.endpointType = 'REGIONAL';
expect(() => awsCompileApigEvents.compileRestApi()).to.not.throw(Error);
});

it('should compile if endpointType property is PRIVATE', () => {
awsCompileApigEvents.serverless.service.provider.endpointType = 'PRIVATE';
expect(() => awsCompileApigEvents.compileRestApi()).to.not.throw(Error);
});

it('should compile if endpointType property is PRIVATE and vpcEndpointIds property is [id1]', () => {
awsCompileApigEvents.serverless.service.provider.endpointType = 'PRIVATE';
awsCompileApigEvents.serverless.service.provider.vpcEndpointIds = ['id1'];
expect(() => awsCompileApigEvents.compileRestApi()).to.not.throw(Error);
});

it('should throw error if endpointType property is PRIVATE and vpcEndpointIds property is not an array', () => {
awsCompileApigEvents.serverless.service.provider.endpointType = 'PRIVATE';
awsCompileApigEvents.serverless.service.provider.vpcEndpointIds = 'id1';
expect(() => awsCompileApigEvents.compileRestApi()).to.throw(Error);
});

it('should throw error if endpointType property is not PRIVATE and vpcEndpointIds property is [id1]', () => {
awsCompileApigEvents.serverless.service.provider.endpointType = 'Testing';
awsCompileApigEvents.serverless.service.provider.vpcEndpointIds = ['id1'];
expect(() => awsCompileApigEvents.compileRestApi()).to.throw(Error);
});

it('throw error if endpointType property is not EDGE or REGIONAL', () => {
awsCompileApigEvents.serverless.service.provider.endpointType = 'Testing';
expect(() => awsCompileApigEvents.compileRestApi()).to.throw('endpointType must be one of');
});

it('should compile correctly if apiKeySourceType property is HEADER', () => {
awsCompileApigEvents.serverless.service.provider.apiGateway = { apiKeySourceType: 'HEADER' };
expect(() => awsCompileApigEvents.compileRestApi()).to.not.throw(Error);
});

it('should compile correctly if apiKeySourceType property is AUTHORIZER', () => {
awsCompileApigEvents.serverless.service.provider.apiGateway = {
apiKeySourceType: 'AUTHORIZER',
};
expect(() => awsCompileApigEvents.compileRestApi()).to.not.throw(Error);
});

it('throw error if apiKeySourceType is not HEADER or AUTHORIZER', () => {
awsCompileApigEvents.serverless.service.provider.apiGateway = { apiKeySourceType: 'Testing' };
return expect(awsCompileApigEvents.compileRestApi()).to.be.rejectedWith(Error);
});

it('should compile correctly if minimumCompressionSize is an integer', () => {
awsCompileApigEvents.serverless.service.provider.apiGateway = {
minimumCompressionSize: 1024,
};
expect(() => awsCompileApigEvents.compileRestApi()).to.not.throw(Error);
});

it('should throw error if minimumCompressionSize is less than 0', () => {
awsCompileApigEvents.serverless.service.provider.apiGateway = {
minimumCompressionSize: -1,
};
return expect(awsCompileApigEvents.compileRestApi()).to.be.rejectedWith(Error);
});

it('should throw error if minimumCompressionSize is greater than 10485760', () => {
awsCompileApigEvents.serverless.service.provider.apiGateway = {
minimumCompressionSize: 10485761,
};
return expect(awsCompileApigEvents.compileRestApi()).to.be.rejectedWith(Error);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const _ = require('lodash');
const BbPromise = require('bluebird');
const apiKeys = require('./apiKeys');

function createUsagePlanKeyResource(that, usagePlanLogicalId, keyNumber, keyName) {
const apiKeyLogicalId = that.provider.naming.getApiKeyLogicalId(keyNumber, keyName);
Expand All @@ -26,10 +25,6 @@ function createUsagePlanKeyResource(that, usagePlanLogicalId, keyNumber, keyName
module.exports = {
compileUsagePlanKeys() {
if (this.serverless.service.provider.apiKeys) {
if (!Array.isArray(this.serverless.service.provider.apiKeys)) {
throw new this.serverless.classes.Error('apiKeys property must be an array');
}

const resources = this.serverless.service.provider.compiledCloudFormationTemplate.Resources;
let keyNumber = 0;

Expand All @@ -48,12 +43,7 @@ module.exports = {
}
if (_.isObject(apiKeyDefinition) && usagePlansIncludeName) {
keyNumber = 0;
apiKeyDefinition[name].forEach(key => {
if (!apiKeys.validateApiKeyInput(key)) {
throw new this.serverless.classes.Error(
'API Key must be a string or an object which contains name and/or value'
);
}
apiKeyDefinition[name].forEach(() => {
keyNumber += 1;
const usagePlanKeyLogicalId = this.provider.naming.getUsagePlanKeyLogicalId(
keyNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,5 @@ describe('#compileUsagePlanKeys()', () => {
/has no usage plan defined/
);
});

it('should throw if api key definitions are not strings or objects', () => {
awsCompileApigEvents.apiGatewayUsagePlanNames = ['free'];
awsCompileApigEvents.serverless.service.provider.apiKeys = [{ free: [{ foo: 'bar' }] }];
expect(() => awsCompileApigEvents.compileUsagePlanKeys()).to.throw(
/must be a string or an object/
);
});
});
});

0 comments on commit e51e0f2

Please sign in to comment.