Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Nov 3, 2019
1 parent 2962fd5 commit 6242bc7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 46 deletions.
53 changes: 14 additions & 39 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (app *App) Init(opt InitOption) error {
MemorySize: aws.Int64(128),
Runtime: aws.String("nodejs10.x"),
Timeout: aws.Int64(3),
Handler: aws.String(""),
Handler: aws.String("index.handler"),
Role: aws.String(
fmt.Sprintf(
"arn:aws:iam::%s:role/YOUR_LAMBDA_ROLE_NAME",
Expand All @@ -56,52 +56,27 @@ func (app *App) Init(opt InitOption) error {
log.Printf("[info] function %s found", *opt.FunctionName)
c = res.Configuration
}
fn := &Function{
Description: c.Description,
FunctionName: c.FunctionName,
Handler: c.Handler,
MemorySize: c.MemorySize,
Role: c.Role,
Runtime: c.Runtime,
Timeout: c.Timeout,
}
if e := c.Environment; e != nil {
fn.Environment = &lambda.Environment{
Variables: e.Variables,
}
}
for _, layer := range c.Layers {
fn.Layers = append(fn.Layers, layer.Arn)
}
if t := c.TracingConfig; t != nil {
fn.TracingConfig = &lambda.TracingConfig{
Mode: t.Mode,
}
}
if v := c.VpcConfig; v != nil && *v.VpcId != "" {
fn.VpcConfig = &lambda.VpcConfig{
SubnetIds: v.SubnetIds,
SecurityGroupIds: v.SecurityGroupIds,
}
}

if *opt.DownloadZip && res.Code != nil && *res.Code.RepositoryType == "S3" {
log.Printf("[info] downloading %s", FunctionZipFilename)
if err := download(*res.Code.Location, FunctionZipFilename); err != nil {
return err
}
}

var tags Tags
if exists {
arn := app.functionArn(fn)
arn := app.functionArn(*c.FunctionName)
log.Printf("[debug] listing tags of %s", arn)
tags, err := app.lambda.ListTags(&lambda.ListTagsInput{
res, err := app.lambda.ListTags(&lambda.ListTagsInput{
Resource: aws.String(arn),
})
if err != nil {
return errors.Wrap(err, "faled to list tags")
}
fn.Tags = tags.Tags
tags = res.Tags
}

fn := newFuctionFrom(c, tags)

if *opt.DownloadZip && res.Code != nil && *res.Code.RepositoryType == "S3" {
log.Printf("[info] downloading %s", FunctionZipFilename)
if err := download(*res.Code.Location, FunctionZipFilename); err != nil {
return err
}
}

log.Printf("[info] creating %s", IgnoreFilename)
Expand Down
41 changes: 39 additions & 2 deletions lambroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ import (
// Function represents configuration of Lambda function
type Function = lambda.CreateFunctionInput

func (app *App) functionArn(fn *Function) string {
// Tags represents tags of function
type Tags = map[string]*string

func (app *App) functionArn(name string) string {
return fmt.Sprintf(
"arn:aws:lambda:%s:%s:function:%s",
*app.sess.Config.Region,
app.AWSAccountID(),
*fn.FunctionName,
name,
)
}

Expand Down Expand Up @@ -90,3 +93,37 @@ func (app *App) loadFunction(path string) (*Function, error) {
}
return &fn, nil
}

func newFuctionFrom(c *lambda.FunctionConfiguration, tags Tags) *Function {
fn := &Function{
Description: c.Description,
FunctionName: c.FunctionName,
Handler: c.Handler,
MemorySize: c.MemorySize,
Role: c.Role,
Runtime: c.Runtime,
Timeout: c.Timeout,
}
if e := c.Environment; e != nil {
fn.Environment = &lambda.Environment{
Variables: e.Variables,
}
}
for _, layer := range c.Layers {
fn.Layers = append(fn.Layers, layer.Arn)
}
if t := c.TracingConfig; t != nil {
fn.TracingConfig = &lambda.TracingConfig{
Mode: t.Mode,
}
}
if v := c.VpcConfig; v != nil && *v.VpcId != "" {
fn.VpcConfig = &lambda.VpcConfig{
SubnetIds: v.SubnetIds,
SecurityGroupIds: v.SecurityGroupIds,
}
}
fn.Tags = tags

return fn
}
13 changes: 11 additions & 2 deletions list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lambroll

import (
"log"
"os"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -22,8 +23,16 @@ func (app *App) List(opt ListOption) error {
if err != nil {
return errors.Wrap(err, "failed to ListFunctions")
}
for _, fn := range res.Functions {
b, _ := marshalJSON(fn)
for _, c := range res.Functions {
arn := app.functionArn(*c.FunctionName)
log.Printf("[debug] listing tags of %s", arn)
res, err := app.lambda.ListTags(&lambda.ListTagsInput{
Resource: aws.String(arn),
})
if err != nil {
return errors.Wrap(err, "faled to list tags")
}
b, _ := marshalJSON(newFuctionFrom(c, res.Tags))
os.Stdout.Write(b)
}
if marker = res.NextMarker; marker == nil {
Expand Down
6 changes: 3 additions & 3 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (app *App) updateTags(fn *Function, opt DeployOption) error {
log.Println("[debug] Tags not defined in function.json skip udpating tags")
return nil
}
arn := app.functionArn(fn)
arn := app.functionArn(*fn.FunctionName)
tags, err := app.lambda.ListTags(&lambda.ListTagsInput{
Resource: aws.String(arn),
})
Expand Down Expand Up @@ -70,8 +70,8 @@ func (app *App) updateTags(fn *Function, opt DeployOption) error {
}

// mergeTags merges old/new tags
func mergeTags(oldTags, newTags map[string]*string) (sets map[string]*string, removes []*string) {
sets = make(map[string]*string)
func mergeTags(oldTags, newTags Tags) (sets Tags, removes []*string) {
sets = make(Tags)
removes = make([]*string, 0)
for key, oldValue := range oldTags {
if newValue, ok := newTags[key]; ok {
Expand Down

0 comments on commit 6242bc7

Please sign in to comment.