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/spot_fleet_request - zero capacity update #12759

Merged
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
23 changes: 19 additions & 4 deletions aws/resource_aws_spot_fleet_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ func resourceAwsSpotFleetRequestCreate(d *schema.ResourceData, meta interface{})
if v, ok := d.GetOk("allocation_strategy"); ok {
spotFleetConfig.AllocationStrategy = aws.String(v.(string))
} else {
spotFleetConfig.AllocationStrategy = aws.String("lowestPrice")
spotFleetConfig.AllocationStrategy = aws.String(ec2.AllocationStrategyLowestPrice)
}

if v, ok := d.GetOk("instance_pools_to_use_count"); ok && v.(int) != 1 {
Expand Down Expand Up @@ -1479,11 +1479,10 @@ func resourceAwsSpotFleetRequestUpdate(d *schema.ResourceData, meta interface{})
updateFlag := false

if d.HasChange("target_capacity") {
if val, ok := d.GetOk("target_capacity"); ok {
if val, ok := d.GetOkExists("target_capacity"); ok {
req.TargetCapacity = aws.Int64(int64(val.(int)))
updateFlag = true
}

updateFlag = true
}

if d.HasChange("excess_capacity_termination_policy") {
Expand All @@ -1495,9 +1494,25 @@ func resourceAwsSpotFleetRequestUpdate(d *schema.ResourceData, meta interface{})
}

if updateFlag {
log.Printf("[DEBUG] Modifying Spot Fleet Request: %#v", req)
if _, err := conn.ModifySpotFleetRequest(req); err != nil {
return fmt.Errorf("error updating spot request (%s): %s", d.Id(), err)
}

log.Println("[INFO] Waiting for Spot Fleet Request to be modified")
stateConf := &resource.StateChangeConf{
Pending: []string{ec2.BatchStateModifying},
Target: []string{ec2.BatchStateActive},
Refresh: resourceAwsSpotFleetRequestStateRefreshFunc(d, meta),
Timeout: 10 * time.Minute,
MinTimeout: 10 * time.Second,
Delay: 30 * time.Second,
}

_, err := stateConf.WaitForState()
if err != nil {
return err
}
}

if d.HasChange("tags") {
Expand Down
62 changes: 62 additions & 0 deletions aws/resource_aws_spot_fleet_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,48 @@ func TestAccAWSSpotFleetRequest_WithTargetGroups(t *testing.T) {
})
}

func TestAccAWSSpotFleetRequest_zero_capacity(t *testing.T) {
var sfr ec2.SpotFleetRequestConfig
rName := acctest.RandomWithPrefix("tf-acc-test")
validUntil := time.Now().UTC().Add(24 * time.Hour).Format(time.RFC3339)
resourceName := "aws_spot_fleet_request.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEc2SpotFleetRequest(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSpotFleetRequestZeroCapacityConfig(rName, validUntil),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr),
resource.TestCheckResourceAttr(resourceName, "target_capacity", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait_for_fulfillment"},
},
{
Config: testAccAWSSpotFleetRequestConfig(rName, validUntil),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr),
resource.TestCheckResourceAttr(resourceName, "target_capacity", "2"),
),
},
{
Config: testAccAWSSpotFleetRequestZeroCapacityConfig(rName, validUntil),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr),
resource.TestCheckResourceAttr(resourceName, "target_capacity", "0"),
),
},
},
})
}

func TestAccAWSSpotFleetRequest_WithInstanceStoreAmi(t *testing.T) {
t.Skip("Test fails due to test harness constraints")
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down Expand Up @@ -2308,3 +2350,23 @@ resource "aws_spot_fleet_request" "test" {
}
`, rName, validUntil)
}

func testAccAWSSpotFleetRequestZeroCapacityConfig(rName string, validUntil string) string {
return testAccAWSSpotFleetRequestConfigBase(rName) + fmt.Sprintf(`
resource "aws_spot_fleet_request" "test" {
iam_fleet_role = "${aws_iam_role.test.arn}"
spot_price = "0.05"
target_capacity = 0
valid_until = %[1]q
terminate_instances_with_expiration = true
instance_interruption_behaviour = "stop"
wait_for_fulfillment = true
launch_specification {
instance_type = "${data.aws_ec2_instance_type_offering.available.instance_type}"
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
key_name = "${aws_key_pair.test.key_name}"
}
depends_on = ["aws_iam_policy_attachment.test"]
}
`, validUntil)
}