Merge pull request #1018 from hashicorp/aws-go-autoscaling

Convert AWS AutoScalingGroup to awslabs/aws-sdk-go
This commit is contained in:
Clint 2015-02-23 10:22:22 -06:00
commit f6249ff6db
2 changed files with 67 additions and 71 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/mitchellh/goamz/rds"
awsGo "github.com/awslabs/aws-sdk-go/aws"
awsAutoScaling "github.com/awslabs/aws-sdk-go/gen/autoscaling"
"github.com/awslabs/aws-sdk-go/gen/route53"
"github.com/awslabs/aws-sdk-go/gen/s3"
)
@ -25,13 +26,14 @@ type Config struct {
}
type AWSClient struct {
ec2conn *ec2.EC2
elbconn *elb.ELB
autoscalingconn *autoscaling.AutoScaling
s3conn *s3.S3
rdsconn *rds.Rds
r53conn *route53.Route53
region string
ec2conn *ec2.EC2
elbconn *elb.ELB
autoscalingconn *autoscaling.AutoScaling
s3conn *s3.S3
rdsconn *rds.Rds
r53conn *route53.Route53
region string
awsAutoScalingconn *awsAutoScaling.AutoScaling
}
// Client configures and returns a fully initailized AWSClient
@ -57,6 +59,7 @@ func (c *Config) Client() (interface{}, error) {
// store AWS region in client struct, for region specific operations such as
// bucket storage in S3
client.region = c.Region
creds := awsGo.Creds(c.AccessKey, c.SecretKey, "")
log.Println("[INFO] Initializing EC2 connection")
@ -66,14 +69,17 @@ func (c *Config) Client() (interface{}, error) {
log.Println("[INFO] Initializing AutoScaling connection")
client.autoscalingconn = autoscaling.New(auth, region)
log.Println("[INFO] Initializing S3 connection")
client.s3conn = s3.New(creds, c.Region, nil)
log.Println("[INFO] Initializing RDS connection")
client.rdsconn = rds.New(auth, region)
log.Println("[INFO] Initializing Route53 connection")
// aws-sdk-go uses v4 for signing requests, which requires all global
// endpoints to use 'us-east-1'.
// See http://docs.aws.amazon.com/general/latest/gr/sigv4_changes.html
log.Println("[INFO] Initializing Route53 connection")
client.r53conn = route53.New(creds, "us-east-1", nil)
client.s3conn = s3.New(creds, c.Region, nil)
log.Println("[INFO] Initializing AWS Go AutoScaling connection")
client.awsAutoScalingconn = awsAutoScaling.New(creds, c.Region, nil)
}
if len(errs) > 0 {

View File

@ -9,7 +9,9 @@ import (
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/autoscaling"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/gen/autoscaling"
)
func resourceAwsAutoscalingGroup() *schema.Resource {
@ -121,32 +123,27 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
}
func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
var autoScalingGroupOpts autoscaling.CreateAutoScalingGroup
autoScalingGroupOpts.Name = d.Get("name").(string)
autoScalingGroupOpts.HealthCheckType = d.Get("health_check_type").(string)
autoScalingGroupOpts.LaunchConfigurationName = d.Get("launch_configuration").(string)
autoScalingGroupOpts.MinSize = d.Get("min_size").(int)
autoScalingGroupOpts.MaxSize = d.Get("max_size").(int)
autoScalingGroupOpts.SetMinSize = true
autoScalingGroupOpts.SetMaxSize = true
autoScalingGroupOpts.AvailZone = expandStringList(
var autoScalingGroupOpts autoscaling.CreateAutoScalingGroupType
autoScalingGroupOpts.AutoScalingGroupName = aws.String(d.Get("name").(string))
autoScalingGroupOpts.HealthCheckType = aws.String(d.Get("health_check_type").(string))
autoScalingGroupOpts.LaunchConfigurationName = aws.String(d.Get("launch_configuration").(string))
autoScalingGroupOpts.MinSize = aws.Integer(d.Get("min_size").(int))
autoScalingGroupOpts.MaxSize = aws.Integer(d.Get("max_size").(int))
autoScalingGroupOpts.AvailabilityZones = expandStringList(
d.Get("availability_zones").(*schema.Set).List())
if v, ok := d.GetOk("default_cooldown"); ok {
autoScalingGroupOpts.DefaultCooldown = v.(int)
autoScalingGroupOpts.SetDefaultCooldown = true
autoScalingGroupOpts.DefaultCooldown = aws.Integer(v.(int))
}
if v, ok := d.GetOk("desired_capacity"); ok {
autoScalingGroupOpts.DesiredCapacity = v.(int)
autoScalingGroupOpts.SetDesiredCapacity = true
autoScalingGroupOpts.DesiredCapacity = aws.Integer(v.(int))
}
if v, ok := d.GetOk("health_check_grace_period"); ok {
autoScalingGroupOpts.HealthCheckGracePeriod = v.(int)
autoScalingGroupOpts.SetHealthCheckGracePeriod = true
autoScalingGroupOpts.HealthCheckGracePeriod = aws.Integer(v.(int))
}
if v, ok := d.GetOk("load_balancers"); ok && v.(*schema.Set).Len() > 0 {
@ -155,8 +152,8 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{})
}
if v, ok := d.GetOk("vpc_zone_identifier"); ok && v.(*schema.Set).Len() > 0 {
autoScalingGroupOpts.VPCZoneIdentifier = expandStringList(
v.(*schema.Set).List())
exp := expandStringList(v.(*schema.Set).List())
autoScalingGroupOpts.VPCZoneIdentifier = aws.String(strings.Join(exp, ","))
}
if v, ok := d.GetOk("termination_policies"); ok && v.(*schema.Set).Len() > 0 {
@ -165,7 +162,7 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{})
}
log.Printf("[DEBUG] AutoScaling Group create configuration: %#v", autoScalingGroupOpts)
_, err := autoscalingconn.CreateAutoScalingGroup(&autoScalingGroupOpts)
err := autoscalingconn.CreateAutoScalingGroup(&autoScalingGroupOpts)
if err != nil {
return fmt.Errorf("Error creating Autoscaling Group: %s", err)
}
@ -186,49 +183,46 @@ func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) e
}
d.Set("availability_zones", g.AvailabilityZones)
d.Set("default_cooldown", g.DefaultCooldown)
d.Set("desired_capacity", g.DesiredCapacity)
d.Set("health_check_grace_period", g.HealthCheckGracePeriod)
d.Set("health_check_type", g.HealthCheckType)
d.Set("launch_configuration", g.LaunchConfigurationName)
d.Set("default_cooldown", *g.DefaultCooldown)
d.Set("desired_capacity", *g.DesiredCapacity)
d.Set("health_check_grace_period", *g.HealthCheckGracePeriod)
d.Set("health_check_type", *g.HealthCheckType)
d.Set("launch_configuration", *g.LaunchConfigurationName)
d.Set("load_balancers", g.LoadBalancerNames)
d.Set("min_size", g.MinSize)
d.Set("max_size", g.MaxSize)
d.Set("name", g.Name)
d.Set("vpc_zone_identifier", strings.Split(g.VPCZoneIdentifier, ","))
d.Set("min_size", *g.MinSize)
d.Set("max_size", *g.MaxSize)
d.Set("name", *g.AutoScalingGroupName)
d.Set("vpc_zone_identifier", strings.Split(*g.VPCZoneIdentifier, ","))
d.Set("termination_policies", g.TerminationPolicies)
return nil
}
func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
opts := autoscaling.UpdateAutoScalingGroup{
Name: d.Id(),
opts := autoscaling.UpdateAutoScalingGroupType{
AutoScalingGroupName: aws.String(d.Id()),
}
if d.HasChange("desired_capacity") {
opts.DesiredCapacity = d.Get("desired_capacity").(int)
opts.SetDesiredCapacity = true
opts.DesiredCapacity = aws.Integer(d.Get("desired_capacity").(int))
}
if d.HasChange("launch_configuration") {
opts.LaunchConfigurationName = d.Get("launch_configuration").(string)
opts.LaunchConfigurationName = aws.String(d.Get("launch_configuration").(string))
}
if d.HasChange("min_size") {
opts.MinSize = d.Get("min_size").(int)
opts.SetMinSize = true
opts.MinSize = aws.Integer(d.Get("min_size").(int))
}
if d.HasChange("max_size") {
opts.MaxSize = d.Get("max_size").(int)
opts.SetMaxSize = true
opts.MaxSize = aws.Integer(d.Get("max_size").(int))
}
log.Printf("[DEBUG] AutoScaling Group update configuration: %#v", opts)
_, err := autoscalingconn.UpdateAutoScalingGroup(&opts)
err := autoscalingconn.UpdateAutoScalingGroup(&opts)
if err != nil {
d.Partial(true)
return fmt.Errorf("Error updating Autoscaling group: %s", err)
@ -238,7 +232,7 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{})
}
func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
// Read the autoscaling group first. If it doesn't exist, we're done.
// We need the group in order to check if there are instances attached.
@ -250,14 +244,14 @@ func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{})
if g == nil {
return nil
}
if len(g.Instances) > 0 || g.DesiredCapacity > 0 {
if len(g.Instances) > 0 || *g.DesiredCapacity > 0 {
if err := resourceAwsAutoscalingGroupDrain(d, meta); err != nil {
return err
}
}
log.Printf("[DEBUG] AutoScaling Group destroy: %v", d.Id())
deleteopts := autoscaling.DeleteAutoScalingGroup{Name: d.Id()}
deleteopts := autoscaling.DeleteAutoScalingGroupType{AutoScalingGroupName: aws.String(d.Id())}
// You can force an autoscaling group to delete
// even if it's in the process of scaling a resource.
@ -265,15 +259,14 @@ func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{})
// and then delete the group. This bypasses that and leaves
// resources potentially dangling.
if d.Get("force_delete").(bool) {
deleteopts.ForceDelete = true
deleteopts.ForceDelete = aws.Boolean(true)
}
if _, err := autoscalingconn.DeleteAutoScalingGroup(&deleteopts); err != nil {
autoscalingerr, ok := err.(*autoscaling.Error)
if err := autoscalingconn.DeleteAutoScalingGroup(&deleteopts); err != nil {
autoscalingerr, ok := err.(aws.APIError)
if ok && autoscalingerr.Code == "InvalidGroup.NotFound" {
return nil
}
return err
}
@ -283,16 +276,16 @@ func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{})
func getAwsAutoscalingGroup(
d *schema.ResourceData,
meta interface{}) (*autoscaling.AutoScalingGroup, error) {
autoscalingconn := meta.(*AWSClient).autoscalingconn
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
describeOpts := autoscaling.DescribeAutoScalingGroups{
Names: []string{d.Id()},
describeOpts := autoscaling.AutoScalingGroupNamesType{
AutoScalingGroupNames: []string{d.Id()},
}
log.Printf("[DEBUG] AutoScaling Group describe configuration: %#v", describeOpts)
describeGroups, err := autoscalingconn.DescribeAutoScalingGroups(&describeOpts)
if err != nil {
autoscalingerr, ok := err.(*autoscaling.Error)
autoscalingerr, ok := err.(aws.APIError)
if ok && autoscalingerr.Code == "InvalidGroup.NotFound" {
d.SetId("")
return nil, nil
@ -303,7 +296,7 @@ func getAwsAutoscalingGroup(
// Search for the autoscaling group
for idx, asc := range describeGroups.AutoScalingGroups {
if asc.Name == d.Id() {
if *asc.AutoScalingGroupName == d.Id() {
return &describeGroups.AutoScalingGroups[idx], nil
}
}
@ -314,20 +307,17 @@ func getAwsAutoscalingGroup(
}
func resourceAwsAutoscalingGroupDrain(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
// First, set the capacity to zero so the group will drain
log.Printf("[DEBUG] Reducing autoscaling group capacity to zero")
opts := autoscaling.UpdateAutoScalingGroup{
Name: d.Id(),
DesiredCapacity: 0,
MinSize: 0,
MaxSize: 0,
SetDesiredCapacity: true,
SetMinSize: true,
SetMaxSize: true,
opts := autoscaling.UpdateAutoScalingGroupType{
AutoScalingGroupName: aws.String(d.Id()),
DesiredCapacity: aws.Integer(0),
MinSize: aws.Integer(0),
MaxSize: aws.Integer(0),
}
if _, err := autoscalingconn.UpdateAutoScalingGroup(&opts); err != nil {
if err := autoscalingconn.UpdateAutoScalingGroup(&opts); err != nil {
return fmt.Errorf("Error setting capacity to zero to drain: %s", err)
}