2015-06-04 22:01:04 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-08-21 00:27:35 +02:00
|
|
|
"log"
|
2015-06-04 22:01:04 +02:00
|
|
|
|
|
|
|
"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{
|
2015-06-05 17:00:39 +02:00
|
|
|
Type: schema.TypeSet,
|
2015-06-04 22:01:04 +02:00
|
|
|
Required: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsAutoscalingNotificationCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).autoscalingconn
|
2015-06-05 17:00:39 +02:00
|
|
|
gl := convertSetToList(d.Get("group_names").(*schema.Set))
|
|
|
|
nl := convertSetToList(d.Get("notifications").(*schema.Set))
|
2015-06-04 22:01:04 +02:00
|
|
|
|
|
|
|
topic := d.Get("topic_arn").(string)
|
2015-06-05 17:00:39 +02:00
|
|
|
if err := addNotificationConfigToGroupsWithTopic(conn, gl, nl, topic); err != nil {
|
2015-06-04 22:01:04 +02:00
|
|
|
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
|
2015-06-05 17:00:39 +02:00
|
|
|
gl := convertSetToList(d.Get("group_names").(*schema.Set))
|
2015-06-04 22:01:04 +02:00
|
|
|
|
|
|
|
opts := &autoscaling.DescribeNotificationConfigurationsInput{
|
2015-06-05 17:00:39 +02:00
|
|
|
AutoScalingGroupNames: gl,
|
2015-06-04 22:01:04 +02:00
|
|
|
}
|
|
|
|
|
2015-06-05 17:00:39 +02:00
|
|
|
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
|
2015-06-04 22:01:04 +02:00
|
|
|
gRaw := make(map[string]bool)
|
|
|
|
nRaw := make(map[string]bool)
|
2015-08-21 00:27:35 +02:00
|
|
|
|
|
|
|
i := 0
|
|
|
|
err := conn.DescribeNotificationConfigurationsPages(opts, func(resp *autoscaling.DescribeNotificationConfigurationsOutput, lastPage bool) bool {
|
|
|
|
if resp != nil {
|
|
|
|
i++
|
2015-08-21 17:51:16 +02:00
|
|
|
log.Printf("[DEBUG] Paging DescribeNotificationConfigurations for (%s), page: %d", d.Id(), i)
|
2015-08-21 00:27:35 +02:00
|
|
|
} else {
|
2015-08-21 17:51:16 +02:00
|
|
|
log.Printf("[DEBUG] Paging finished for DescribeNotificationConfigurations (%s)", d.Id())
|
2015-06-04 22:01:04 +02:00
|
|
|
}
|
2015-08-21 00:27:35 +02:00
|
|
|
|
|
|
|
for _, n := range resp.NotificationConfigurations {
|
|
|
|
if *n.TopicARN == topic {
|
|
|
|
gRaw[*n.AutoScalingGroupName] = true
|
|
|
|
nRaw[*n.NotificationType] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true // return false to stop paging
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-06-04 22:01:04 +02:00
|
|
|
}
|
|
|
|
|
2015-06-05 17:00:39 +02:00
|
|
|
// Grab the keys here as the list of Groups
|
2015-06-04 22:01:04 +02:00
|
|
|
var gList []string
|
|
|
|
for k, _ := range gRaw {
|
|
|
|
gList = append(gList, k)
|
|
|
|
}
|
2015-06-05 17:00:39 +02:00
|
|
|
|
|
|
|
// Grab the keys here as the list of Types
|
2015-06-04 22:01:04 +02:00
|
|
|
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
|
|
|
|
|
2015-06-05 17:00:39 +02:00
|
|
|
// 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))
|
2015-06-04 22:01:04 +02:00
|
|
|
|
|
|
|
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)
|
2015-06-05 17:00:39 +02:00
|
|
|
remove := convertSetToList(os.Difference(ns))
|
|
|
|
add := convertSetToList(ns.Difference(os))
|
2015-06-04 22:01:04 +02:00
|
|
|
|
|
|
|
topic := d.Get("topic_arn").(string)
|
|
|
|
|
|
|
|
if err := removeNotificationConfigToGroupsWithTopic(conn, remove, topic); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-05 17:00:39 +02:00
|
|
|
var update []*string
|
2015-06-04 22:01:04 +02:00
|
|
|
if d.HasChange("notifications") {
|
2015-06-05 17:00:39 +02:00
|
|
|
update = convertSetToList(d.Get("group_names").(*schema.Set))
|
2015-06-04 22:01:04 +02:00
|
|
|
} else {
|
|
|
|
update = add
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addNotificationConfigToGroupsWithTopic(conn, update, nl, topic); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceAwsAutoscalingNotificationRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2015-06-05 17:00:39 +02:00
|
|
|
func addNotificationConfigToGroupsWithTopic(conn *autoscaling.AutoScaling, groups []*string, nl []*string, topic string) error {
|
2015-06-04 22:01:04 +02:00
|
|
|
for _, a := range groups {
|
|
|
|
opts := &autoscaling.PutNotificationConfigurationInput{
|
2015-06-05 17:00:39 +02:00
|
|
|
AutoScalingGroupName: a,
|
2015-06-04 22:01:04 +02:00
|
|
|
NotificationTypes: nl,
|
|
|
|
TopicARN: aws.String(topic),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.PutNotificationConfiguration(opts)
|
|
|
|
if err != nil {
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
2015-06-05 17:00:39 +02:00
|
|
|
return fmt.Errorf("[WARN] Error creating Autoscaling Group Notification for Group %s, error: \"%s\", code: \"%s\"", *a, awsErr.Message(), awsErr.Code())
|
2015-06-04 22:01:04 +02:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-05 17:00:39 +02:00
|
|
|
func removeNotificationConfigToGroupsWithTopic(conn *autoscaling.AutoScaling, groups []*string, topic string) error {
|
2015-06-04 22:01:04 +02:00
|
|
|
for _, r := range groups {
|
|
|
|
opts := &autoscaling.DeleteNotificationConfigurationInput{
|
2015-06-05 17:00:39 +02:00
|
|
|
AutoScalingGroupName: r,
|
2015-06-04 22:01:04 +02:00
|
|
|
TopicARN: aws.String(topic),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.DeleteNotificationConfiguration(opts)
|
|
|
|
if err != nil {
|
2015-06-05 17:00:39 +02:00
|
|
|
return fmt.Errorf("[WARN] Error deleting notification configuration for ASG \"%s\", Topic ARN \"%s\"", *r, topic)
|
2015-06-04 22:01:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsAutoscalingNotificationDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).autoscalingconn
|
2015-06-05 17:00:39 +02:00
|
|
|
gl := convertSetToList(d.Get("group_names").(*schema.Set))
|
2015-06-04 22:01:04 +02:00
|
|
|
|
|
|
|
topic := d.Get("topic_arn").(string)
|
2015-06-05 17:00:39 +02:00
|
|
|
if err := removeNotificationConfigToGroupsWithTopic(conn, gl, topic); err != nil {
|
2015-06-04 22:01:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-05 17:00:39 +02:00
|
|
|
func convertSetToList(s *schema.Set) (nl []*string) {
|
|
|
|
l := s.List()
|
2015-06-04 22:01:04 +02:00
|
|
|
for _, n := range l {
|
2015-06-05 17:00:39 +02:00
|
|
|
nl = append(nl, aws.String(n.(string)))
|
2015-06-04 22:01:04 +02:00
|
|
|
}
|
|
|
|
|
2015-06-05 17:00:39 +02:00
|
|
|
return nl
|
2015-06-04 22:01:04 +02:00
|
|
|
}
|