From 9d2ffbaf342f91d5e3beac8d0b01dfe1d74f4e63 Mon Sep 17 00:00:00 2001 From: Maciej Winnicki Date: Fri, 8 Jan 2016 00:52:01 +0100 Subject: [PATCH] Function prefixing --- function/function.go | 50 ++++++++++++++++++-------------- function/function_test.go | 60 +++++++++++++++++++-------------------- project/project.go | 2 +- 3 files changed, 60 insertions(+), 52 deletions(-) diff --git a/function/function.go b/function/function.go index a37aa97..da602bf 100644 --- a/function/function.go +++ b/function/function.go @@ -62,20 +62,22 @@ type Config struct { } // Function represents a Lambda function, with configuration loaded -// from the "lambda.json" file on disk. Operations are performed +// from the "function.json" file on disk. Operations are performed // against the function directory as the CWD, so os.Chdir() first. type Function struct { Config - Path string - Verbose bool - Service lambdaiface.LambdaAPI - Log log.Interface - runtime runtime.Runtime - env map[string]string + Path string + ProjectName string + LambdaName string + Verbose bool + Service lambdaiface.LambdaAPI + Log log.Interface + runtime runtime.Runtime + env map[string]string } // Open the function.json file and prime the config. -func (f *Function) Open() error { +func (f *Function) Open(projectName string) error { p, err := os.Open(filepath.Join(f.Path, "function.json")) if err != nil { return err @@ -85,6 +87,8 @@ func (f *Function) Open() error { return err } + f.lambdaName(projectName) + r, err := runtime.ByName(f.Runtime) if err != nil { return err @@ -148,7 +152,7 @@ func (f *Function) DeployConfig() error { f.Log.Info("deploying config") _, err := f.Service.UpdateFunctionConfiguration(&lambda.UpdateFunctionConfigurationInput{ - FunctionName: &f.Name, + FunctionName: &f.LambdaName, MemorySize: &f.Memory, Timeout: &f.Timeout, Description: &f.Description, @@ -163,7 +167,7 @@ func (f *Function) DeployConfig() error { func (f *Function) Delete() error { f.Log.Info("deleting") _, err := f.Service.DeleteFunction(&lambda.DeleteFunctionInput{ - FunctionName: &f.Name, + FunctionName: &f.LambdaName, }) return err } @@ -172,7 +176,7 @@ func (f *Function) Delete() error { func (f *Function) Info() (*lambda.GetFunctionOutput, error) { f.Log.Info("fetching config") return f.Service.GetFunction(&lambda.GetFunctionInput{ - FunctionName: &f.Name, + FunctionName: &f.LambdaName, }) } @@ -181,7 +185,7 @@ func (f *Function) Update(zip []byte) error { f.Log.Info("updating function") updated, err := f.Service.UpdateFunctionCode(&lambda.UpdateFunctionCodeInput{ - FunctionName: &f.Name, + FunctionName: &f.LambdaName, Publish: aws.Bool(true), ZipFile: zip, }) @@ -193,7 +197,7 @@ func (f *Function) Update(zip []byte) error { f.Log.Info("updating alias") _, err = f.Service.UpdateAlias(&lambda.UpdateAliasInput{ - FunctionName: &f.Name, + FunctionName: &f.LambdaName, Name: aws.String(CurrentAlias), FunctionVersion: updated.Version, }) @@ -206,7 +210,7 @@ func (f *Function) Create(zip []byte) error { f.Log.Info("creating function") created, err := f.Service.CreateFunction(&lambda.CreateFunctionInput{ - FunctionName: &f.Name, + FunctionName: &f.LambdaName, Description: &f.Description, MemorySize: &f.Memory, Timeout: &f.Timeout, @@ -226,7 +230,7 @@ func (f *Function) Create(zip []byte) error { f.Log.Info("creating alias") _, err = f.Service.CreateAlias(&lambda.CreateAliasInput{ - FunctionName: &f.Name, + FunctionName: &f.LambdaName, FunctionVersion: created.Version, Name: aws.String(CurrentAlias), }) @@ -248,7 +252,7 @@ func (f *Function) Invoke(event, context interface{}, kind InvocationType) (repl res, err := f.Service.Invoke(&lambda.InvokeInput{ ClientContext: aws.String(base64.StdEncoding.EncodeToString(contextBytes)), - FunctionName: aws.String(f.Name), + FunctionName: &f.LambdaName, InvocationType: aws.String(string(kind)), LogType: aws.String("Tail"), Qualifier: aws.String(CurrentAlias), @@ -285,7 +289,7 @@ func (f *Function) Rollback() error { f.Log.Info("rolling back") alias, err := f.Service.GetAlias(&lambda.GetAliasInput{ - FunctionName: &f.Name, + FunctionName: &f.LambdaName, Name: aws.String(CurrentAlias), }) if err != nil { @@ -295,7 +299,7 @@ func (f *Function) Rollback() error { f.Log.Infof("current version: %s", *alias.FunctionVersion) list, err := f.Service.ListVersionsByFunction(&lambda.ListVersionsByFunctionInput{ - FunctionName: &f.Name, + FunctionName: &f.LambdaName, }) if err != nil { return err @@ -317,7 +321,7 @@ func (f *Function) Rollback() error { f.Log.Infof("rollback to version: %s", rollbackToVersion) _, err = f.Service.UpdateAlias(&lambda.UpdateAliasInput{ - FunctionName: &f.Name, + FunctionName: &f.LambdaName, Name: aws.String(CurrentAlias), FunctionVersion: &rollbackToVersion, }) @@ -330,7 +334,7 @@ func (f *Function) RollbackVersion(version string) error { f.Log.Info("rolling back") alias, err := f.Service.GetAlias(&lambda.GetAliasInput{ - FunctionName: &f.Name, + FunctionName: &f.LambdaName, Name: aws.String(CurrentAlias), }) if err != nil { @@ -344,7 +348,7 @@ func (f *Function) RollbackVersion(version string) error { } _, err = f.Service.UpdateAlias(&lambda.UpdateAliasInput{ - FunctionName: &f.Name, + FunctionName: &f.LambdaName, Name: aws.String(CurrentAlias), FunctionVersion: &version, }) @@ -418,3 +422,7 @@ func (f *Function) ZipBytes() ([]byte, error) { f.Log.Infof("created zip (%s)", humanize.Bytes(uint64(len(b)))) return b, nil } + +func (f *Function) lambdaName(projectName string) { + f.LambdaName = fmt.Sprintf("%s_%s", projectName, f.Name) +} diff --git a/function/function_test.go b/function/function_test.go index a9ef740..9030293 100644 --- a/function/function_test.go +++ b/function/function_test.go @@ -28,9 +28,9 @@ func TestFunction_Delete_success(t *testing.T) { }).Return(&lambda.DeleteFunctionOutput{}, nil) fn := &function.Function{ - Config: function.Config{Name: "testfn"}, - Service: serviceMock, - Log: log.Log, + LambdaName: "testfn", + Service: serviceMock, + Log: log.Log, } err := fn.Delete() @@ -45,9 +45,9 @@ func TestFunction_Delete_failed(t *testing.T) { serviceMock.EXPECT().DeleteFunction(gomock.Any()).Return(nil, errors.New("API err")) fn := &function.Function{ - Config: function.Config{Name: "testfn"}, - Service: serviceMock, - Log: log.Log, + LambdaName: "testfn", + Service: serviceMock, + Log: log.Log, } err := fn.Delete() @@ -65,9 +65,9 @@ func TestFunction_Rollback_GetAlias_failed(t *testing.T) { }).Return(nil, errors.New("API err")) fn := &function.Function{ - Config: function.Config{Name: "testfn"}, - Service: serviceMock, - Log: log.Log, + LambdaName: "testfn", + Service: serviceMock, + Log: log.Log, } err := fn.Rollback() @@ -85,9 +85,9 @@ func TestFunction_Rollback_ListVersions_failed(t *testing.T) { }).Return(nil, errors.New("API err")) fn := &function.Function{ - Config: function.Config{Name: "testfn"}, - Service: serviceMock, - Log: log.Log, + LambdaName: "testfn", + Service: serviceMock, + Log: log.Log, } err := fn.Rollback() @@ -105,9 +105,9 @@ func TestFunction_Rollback_fewVersions(t *testing.T) { }, nil) fn := &function.Function{ - Config: function.Config{Name: "testfn"}, - Service: serviceMock, - Log: log.Log, + LambdaName: "testfn", + Service: serviceMock, + Log: log.Log, } err := fn.Rollback() @@ -135,9 +135,9 @@ func TestFunction_Rollback_previousVersion(t *testing.T) { }) fn := &function.Function{ - Config: function.Config{Name: "testfn"}, - Service: serviceMock, - Log: log.Log, + LambdaName: "testfn", + Service: serviceMock, + Log: log.Log, } err := fn.Rollback() @@ -165,9 +165,9 @@ func TestFunction_Rollback_latestVersion(t *testing.T) { }) fn := &function.Function{ - Config: function.Config{Name: "testfn"}, - Service: serviceMock, - Log: log.Log, + LambdaName: "testfn", + Service: serviceMock, + Log: log.Log, } err := fn.Rollback() @@ -190,9 +190,9 @@ func TestFunction_Rollback_UpdateAlias_failed(t *testing.T) { serviceMock.EXPECT().UpdateAlias(gomock.Any()).Return(nil, errors.New("API err")) fn := &function.Function{ - Config: function.Config{Name: "testfn"}, - Service: serviceMock, - Log: log.Log, + LambdaName: "testfn", + Service: serviceMock, + Log: log.Log, } err := fn.Rollback() @@ -210,9 +210,9 @@ func TestFunction_RollbackVersion_GetAlias_failed(t *testing.T) { }).Return(nil, errors.New("API err")) fn := &function.Function{ - Config: function.Config{Name: "testfn"}, - Service: serviceMock, - Log: log.Log, + LambdaName: "testfn", + Service: serviceMock, + Log: log.Log, } err := fn.RollbackVersion("1") @@ -251,9 +251,9 @@ func TestFunction_RollbackVersion_success(t *testing.T) { }) fn := &function.Function{ - Config: function.Config{Name: "testfn"}, - Service: serviceMock, - Log: log.Log, + LambdaName: "testfn", + Service: serviceMock, + Log: log.Log, } err := fn.RollbackVersion("3") diff --git a/project/project.go b/project/project.go index 3b9f2e9..b1e6dd0 100644 --- a/project/project.go +++ b/project/project.go @@ -208,7 +208,7 @@ func (p *Project) loadFunction(name string) (*function.Function, error) { Log: p.Log.WithField("function", name), } - if err := fn.Open(); err != nil { + if err := fn.Open(p.Name); err != nil { return nil, err }