2014-07-10 01:00:11 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/flatmap"
|
|
|
|
"github.com/hashicorp/terraform/helper/diff"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/goamz/autoscaling"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resource_aws_autoscaling_group_create(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
d *terraform.ResourceDiff,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
autoscalingconn := p.autoscalingconn
|
|
|
|
|
|
|
|
// Merge the diff into the state so that we have all the attributes
|
|
|
|
// properly.
|
|
|
|
rs := s.MergeDiff(d)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
autoScalingGroupOpts := autoscaling.CreateAutoScalingGroup{}
|
|
|
|
|
|
|
|
if rs.Attributes["min_size"] != "" {
|
|
|
|
autoScalingGroupOpts.MinSize, err = strconv.Atoi(rs.Attributes["min_size"])
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Attributes["max_size"] != "" {
|
|
|
|
autoScalingGroupOpts.MaxSize, err = strconv.Atoi(rs.Attributes["max_size"])
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Attributes["default_cooldown"] != "" {
|
|
|
|
autoScalingGroupOpts.DefaultCooldown, err = strconv.Atoi(rs.Attributes["default_cooldown"])
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Attributes["desired_capicity"] != "" {
|
|
|
|
autoScalingGroupOpts.DesiredCapacity, err = strconv.Atoi(rs.Attributes["desired_capicity"])
|
|
|
|
}
|
|
|
|
|
2014-07-14 17:36:25 +02:00
|
|
|
if rs.Attributes["health_check_grace_period"] != "" {
|
|
|
|
autoScalingGroupOpts.HealthCheckGracePeriod, err = strconv.Atoi(rs.Attributes["health_check_grace_period"])
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error parsing configuration: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-07-10 01:30:39 +02:00
|
|
|
if _, ok := rs.Attributes["availability_zones.#"]; ok {
|
2014-07-10 01:00:11 +02:00
|
|
|
autoScalingGroupOpts.AvailZone = expandStringList(flatmap.Expand(
|
|
|
|
rs.Attributes, "availability_zones").([]interface{}))
|
|
|
|
}
|
|
|
|
|
2014-07-10 01:30:39 +02:00
|
|
|
if _, ok := rs.Attributes["load_balancers.#"]; ok {
|
2014-07-10 01:00:11 +02:00
|
|
|
autoScalingGroupOpts.LoadBalancerNames = expandStringList(flatmap.Expand(
|
|
|
|
rs.Attributes, "load_balancers").([]interface{}))
|
|
|
|
}
|
|
|
|
|
2014-07-10 01:30:39 +02:00
|
|
|
if _, ok := rs.Attributes["vpc_identifier.#"]; ok {
|
2014-07-10 01:00:11 +02:00
|
|
|
autoScalingGroupOpts.VPCZoneIdentifier = expandStringList(flatmap.Expand(
|
|
|
|
rs.Attributes, "vpc_identifier").([]interface{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
autoScalingGroupOpts.Name = rs.Attributes["name"]
|
|
|
|
autoScalingGroupOpts.HealthCheckType = rs.Attributes["health_check_type"]
|
|
|
|
autoScalingGroupOpts.LaunchConfigurationName = rs.Attributes["launch_configuration"]
|
|
|
|
|
2014-07-14 17:36:25 +02:00
|
|
|
log.Printf("[DEBUG] AutoScaling Group create configuration: %#v", autoScalingGroupOpts)
|
2014-07-10 01:00:11 +02:00
|
|
|
_, err = autoscalingconn.CreateAutoScalingGroup(&autoScalingGroupOpts)
|
|
|
|
if err != nil {
|
2014-07-14 17:36:25 +02:00
|
|
|
return nil, fmt.Errorf("Error creating AutoScaling Group: %s", err)
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rs.ID = rs.Attributes["name"]
|
|
|
|
|
2014-07-14 17:36:25 +02:00
|
|
|
log.Printf("[INFO] AutoScaling Group ID: %s", rs.ID)
|
2014-07-10 01:00:11 +02:00
|
|
|
|
|
|
|
g, err := resource_aws_autoscaling_group_retrieve(rs.ID, autoscalingconn)
|
|
|
|
if err != nil {
|
|
|
|
return rs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource_aws_autoscaling_group_update_state(rs, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_autoscaling_group_update(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
d *terraform.ResourceDiff,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
|
|
|
|
|
|
|
rs := s.MergeDiff(d)
|
|
|
|
log.Printf("ResourceDiff: %s", d)
|
|
|
|
log.Printf("ResourceState: %s", s)
|
|
|
|
log.Printf("Merged: %s", rs)
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("Did not update")
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_autoscaling_group_destroy(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
meta interface{}) error {
|
2014-07-10 21:41:06 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
autoscalingconn := p.autoscalingconn
|
2014-07-10 01:00:11 +02:00
|
|
|
|
2014-07-14 17:36:25 +02:00
|
|
|
log.Printf("[DEBUG] AutoScaling Group destroy: %v", s.ID)
|
2014-07-10 01:00:11 +02:00
|
|
|
|
2014-07-14 17:36:25 +02:00
|
|
|
deleteopts := autoscaling.DeleteAutoScalingGroup{Name: s.ID}
|
|
|
|
|
|
|
|
// You can force an autoscaling group to delete
|
|
|
|
// even if it's in the process of scaling a resource.
|
|
|
|
// Normally, you would set the min-size and max-size to 0,0
|
|
|
|
// and then delete the group. This bypasses that and leaves
|
|
|
|
// resources potentially dangling.
|
|
|
|
if s.Attributes["force_delete"] != "" {
|
|
|
|
deleteopts.ForceDelete = true
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := autoscalingconn.DeleteAutoScalingGroup(&deleteopts)
|
2014-07-10 01:00:11 +02:00
|
|
|
|
2014-07-10 21:41:06 +02:00
|
|
|
if err != nil {
|
|
|
|
autoscalingerr, ok := err.(*autoscaling.Error)
|
|
|
|
if ok && autoscalingerr.Code == "InvalidGroup.NotFound" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2014-07-10 01:00:11 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_autoscaling_group_refresh(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
autoscalingconn := p.autoscalingconn
|
|
|
|
|
|
|
|
g, err := resource_aws_autoscaling_group_retrieve(s.ID, autoscalingconn)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource_aws_autoscaling_group_update_state(s, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_autoscaling_group_diff(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
c *terraform.ResourceConfig,
|
|
|
|
meta interface{}) (*terraform.ResourceDiff, error) {
|
|
|
|
|
|
|
|
b := &diff.ResourceBuilder{
|
|
|
|
Attrs: map[string]diff.AttrType{
|
|
|
|
"min_size": diff.AttrTypeCreate,
|
|
|
|
"max_size": diff.AttrTypeCreate,
|
|
|
|
"default_cooldown": diff.AttrTypeCreate,
|
|
|
|
"name": diff.AttrTypeCreate,
|
|
|
|
"desired_capicity": diff.AttrTypeCreate,
|
|
|
|
"health_check_grace_period": diff.AttrTypeCreate,
|
|
|
|
"health_check_type": diff.AttrTypeCreate,
|
|
|
|
"launch_configuration": diff.AttrTypeCreate,
|
|
|
|
"vpc_zone_identifier": diff.AttrTypeCreate,
|
|
|
|
"load_balancers": diff.AttrTypeCreate,
|
2014-07-10 01:30:39 +02:00
|
|
|
"availability_zone": diff.AttrTypeCreate,
|
2014-07-14 17:36:25 +02:00
|
|
|
"force_delete": diff.AttrTypeCreate,
|
2014-07-10 01:00:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ComputedAttrs: []string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Diff(s, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_autoscaling_group_update_state(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
g *autoscaling.AutoScalingGroup) (*terraform.ResourceState, error) {
|
|
|
|
|
|
|
|
s.Attributes["min_size"] = strconv.Itoa(g.MinSize)
|
|
|
|
s.Attributes["max_size"] = strconv.Itoa(g.MaxSize)
|
|
|
|
s.Attributes["default_cooldown"] = strconv.Itoa(g.DefaultCooldown)
|
|
|
|
s.Attributes["name"] = g.Name
|
|
|
|
s.Attributes["desired_capacity"] = strconv.Itoa(g.DesiredCapacity)
|
|
|
|
s.Attributes["health_check_grace_period"] = strconv.Itoa(g.HealthCheckGracePeriod)
|
|
|
|
s.Attributes["health_check_type"] = g.HealthCheckType
|
|
|
|
s.Attributes["launch_configuration"] = g.LaunchConfigurationName
|
|
|
|
s.Attributes["vpc_zone_identifier"] = g.VPCZoneIdentifier
|
|
|
|
|
2014-07-10 01:30:39 +02:00
|
|
|
// Flatten our group values
|
2014-07-10 01:00:11 +02:00
|
|
|
toFlatten := make(map[string]interface{})
|
2014-07-10 01:30:39 +02:00
|
|
|
|
|
|
|
// Special case the return of amazons load balancers names in the XML having
|
|
|
|
// a blank entry
|
|
|
|
if len(g.LoadBalancerNames) > 0 && g.LoadBalancerNames[0].LoadBalancerName != "" {
|
|
|
|
toFlatten["load_balancers"] = flattenLoadBalancers(g.LoadBalancerNames)
|
|
|
|
}
|
|
|
|
|
2014-07-10 01:00:11 +02:00
|
|
|
toFlatten["availability_zones"] = flattenAvailabilityZones(g.AvailabilityZones)
|
|
|
|
|
|
|
|
for k, v := range flatmap.Flatten(toFlatten) {
|
|
|
|
s.Attributes[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a single group by it's ID
|
|
|
|
func resource_aws_autoscaling_group_retrieve(id string, autoscalingconn *autoscaling.AutoScaling) (*autoscaling.AutoScalingGroup, error) {
|
|
|
|
describeOpts := autoscaling.DescribeAutoScalingGroups{
|
|
|
|
Names: []string{id},
|
|
|
|
}
|
|
|
|
|
2014-07-14 17:36:25 +02:00
|
|
|
log.Printf("[DEBUG] AutoScaling Group describe configuration: %#v", describeOpts)
|
2014-07-10 01:00:11 +02:00
|
|
|
|
|
|
|
describeGroups, err := autoscalingconn.DescribeAutoScalingGroups(&describeOpts)
|
|
|
|
|
|
|
|
if err != nil {
|
2014-07-14 17:36:25 +02:00
|
|
|
return nil, fmt.Errorf("Error retrieving AutoScaling groups: %s", err)
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify AWS returned our sg
|
|
|
|
if len(describeGroups.AutoScalingGroups) != 1 ||
|
|
|
|
describeGroups.AutoScalingGroups[0].Name != id {
|
|
|
|
if err != nil {
|
2014-07-14 17:36:25 +02:00
|
|
|
return nil, fmt.Errorf("Unable to find AutoScaling group: %#v", describeGroups.AutoScalingGroups)
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g := describeGroups.AutoScalingGroups[0]
|
|
|
|
|
|
|
|
return &g, nil
|
|
|
|
}
|