Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_launch_template: Set latest version computed on change #5250

Merged
merged 1 commit into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}"
}
}
`