Skip to content

Commit

Permalink
Merge pull request #405 from fujiwara/fix/defaults
Browse files Browse the repository at this point in the history
fix fillDefaultValues
  • Loading branch information
fujiwara authored Jun 17, 2024
2 parents f785498 + 920c189 commit 293d64e
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 8 deletions.
4 changes: 0 additions & 4 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ func prepareZipfile(src string, excludes []string) (*os.File, os.FileInfo, error
}

func (app *App) prepareFunctionCodeForDeploy(ctx context.Context, opt *DeployOption, fn *Function) error {
if fn.Layers == nil {
fn.Layers = []string{} // set explicitly
}

if fn.PackageType == types.PackageTypeImage {
if fn.Code == nil || fn.Code.ImageUri == nil {
return fmt.Errorf("PackageType=Image requires Code.ImageUri in function definition")
Expand Down
1 change: 1 addition & 0 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var (
ExpandExcludeFile = expandExcludeFile
LoadZipArchive = loadZipArchive
MergeTags = mergeTags
FillDefaultValues = fillDefaultValues
)

type VersionsOutput = versionsOutput
Expand Down
24 changes: 21 additions & 3 deletions lambroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,28 @@ func fillDefaultValues(fn *Function) {
if fn.MemorySize == nil {
fn.MemorySize = aws.Int32(128)
}
if fn.LoggingConfig == nil {
if fn.Layers == nil {
fn.Layers = []string{}
}
if lc := fn.LoggingConfig; lc == nil {
fn.LoggingConfig = &types.LoggingConfig{
LogFormat: types.LogFormatText,
LogGroup: aws.String(resolveLogGroup(fn)),
LogFormat: types.LogFormatText,
LogGroup: aws.String(resolveLogGroup(fn)),
ApplicationLogLevel: types.ApplicationLogLevelInfo,
SystemLogLevel: types.SystemLogLevelInfo,
}
} else {
if lc.LogFormat == "" {
lc.LogFormat = types.LogFormatText
}
if lc.LogGroup == nil {
lc.LogGroup = aws.String(resolveLogGroup(fn))
}
if lc.ApplicationLogLevel == "" {
lc.ApplicationLogLevel = types.ApplicationLogLevelInfo
}
if lc.SystemLogLevel == "" {
lc.SystemLogLevel = types.SystemLogLevelInfo
}
}
if fn.TracingConfig == nil {
Expand Down
89 changes: 89 additions & 0 deletions lambroll_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package lambroll_test

import (
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/lambda/types"
"github.com/fujiwara/lambroll"
"github.com/google/go-cmp/cmp"
)

var testCasesFillDefaultValues = []struct {
name string
in *lambroll.Function
expect *lambroll.Function
}{
{
name: "normal",
in: &lambroll.Function{FunctionName: aws.String("test")},
expect: &lambroll.Function{
FunctionName: aws.String("test"),
Description: aws.String(""),
Architectures: []types.Architecture{types.ArchitectureX8664},
EphemeralStorage: &types.EphemeralStorage{
Size: aws.Int32(512),
},
Layers: []string{},
LoggingConfig: &types.LoggingConfig{
ApplicationLogLevel: "INFO",
LogFormat: types.LogFormatText,
LogGroup: aws.String("/aws/lambda/test"),
SystemLogLevel: "INFO",
},
MemorySize: aws.Int32(128),
SnapStart: &types.SnapStart{
ApplyOn: types.SnapStartApplyOnNone,
},
Timeout: aws.Int32(3),
TracingConfig: &types.TracingConfig{
Mode: types.TracingModePassThrough,
},
},
},
{
name: "logging config JSON",
in: &lambroll.Function{
FunctionName: aws.String("test"),
LoggingConfig: &types.LoggingConfig{
LogFormat: types.LogFormatJson,
},
},
expect: &lambroll.Function{
FunctionName: aws.String("test"),
Description: aws.String(""),
Architectures: []types.Architecture{types.ArchitectureX8664},
EphemeralStorage: &types.EphemeralStorage{
Size: aws.Int32(512),
},
Layers: []string{},
LoggingConfig: &types.LoggingConfig{
ApplicationLogLevel: "INFO",
LogFormat: types.LogFormatJson,
LogGroup: aws.String("/aws/lambda/test"),
SystemLogLevel: "INFO",
},
MemorySize: aws.Int32(128),
SnapStart: &types.SnapStart{
ApplyOn: types.SnapStartApplyOnNone,
},
Timeout: aws.Int32(3),
TracingConfig: &types.TracingConfig{
Mode: types.TracingModePassThrough,
},
},
},
}

func TestFillDefaultValues(t *testing.T) {
for _, tt := range testCasesFillDefaultValues {
t.Run(tt.name, func(t *testing.T) {
lambroll.FillDefaultValues(tt.in)
in := lambroll.ToJSONString(tt.in)
expect := lambroll.ToJSONString(tt.expect)
if diff := cmp.Diff(in, expect); diff != "" {
t.Errorf("differs: (-got +want)\n%s", diff)
}
})
}
}
3 changes: 2 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/Songmu/prompter"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/google/go-jsonnet/formatter"
)

Expand Down Expand Up @@ -113,7 +114,7 @@ func resolveLogGroup(fn *Function) string {
if fn.LoggingConfig != nil && fn.LoggingConfig.LogGroup != nil {
return *fn.LoggingConfig.LogGroup
}
return fmt.Sprintf("/aws/lambda/%s", *fn.FunctionName)
return fmt.Sprintf("/aws/lambda/%s", aws.ToString(fn.FunctionName))
}

func fullQualifiedFunctionName(name string, qualifier *string) string {
Expand Down

0 comments on commit 293d64e

Please sign in to comment.