Skip to content

Commit

Permalink
feat(Config Schema): Schema for AWS alexaSmartHome event (#8255)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oz Weiss committed Sep 24, 2020
1 parent 8409d61 commit bd5099e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 48 deletions.
30 changes: 12 additions & 18 deletions lib/plugins/aws/package/compile/events/alexaSmartHome/index.js
Expand Up @@ -11,9 +11,19 @@ class AwsCompileAlexaSmartHomeEvents {
'package:compileEvents': this.compileAlexaSmartHomeEvents.bind(this),
};

// TODO: Complete schema, see https://github.com/serverless/serverless/issues/8024
this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'alexaSmartHome', {
anyOf: [{ type: 'string' }, { type: 'object' }],
anyOf: [
{ $ref: '#/definitions/awsAlexaEventToken' },
{
type: 'object',
properties: {
appId: { $ref: '#/definitions/awsAlexaEventToken' },
enabled: { type: 'boolean' },
},
required: ['appId'],
additionalProperties: false,
},
],
});
}

Expand All @@ -30,15 +40,6 @@ class AwsCompileAlexaSmartHomeEvents {
let Action;

if (typeof event.alexaSmartHome === 'object') {
if (!event.alexaSmartHome.appId) {
const errorMessage = [
`Missing "appId" property for alexaSmartHome event in function ${functionName}`,
' The correct syntax is: appId: amzn1.ask.skill.xxxx-xxxx',
' OR an object with "appId" property.',
' Please check the docs for more info.',
].join('');
throw new this.serverless.classes.Error(errorMessage);
}
EventSourceToken = event.alexaSmartHome.appId;
Action =
event.alexaSmartHome.enabled !== false
Expand All @@ -47,13 +48,6 @@ class AwsCompileAlexaSmartHomeEvents {
} else if (typeof event.alexaSmartHome === 'string') {
EventSourceToken = event.alexaSmartHome;
Action = 'lambda:InvokeFunction';
} else {
const errorMessage = [
`Alexa Smart Home event of function "${functionName}" is not an object or string.`,
' The correct syntax is: alexaSmartHome.',
' Please check the docs for more info.',
].join('');
throw new this.serverless.classes.Error(errorMessage);
}
const lambdaLogicalId = this.provider.naming.getLambdaLogicalId(functionName);

Expand Down
Expand Up @@ -23,36 +23,6 @@ describe('AwsCompileAlexaSmartHomeEvents', () => {
});

describe('#compileAlexaSmartHomeEvents()', () => {
it('should throw an error if alexaSmartHome event type is not a string or an object', () => {
awsCompileAlexaSmartHomeEvents.serverless.service.functions = {
first: {
events: [
{
alexaSmartHome: 42,
},
],
},
};

expect(() => awsCompileAlexaSmartHomeEvents.compileAlexaSmartHomeEvents()).to.throw(Error);
});

it('should throw an error if the "appId" property is not given', () => {
awsCompileAlexaSmartHomeEvents.serverless.service.functions = {
first: {
events: [
{
alexaSmartHome: {
appId: null,
},
},
],
},
};

expect(() => awsCompileAlexaSmartHomeEvents.compileAlexaSmartHomeEvents()).to.throw(Error);
});

it('should create corresponding resources when alexaSmartHome events are given', () => {
awsCompileAlexaSmartHomeEvents.serverless.service.functions = {
first: {
Expand Down
6 changes: 6 additions & 0 deletions lib/plugins/aws/provider/awsProvider.js
Expand Up @@ -142,6 +142,12 @@ class AwsProvider {
// TODO: Complete schema, see https://github.com/serverless/serverless/issues/8016
serverless.configSchemaHandler.defineProvider('aws', {
definitions: {
awsAlexaEventToken: {
type: 'string',
minLength: 0,
maxLength: 256,
pattern: '^[a-zA-Z0-9._\\-]+$',
},
awsArn: {
oneOf: [
{ $ref: '#/definitions/awsArnString' },
Expand Down

0 comments on commit bd5099e

Please sign in to comment.