Skip to content

Commit

Permalink
Function prefixing
Browse files Browse the repository at this point in the history
  • Loading branch information
mthenw committed Jan 7, 2016
1 parent 569b078 commit 9d2ffba
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 52 deletions.
50 changes: 29 additions & 21 deletions function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand All @@ -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,
})
}

Expand All @@ -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,
})
Expand All @@ -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,
})
Expand All @@ -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,
Expand All @@ -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),
})
Expand All @@ -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),
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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,
})
Expand All @@ -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 {
Expand All @@ -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,
})
Expand Down Expand Up @@ -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)
}
60 changes: 30 additions & 30 deletions function/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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")

Expand Down Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 9d2ffba

Please sign in to comment.