Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added parsing of the function node in the serverless.yml #364

Merged
merged 2 commits into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
**/TestGenerations/
/Blueprints/YeomanGenerator/generator-aws-lambda-dotnet/Blueprints/**
/Blueprints/YeomanGenerator/generator-aws-lambda-dotnet/app/manifest.js
/Tools/LambdaTestTool/Amazon.Lambda.TestTool/ExternalCommands/App
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void LoadServerlessTemplateConfig()
}

[Fact]
public void LoadServerlessYamlTemplateConfig()
public void LoadServerlessResourceBasedYamlTemplateConfig()
{
var defaultsFilePath = Path.GetFullPath(@"../../../../LambdaFunctions/ServerlessTemplateYamlExample/aws-lambda-tools-defaults.json");

Expand All @@ -138,6 +138,28 @@ public void LoadServerlessYamlTemplateConfig()
Assert.Equal("MyToUpper", configInfo.FunctionInfos[1].Name);
Assert.Equal("ServerlessTemplateYamlExample::ServerlessTemplateYamlExample.Functions::ToUpper", configInfo.FunctionInfos[1].Handler);

}
[Fact]
public void LoadServerlessFuncitonBasedYamlTemplateConfig()
{
var defaultsFilePath = Path.GetFullPath(@"../../../../LambdaFunctions/ServerlessFunctionTemplateYamlExample/aws-lambda-tools-defaults.json");

var configInfo = LambdaDefaultsConfigFileParser.LoadFromFile(defaultsFilePath);

Assert.Equal(3, configInfo.FunctionInfos.Count);
Assert.Equal("default", configInfo.AWSProfile);
Assert.Equal("us-west-2", configInfo.AWSRegion);

Assert.Equal("create", configInfo.FunctionInfos[0].Name);
Assert.Equal("DotNetServerless.Lambda::DotNetServerless.Lambda.Functions.CreateItemFunction::Run", configInfo.FunctionInfos[0].Handler);

Assert.Equal("get", configInfo.FunctionInfos[1].Name);
Assert.Equal("DotNetServerless.Lambda::DotNetServerless.Lambda.Functions.GetItemFunction::Run", configInfo.FunctionInfos[1].Handler);


Assert.Equal("update", configInfo.FunctionInfos[2].Name);
Assert.Equal("DotNetServerless.Lambda::DotNetServerless.Lambda.Functions.UpdateItemFunction::Run", configInfo.FunctionInfos[2].Handler);

}

private string WriteTempConfigFile(string json)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.IO;
using System.Collections.Generic;

Expand Down Expand Up @@ -88,29 +89,52 @@ private static void ProcessYamlServerlessTemplate(LambdaConfigInfo configInfo, s
if (root == null)
return;

var resources = root.Children.ContainsKey("Resources") ? root.Children["Resources"] as YamlMappingNode : null;
YamlMappingNode resources = null;

if (root.Children.ContainsKey("Resources"))
{
resources = root.Children["Resources"] as YamlMappingNode;
ProcessYamlServerlessTemplateResourcesBased(configInfo, resources);
} else if (root.Children.ContainsKey("functions"))
{
resources = (YamlMappingNode) root.Children["functions"];
ProcessYamlServerlessTemplateFunctionBased(configInfo, resources);
}


;
}

private static void ProcessYamlServerlessTemplateResourcesBased(LambdaConfigInfo configInfo, YamlMappingNode resources)
{
if (resources == null)
return;

foreach (var resource in resources.Children)
{
var resourceBody = (YamlMappingNode)resource.Value;
var type = resourceBody.Children.ContainsKey("Type") ? ((YamlScalarNode)resourceBody.Children["Type"])?.Value : null;

var resourceBody = (YamlMappingNode) resource.Value;
var type = resourceBody.Children.ContainsKey("Type")
? ((YamlScalarNode) resourceBody.Children["Type"])?.Value
: null;


if (!string.Equals("AWS::Serverless::Function", type, StringComparison.Ordinal) &&
!string.Equals("AWS::Lambda::Function", type, StringComparison.Ordinal))
{
continue;
}

var properties = resourceBody.Children.ContainsKey("Properties") ? resourceBody.Children["Properties"] as YamlMappingNode : null;
var properties = resourceBody.Children.ContainsKey("Properties")
? resourceBody.Children["Properties"] as YamlMappingNode
: null;
if (properties == null)
{
continue;
}

var handler = properties.Children.ContainsKey("Handler") ? ((YamlScalarNode)properties.Children["Handler"])?.Value : null;

var handler = properties.Children.ContainsKey("Handler")
? ((YamlScalarNode) properties.Children["Handler"])?.Value
: null;

if (!string.IsNullOrEmpty(handler))
{
Expand All @@ -124,6 +148,34 @@ private static void ProcessYamlServerlessTemplate(LambdaConfigInfo configInfo, s
}
}
}

private static void ProcessYamlServerlessTemplateFunctionBased(LambdaConfigInfo configInfo, YamlMappingNode resources)
{
if (resources == null)
return;

foreach (var resource in resources.Children)
{
var resourceBody = (YamlMappingNode) resource.Value;

var handler = resourceBody.Children.ContainsKey("handler")
? ((YamlScalarNode) resourceBody.Children["handler"])?.Value
: null;


if (handler == null) continue;
if (string.IsNullOrEmpty(handler)) continue;


var functionInfo = new LambdaFunctionInfo
{
Name = resource.Key.ToString(),
Handler = handler
};

configInfo.FunctionInfos.Add(functionInfo);
}
}

private static void ProcessJsonServerlessTemplate(LambdaConfigInfo configInfo, string content)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Threading.Tasks;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Microsoft.Extensions.DependencyInjection;

namespace DotNetServerless.Lambda.Functions
{
public class CreateItemFunction
{
private readonly IServiceProvider _serviceProvider;

public CreateItemFunction() : this(Startup
.BuildContainer()
.BuildServiceProvider())
{
}

public CreateItemFunction(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
public async Task<APIGatewayProxyResponse> Run(APIGatewayProxyRequest request)
{
return new APIGatewayProxyResponse();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Threading.Tasks;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Microsoft.Extensions.DependencyInjection;

namespace DotNetServerless.Lambda.Functions
{
public class GetItemFunction
{
private readonly IServiceProvider _serviceProvider;

public GetItemFunction() : this(Startup
.BuildContainer()
.BuildServiceProvider())
{
}

public GetItemFunction(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
public async Task<APIGatewayProxyResponse> Run(APIGatewayProxyRequest request)
{
return new APIGatewayProxyResponse();

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Threading.Tasks;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Microsoft.Extensions.DependencyInjection;

namespace DotNetServerless.Lambda.Functions
{
public class UpdateItemFunction
{
private readonly IServiceProvider _serviceProvider;

public UpdateItemFunction() : this(Startup
.BuildContainer()
.BuildServiceProvider())
{
}

public UpdateItemFunction(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
public async Task<APIGatewayProxyResponse> Run(APIGatewayProxyRequest request)
{
return new APIGatewayProxyResponse();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DotNetServerless.Lambda
{
public class Program
{
public static void Main(string[] args)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="1.1.3" />
<PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
<PackageReference Include="Amazon.Lambda.Logging.AspNetCore" Version="2.0.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.4.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
</ItemGroup>



<ItemGroup>
<DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<Content Remove="package-lock.json" />
<None Include="package-lock.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
<Content Remove="package.json" />
<None Include="package.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace DotNetServerless.Lambda
{
public class Startup
{
public static IServiceCollection BuildContainer()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddEnvironmentVariables()
.Build();

return ConfigureServices(configuration);
}


private static IServiceCollection ConfigureServices(IConfigurationRoot configurationRoot)
{
var services = new ServiceCollection();

return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"Information" : [
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",

"dotnet lambda help",

"All the command line options for the Lambda command can be specified in this file."
],

"profile":"default",
"region" : "us-west-2",
"configuration": "Release",
"framework": "netcoreapp2.1",
"s3-prefix": "ServerlessTemplateExample/",
"template": "serverless.yml"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
feature: samueleresca-TEST
version: 1.0.0.0
region: eu-west-1
environment: qa100
accountId: 394300096641
dynamoTable: samuele-qa100-test
Loading