From d108511af51981b9584ede23d9891c1b6914e84f Mon Sep 17 00:00:00 2001 From: Kash Date: Fri, 21 Sep 2018 15:18:51 -0400 Subject: [PATCH] only set market options if present --- aws/resource_aws_launch_template.go | 32 ++++-- aws/resource_aws_launch_template_test.go | 123 +++++++++++++++++++++++ 2 files changed, 146 insertions(+), 9 deletions(-) diff --git a/aws/resource_aws_launch_template.go b/aws/resource_aws_launch_template.go index 14105381880..667298b6e0d 100644 --- a/aws/resource_aws_launch_template.go +++ b/aws/resource_aws_launch_template.go @@ -727,17 +727,31 @@ func getInstanceMarketOptions(m *ec2.LaunchTemplateInstanceMarketOptions) []inte mo := map[string]interface{}{ "market_type": aws.StringValue(m.MarketType), } - spot := []interface{}{} so := m.SpotOptions if so != nil { - spot = append(spot, map[string]interface{}{ - "block_duration_minutes": aws.Int64Value(so.BlockDurationMinutes), - "instance_interruption_behavior": aws.StringValue(so.InstanceInterruptionBehavior), - "max_price": aws.StringValue(so.MaxPrice), - "spot_instance_type": aws.StringValue(so.SpotInstanceType), - "valid_until": aws.TimeValue(so.ValidUntil).Format(time.RFC3339), - }) - mo["spot_options"] = spot + spotOptions := map[string]interface{}{} + + if so.BlockDurationMinutes != nil { + spotOptions["block_duration_minutes"] = aws.Int64Value(so.BlockDurationMinutes) + } + + if so.InstanceInterruptionBehavior != nil { + spotOptions["instance_interruption_behavior"] = aws.StringValue(so.InstanceInterruptionBehavior) + } + + if so.MaxPrice != nil { + spotOptions["max_price"] = aws.StringValue(so.MaxPrice) + } + + if so.SpotInstanceType != nil { + spotOptions["spot_instance_type"] = aws.StringValue(so.SpotInstanceType) + } + + if so.ValidUntil != nil { + spotOptions["valid_until"] = aws.TimeValue(so.ValidUntil).Format(time.RFC3339) + } + + mo["spot_options"] = []interface{}{spotOptions} } s = append(s, mo) } diff --git a/aws/resource_aws_launch_template_test.go b/aws/resource_aws_launch_template_test.go index 57f748b0c2c..abe0d519d43 100644 --- a/aws/resource_aws_launch_template_test.go +++ b/aws/resource_aws_launch_template_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/autoscaling" "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" @@ -379,6 +380,43 @@ func TestAccAWSLaunchTemplate_networkInterface_ipv6AddressCount(t *testing.T) { }) } +func TestAccAWSLaunchTemplate_instanceMarketOptions(t *testing.T) { + var template ec2.LaunchTemplate + var group autoscaling.Group + templateName := "aws_launch_template.test" + groupName := "aws_autoscaling_group.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLaunchTemplateDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLaunchTemplateConfig_instanceMarketOptions_basic, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSLaunchTemplateExists(templateName, &template), + testAccCheckAWSAutoScalingGroupExists(groupName, &group), + resource.TestCheckResourceAttr(templateName, "instance_market_options.#", "1"), + resource.TestCheckResourceAttr(templateName, "instance_market_options.0.spot_options.#", "1"), + resource.TestCheckResourceAttr(groupName, "launch_template.#", "1"), + resource.TestCheckResourceAttr(groupName, "launch_template.0.version", "1"), + ), + }, + { + Config: testAccAWSLaunchTemplateConfig_instanceMarketOptions_update, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSLaunchTemplateExists(templateName, &template), + testAccCheckAWSAutoScalingGroupExists(groupName, &group), + resource.TestCheckResourceAttr(templateName, "instance_market_options.#", "1"), + resource.TestCheckResourceAttr(templateName, "instance_market_options.0.spot_options.#", "1"), + resource.TestCheckResourceAttr(groupName, "launch_template.#", "1"), + resource.TestCheckResourceAttr(groupName, "launch_template.0.version", "2"), + ), + }, + }, + }) +} + func testAccCheckAWSLaunchTemplateExists(n string, t *ec2.LaunchTemplate) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -752,3 +790,88 @@ resource "aws_autoscaling_group" "bar" { } } ` + +const testAccAWSLaunchTemplateConfig_instanceMarketOptions_basic = ` +data "aws_ami" "test" { + most_recent = true + + filter { + name = "owner-alias" + values = ["amazon"] + } + + filter { + name = "name" + values = ["amzn-ami-hvm-*-x86_64-gp2"] + } +} + +resource "aws_launch_template" "test" { + name_prefix = "instance_market_options" + image_id = "${data.aws_ami.test.id}" + + instance_market_options { + market_type = "spot" + spot_options { + spot_instance_type = "one-time" + } + } +} + +data "aws_availability_zones" "available" {} + +resource "aws_autoscaling_group" "test" { + availability_zones = ["${data.aws_availability_zones.available.names[0]}"] + desired_capacity = 0 + min_size = 0 + max_size = 0 + + launch_template { + id = "${aws_launch_template.test.id}" + version = "${aws_launch_template.test.latest_version}" + } +} +` + +const testAccAWSLaunchTemplateConfig_instanceMarketOptions_update = ` +data "aws_ami" "test" { + most_recent = true + + filter { + name = "owner-alias" + values = ["amazon"] + } + + filter { + name = "name" + values = ["amzn-ami-hvm-*-x86_64-gp2"] + } +} + +resource "aws_launch_template" "test" { + name_prefix = "instance_market_options" + image_id = "${data.aws_ami.test.id}" + instance_type = "t2.micro" + + instance_market_options { + market_type = "spot" + spot_options { + spot_instance_type = "one-time" + } + } +} + +data "aws_availability_zones" "available" {} + +resource "aws_autoscaling_group" "test" { + availability_zones = ["${data.aws_availability_zones.available.names[0]}"] + desired_capacity = 0 + min_size = 0 + max_size = 0 + + launch_template { + id = "${aws_launch_template.test.id}" + version = "${aws_launch_template.test.latest_version}" + } +} +`