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/launch_template: fix market options #5957

Merged
merged 1 commit into from
Sep 24, 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
32 changes: 23 additions & 9 deletions aws/resource_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
123 changes: 123 additions & 0 deletions aws/resource_aws_launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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}"
}
}
`