-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#7741: Pinpoint App - Added resource tag support #9460
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,6 +128,11 @@ func resourceAwsPinpointApp() *schema.Resource { | |
}, | ||
}, | ||
}, | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"tags": tagsSchema(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new argument is missing documentation in
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added 👍 |
||
}, | ||
} | ||
} | ||
|
@@ -136,6 +141,7 @@ func resourceAwsPinpointAppCreate(d *schema.ResourceData, meta interface{}) erro | |
pinpointconn := meta.(*AWSClient).pinpointconn | ||
|
||
var name string | ||
|
||
if v, ok := d.GetOk("name"); ok { | ||
name = v.(string) | ||
} else if v, ok := d.GetOk("name_prefix"); ok { | ||
|
@@ -152,12 +158,17 @@ func resourceAwsPinpointAppCreate(d *schema.ResourceData, meta interface{}) erro | |
}, | ||
} | ||
|
||
if v, ok := d.GetOk("tags"); ok { | ||
req.CreateApplicationRequest.Tags = tagsFromMapPinPointApp(v.(map[string]interface{})) | ||
} | ||
|
||
output, err := pinpointconn.CreateApp(req) | ||
if err != nil { | ||
return fmt.Errorf("error creating Pinpoint app: %s", err) | ||
} | ||
|
||
d.SetId(*output.ApplicationResponse.Id) | ||
d.Set("arn", output.ApplicationResponse.Arn) | ||
|
||
return resourceAwsPinpointAppUpdate(d, meta) | ||
} | ||
|
@@ -193,6 +204,10 @@ func resourceAwsPinpointAppUpdate(d *schema.ResourceData, meta interface{}) erro | |
return err | ||
} | ||
|
||
if err := setTagsPinPointApp(conn, d); err != nil { | ||
return fmt.Errorf("error updating PinPoint Application (%s) tags: %s", d.Id(), err) | ||
} | ||
|
||
return resourceAwsPinpointAppRead(d, meta) | ||
} | ||
|
||
|
@@ -229,6 +244,7 @@ func resourceAwsPinpointAppRead(d *schema.ResourceData, meta interface{}) error | |
|
||
d.Set("name", app.ApplicationResponse.Name) | ||
d.Set("application_id", app.ApplicationResponse.Id) | ||
d.Set("arn", app.ApplicationResponse.Arn) | ||
|
||
if err := d.Set("campaign_hook", flattenPinpointCampaignHook(settings.ApplicationSettingsResource.CampaignHook)); err != nil { | ||
return fmt.Errorf("error setting campaign_hook: %s", err) | ||
|
@@ -240,6 +256,10 @@ func resourceAwsPinpointAppRead(d *schema.ResourceData, meta interface{}) error | |
return fmt.Errorf("error setting quiet_time: %s", err) | ||
} | ||
|
||
if err := getTagsPinPointApp(conn, d); err != nil { | ||
return fmt.Errorf("error setting tags: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
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,133 @@ | ||
package aws | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/pinpoint" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"log" | ||
"regexp" | ||
) | ||
|
||
// getTags is a helper to get the tags for a resource. It expects the | ||
// tags field to be named "tags" | ||
func getTagsPinPointApp(conn *pinpoint.Pinpoint, d *schema.ResourceData) error { | ||
resp, err := conn.ListTagsForResource(&pinpoint.ListTagsForResourceInput{ | ||
ResourceArn: aws.String(d.Get("arn").(string)), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := d.Set("tags", tagsToMapPinPointApp(resp.TagsModel)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// tagsToMap turns the list of tags into a map. | ||
func tagsToMapPinPointApp(tm *pinpoint.TagsModel) map[string]string { | ||
result := make(map[string]string) | ||
for key, value := range tm.Tags { | ||
if !tagIgnoredPinPointApp(key, *value) { | ||
result[key] = aws.StringValue(value) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
// compare a tag against a list of strings and checks if it should | ||
// be ignored or not | ||
func tagIgnoredPinPointApp(tagKey string, tagValue string) bool { | ||
filter := []string{"^aws:"} | ||
for _, v := range filter { | ||
log.Printf("[DEBUG] Matching %v with %v\n", v, tagKey) | ||
r, _ := regexp.MatchString(v, tagKey) | ||
if r { | ||
log.Printf("[DEBUG] Found AWS specific tag %s (val: %s), ignoring.\n", tagKey, tagValue) | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// tagsFromMap returns the tags for the given map of data. | ||
func tagsFromMapPinPointApp(m map[string]interface{}) map[string]*string { | ||
result := make(map[string]*string) | ||
for k, v := range m { | ||
if !tagIgnoredPinPointApp(k, v.(string)) { | ||
result[k] = aws.String(v.(string)) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
// setTags is a helper to set the tags for a resource. It expects the | ||
// tags field to be named "tags" | ||
func setTagsPinPointApp(conn *pinpoint.Pinpoint, d *schema.ResourceData) error { | ||
if d.HasChange("tags") { | ||
oraw, nraw := d.GetChange("tags") | ||
o := oraw.(map[string]interface{}) | ||
n := nraw.(map[string]interface{}) | ||
create, remove := diffTagsPinPointApp(tagsFromMapPinPointApp(o), tagsFromMapPinPointApp(n)) | ||
|
||
// Set tags | ||
if len(remove) > 0 { | ||
log.Printf("[DEBUG] Removing tags: %#v", remove) | ||
k := make([]*string, 0, len(remove)) | ||
for i := range remove { | ||
k = append(k, &i) | ||
} | ||
|
||
log.Printf("[DEBUG] Removing old tags: %#v", k) | ||
log.Printf("[DEBUG] Removing for arn: %#v", aws.String(d.Get("arn").(string))) | ||
|
||
_, err := conn.UntagResource(&pinpoint.UntagResourceInput{ | ||
ResourceArn: aws.String(d.Get("arn").(string)), | ||
TagKeys: k, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if len(create) > 0 { | ||
log.Printf("[DEBUG] Creating tags: %#v", create) | ||
_, err := conn.TagResource(&pinpoint.TagResourceInput{ | ||
ResourceArn: aws.String(d.Get("arn").(string)), | ||
TagsModel: &pinpoint.TagsModel{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 diffTagsPinPointApp(oldTags, newTags map[string]*string) (map[string]*string, map[string]*string) { | ||
// First, we're creating everything we have | ||
create := make(map[string]interface{}) | ||
for k, v := range newTags { | ||
create[k] = aws.StringValue(v) | ||
} | ||
|
||
// Build the list of what to remove | ||
var remove = make(map[string]*string) | ||
for k, v := range oldTags { | ||
old, ok := create[k] | ||
if !ok || old != aws.StringValue(v) { | ||
// Delete it! | ||
remove[k] = v | ||
} else if ok { | ||
delete(create, k) | ||
} | ||
} | ||
|
||
return tagsFromMapPinPointApp(create), remove | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new attribute is missing documentation in
website/docs/r/pinpoint_app.html.markdown
, e.g. (under## Attributes
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added 👍