diff --git a/create.go b/create.go index 90dfff7..d50f963 100644 --- a/create.go +++ b/create.go @@ -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") diff --git a/export_test.go b/export_test.go index 4b903ad..ac929ef 100644 --- a/export_test.go +++ b/export_test.go @@ -5,6 +5,7 @@ var ( ExpandExcludeFile = expandExcludeFile LoadZipArchive = loadZipArchive MergeTags = mergeTags + FillDefaultValues = fillDefaultValues ) type VersionsOutput = versionsOutput diff --git a/lambroll.go b/lambroll.go index 5596d77..806a1e9 100644 --- a/lambroll.go +++ b/lambroll.go @@ -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 { diff --git a/lambroll_test.go b/lambroll_test.go new file mode 100644 index 0000000..cd8ecc2 --- /dev/null +++ b/lambroll_test.go @@ -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) + } + }) + } +} diff --git a/utils.go b/utils.go index 562796b..de5e3eb 100644 --- a/utils.go +++ b/utils.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/Songmu/prompter" + "github.com/aws/aws-sdk-go-v2/aws" "github.com/google/go-jsonnet/formatter" ) @@ -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 {