serverless-sam
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -60,2 +60,6 @@ /* | ||
if (http.path.charAt(0) != "/") { | ||
http.path = "/" + http.path; | ||
} | ||
this.addHttpMethod(http, targetResourceName); | ||
@@ -68,6 +72,6 @@ | ||
Properties: { | ||
StageName: DEFAULT_STAGE_NAME, | ||
StageName: this.serverless.service.provider.stage || DEFAULT_STAGE_NAME, | ||
DefinitionBody: this.apiDefinition | ||
} | ||
} | ||
}; | ||
@@ -89,15 +93,12 @@ let returnEvent = { | ||
addHttpMethod(httpConfig, targetResourceName) { | ||
let httpPath = httpConfig.path; | ||
if (httpPath.charAt(0) != "/") { | ||
httpPath = "/" + httpPath; | ||
} | ||
let httpMethod = httpConfig.method.toLowerCase(); | ||
if (!this.apiDefinition.paths[httpPath]) { | ||
this.apiDefinition.paths[httpPath] = {}; | ||
if (!this.apiDefinition.paths[httpConfig.path]) { | ||
this.apiDefinition.paths[httpConfig.path] = {}; | ||
} | ||
if (this.apiDefinition.paths[httpPath][httpMethod] && Object.keys(this.apiDefinition.paths[httpPath][httpMethod]).length > 0) { | ||
throw new Error("Error while generating Swagger definition: " + | ||
"Method " + httpMethod + " already exists on " + httpPath + " resource"); | ||
if (this.apiDefinition.paths[httpConfig.path][httpMethod] && Object.keys(this.apiDefinition.paths[httpConfig.path][httpMethod]).length > 0) { | ||
throw new Error("Error while generating Swagger definition: " + | ||
"Method " + httpMethod + " already exists on " + httpConfig.path + " resource"); | ||
} | ||
@@ -119,3 +120,3 @@ | ||
let headers = optionsMethod["x-amazon-apigateway-integration"].responses.default.responseParameters["method.response.header.Access-Control-Allow-Headers"]; | ||
let methods = Object.keys(this.apiDefinition.paths[httpPath]).concat([httpMethod]).join(","); | ||
let methods = Object.keys(this.apiDefinition.paths[httpConfig.path]).concat([httpMethod]).join(","); | ||
let origins = optionsMethod["x-amazon-apigateway-integration"].responses.default.responseParameters["method.response.header.Access-Control-Allow-Origin"]; | ||
@@ -136,3 +137,3 @@ if (typeof(httpConfig.cors) === "object") { | ||
this.apiDefinition.paths[httpPath]["options"] = optionsMethod; | ||
this.apiDefinition.paths[httpConfig.path]["options"] = optionsMethod; | ||
} | ||
@@ -147,3 +148,3 @@ | ||
this.apiDefinition.paths[httpPath][httpMethod] = methodConfig; | ||
this.apiDefinition.paths[httpConfig.path][httpMethod] = methodConfig; | ||
} | ||
@@ -150,0 +151,0 @@ |
@@ -29,3 +29,3 @@ /* | ||
convertEvent(event, targetResourceName) { | ||
this.eventType = event.type.charAt(0).toUpperCase() + serverlessEvent.type.slice(1); | ||
this.eventType = event.type.charAt(0).toUpperCase() + event.type.slice(1); | ||
let streamEvent = { | ||
@@ -50,2 +50,2 @@ Stream: event.arn | ||
module.exports = StreamEventConverter; | ||
module.exports = StreamEventConverter; |
@@ -34,6 +34,12 @@ /* | ||
serverlessFunctionToSam(resourceName, serverlessFunction) { | ||
let handlerArray = serverlessFunction.handler.split(path.sep); | ||
const lambdaHandler = handlerArray[handlerArray.length - 1]; | ||
handlerArray.pop(); | ||
let codeUri = path.resolve(handlerArray.join(path.sep)); | ||
let lambdaHandler = serverlessFunction.handler; | ||
let codeUri = this.serverless.service.package.artifact; | ||
// looks like this is not required. Commenting out for the time being | ||
/*if (!codeUri) { | ||
let handlerArray = serverlessFunction.handler.split(path.sep); | ||
lambdaHandler = handlerArray[handlerArray.length - 1]; | ||
handlerArray.pop(); | ||
codeUri = path.resolve(handlerArray.join(path.sep)); | ||
}*/ | ||
@@ -43,7 +49,2 @@ // begin building the function object | ||
// custom artifact for Java | ||
if (this.serverless.service.package.artifact) { | ||
codeUri = this.serverless.service.package.artifact; | ||
} | ||
const runtime = this.serverless.service.provider.runtime; | ||
@@ -65,7 +66,6 @@ | ||
// set environment variables if there are any | ||
if (this.serverless.service.provider.environment && Object.keys(this.serverless.service.provider.environment).length > 0) { | ||
Object.keys(this.serverless.service.provider.environment).forEach((key, idx) => { | ||
samFunctionBuilder = samFunctionBuilder.setEnvironmentVariable(key, this.serverless.service.provider.environment[key]); | ||
}) | ||
} | ||
let environment = Object.assign({}, this.serverless.service.provider.environment, serverlessFunction.environment); | ||
Object.keys(environment).forEach((key, idx) => { | ||
samFunctionBuilder = samFunctionBuilder.setEnvironmentVariable(key, environment[key]); | ||
}) | ||
@@ -103,3 +103,4 @@ // if we have a role defined then we just set it | ||
// add the event ot the function | ||
samFunctionBuilder = samFunctionBuilder.addEvent(eventConverter.getEventType(), convertedEvent.event); | ||
let eventType = eventConverter.getEventType(); | ||
samFunctionBuilder = samFunctionBuilder.addEvent(eventType, convertedEvent.event); | ||
@@ -112,2 +113,7 @@ // if the event converter needed to generate new resources for the template, loop over them and add them as custom resources | ||
} | ||
// if eventType is Api, add appropriate permission | ||
if (eventType === "Api") { | ||
this.samBuilder.addLambdaPermission(resourceName); | ||
} | ||
}); | ||
@@ -114,0 +120,0 @@ }); |
@@ -59,5 +59,5 @@ /* | ||
if (this.Policies == null) { | ||
this.Policies = []; | ||
this.Policies.push(policyDocument); | ||
this.Policies = [{ Version: '2012-10-17', Statement: []}]; | ||
} | ||
this.Policies[0].Statement.push(policyDocument); | ||
@@ -64,0 +64,0 @@ return this; |
@@ -60,2 +60,16 @@ /* | ||
addLambdaPermission(resourceName) { | ||
this.addCustomResource(`${resourceName}LambdaPermission`, | ||
{ | ||
Type: 'AWS::Lambda::Permission', | ||
DependsOn: [resourceName], | ||
Properties: { | ||
Action: 'lambda:InvokeFunction', | ||
FunctionName: { Ref: resourceName }, | ||
Principal: 'apigateway.amazonaws.com', | ||
}, | ||
}, | ||
false); | ||
} | ||
addOutput(logicalId, obj) { | ||
@@ -62,0 +76,0 @@ if (!this.outputs) { |
@@ -75,5 +75,5 @@ /* | ||
this.serverless.cli.log("Exporting outputs"); | ||
if (service.Outputs) { | ||
Object.keys(service.Outputs).forEach((id, idx) => { | ||
this.samBuilder.addOutput(id, service.Outputs[id]); | ||
if (service.resources && service.resources.Outputs) { | ||
Object.keys(service.resources.Outputs).forEach((id, idx) => { | ||
this.samBuilder.addOutput(id, service.resources.Outputs[id]); | ||
}); | ||
@@ -85,4 +85,5 @@ } | ||
this.serverless.cli.log("Exporting functions"); | ||
if (service.getAllFunctions() && service.getAllFunctions().length > 0) { | ||
service.getAllFunctions().forEach((name, idx) => { | ||
let allFunctions = service.getAllFunctions(); | ||
if (allFunctions && allFunctions.length > 0) { | ||
allFunctions.forEach((name, idx) => { | ||
const sFunction = service.getFunction(name) | ||
@@ -89,0 +90,0 @@ const functionName = (sFunction.name ? sFunction.name : name); |
@@ -20,3 +20,3 @@ /* | ||
return input.charAt(0).toUpperCase() + | ||
input.replace(/-([a-z])/g, (g) => { | ||
input.replace(/-([a-zA-Z])/g, (g) => { | ||
return g[1].toUpperCase(); | ||
@@ -51,2 +51,2 @@ }).slice(1); | ||
} | ||
} | ||
} |
{ | ||
"name": "serverless-sam", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Serverless framework plugin to export AWS SAM templates for a service", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
73984
26
1509
13