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

Use placement group in spot fleet requests when it is specified #10103

Merged
merged 1 commit into from
Sep 24, 2019
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
8 changes: 8 additions & 0 deletions aws/resource_aws_spot_fleet_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,14 @@ func buildSpotFleetLaunchSpecification(d map[string]interface{}, meta interface{
opts.Placement = placement
}

if v, ok := d["placement_group"]; ok {
if v.(string) != "" {
// If instanceInterruptionBehavior is set to STOP, this can't be set at all, even to an empty string, so check for "" to avoid those errors
placement.GroupName = aws.String(v.(string))
opts.Placement = placement
}
}

if v, ok := d["ebs_optimized"]; ok {
opts.EbsOptimized = aws.Bool(v.(bool))
}
Expand Down
25 changes: 17 additions & 8 deletions aws/resource_aws_spot_fleet_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func TestAccAWSSpotFleetRequest_withTags(t *testing.T) {
})
}

func TestAccAWSSpotFleetRequest_placementTenancy(t *testing.T) {
func TestAccAWSSpotFleetRequest_placementTenancyAndGroup(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: It is generally preferable to create new testing rather than modify existing tests to ensure we do not introduce regressions (even if it is very verbose to do so). 👍

var sfr ec2.SpotFleetRequestConfig
rName := acctest.RandString(10)
rInt := acctest.RandInt()
Expand All @@ -631,11 +631,11 @@ func TestAccAWSSpotFleetRequest_placementTenancy(t *testing.T) {
CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSpotFleetRequestTenancyConfig(rName, rInt),
Config: testAccAWSSpotFleetRequestTenancyGroupConfig(rName, rInt),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSSpotFleetRequestExists("aws_spot_fleet_request.foo", &sfr),
resource.TestCheckResourceAttr("aws_spot_fleet_request.foo", "spot_request_state", "active"),
testAccCheckAWSSpotFleetRequest_PlacementAttributes(&sfr),
testAccCheckAWSSpotFleetRequest_PlacementAttributes(&sfr, rName),
),
},
},
Expand Down Expand Up @@ -780,7 +780,7 @@ func testAccCheckAWSSpotFleetRequest_EBSAttributes(
}

func testAccCheckAWSSpotFleetRequest_PlacementAttributes(
sfr *ec2.SpotFleetRequestConfig) resource.TestCheckFunc {
sfr *ec2.SpotFleetRequestConfig, rName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if len(sfr.SpotFleetRequestConfig.LaunchSpecifications) == 0 {
return errors.New("Missing launch specification")
Expand All @@ -795,6 +795,9 @@ func testAccCheckAWSSpotFleetRequest_PlacementAttributes(
if *placement.Tenancy != "dedicated" {
return fmt.Errorf("Expected placement tenancy to be %q, got %q", "dedicated", *placement.Tenancy)
}
if aws.StringValue(placement.GroupName) != fmt.Sprintf("test-pg-%s", rName) {
return fmt.Errorf("Expected placement group to be %q, got %q", fmt.Sprintf("test-pg-%s", rName), aws.StringValue(placement.GroupName))
}

return nil
}
Expand Down Expand Up @@ -1646,8 +1649,13 @@ resource "aws_spot_fleet_request" "foo" {
`)
}

func testAccAWSSpotFleetRequestTenancyConfig(rName string, rInt int) string {
return testAccAWSSpotFleetRequestConfigBase(rName, rInt) + fmt.Sprint(`
func testAccAWSSpotFleetRequestTenancyGroupConfig(rName string, rInt int) string {
return testAccAWSSpotFleetRequestConfigBase(rName, rInt) + fmt.Sprintf(`
resource "aws_placement_group" "test" {
name = "test-pg-%s"
strategy = "cluster"
}

resource "aws_spot_fleet_request" "foo" {
iam_fleet_role = "${aws_iam_role.test-role.arn}"
spot_price = "0.005"
Expand All @@ -1658,9 +1666,10 @@ resource "aws_spot_fleet_request" "foo" {
instance_type = "m1.small"
ami = "ami-d06a90b0"
key_name = "${aws_key_pair.debugging.key_name}"
placement_tenancy = "dedicated"
placement_tenancy = "dedicated"
placement_group = "${aws_placement_group.test.name}"
}
depends_on = ["aws_iam_policy_attachment.test-attach"]
}
`)
`, rName)
}