forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for tags on Directory Service (hashicorp#1398)
* add support for tags on Directory Service * update doc for directory service tags argument * add acceptance test for tags on directory service * Add tests for the diff function
- Loading branch information
1 parent
b7f9caf
commit b976976
Showing
4 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package aws | ||
|
||
import ( | ||
"log" | ||
"regexp" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/directoryservice" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
// setTags is a helper to set the tags for a resource. It expects the | ||
// tags field to be named "tags" | ||
func setTagsDS(conn *directoryservice.DirectoryService, d *schema.ResourceData, resourceId string) error { | ||
if d.HasChange("tags") { | ||
oraw, nraw := d.GetChange("tags") | ||
o := oraw.(map[string]interface{}) | ||
n := nraw.(map[string]interface{}) | ||
create, remove := diffTagsDS(tagsFromMapDS(o), tagsFromMapDS(n)) | ||
|
||
// Set tags | ||
if len(remove) > 0 { | ||
log.Printf("[DEBUG] Removing tags: %s", remove) | ||
k := make([]*string, len(remove), len(remove)) | ||
for i, t := range remove { | ||
k[i] = t.Key | ||
} | ||
|
||
_, err := conn.RemoveTagsFromResource(&directoryservice.RemoveTagsFromResourceInput{ | ||
ResourceId: aws.String(resourceId), | ||
TagKeys: k, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if len(create) > 0 { | ||
log.Printf("[DEBUG] Creating tags: %s", create) | ||
_, err := conn.AddTagsToResource(&directoryservice.AddTagsToResourceInput{ | ||
ResourceId: aws.String(resourceId), | ||
Tags: create, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// 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 diffTagsDS(oldTags, newTags []*directoryservice.Tag) ([]*directoryservice.Tag, []*directoryservice.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 []*directoryservice.Tag | ||
for _, t := range oldTags { | ||
old, ok := create[*t.Key] | ||
if !ok || old != *t.Value { | ||
// Delete it! | ||
remove = append(remove, t) | ||
} | ||
} | ||
|
||
return tagsFromMapDS(create), remove | ||
} | ||
|
||
// tagsFromMap returns the tags for the given map of data. | ||
func tagsFromMapDS(m map[string]interface{}) []*directoryservice.Tag { | ||
result := make([]*directoryservice.Tag, 0, len(m)) | ||
for k, v := range m { | ||
t := &directoryservice.Tag{ | ||
Key: aws.String(k), | ||
Value: aws.String(v.(string)), | ||
} | ||
if !tagIgnoredDS(t) { | ||
result = append(result, t) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
// tagsToMap turns the list of tags into a map. | ||
func tagsToMapDS(ts []*directoryservice.Tag) map[string]string { | ||
result := make(map[string]string) | ||
for _, t := range ts { | ||
if !tagIgnoredDS(t) { | ||
result[*t.Key] = *t.Value | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
// compare a tag against a list of strings and checks if it should | ||
// be ignored or not | ||
func tagIgnoredDS(t *directoryservice.Tag) bool { | ||
filter := []string{"^aws:"} | ||
for _, v := range filter { | ||
log.Printf("[DEBUG] Matching %v with %v\n", v, *t.Key) | ||
if r, _ := regexp.MatchString(v, *t.Key); r == true { | ||
log.Printf("[DEBUG] Found AWS specific tag %s (val: %s), ignoring.\n", *t.Key, *t.Value) | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters