providers/aws: Convert Launch Configurations to awslabs/aws-sdk-go

This commit is contained in:
Clint Shryock 2015-02-20 16:26:43 -06:00
parent 39e8f15752
commit 92bf85925b
5 changed files with 119 additions and 80 deletions

View File

@ -7,14 +7,13 @@ import (
"unicode"
"github.com/hashicorp/terraform/helper/multierror"
"github.com/mitchellh/goamz/autoscaling"
"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/goamz/elb"
"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/autoscaling"
"github.com/awslabs/aws-sdk-go/gen/route53"
"github.com/awslabs/aws-sdk-go/gen/s3"
)
@ -26,14 +25,13 @@ 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
awsAutoScalingconn *awsAutoScaling.AutoScaling
ec2conn *ec2.EC2
elbconn *elb.ELB
autoscalingconn *autoscaling.AutoScaling
s3conn *s3.S3
rdsconn *rds.Rds
r53conn *route53.Route53
region string
}
// Client configures and returns a fully initailized AWSClient
@ -67,7 +65,7 @@ func (c *Config) Client() (interface{}, error) {
log.Println("[INFO] Initializing ELB connection")
client.elbconn = elb.New(auth, region)
log.Println("[INFO] Initializing AutoScaling connection")
client.autoscalingconn = autoscaling.New(auth, region)
client.autoscalingconn = autoscaling.New(creds, c.Region, nil)
log.Println("[INFO] Initializing S3 connection")
client.s3conn = s3.New(creds, c.Region, nil)
log.Println("[INFO] Initializing RDS connection")
@ -78,8 +76,6 @@ func (c *Config) Client() (interface{}, error) {
// 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)
log.Println("[INFO] Initializing AWS Go AutoScaling connection")
client.awsAutoScalingconn = awsAutoScaling.New(creds, c.Region, nil)
}
if len(errs) > 0 {

View File

@ -123,7 +123,7 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
}
func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
autoscalingconn := meta.(*AWSClient).autoscalingconn
var autoScalingGroupOpts autoscaling.CreateAutoScalingGroupType
autoScalingGroupOpts.AutoScalingGroupName = aws.String(d.Get("name").(string))
@ -199,7 +199,7 @@ func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) e
}
func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
autoscalingconn := meta.(*AWSClient).autoscalingconn
opts := autoscaling.UpdateAutoScalingGroupType{
AutoScalingGroupName: aws.String(d.Id()),
@ -232,7 +232,7 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{})
}
func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
autoscalingconn := meta.(*AWSClient).autoscalingconn
// 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.
@ -276,7 +276,7 @@ func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{})
func getAwsAutoscalingGroup(
d *schema.ResourceData,
meta interface{}) (*autoscaling.AutoScalingGroup, error) {
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
autoscalingconn := meta.(*AWSClient).autoscalingconn
describeOpts := autoscaling.AutoScalingGroupNamesType{
AutoScalingGroupNames: []string{d.Id()},
@ -307,7 +307,7 @@ func getAwsAutoscalingGroup(
}
func resourceAwsAutoscalingGroupDrain(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
autoscalingconn := meta.(*AWSClient).autoscalingconn
// First, set the capacity to zero so the group will drain
log.Printf("[DEBUG] Reducing autoscaling group capacity to zero")

View File

@ -4,9 +4,10 @@ import (
"fmt"
"testing"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/gen/autoscaling"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/autoscaling"
)
func TestAccAWSAutoScalingGroup_basic(t *testing.T) {
@ -51,8 +52,7 @@ func TestAccAWSAutoScalingGroup_basic(t *testing.T) {
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.new", &lc),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "desired_capacity", "5"),
resource.TestCheckResourceAttrPtr(
"aws_autoscaling_group.bar", "launch_configuration", &lc.Name),
testLaunchConfigurationName("aws_autoscaling_group.bar", &lc),
),
},
},
@ -87,19 +87,19 @@ func testAccCheckAWSAutoScalingGroupDestroy(s *terraform.State) error {
// Try to find the Group
describeGroups, err := conn.DescribeAutoScalingGroups(
&autoscaling.DescribeAutoScalingGroups{
Names: []string{rs.Primary.ID},
&autoscaling.AutoScalingGroupNamesType{
AutoScalingGroupNames: []string{rs.Primary.ID},
})
if err == nil {
if len(describeGroups.AutoScalingGroups) != 0 &&
describeGroups.AutoScalingGroups[0].Name == rs.Primary.ID {
*describeGroups.AutoScalingGroups[0].AutoScalingGroupName == rs.Primary.ID {
return fmt.Errorf("AutoScaling Group still exists")
}
}
// Verify the error
ec2err, ok := err.(*autoscaling.Error)
ec2err, ok := err.(aws.APIError)
if !ok {
return err
}
@ -117,32 +117,32 @@ func testAccCheckAWSAutoScalingGroupAttributes(group *autoscaling.AutoScalingGro
return fmt.Errorf("Bad availability_zones: %s", group.AvailabilityZones[0])
}
if group.Name != "foobar3-terraform-test" {
return fmt.Errorf("Bad name: %s", group.Name)
if *group.AutoScalingGroupName != "foobar3-terraform-test" {
return fmt.Errorf("Bad name: %s", *group.AutoScalingGroupName)
}
if group.MaxSize != 5 {
return fmt.Errorf("Bad max_size: %d", group.MaxSize)
if *group.MaxSize != 5 {
return fmt.Errorf("Bad max_size: %d", *group.MaxSize)
}
if group.MinSize != 2 {
return fmt.Errorf("Bad max_size: %d", group.MinSize)
if *group.MinSize != 2 {
return fmt.Errorf("Bad max_size: %d", *group.MinSize)
}
if group.HealthCheckType != "ELB" {
return fmt.Errorf("Bad health_check_type: %s", group.HealthCheckType)
if *group.HealthCheckType != "ELB" {
return fmt.Errorf("Bad health_check_type: %s", *group.HealthCheckType)
}
if group.HealthCheckGracePeriod != 300 {
return fmt.Errorf("Bad health_check_grace_period: %d", group.HealthCheckGracePeriod)
if *group.HealthCheckGracePeriod != 300 {
return fmt.Errorf("Bad health_check_grace_period: %d", *group.HealthCheckGracePeriod)
}
if group.DesiredCapacity != 4 {
return fmt.Errorf("Bad desired_capacity: %d", group.DesiredCapacity)
if *group.DesiredCapacity != 4 {
return fmt.Errorf("Bad desired_capacity: %d", *group.DesiredCapacity)
}
if group.LaunchConfigurationName == "" {
return fmt.Errorf("Bad launch configuration name: %s", group.LaunchConfigurationName)
if *group.LaunchConfigurationName == "" {
return fmt.Errorf("Bad launch configuration name: %s", *group.LaunchConfigurationName)
}
return nil
@ -172,8 +172,8 @@ func testAccCheckAWSAutoScalingGroupExists(n string, group *autoscaling.AutoScal
conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
describeOpts := autoscaling.DescribeAutoScalingGroups{
Names: []string{rs.Primary.ID},
describeOpts := autoscaling.AutoScalingGroupNamesType{
AutoScalingGroupNames: []string{rs.Primary.ID},
}
describeGroups, err := conn.DescribeAutoScalingGroups(&describeOpts)
@ -182,7 +182,7 @@ func testAccCheckAWSAutoScalingGroupExists(n string, group *autoscaling.AutoScal
}
if len(describeGroups.AutoScalingGroups) != 1 ||
describeGroups.AutoScalingGroups[0].Name != rs.Primary.ID {
*describeGroups.AutoScalingGroups[0].AutoScalingGroupName != rs.Primary.ID {
return fmt.Errorf("AutoScaling Group not found")
}
@ -192,6 +192,21 @@ func testAccCheckAWSAutoScalingGroupExists(n string, group *autoscaling.AutoScal
}
}
func testLaunchConfigurationName(n string, lc *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if *lc.LaunchConfigurationName != rs.Primary.Attributes["launch_configuration"] {
return fmt.Errorf("Launch configuration names do not match")
}
return nil
}
}
const testAccAWSAutoScalingGroupConfig = `
resource "aws_launch_configuration" "foobar" {
name = "foobarautoscaling-terraform-test"

View File

@ -2,15 +2,17 @@ package aws
import (
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"fmt"
"log"
"time"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/gen/autoscaling"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/autoscaling"
)
func resourceAwsLaunchConfiguration() *schema.Resource {
@ -94,15 +96,26 @@ func resourceAwsLaunchConfiguration() *schema.Resource {
func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
var createLaunchConfigurationOpts autoscaling.CreateLaunchConfiguration
createLaunchConfigurationOpts.Name = d.Get("name").(string)
createLaunchConfigurationOpts.IamInstanceProfile = d.Get("iam_instance_profile").(string)
createLaunchConfigurationOpts.ImageId = d.Get("image_id").(string)
createLaunchConfigurationOpts.InstanceType = d.Get("instance_type").(string)
createLaunchConfigurationOpts.KeyName = d.Get("key_name").(string)
createLaunchConfigurationOpts.UserData = d.Get("user_data").(string)
createLaunchConfigurationOpts.AssociatePublicIpAddress = d.Get("associate_public_ip_address").(bool)
createLaunchConfigurationOpts.SpotPrice = d.Get("spot_price").(string)
var createLaunchConfigurationOpts autoscaling.CreateLaunchConfigurationType
createLaunchConfigurationOpts.LaunchConfigurationName = aws.String(d.Get("name").(string))
createLaunchConfigurationOpts.ImageID = aws.String(d.Get("image_id").(string))
createLaunchConfigurationOpts.InstanceType = aws.String(d.Get("instance_type").(string))
if v, ok := d.GetOk("user_data"); ok {
createLaunchConfigurationOpts.UserData = aws.String(base64.StdEncoding.EncodeToString([]byte(v.(string))))
}
if v, ok := d.GetOk("associate_public_ip_address"); ok {
createLaunchConfigurationOpts.AssociatePublicIPAddress = aws.Boolean(v.(bool))
}
if v, ok := d.GetOk("iam_instance_profile"); ok {
createLaunchConfigurationOpts.IAMInstanceProfile = aws.String(v.(string))
}
if v, ok := d.GetOk("key_name"); ok {
createLaunchConfigurationOpts.KeyName = aws.String(v.(string))
}
if v, ok := d.GetOk("spot_price"); ok {
createLaunchConfigurationOpts.SpotPrice = aws.String(v.(string))
}
if v, ok := d.GetOk("security_groups"); ok {
createLaunchConfigurationOpts.SecurityGroups = expandStringList(
@ -110,7 +123,7 @@ func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface
}
log.Printf("[DEBUG] autoscaling create launch configuration: %#v", createLaunchConfigurationOpts)
_, err := autoscalingconn.CreateLaunchConfiguration(&createLaunchConfigurationOpts)
err := autoscalingconn.CreateLaunchConfiguration(&createLaunchConfigurationOpts)
if err != nil {
return fmt.Errorf("Error creating launch configuration: %s", err)
}
@ -128,8 +141,8 @@ func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface
func resourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
describeOpts := autoscaling.DescribeLaunchConfigurations{
Names: []string{d.Id()},
describeOpts := autoscaling.LaunchConfigurationNamesType{
LaunchConfigurationNames: []string{d.Id()},
}
log.Printf("[DEBUG] launch configuration describe configuration: %#v", describeOpts)
@ -143,7 +156,7 @@ func resourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{}
}
// Verify AWS returned our launch configuration
if describConfs.LaunchConfigurations[0].Name != d.Id() {
if *describConfs.LaunchConfigurations[0].LaunchConfigurationName != d.Id() {
return fmt.Errorf(
"Unable to find launch configuration: %#v",
describConfs.LaunchConfigurations)
@ -151,14 +164,28 @@ func resourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{}
lc := describConfs.LaunchConfigurations[0]
d.Set("key_name", lc.KeyName)
d.Set("iam_instance_profile", lc.IamInstanceProfile)
d.Set("image_id", lc.ImageId)
d.Set("instance_type", lc.InstanceType)
d.Set("name", lc.Name)
d.Set("security_groups", lc.SecurityGroups)
d.Set("spot_price", lc.SpotPrice)
d.Set("key_name", *lc.KeyName)
d.Set("image_id", *lc.ImageID)
d.Set("instance_type", *lc.InstanceType)
d.Set("name", *lc.LaunchConfigurationName)
if lc.IAMInstanceProfile != nil {
d.Set("iam_instance_profile", *lc.IAMInstanceProfile)
} else {
d.Set("iam_instance_profile", nil)
}
if lc.SpotPrice != nil {
d.Set("spot_price", *lc.SpotPrice)
} else {
d.Set("spot_price", nil)
}
if lc.SecurityGroups != nil {
d.Set("security_groups", lc.SecurityGroups)
} else {
d.Set("security_groups", nil)
}
return nil
}
@ -166,10 +193,10 @@ func resourceAwsLaunchConfigurationDelete(d *schema.ResourceData, meta interface
autoscalingconn := meta.(*AWSClient).autoscalingconn
log.Printf("[DEBUG] Launch Configuration destroy: %v", d.Id())
_, err := autoscalingconn.DeleteLaunchConfiguration(
&autoscaling.DeleteLaunchConfiguration{Name: d.Id()})
err := autoscalingconn.DeleteLaunchConfiguration(
&autoscaling.LaunchConfigurationNameType{LaunchConfigurationName: aws.String(d.Id())})
if err != nil {
autoscalingerr, ok := err.(*autoscaling.Error)
autoscalingerr, ok := err.(aws.APIError)
if ok && autoscalingerr.Code == "InvalidConfiguration.NotFound" {
return nil
}

View File

@ -4,9 +4,10 @@ import (
"fmt"
"testing"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/gen/autoscaling"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/autoscaling"
)
func TestAccAWSLaunchConfiguration(t *testing.T) {
@ -57,19 +58,19 @@ func testAccCheckAWSLaunchConfigurationDestroy(s *terraform.State) error {
}
describe, err := conn.DescribeLaunchConfigurations(
&autoscaling.DescribeLaunchConfigurations{
Names: []string{rs.Primary.ID},
&autoscaling.LaunchConfigurationNamesType{
LaunchConfigurationNames: []string{rs.Primary.ID},
})
if err == nil {
if len(describe.LaunchConfigurations) != 0 &&
describe.LaunchConfigurations[0].Name == rs.Primary.ID {
*describe.LaunchConfigurations[0].LaunchConfigurationName == rs.Primary.ID {
return fmt.Errorf("Launch Configuration still exists")
}
}
// Verify the error
providerErr, ok := err.(*autoscaling.Error)
providerErr, ok := err.(aws.APIError)
if !ok {
return err
}
@ -83,16 +84,16 @@ func testAccCheckAWSLaunchConfigurationDestroy(s *terraform.State) error {
func testAccCheckAWSLaunchConfigurationAttributes(conf *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
return func(s *terraform.State) error {
if conf.ImageId != "ami-21f78e11" {
return fmt.Errorf("Bad image_id: %s", conf.ImageId)
if *conf.ImageID != "ami-21f78e11" {
return fmt.Errorf("Bad image_id: %s", *conf.ImageID)
}
if conf.Name != "foobar-terraform-test" {
return fmt.Errorf("Bad name: %s", conf.Name)
if *conf.LaunchConfigurationName != "foobar-terraform-test" {
return fmt.Errorf("Bad name: %s", *conf.LaunchConfigurationName)
}
if conf.InstanceType != "t1.micro" {
return fmt.Errorf("Bad instance_type: %s", conf.InstanceType)
if *conf.InstanceType != "t1.micro" {
return fmt.Errorf("Bad instance_type: %s", *conf.InstanceType)
}
return nil
@ -112,8 +113,8 @@ func testAccCheckAWSLaunchConfigurationExists(n string, res *autoscaling.LaunchC
conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
describeOpts := autoscaling.DescribeLaunchConfigurations{
Names: []string{rs.Primary.ID},
describeOpts := autoscaling.LaunchConfigurationNamesType{
LaunchConfigurationNames: []string{rs.Primary.ID},
}
describe, err := conn.DescribeLaunchConfigurations(&describeOpts)
@ -122,7 +123,7 @@ func testAccCheckAWSLaunchConfigurationExists(n string, res *autoscaling.LaunchC
}
if len(describe.LaunchConfigurations) != 1 ||
describe.LaunchConfigurations[0].Name != rs.Primary.ID {
*describe.LaunchConfigurations[0].LaunchConfigurationName != rs.Primary.ID {
return fmt.Errorf("Launch Configuration Group not found")
}