Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ your stack in a different region.

## Additional Flags

* `--stackit-tags "key=val,key2=val2"`
If the s3 bucket stackit creates requires tags, you can add them with this flag.

TODO: Document these properly

* `--service-role VAL`
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func init() {
RootCmd.PersistentFlags().String("region", "", "")
RootCmd.PersistentFlags().String("profile", "", "")
RootCmd.PersistentFlags().String("stack-name", "", "")
RootCmd.PersistentFlags().String("stackit-tags", "", "")

// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here,
Expand Down
47 changes: 47 additions & 0 deletions pkg/stackit/packager/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package packager

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/pkg/errors"
"github.com/spf13/viper"
)

func (p *Packager) s3BucketName() (string, error) {
Expand Down Expand Up @@ -61,6 +64,50 @@ func (p *Packager) s3BucketName() (string, error) {
}
}

addTags := func() error {
tags := getStackitTags()
_, err := p.s3.PutBucketTagging(&s3.PutBucketTaggingInput{
Bucket: &bucketName,
Tagging: &s3.Tagging{
TagSet: tags,
},
})
return errors.Wrap(err, "adding tags on bucket")
}

_, getTagErr := p.s3.GetBucketTagging(&s3.GetBucketTaggingInput{Bucket: &bucketName})
if tagError, ok := getTagErr.(awserr.Error); ok && tagError.Code() == "NoSuchTagSet" {
err = addTags()
if err != nil {
return "", err
}
}

p.cachedBucketName = bucketName
return bucketName, nil
}

func getStackitTags() []*s3.Tag {
tags := viper.GetString("stackit-tags")
if tags == "" {
return nil
}

tagList := strings.Split(tags,",")
tagMap := make(map[string]string)
for _, pair := range tagList {
dict := strings.Split(pair, "=")
tagMap[dict[0]] = dict[1]
}

result := make([]*s3.Tag, 0, len(tagMap))
for k, v := range tagMap {
var t = &s3.Tag{
Key: aws.String(k),
Value: aws.String(v),
}
result = append(result, t)
}

return result
}