Merge pull request #1018 from hashicorp/aws-go-autoscaling
Convert AWS AutoScalingGroup to awslabs/aws-sdk-go
This commit is contained in:
commit
f6249ff6db
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/mitchellh/goamz/rds"
|
"github.com/mitchellh/goamz/rds"
|
||||||
|
|
||||||
awsGo "github.com/awslabs/aws-sdk-go/aws"
|
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/route53"
|
||||||
"github.com/awslabs/aws-sdk-go/gen/s3"
|
"github.com/awslabs/aws-sdk-go/gen/s3"
|
||||||
)
|
)
|
||||||
|
@ -32,6 +33,7 @@ type AWSClient struct {
|
||||||
rdsconn *rds.Rds
|
rdsconn *rds.Rds
|
||||||
r53conn *route53.Route53
|
r53conn *route53.Route53
|
||||||
region string
|
region string
|
||||||
|
awsAutoScalingconn *awsAutoScaling.AutoScaling
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client configures and returns a fully initailized AWSClient
|
// 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
|
// store AWS region in client struct, for region specific operations such as
|
||||||
// bucket storage in S3
|
// bucket storage in S3
|
||||||
client.region = c.Region
|
client.region = c.Region
|
||||||
|
|
||||||
creds := awsGo.Creds(c.AccessKey, c.SecretKey, "")
|
creds := awsGo.Creds(c.AccessKey, c.SecretKey, "")
|
||||||
|
|
||||||
log.Println("[INFO] Initializing EC2 connection")
|
log.Println("[INFO] Initializing EC2 connection")
|
||||||
|
@ -66,14 +69,17 @@ func (c *Config) Client() (interface{}, error) {
|
||||||
log.Println("[INFO] Initializing AutoScaling connection")
|
log.Println("[INFO] Initializing AutoScaling connection")
|
||||||
client.autoscalingconn = autoscaling.New(auth, region)
|
client.autoscalingconn = autoscaling.New(auth, region)
|
||||||
log.Println("[INFO] Initializing S3 connection")
|
log.Println("[INFO] Initializing S3 connection")
|
||||||
|
client.s3conn = s3.New(creds, c.Region, nil)
|
||||||
log.Println("[INFO] Initializing RDS connection")
|
log.Println("[INFO] Initializing RDS connection")
|
||||||
client.rdsconn = rds.New(auth, region)
|
client.rdsconn = rds.New(auth, region)
|
||||||
log.Println("[INFO] Initializing Route53 connection")
|
|
||||||
// aws-sdk-go uses v4 for signing requests, which requires all global
|
// aws-sdk-go uses v4 for signing requests, which requires all global
|
||||||
// endpoints to use 'us-east-1'.
|
// endpoints to use 'us-east-1'.
|
||||||
// See http://docs.aws.amazon.com/general/latest/gr/sigv4_changes.html
|
// 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.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 {
|
if len(errs) > 0 {
|
||||||
|
|
|
@ -9,7 +9,9 @@ import (
|
||||||
"github.com/hashicorp/terraform/helper/hashcode"
|
"github.com/hashicorp/terraform/helper/hashcode"
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"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 {
|
func resourceAwsAutoscalingGroup() *schema.Resource {
|
||||||
|
@ -121,32 +123,27 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
|
||||||
|
|
||||||
var autoScalingGroupOpts autoscaling.CreateAutoScalingGroup
|
var autoScalingGroupOpts autoscaling.CreateAutoScalingGroupType
|
||||||
autoScalingGroupOpts.Name = d.Get("name").(string)
|
autoScalingGroupOpts.AutoScalingGroupName = aws.String(d.Get("name").(string))
|
||||||
autoScalingGroupOpts.HealthCheckType = d.Get("health_check_type").(string)
|
autoScalingGroupOpts.HealthCheckType = aws.String(d.Get("health_check_type").(string))
|
||||||
autoScalingGroupOpts.LaunchConfigurationName = d.Get("launch_configuration").(string)
|
autoScalingGroupOpts.LaunchConfigurationName = aws.String(d.Get("launch_configuration").(string))
|
||||||
autoScalingGroupOpts.MinSize = d.Get("min_size").(int)
|
autoScalingGroupOpts.MinSize = aws.Integer(d.Get("min_size").(int))
|
||||||
autoScalingGroupOpts.MaxSize = d.Get("max_size").(int)
|
autoScalingGroupOpts.MaxSize = aws.Integer(d.Get("max_size").(int))
|
||||||
autoScalingGroupOpts.SetMinSize = true
|
autoScalingGroupOpts.AvailabilityZones = expandStringList(
|
||||||
autoScalingGroupOpts.SetMaxSize = true
|
|
||||||
autoScalingGroupOpts.AvailZone = expandStringList(
|
|
||||||
d.Get("availability_zones").(*schema.Set).List())
|
d.Get("availability_zones").(*schema.Set).List())
|
||||||
|
|
||||||
if v, ok := d.GetOk("default_cooldown"); ok {
|
if v, ok := d.GetOk("default_cooldown"); ok {
|
||||||
autoScalingGroupOpts.DefaultCooldown = v.(int)
|
autoScalingGroupOpts.DefaultCooldown = aws.Integer(v.(int))
|
||||||
autoScalingGroupOpts.SetDefaultCooldown = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := d.GetOk("desired_capacity"); ok {
|
if v, ok := d.GetOk("desired_capacity"); ok {
|
||||||
autoScalingGroupOpts.DesiredCapacity = v.(int)
|
autoScalingGroupOpts.DesiredCapacity = aws.Integer(v.(int))
|
||||||
autoScalingGroupOpts.SetDesiredCapacity = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := d.GetOk("health_check_grace_period"); ok {
|
if v, ok := d.GetOk("health_check_grace_period"); ok {
|
||||||
autoScalingGroupOpts.HealthCheckGracePeriod = v.(int)
|
autoScalingGroupOpts.HealthCheckGracePeriod = aws.Integer(v.(int))
|
||||||
autoScalingGroupOpts.SetHealthCheckGracePeriod = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := d.GetOk("load_balancers"); ok && v.(*schema.Set).Len() > 0 {
|
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 {
|
if v, ok := d.GetOk("vpc_zone_identifier"); ok && v.(*schema.Set).Len() > 0 {
|
||||||
autoScalingGroupOpts.VPCZoneIdentifier = expandStringList(
|
exp := expandStringList(v.(*schema.Set).List())
|
||||||
v.(*schema.Set).List())
|
autoScalingGroupOpts.VPCZoneIdentifier = aws.String(strings.Join(exp, ","))
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := d.GetOk("termination_policies"); ok && v.(*schema.Set).Len() > 0 {
|
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)
|
log.Printf("[DEBUG] AutoScaling Group create configuration: %#v", autoScalingGroupOpts)
|
||||||
_, err := autoscalingconn.CreateAutoScalingGroup(&autoScalingGroupOpts)
|
err := autoscalingconn.CreateAutoScalingGroup(&autoScalingGroupOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error creating Autoscaling Group: %s", err)
|
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("availability_zones", g.AvailabilityZones)
|
||||||
d.Set("default_cooldown", g.DefaultCooldown)
|
d.Set("default_cooldown", *g.DefaultCooldown)
|
||||||
d.Set("desired_capacity", g.DesiredCapacity)
|
d.Set("desired_capacity", *g.DesiredCapacity)
|
||||||
d.Set("health_check_grace_period", g.HealthCheckGracePeriod)
|
d.Set("health_check_grace_period", *g.HealthCheckGracePeriod)
|
||||||
d.Set("health_check_type", g.HealthCheckType)
|
d.Set("health_check_type", *g.HealthCheckType)
|
||||||
d.Set("launch_configuration", g.LaunchConfigurationName)
|
d.Set("launch_configuration", *g.LaunchConfigurationName)
|
||||||
d.Set("load_balancers", g.LoadBalancerNames)
|
d.Set("load_balancers", g.LoadBalancerNames)
|
||||||
d.Set("min_size", g.MinSize)
|
d.Set("min_size", *g.MinSize)
|
||||||
d.Set("max_size", g.MaxSize)
|
d.Set("max_size", *g.MaxSize)
|
||||||
d.Set("name", g.Name)
|
d.Set("name", *g.AutoScalingGroupName)
|
||||||
d.Set("vpc_zone_identifier", strings.Split(g.VPCZoneIdentifier, ","))
|
d.Set("vpc_zone_identifier", strings.Split(*g.VPCZoneIdentifier, ","))
|
||||||
d.Set("termination_policies", g.TerminationPolicies)
|
d.Set("termination_policies", g.TerminationPolicies)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{}) error {
|
func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||||
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
|
||||||
|
|
||||||
opts := autoscaling.UpdateAutoScalingGroup{
|
opts := autoscaling.UpdateAutoScalingGroupType{
|
||||||
Name: d.Id(),
|
AutoScalingGroupName: aws.String(d.Id()),
|
||||||
}
|
}
|
||||||
|
|
||||||
if d.HasChange("desired_capacity") {
|
if d.HasChange("desired_capacity") {
|
||||||
opts.DesiredCapacity = d.Get("desired_capacity").(int)
|
opts.DesiredCapacity = aws.Integer(d.Get("desired_capacity").(int))
|
||||||
opts.SetDesiredCapacity = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if d.HasChange("launch_configuration") {
|
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") {
|
if d.HasChange("min_size") {
|
||||||
opts.MinSize = d.Get("min_size").(int)
|
opts.MinSize = aws.Integer(d.Get("min_size").(int))
|
||||||
opts.SetMinSize = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if d.HasChange("max_size") {
|
if d.HasChange("max_size") {
|
||||||
opts.MaxSize = d.Get("max_size").(int)
|
opts.MaxSize = aws.Integer(d.Get("max_size").(int))
|
||||||
opts.SetMaxSize = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("[DEBUG] AutoScaling Group update configuration: %#v", opts)
|
log.Printf("[DEBUG] AutoScaling Group update configuration: %#v", opts)
|
||||||
_, err := autoscalingconn.UpdateAutoScalingGroup(&opts)
|
err := autoscalingconn.UpdateAutoScalingGroup(&opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.Partial(true)
|
d.Partial(true)
|
||||||
return fmt.Errorf("Error updating Autoscaling group: %s", err)
|
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 {
|
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.
|
// 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.
|
// 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 {
|
if g == nil {
|
||||||
return 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 {
|
if err := resourceAwsAutoscalingGroupDrain(d, meta); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("[DEBUG] AutoScaling Group destroy: %v", d.Id())
|
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
|
// You can force an autoscaling group to delete
|
||||||
// even if it's in the process of scaling a resource.
|
// 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
|
// and then delete the group. This bypasses that and leaves
|
||||||
// resources potentially dangling.
|
// resources potentially dangling.
|
||||||
if d.Get("force_delete").(bool) {
|
if d.Get("force_delete").(bool) {
|
||||||
deleteopts.ForceDelete = true
|
deleteopts.ForceDelete = aws.Boolean(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := autoscalingconn.DeleteAutoScalingGroup(&deleteopts); err != nil {
|
if err := autoscalingconn.DeleteAutoScalingGroup(&deleteopts); err != nil {
|
||||||
autoscalingerr, ok := err.(*autoscaling.Error)
|
autoscalingerr, ok := err.(aws.APIError)
|
||||||
if ok && autoscalingerr.Code == "InvalidGroup.NotFound" {
|
if ok && autoscalingerr.Code == "InvalidGroup.NotFound" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -283,16 +276,16 @@ func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{})
|
||||||
func getAwsAutoscalingGroup(
|
func getAwsAutoscalingGroup(
|
||||||
d *schema.ResourceData,
|
d *schema.ResourceData,
|
||||||
meta interface{}) (*autoscaling.AutoScalingGroup, error) {
|
meta interface{}) (*autoscaling.AutoScalingGroup, error) {
|
||||||
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
|
||||||
|
|
||||||
describeOpts := autoscaling.DescribeAutoScalingGroups{
|
describeOpts := autoscaling.AutoScalingGroupNamesType{
|
||||||
Names: []string{d.Id()},
|
AutoScalingGroupNames: []string{d.Id()},
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("[DEBUG] AutoScaling Group describe configuration: %#v", describeOpts)
|
log.Printf("[DEBUG] AutoScaling Group describe configuration: %#v", describeOpts)
|
||||||
describeGroups, err := autoscalingconn.DescribeAutoScalingGroups(&describeOpts)
|
describeGroups, err := autoscalingconn.DescribeAutoScalingGroups(&describeOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
autoscalingerr, ok := err.(*autoscaling.Error)
|
autoscalingerr, ok := err.(aws.APIError)
|
||||||
if ok && autoscalingerr.Code == "InvalidGroup.NotFound" {
|
if ok && autoscalingerr.Code == "InvalidGroup.NotFound" {
|
||||||
d.SetId("")
|
d.SetId("")
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -303,7 +296,7 @@ func getAwsAutoscalingGroup(
|
||||||
|
|
||||||
// Search for the autoscaling group
|
// Search for the autoscaling group
|
||||||
for idx, asc := range describeGroups.AutoScalingGroups {
|
for idx, asc := range describeGroups.AutoScalingGroups {
|
||||||
if asc.Name == d.Id() {
|
if *asc.AutoScalingGroupName == d.Id() {
|
||||||
return &describeGroups.AutoScalingGroups[idx], nil
|
return &describeGroups.AutoScalingGroups[idx], nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -314,20 +307,17 @@ func getAwsAutoscalingGroup(
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceAwsAutoscalingGroupDrain(d *schema.ResourceData, meta interface{}) error {
|
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
|
// First, set the capacity to zero so the group will drain
|
||||||
log.Printf("[DEBUG] Reducing autoscaling group capacity to zero")
|
log.Printf("[DEBUG] Reducing autoscaling group capacity to zero")
|
||||||
opts := autoscaling.UpdateAutoScalingGroup{
|
opts := autoscaling.UpdateAutoScalingGroupType{
|
||||||
Name: d.Id(),
|
AutoScalingGroupName: aws.String(d.Id()),
|
||||||
DesiredCapacity: 0,
|
DesiredCapacity: aws.Integer(0),
|
||||||
MinSize: 0,
|
MinSize: aws.Integer(0),
|
||||||
MaxSize: 0,
|
MaxSize: aws.Integer(0),
|
||||||
SetDesiredCapacity: true,
|
|
||||||
SetMinSize: true,
|
|
||||||
SetMaxSize: true,
|
|
||||||
}
|
}
|
||||||
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)
|
return fmt.Errorf("Error setting capacity to zero to drain: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue