Skip to content

Commit

Permalink
Merge pull request #5250 from gordonbondon/launch-template-latest-ver…
Browse files Browse the repository at this point in the history
…sion

r/aws_launch_template: Set latest version computed on change
  • Loading branch information
bflad authored Jul 30, 2018
2 parents 0e73074 + 86ebbff commit 2a1a0d8
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
15 changes: 15 additions & 0 deletions aws/resource_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/customdiff"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
Expand Down Expand Up @@ -415,6 +416,20 @@ func resourceAwsLaunchTemplate() *schema.Resource {

"tags": tagsSchema(),
},

CustomizeDiff: customdiff.Sequence(
customdiff.ComputedIf("latest_version", func(diff *schema.ResourceDiff, meta interface{}) bool {
for _, changedKey := range diff.GetChangedKeysPrefix("") {
switch changedKey {
case "name", "name_prefix", "description", "default_version", "latest_version":
continue
default:
return true
}
}
return false
}),
),
}
}

Expand Down
80 changes: 76 additions & 4 deletions aws/resource_aws_launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,31 @@ func TestAccAWSLaunchTemplate_data(t *testing.T) {
func TestAccAWSLaunchTemplate_update(t *testing.T) {
var template ec2.LaunchTemplate
resName := "aws_launch_template.foo"
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchTemplateConfig_basic(rInt),
Config: testAccAWSLaunchTemplateConfig_asg_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchTemplateExists(resName, &template),
resource.TestCheckResourceAttr(resName, "default_version", "1"),
resource.TestCheckResourceAttr(resName, "latest_version", "1"),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "launch_template.0.version", "1"),
),
},
{
Config: testAccAWSLaunchTemplateConfig_data(rInt),
Config: testAccAWSLaunchTemplateConfig_asg_update,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchTemplateExists(resName, &template),
resource.TestCheckResourceAttr(resName, "default_version", "1"),
resource.TestCheckResourceAttr(resName, "latest_version", "2"),
resource.TestCheckResourceAttrSet(resName, "image_id"),
resource.TestCheckResourceAttrSet(resName, "instance_type"),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "launch_template.0.version", "2"),
),
},
},
Expand Down Expand Up @@ -415,3 +418,72 @@ resource "aws_launch_template" "test" {
}
}
`

const testAccAWSLaunchTemplateConfig_asg_basic = `
data "aws_ami" "test_ami" {
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn-ami-hvm-*-x86_64-gp2"]
}
}
resource "aws_launch_template" "foo" {
name_prefix = "foobar"
image_id = "${data.aws_ami.test_ami.id}"
}
data "aws_availability_zones" "available" {}
resource "aws_autoscaling_group" "bar" {
availability_zones = ["${data.aws_availability_zones.available.names[0]}"]
desired_capacity = 0
max_size = 0
min_size = 0
launch_template = {
id = "${aws_launch_template.foo.id}"
version = "${aws_launch_template.foo.latest_version}"
}
}
`

const testAccAWSLaunchTemplateConfig_asg_update = `
data "aws_ami" "test_ami" {
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn-ami-hvm-*-x86_64-gp2"]
}
}
resource "aws_launch_template" "foo" {
name_prefix = "foobar"
image_id = "${data.aws_ami.test_ami.id}"
instance_type = "t2.nano"
}
data "aws_availability_zones" "available" {}
resource "aws_autoscaling_group" "bar" {
availability_zones = ["${data.aws_availability_zones.available.names[0]}"]
desired_capacity = 0
max_size = 0
min_size = 0
launch_template = {
id = "${aws_launch_template.foo.id}"
version = "${aws_launch_template.foo.latest_version}"
}
}
`

0 comments on commit 2a1a0d8

Please sign in to comment.