-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
provider/aws: ASG Notifications Resource #2197
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 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
200 changes: 200 additions & 0 deletions
200
builtin/providers/aws/resource_aws_autoscaling_notification.go
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,200 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/autoscaling" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsAutoscalingNotification() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsAutoscalingNotificationCreate, | ||
Read: resourceAwsAutoscalingNotificationRead, | ||
Update: resourceAwsAutoscalingNotificationUpdate, | ||
Delete: resourceAwsAutoscalingNotificationDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"topic_arn": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"group_names": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Required: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
|
||
"notifications": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Required: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsAutoscalingNotificationCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).autoscalingconn | ||
gl := convertSetToList(d.Get("group_names").(*schema.Set)) | ||
nl := convertSetToList(d.Get("notifications").(*schema.Set)) | ||
|
||
topic := d.Get("topic_arn").(string) | ||
if err := addNotificationConfigToGroupsWithTopic(conn, gl, nl, topic); err != nil { | ||
return err | ||
} | ||
|
||
// ARNs are unique, and these notifications are per ARN, so we re-use the ARN | ||
// here as the ID | ||
d.SetId(topic) | ||
return resourceAwsAutoscalingNotificationRead(d, meta) | ||
} | ||
|
||
func resourceAwsAutoscalingNotificationRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).autoscalingconn | ||
gl := convertSetToList(d.Get("group_names").(*schema.Set)) | ||
|
||
opts := &autoscaling.DescribeNotificationConfigurationsInput{ | ||
AutoScalingGroupNames: gl, | ||
} | ||
|
||
resp, err := conn.DescribeNotificationConfigurations(opts) | ||
if err != nil { | ||
return fmt.Errorf("Error describing notifications") | ||
} | ||
|
||
topic := d.Get("topic_arn").(string) | ||
// Grab all applicable notifcation configurations for this Topic. | ||
// Each NotificationType will have a record, so 1 Group with 3 Types results | ||
// in 3 records, all with the same Group name | ||
gRaw := make(map[string]bool) | ||
nRaw := make(map[string]bool) | ||
for _, n := range resp.NotificationConfigurations { | ||
if *n.TopicARN == topic { | ||
gRaw[*n.AutoScalingGroupName] = true | ||
nRaw[*n.NotificationType] = true | ||
} | ||
} | ||
|
||
// Grab the keys here as the list of Groups | ||
var gList []string | ||
for k, _ := range gRaw { | ||
gList = append(gList, k) | ||
} | ||
|
||
// Grab the keys here as the list of Types | ||
var nList []string | ||
for k, _ := range nRaw { | ||
nList = append(nList, k) | ||
} | ||
|
||
if err := d.Set("group_names", gList); err != nil { | ||
return err | ||
} | ||
if err := d.Set("notifications", nList); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsAutoscalingNotificationUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).autoscalingconn | ||
|
||
// Notifications API call is a PUT, so we don't need to diff the list, just | ||
// push whatever it is and AWS sorts it out | ||
nl := convertSetToList(d.Get("notifications").(*schema.Set)) | ||
|
||
o, n := d.GetChange("group_names") | ||
if o == nil { | ||
o = new(schema.Set) | ||
} | ||
if n == nil { | ||
n = new(schema.Set) | ||
} | ||
|
||
os := o.(*schema.Set) | ||
ns := n.(*schema.Set) | ||
remove := convertSetToList(os.Difference(ns)) | ||
add := convertSetToList(ns.Difference(os)) | ||
|
||
topic := d.Get("topic_arn").(string) | ||
|
||
if err := removeNotificationConfigToGroupsWithTopic(conn, remove, topic); err != nil { | ||
return err | ||
} | ||
|
||
var update []*string | ||
if d.HasChange("notifications") { | ||
update = convertSetToList(d.Get("group_names").(*schema.Set)) | ||
} else { | ||
update = add | ||
} | ||
|
||
if err := addNotificationConfigToGroupsWithTopic(conn, update, nl, topic); err != nil { | ||
return err | ||
} | ||
|
||
return resourceAwsAutoscalingNotificationRead(d, meta) | ||
} | ||
|
||
func addNotificationConfigToGroupsWithTopic(conn *autoscaling.AutoScaling, groups []*string, nl []*string, topic string) error { | ||
for _, a := range groups { | ||
opts := &autoscaling.PutNotificationConfigurationInput{ | ||
AutoScalingGroupName: a, | ||
NotificationTypes: nl, | ||
TopicARN: aws.String(topic), | ||
} | ||
|
||
_, err := conn.PutNotificationConfiguration(opts) | ||
if err != nil { | ||
if awsErr, ok := err.(awserr.Error); ok { | ||
return fmt.Errorf("[WARN] Error creating Autoscaling Group Notification for Group %s, error: \"%s\", code: \"%s\"", *a, awsErr.Message(), awsErr.Code()) | ||
} | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func removeNotificationConfigToGroupsWithTopic(conn *autoscaling.AutoScaling, groups []*string, topic string) error { | ||
for _, r := range groups { | ||
opts := &autoscaling.DeleteNotificationConfigurationInput{ | ||
AutoScalingGroupName: r, | ||
TopicARN: aws.String(topic), | ||
} | ||
|
||
_, err := conn.DeleteNotificationConfiguration(opts) | ||
if err != nil { | ||
return fmt.Errorf("[WARN] Error deleting notification configuration for ASG \"%s\", Topic ARN \"%s\"", *r, topic) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func resourceAwsAutoscalingNotificationDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).autoscalingconn | ||
gl := convertSetToList(d.Get("group_names").(*schema.Set)) | ||
|
||
topic := d.Get("topic_arn").(string) | ||
if err := removeNotificationConfigToGroupsWithTopic(conn, gl, topic); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func convertSetToList(s *schema.Set) (nl []*string) { | ||
l := s.List() | ||
for _, n := range l { | ||
nl = append(nl, aws.String(n.(string))) | ||
} | ||
|
||
return nl | ||
} | ||
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.
I feel like we have this code elsewhere, but I'm unable to find it at the moment. Seems like a thing we should promote somewhere shared if it's common enough.
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.
Would be nice on
schema.Set.toList()
or similar but this one is AWS-specific.