Making the AutoScalingGroup name optiona

This commit is contained in:
stack72 2015-10-31 09:24:46 +00:00
parent 5b78a9f635
commit 69b905fb92
3 changed files with 60 additions and 3 deletions

View File

@ -25,7 +25,8 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"name": &schema.Schema{ "name": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Optional: true,
Computed: true,
ForceNew: true, ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
// https://github.com/boto/botocore/blob/9f322b1/botocore/data/autoscaling/2011-01-01/service-2.json#L1862-L1873 // https://github.com/boto/botocore/blob/9f322b1/botocore/data/autoscaling/2011-01-01/service-2.json#L1862-L1873
@ -144,7 +145,16 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{})
conn := meta.(*AWSClient).autoscalingconn conn := meta.(*AWSClient).autoscalingconn
var autoScalingGroupOpts autoscaling.CreateAutoScalingGroupInput var autoScalingGroupOpts autoscaling.CreateAutoScalingGroupInput
autoScalingGroupOpts.AutoScalingGroupName = aws.String(d.Get("name").(string))
var asgName string
if v, ok := d.GetOk("name"); ok {
asgName = v.(string)
} else {
asgName = resource.PrefixedUniqueId("tf-asg-")
d.Set("name", asgName)
}
autoScalingGroupOpts.AutoScalingGroupName = aws.String(asgName)
autoScalingGroupOpts.LaunchConfigurationName = aws.String(d.Get("launch_configuration").(string)) autoScalingGroupOpts.LaunchConfigurationName = aws.String(d.Get("launch_configuration").(string))
autoScalingGroupOpts.MinSize = aws.Int64(int64(d.Get("min_size").(int))) autoScalingGroupOpts.MinSize = aws.Int64(int64(d.Get("min_size").(int)))
autoScalingGroupOpts.MaxSize = aws.Int64(int64(d.Get("max_size").(int))) autoScalingGroupOpts.MaxSize = aws.Int64(int64(d.Get("max_size").(int)))

View File

@ -3,6 +3,7 @@ package aws
import ( import (
"fmt" "fmt"
"reflect" "reflect"
"regexp"
"strings" "strings"
"testing" "testing"
@ -71,6 +72,26 @@ func TestAccAWSAutoScalingGroup_basic(t *testing.T) {
}) })
} }
func TestAccAWSAutoScalingGroup_autoGeneratedName(t *testing.T) {
asgNameRegexp := regexp.MustCompile("^tf-asg-")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSAutoScalingGroupConfig_autoGeneratedName,
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
"aws_autoscaling_group.bar", "name", asgNameRegexp),
),
},
},
})
}
func TestAccAWSAutoScalingGroup_tags(t *testing.T) { func TestAccAWSAutoScalingGroup_tags(t *testing.T) {
var group autoscaling.Group var group autoscaling.Group
@ -348,6 +369,32 @@ func testAccCheckAWSAutoScalingGroupAttributesVPCZoneIdentifer(group *autoscalin
} }
} }
const testAccAWSAutoScalingGroupConfig_autoGeneratedName = `
resource "aws_launch_configuration" "foobar" {
image_id = "ami-21f78e11"
instance_type = "t1.micro"
}
resource "aws_autoscaling_group" "bar" {
availability_zones = ["us-west-2a"]
max_size = 1
min_size = 1
health_check_grace_period = 300
health_check_type = "ELB"
desired_capacity = 1
force_delete = true
termination_policies = ["OldestInstance","ClosestToNextInstanceHour"]
launch_configuration = "${aws_launch_configuration.foobar.name}"
tag {
key = "Foo"
value = "foo-bar"
propagate_at_launch = true
}
}
`
const testAccAWSAutoScalingGroupConfig = ` const testAccAWSAutoScalingGroupConfig = `
resource "aws_launch_configuration" "foobar" { resource "aws_launch_configuration" "foobar" {
image_id = "ami-21f78e11" image_id = "ami-21f78e11"

View File

@ -41,7 +41,7 @@ resource "aws_autoscaling_group" "bar" {
The following arguments are supported: The following arguments are supported:
* `name` - (Required) The name of the auto scale group. * `name` - (Optional) The name of the auto scale group. By default generated by terraform.
* `max_size` - (Required) The maximum size of the auto scale group. * `max_size` - (Required) The maximum size of the auto scale group.
* `min_size` - (Required) The minimum size of the auto scale group. * `min_size` - (Required) The minimum size of the auto scale group.
(See also [Waiting for Capacity](#waiting-for-capacity) below.) (See also [Waiting for Capacity](#waiting-for-capacity) below.)