Skip to content

Commit

Permalink
bare minimum tag implementation for Elastic Beanstalk
Browse files Browse the repository at this point in the history
  • Loading branch information
catsby committed Oct 8, 2015
1 parent aa550ff commit 23903cb
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
52 changes: 52 additions & 0 deletions builtin/providers/aws/tagsBeanstalk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package aws

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
)

// diffTags takes our tags locally and the ones remotely and returns
// the set of tags that must be created, and the set of tags that must
// be destroyed.
func diffTagsBeanstalk(oldTags, newTags []*elasticbeanstalk.Tag) ([]*elasticbeanstalk.Tag, []*elasticbeanstalk.Tag) {
// First, we're creating everything we have
create := make(map[string]interface{})
for _, t := range newTags {
create[*t.Key] = *t.Value
}

// Build the list of what to remove
var remove []*elasticbeanstalk.Tag
for _, t := range oldTags {
old, ok := create[*t.Key]
if !ok || old != *t.Value {
// Delete it!
remove = append(remove, t)
}
}

return tagsFromMapBeanstalk(create), remove
}

// tagsFromMap returns the tags for the given map of data.
func tagsFromMapBeanstalk(m map[string]interface{}) []*elasticbeanstalk.Tag {
var result []*elasticbeanstalk.Tag
for k, v := range m {
result = append(result, &elasticbeanstalk.Tag{
Key: aws.String(k),
Value: aws.String(v.(string)),
})
}

return result
}

// tagsToMap turns the list of tags into a map.
func tagsToMapBeanstalk(ts []*elasticbeanstalk.Tag) map[string]string {
result := make(map[string]string)
for _, t := range ts {
result[*t.Key] = *t.Value
}

return result
}
85 changes: 85 additions & 0 deletions builtin/providers/aws/tagsBeanstalk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package aws

import (
"fmt"
"reflect"
"testing"

"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestDiffBeanstalkTags(t *testing.T) {
cases := []struct {
Old, New map[string]interface{}
Create, Remove map[string]string
}{
// Basic add/remove
{
Old: map[string]interface{}{
"foo": "bar",
},
New: map[string]interface{}{
"bar": "baz",
},
Create: map[string]string{
"bar": "baz",
},
Remove: map[string]string{
"foo": "bar",
},
},

// Modify
{
Old: map[string]interface{}{
"foo": "bar",
},
New: map[string]interface{}{
"foo": "baz",
},
Create: map[string]string{
"foo": "baz",
},
Remove: map[string]string{
"foo": "bar",
},
},
}

for i, tc := range cases {
c, r := diffTagsBeanstalk(tagsFromMapBeanstalk(tc.Old), tagsFromMapBeanstalk(tc.New))
cm := tagsToMapBeanstalk(c)
rm := tagsToMapBeanstalk(r)
if !reflect.DeepEqual(cm, tc.Create) {
t.Fatalf("%d: bad create: %#v", i, cm)
}
if !reflect.DeepEqual(rm, tc.Remove) {
t.Fatalf("%d: bad remove: %#v", i, rm)
}
}
}

// testAccCheckTags can be used to check the tags on a resource.
func testAccCheckBeanstalkTags(
ts *[]*elasticbeanstalk.Tag, key string, value string) resource.TestCheckFunc {
return func(s *terraform.State) error {
m := tagsToMapBeanstalk(*ts)
v, ok := m[key]
if value != "" && !ok {
return fmt.Errorf("Missing tag: %s", key)
} else if value == "" && ok {
return fmt.Errorf("Extra tag: %s", key)
}
if value == "" {
return nil
}

if v != value {
return fmt.Errorf("%s: bad value: %s", key, v)
}

return nil
}
}

0 comments on commit 23903cb

Please sign in to comment.