From d3f5abca1be772421c4694e28b3bb1f78e75675c Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 1 May 2020 22:20:13 +0300 Subject: [PATCH 01/21] add on demand parmas --- aws/resource_aws_spot_fleet_request.go | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index 19294744fdf..504e29cd07f 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -545,6 +545,22 @@ func resourceAwsSpotFleetRequest() *schema.Resource { }, Set: schema.HashString, }, + "on_demand_allocation_strategy": { + Type: schema.TypeString, + Optional: true, + }, + "on_demand_fulfilled_capacity": { + Type: schema.TypeFloat, + Optional: true, + }, + "on_demand_max_total_price": { + Type: schema.TypeString, + Optional: true, + }, + "on_demand_target_capacity": { + Type: schema.TypeInt, + Optional: true, + }, "tags": tagsSchema(), "tags_all": tagsSchemaComputed(), }, @@ -988,6 +1004,22 @@ func resourceAwsSpotFleetRequestCreate(d *schema.ResourceData, meta interface{}) spotFleetConfig.SpotPrice = aws.String(v.(string)) } + if v, ok := d.GetOk("on_demand_target_capacity"); ok { + spotFleetConfig.OnDemandTargetCapacity = aws.Int64(int64(v.(int))) + } + + if v, ok := d.GetOk("on_demand_allocation_strategy"); ok { + spotFleetConfig.OnDemandAllocationStrategy = aws.String(v.(string)) + } + + if v, ok := d.GetOk("on_demand_max_total_price"); ok { + spotFleetConfig.OnDemandMaxTotalPrice = aws.String(v.(string)) + } + + if v, ok := d.GetOk("on_demand_fulfilled_capacity"); ok { + spotFleetConfig.OnDemandFulfilledCapacity = aws.Float64(v.(float64)) + } + if v, ok := d.GetOk("valid_from"); ok { validFrom, err := time.Parse(time.RFC3339, v.(string)) if err != nil { @@ -1310,6 +1342,22 @@ func resourceAwsSpotFleetRequestRead(d *schema.ResourceData, meta interface{}) e } } + if config.OnDemandTargetCapacity != nil { + d.Set("on_demand_target_capacity", aws.Int64Value(config.OnDemandTargetCapacity)) + } + + if config.OnDemandAllocationStrategy != nil { + d.Set("on_demand_allocation_strategy", aws.StringValue(config.OnDemandAllocationStrategy)) + } + + if config.OnDemandMaxTotalPrice != nil { + d.Set("on_demand_max_total_price", aws.StringValue(config.OnDemandMaxTotalPrice)) + } + + if config.OnDemandFulfilledCapacity != nil { + d.Set("on_demand_fulfilled_capacity", aws.Float64Value(config.OnDemandFulfilledCapacity)) + } + if config.LoadBalancersConfig != nil { lbConf := config.LoadBalancersConfig From 55cb6c36c211340ac1a2d5db5b0be4ae533f5182 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 1 May 2020 22:33:09 +0300 Subject: [PATCH 02/21] allow updating `on_demand_target_capacity` and force new to other on demand arguments --- aws/resource_aws_spot_fleet_request.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index 504e29cd07f..252430198bc 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -548,14 +548,17 @@ func resourceAwsSpotFleetRequest() *schema.Resource { "on_demand_allocation_strategy": { Type: schema.TypeString, Optional: true, + ForceNew: true, }, "on_demand_fulfilled_capacity": { Type: schema.TypeFloat, Optional: true, + ForceNew: true, }, "on_demand_max_total_price": { Type: schema.TypeString, Optional: true, + ForceNew: true, }, "on_demand_target_capacity": { Type: schema.TypeInt, @@ -1647,6 +1650,14 @@ func resourceAwsSpotFleetRequestUpdate(d *schema.ResourceData, meta interface{}) } } + if d.HasChange("on_demand_target_capacity") { + if val, ok := d.GetOk("on_demand_target_capacity"); ok { + req.OnDemandTargetCapacity = aws.Int64(int64(val.(int))) + } + + updateFlag = true + } + if d.HasChange("excess_capacity_termination_policy") { if val, ok := d.GetOk("excess_capacity_termination_policy"); ok { req.ExcessCapacityTerminationPolicy = aws.String(val.(string)) From f5ec0499c0344e8334b2905452ceabdd435e3452 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 1 May 2020 22:35:37 +0300 Subject: [PATCH 03/21] allow zero value on demand target capacity --- aws/resource_aws_spot_fleet_request.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index 252430198bc..a5297f2204e 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -1007,7 +1007,7 @@ func resourceAwsSpotFleetRequestCreate(d *schema.ResourceData, meta interface{}) spotFleetConfig.SpotPrice = aws.String(v.(string)) } - if v, ok := d.GetOk("on_demand_target_capacity"); ok { + if v, ok := d.GetOkExists("on_demand_target_capacity"); ok { spotFleetConfig.OnDemandTargetCapacity = aws.Int64(int64(v.(int))) } From 7e0b219cd29d4885b6a1420e1da6b80861fee968 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 1 May 2020 22:40:46 +0300 Subject: [PATCH 04/21] add default and validation for `on_demand_allocation_strategy` --- aws/resource_aws_spot_fleet_request.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index a5297f2204e..377c57565aa 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -549,6 +549,11 @@ func resourceAwsSpotFleetRequest() *schema.Resource { Type: schema.TypeString, Optional: true, ForceNew: true, + Default: ec2.OnDemandAllocationStrategyLowestPrice, + ValidateFunc: validation.StringInSlice([]string{ + ec2.OnDemandAllocationStrategyPrioritized, + ec2.OnDemandAllocationStrategyLowestPrice, + }, false), }, "on_demand_fulfilled_capacity": { Type: schema.TypeFloat, From c75b1296ee81920535ba19c712967e1eb7c5dd39 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 1 May 2020 22:55:02 +0300 Subject: [PATCH 05/21] add docs --- website/docs/r/spot_fleet_request.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/website/docs/r/spot_fleet_request.html.markdown b/website/docs/r/spot_fleet_request.html.markdown index c2438c69fde..70d0cba0481 100644 --- a/website/docs/r/spot_fleet_request.html.markdown +++ b/website/docs/r/spot_fleet_request.html.markdown @@ -201,6 +201,10 @@ across different markets and instance types. Conflicts with `launch_template_con * `valid_from` - (Optional) The start date and time of the request, in UTC [RFC3339](https://tools.ietf.org/html/rfc3339#section-5.8) format(for example, YYYY-MM-DDTHH:MM:SSZ). The default is to start fulfilling the request immediately. * `load_balancers` (Optional) A list of elastic load balancer names to add to the Spot fleet. * `target_group_arns` (Optional) A list of `aws_alb_target_group` ARNs, for use with Application Load Balancing. +* `on_demand_allocation_strategy` - The order of the launch template overrides to use in fulfilling On-Demand capacity. the possible values are: `lowestPrice` and `prioritized`. the default is `lowestPrice`. +* `on_demand_fulfilled_capacity` - The number of On-Demand units fulfilled by this request compared to the set target On-Demand capacity. +* `on_demand_max_total_price` - The maximum amount per hour for On-Demand Instances that you're willing to pay. When the maximum amount you're willing to pay is reached, the fleet stops launching instances even if it hasn’t met the target capacity. +* `on_demand_target_capacity` - The number of On-Demand units to request. If the request type is `maintain`, you can specify a target capacity of 0 and add capacity later. * `tags` - (Optional) A map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level. ### Launch Template Configs From 3471810e2b43f240489c2029d3198d164b6a3578 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Sat, 2 May 2020 23:55:30 +0300 Subject: [PATCH 06/21] add test for `on_demand_target_capacity` --- aws/resource_aws_spot_fleet_request.go | 5 +- aws/resource_aws_spot_fleet_request_test.go | 75 +++++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index 377c57565aa..c78de14c6e5 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -1656,11 +1656,10 @@ func resourceAwsSpotFleetRequestUpdate(d *schema.ResourceData, meta interface{}) } if d.HasChange("on_demand_target_capacity") { - if val, ok := d.GetOk("on_demand_target_capacity"); ok { + if val, ok := d.GetOkExists("on_demand_target_capacity"); ok { req.OnDemandTargetCapacity = aws.Int64(int64(val.(int))) + updateFlag = true } - - updateFlag = true } if d.HasChange("excess_capacity_termination_policy") { diff --git a/aws/resource_aws_spot_fleet_request_test.go b/aws/resource_aws_spot_fleet_request_test.go index 30138bb5fc5..f46401dbadb 100644 --- a/aws/resource_aws_spot_fleet_request_test.go +++ b/aws/resource_aws_spot_fleet_request_test.go @@ -349,6 +349,49 @@ func TestAccAWSSpotFleetRequest_launchSpecToLaunchTemplate(t *testing.T) { }) } +func TestAccAWSSpotFleetRequest_onDemandTargetCapacity(t *testing.T) { + var sfr ec2.SpotFleetRequestConfig + rName := acctest.RandString(10) + rInt := acctest.RandInt() + 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) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSpotFleetRequestOnDemandTargetCapacityConfig(rName, rInt, validUntil, 0), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr), + resource.TestCheckResourceAttr(resourceName, "on_demand_target_capacity", "0"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"wait_for_fulfillment"}, + }, + { + Config: testAccAWSSpotFleetRequestOnDemandTargetCapacityConfig(rName, rInt, validUntil, 1), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr), + resource.TestCheckResourceAttr(resourceName, "on_demand_target_capacity", "1"), + ), + }, + { + Config: testAccAWSSpotFleetRequestOnDemandTargetCapacityConfig(rName, rInt, validUntil, 0), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr), + resource.TestCheckResourceAttr(resourceName, "on_demand_target_capacity", "0"), + ), + }, + }, + }) +} + func TestAccAWSSpotFleetRequest_instanceInterruptionBehavior(t *testing.T) { var sfr ec2.SpotFleetRequestConfig rName := acctest.RandomWithPrefix("tf-acc-test") @@ -2663,3 +2706,35 @@ resource "aws_spot_fleet_request" "test" { } `, validUntil) } + +func testAccAWSSpotFleetRequestOnDemandTargetCapacityConfig(rName string, rInt int, validUntil string, targetCapacity int) string { + return testAccAWSSpotFleetRequestConfigBase(rName, rInt) + + fmt.Sprintf(` +resource "aws_launch_template" "test" { + name = %[2]q + image_id = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = "${data.aws_ec2_instance_type_offering.available.instance_type}" + key_name = "${aws_key_pair.test.key_name}" +} + +resource "aws_spot_fleet_request" "test" { + iam_fleet_role = "${aws_iam_role.test-role.arn}" + spot_price = "0.005" + target_capacity = 2 + valid_until = %[1]q + terminate_instances_with_expiration = true + instance_interruption_behaviour = "stop" + wait_for_fulfillment = true + on_demand_target_capacity = %[3]d + + launch_template_config { + launch_template_specification { + name = "${aws_launch_template.test.name}" + version = "${aws_launch_template.test.latest_version}" + } + } + + depends_on = ["aws_iam_policy_attachment.test-attach"] +} +`, validUntil, rName, targetCapacity) +} From e40ee1ee8047727bd6f2069d0a733d5cd3691559 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 7 Aug 2020 00:20:33 +0300 Subject: [PATCH 07/21] `on_demand_fulfilled_capacity` can change by other actions, adding computed add tests for on demand max price and allocation strategy --- aws/resource_aws_spot_fleet_request.go | 1 + aws/resource_aws_spot_fleet_request_test.go | 149 ++++++++++++++++++-- 2 files changed, 135 insertions(+), 15 deletions(-) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index c78de14c6e5..15375366d6e 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -559,6 +559,7 @@ func resourceAwsSpotFleetRequest() *schema.Resource { Type: schema.TypeFloat, Optional: true, ForceNew: true, + Computed: true, }, "on_demand_max_total_price": { Type: schema.TypeString, diff --git a/aws/resource_aws_spot_fleet_request_test.go b/aws/resource_aws_spot_fleet_request_test.go index f46401dbadb..2de4a55b80c 100644 --- a/aws/resource_aws_spot_fleet_request_test.go +++ b/aws/resource_aws_spot_fleet_request_test.go @@ -351,8 +351,7 @@ func TestAccAWSSpotFleetRequest_launchSpecToLaunchTemplate(t *testing.T) { func TestAccAWSSpotFleetRequest_onDemandTargetCapacity(t *testing.T) { var sfr ec2.SpotFleetRequestConfig - rName := acctest.RandString(10) - rInt := acctest.RandInt() + rName := acctest.RandomWithPrefix("tf-acc-test") validUntil := time.Now().UTC().Add(24 * time.Hour).Format(time.RFC3339) resourceName := "aws_spot_fleet_request.test" @@ -362,7 +361,7 @@ func TestAccAWSSpotFleetRequest_onDemandTargetCapacity(t *testing.T) { CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSSpotFleetRequestOnDemandTargetCapacityConfig(rName, rInt, validUntil, 0), + Config: testAccAWSSpotFleetRequestOnDemandTargetCapacityConfig(rName, validUntil, 0), Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr), resource.TestCheckResourceAttr(resourceName, "on_demand_target_capacity", "0"), @@ -375,14 +374,14 @@ func TestAccAWSSpotFleetRequest_onDemandTargetCapacity(t *testing.T) { ImportStateVerifyIgnore: []string{"wait_for_fulfillment"}, }, { - Config: testAccAWSSpotFleetRequestOnDemandTargetCapacityConfig(rName, rInt, validUntil, 1), + Config: testAccAWSSpotFleetRequestOnDemandTargetCapacityConfig(rName, validUntil, 1), Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr), resource.TestCheckResourceAttr(resourceName, "on_demand_target_capacity", "1"), ), }, { - Config: testAccAWSSpotFleetRequestOnDemandTargetCapacityConfig(rName, rInt, validUntil, 0), + Config: testAccAWSSpotFleetRequestOnDemandTargetCapacityConfig(rName, validUntil, 0), Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr), resource.TestCheckResourceAttr(resourceName, "on_demand_target_capacity", "0"), @@ -392,6 +391,62 @@ func TestAccAWSSpotFleetRequest_onDemandTargetCapacity(t *testing.T) { }) } +func TestAccAWSSpotFleetRequest_onDemandMaxTotalPrice(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) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSpotFleetRequestOnDemandMaxTotalPriceConfig(rName, validUntil, "0.05"), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr), + resource.TestCheckResourceAttr(resourceName, "on_demand_max_total_price", "0.05"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"wait_for_fulfillment"}, + }, + }, + }) +} + +func TestAccAWSSpotFleetRequest_onDemandAllocationStrategy(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) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSpotFleetRequestOnDemandAllocationStrategyConfig(rName, validUntil, "prioritized"), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr), + resource.TestCheckResourceAttr(resourceName, "on_demand_allocation_strategy", "prioritized"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"wait_for_fulfillment"}, + }, + }, + }) +} + func TestAccAWSSpotFleetRequest_instanceInterruptionBehavior(t *testing.T) { var sfr ec2.SpotFleetRequestConfig rName := acctest.RandomWithPrefix("tf-acc-test") @@ -2711,17 +2766,17 @@ func testAccAWSSpotFleetRequestOnDemandTargetCapacityConfig(rName string, rInt i return testAccAWSSpotFleetRequestConfigBase(rName, rInt) + fmt.Sprintf(` resource "aws_launch_template" "test" { - name = %[2]q - image_id = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "${data.aws_ec2_instance_type_offering.available.instance_type}" - key_name = "${aws_key_pair.test.key_name}" + name = %[1]q + image_id = data.aws_ami.amzn-ami-minimal-hvm-ebs.id + instance_type = data.aws_ec2_instance_type_offering.available.instance_type + key_name = aws_key_pair.test.key_name } resource "aws_spot_fleet_request" "test" { - iam_fleet_role = "${aws_iam_role.test-role.arn}" + iam_fleet_role = aws_iam_role.test.arn spot_price = "0.005" target_capacity = 2 - valid_until = %[1]q + valid_until = %[2]q terminate_instances_with_expiration = true instance_interruption_behaviour = "stop" wait_for_fulfillment = true @@ -2729,12 +2784,76 @@ resource "aws_spot_fleet_request" "test" { launch_template_config { launch_template_specification { - name = "${aws_launch_template.test.name}" - version = "${aws_launch_template.test.latest_version}" + name = aws_launch_template.test.name + version = aws_launch_template.test.latest_version + } + } + + depends_on = ["aws_iam_policy_attachment.test"] +} +`, rName, validUntil, targetCapacity) +} + +func testAccAWSSpotFleetRequestOnDemandMaxTotalPriceConfig(rName, validUntil, price string) string { + return testAccAWSSpotFleetRequestConfigBase(rName) + + fmt.Sprintf(` +resource "aws_launch_template" "test" { + name = %[1]q + image_id = data.aws_ami.amzn-ami-minimal-hvm-ebs.id + instance_type = data.aws_ec2_instance_type_offering.available.instance_type + key_name = aws_key_pair.test.key_name +} + +resource "aws_spot_fleet_request" "test" { + iam_fleet_role = aws_iam_role.test.arn + spot_price = "0.005" + target_capacity = 2 + valid_until = %[2]q + terminate_instances_with_expiration = true + instance_interruption_behaviour = "stop" + wait_for_fulfillment = true + on_demand_max_total_price = %[3]q + + launch_template_config { + launch_template_specification { + name = aws_launch_template.test.name + version = aws_launch_template.test.latest_version + } + } + + depends_on = ["aws_iam_policy_attachment.test"] +} +`, rName, validUntil, price) +} + +func testAccAWSSpotFleetRequestOnDemandAllocationStrategyConfig(rName, validUntil, strategy string) string { + return testAccAWSSpotFleetRequestConfigBase(rName) + + fmt.Sprintf(` +resource "aws_launch_template" "test" { + name = %[1]q + image_id = data.aws_ami.amzn-ami-minimal-hvm-ebs.id + instance_type = data.aws_ec2_instance_type_offering.available.instance_type + key_name = aws_key_pair.test.key_name +} + +resource "aws_spot_fleet_request" "test" { + iam_fleet_role = aws_iam_role.test.arn + spot_price = "0.005" + target_capacity = 2 + valid_until = %[2]q + terminate_instances_with_expiration = true + instance_interruption_behaviour = "stop" + wait_for_fulfillment = true + on_demand_allocation_strategy = %[3]q + + launch_template_config { + launch_template_specification { + name = aws_launch_template.test.name + version = aws_launch_template.test.latest_version } } - depends_on = ["aws_iam_policy_attachment.test-attach"] + depends_on = ["aws_iam_policy_attachment.test"] } -`, validUntil, rName, targetCapacity) +`, rName, validUntil, strategy) } From 977a5a7e366559274a768883468fd9a26ef98b09 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 7 Aug 2020 00:37:32 +0300 Subject: [PATCH 08/21] add test for fulfilled capacity --- aws/resource_aws_spot_fleet_request_test.go | 60 +++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/aws/resource_aws_spot_fleet_request_test.go b/aws/resource_aws_spot_fleet_request_test.go index 2de4a55b80c..7617fee2c1e 100644 --- a/aws/resource_aws_spot_fleet_request_test.go +++ b/aws/resource_aws_spot_fleet_request_test.go @@ -447,6 +447,34 @@ func TestAccAWSSpotFleetRequest_onDemandAllocationStrategy(t *testing.T) { }) } +func TestAccAWSSpotFleetRequest_onDemandFulfilledCapacity(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) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSpotFleetRequestOnDemandFulfilledCapacityConfig(rName, validUntil, "2"), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr), + resource.TestCheckResourceAttr(resourceName, "on_demand_fulfilled_capacity", "2"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"wait_for_fulfillment"}, + }, + }, + }) +} + func TestAccAWSSpotFleetRequest_instanceInterruptionBehavior(t *testing.T) { var sfr ec2.SpotFleetRequestConfig rName := acctest.RandomWithPrefix("tf-acc-test") @@ -2857,3 +2885,35 @@ resource "aws_spot_fleet_request" "test" { } `, rName, validUntil, strategy) } + +func testAccAWSSpotFleetRequestOnDemandFulfilledCapacityConfig(rName, validUntil, capacity string) string { + return testAccAWSSpotFleetRequestConfigBase(rName) + + fmt.Sprintf(` +resource "aws_launch_template" "test" { + name = %[1]q + image_id = data.aws_ami.amzn-ami-minimal-hvm-ebs.id + instance_type = data.aws_ec2_instance_type_offering.available.instance_type + key_name = aws_key_pair.test.key_name +} + +resource "aws_spot_fleet_request" "test" { + iam_fleet_role = aws_iam_role.test.arn + spot_price = "0.005" + target_capacity = 2 + valid_until = %[2]q + terminate_instances_with_expiration = true + instance_interruption_behaviour = "stop" + wait_for_fulfillment = true + on_demand_fulfilled_capacity = %[3]q + + launch_template_config { + launch_template_specification { + name = aws_launch_template.test.name + version = aws_launch_template.test.latest_version + } + } + + depends_on = ["aws_iam_policy_attachment.test"] +} +`, rName, validUntil, capacity) +} From 114701f0259fbe381ec7c06923f202df359b1957 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 7 Aug 2020 00:48:44 +0300 Subject: [PATCH 09/21] remove fulfilled capacity --- aws/resource_aws_spot_fleet_request.go | 18 +------ aws/resource_aws_spot_fleet_request_test.go | 60 --------------------- 2 files changed, 2 insertions(+), 76 deletions(-) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index 15375366d6e..b8cb369bdaa 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -555,12 +555,6 @@ func resourceAwsSpotFleetRequest() *schema.Resource { ec2.OnDemandAllocationStrategyLowestPrice, }, false), }, - "on_demand_fulfilled_capacity": { - Type: schema.TypeFloat, - Optional: true, - ForceNew: true, - Computed: true, - }, "on_demand_max_total_price": { Type: schema.TypeString, Optional: true, @@ -1025,10 +1019,6 @@ func resourceAwsSpotFleetRequestCreate(d *schema.ResourceData, meta interface{}) spotFleetConfig.OnDemandMaxTotalPrice = aws.String(v.(string)) } - if v, ok := d.GetOk("on_demand_fulfilled_capacity"); ok { - spotFleetConfig.OnDemandFulfilledCapacity = aws.Float64(v.(float64)) - } - if v, ok := d.GetOk("valid_from"); ok { validFrom, err := time.Parse(time.RFC3339, v.(string)) if err != nil { @@ -1175,7 +1165,7 @@ func resourceAwsSpotFleetRequestStateRefreshFunc(d *schema.ResourceData, meta in spotFleetRequest := resp.SpotFleetRequestConfigs[0] - return spotFleetRequest, *spotFleetRequest.SpotFleetRequestState, nil + return spotFleetRequest, aws.StringValue(spotFleetRequest.SpotFleetRequestState), nil } } @@ -1200,7 +1190,7 @@ func resourceAwsSpotFleetRequestFulfillmentRefreshFunc(id string, conn *ec2.EC2) } cfg := resp.SpotFleetRequestConfigs[0] - status := *cfg.ActivityStatus + status := aws.StringValue(cfg.ActivityStatus) var fleetError error if status == ec2.ActivityStatusError { @@ -1363,10 +1353,6 @@ func resourceAwsSpotFleetRequestRead(d *schema.ResourceData, meta interface{}) e d.Set("on_demand_max_total_price", aws.StringValue(config.OnDemandMaxTotalPrice)) } - if config.OnDemandFulfilledCapacity != nil { - d.Set("on_demand_fulfilled_capacity", aws.Float64Value(config.OnDemandFulfilledCapacity)) - } - if config.LoadBalancersConfig != nil { lbConf := config.LoadBalancersConfig diff --git a/aws/resource_aws_spot_fleet_request_test.go b/aws/resource_aws_spot_fleet_request_test.go index 7617fee2c1e..2de4a55b80c 100644 --- a/aws/resource_aws_spot_fleet_request_test.go +++ b/aws/resource_aws_spot_fleet_request_test.go @@ -447,34 +447,6 @@ func TestAccAWSSpotFleetRequest_onDemandAllocationStrategy(t *testing.T) { }) } -func TestAccAWSSpotFleetRequest_onDemandFulfilledCapacity(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) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSSpotFleetRequestOnDemandFulfilledCapacityConfig(rName, validUntil, "2"), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr), - resource.TestCheckResourceAttr(resourceName, "on_demand_fulfilled_capacity", "2"), - ), - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"wait_for_fulfillment"}, - }, - }, - }) -} - func TestAccAWSSpotFleetRequest_instanceInterruptionBehavior(t *testing.T) { var sfr ec2.SpotFleetRequestConfig rName := acctest.RandomWithPrefix("tf-acc-test") @@ -2885,35 +2857,3 @@ resource "aws_spot_fleet_request" "test" { } `, rName, validUntil, strategy) } - -func testAccAWSSpotFleetRequestOnDemandFulfilledCapacityConfig(rName, validUntil, capacity string) string { - return testAccAWSSpotFleetRequestConfigBase(rName) + - fmt.Sprintf(` -resource "aws_launch_template" "test" { - name = %[1]q - image_id = data.aws_ami.amzn-ami-minimal-hvm-ebs.id - instance_type = data.aws_ec2_instance_type_offering.available.instance_type - key_name = aws_key_pair.test.key_name -} - -resource "aws_spot_fleet_request" "test" { - iam_fleet_role = aws_iam_role.test.arn - spot_price = "0.005" - target_capacity = 2 - valid_until = %[2]q - terminate_instances_with_expiration = true - instance_interruption_behaviour = "stop" - wait_for_fulfillment = true - on_demand_fulfilled_capacity = %[3]q - - launch_template_config { - launch_template_specification { - name = aws_launch_template.test.name - version = aws_launch_template.test.latest_version - } - } - - depends_on = ["aws_iam_policy_attachment.test"] -} -`, rName, validUntil, capacity) -} From d538543464b4d367f5244b9967a2386dce8332ef Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 7 Aug 2020 01:02:04 +0300 Subject: [PATCH 10/21] remove fulfilled capacity --- website/docs/r/spot_fleet_request.html.markdown | 1 - 1 file changed, 1 deletion(-) diff --git a/website/docs/r/spot_fleet_request.html.markdown b/website/docs/r/spot_fleet_request.html.markdown index 70d0cba0481..4feac1dec1f 100644 --- a/website/docs/r/spot_fleet_request.html.markdown +++ b/website/docs/r/spot_fleet_request.html.markdown @@ -202,7 +202,6 @@ across different markets and instance types. Conflicts with `launch_template_con * `load_balancers` (Optional) A list of elastic load balancer names to add to the Spot fleet. * `target_group_arns` (Optional) A list of `aws_alb_target_group` ARNs, for use with Application Load Balancing. * `on_demand_allocation_strategy` - The order of the launch template overrides to use in fulfilling On-Demand capacity. the possible values are: `lowestPrice` and `prioritized`. the default is `lowestPrice`. -* `on_demand_fulfilled_capacity` - The number of On-Demand units fulfilled by this request compared to the set target On-Demand capacity. * `on_demand_max_total_price` - The maximum amount per hour for On-Demand Instances that you're willing to pay. When the maximum amount you're willing to pay is reached, the fleet stops launching instances even if it hasn’t met the target capacity. * `on_demand_target_capacity` - The number of On-Demand units to request. If the request type is `maintain`, you can specify a target capacity of 0 and add capacity later. * `tags` - (Optional) A map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level. From 9ccf51c10340f8919d976adac7f9a09e8a0bab79 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Thu, 15 Oct 2020 21:04:17 +0300 Subject: [PATCH 11/21] use enum slice --- aws/resource_aws_spot_fleet_request.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index b8cb369bdaa..f70337df841 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -546,14 +546,11 @@ func resourceAwsSpotFleetRequest() *schema.Resource { Set: schema.HashString, }, "on_demand_allocation_strategy": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - Default: ec2.OnDemandAllocationStrategyLowestPrice, - ValidateFunc: validation.StringInSlice([]string{ - ec2.OnDemandAllocationStrategyPrioritized, - ec2.OnDemandAllocationStrategyLowestPrice, - }, false), + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Default: ec2.OnDemandAllocationStrategyLowestPrice, + ValidateFunc: validation.StringInSlice(ec2.OnDemandAllocationStrategy_Values(), false), }, "on_demand_max_total_price": { Type: schema.TypeString, From 9724cd791879bcb8c1f5b949928cbb225af8a360 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sat, 5 Dec 2020 21:03:46 +0200 Subject: [PATCH 12/21] rebase --- aws/resource_aws_spot_fleet_request_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_spot_fleet_request_test.go b/aws/resource_aws_spot_fleet_request_test.go index 2de4a55b80c..d1b5de2757e 100644 --- a/aws/resource_aws_spot_fleet_request_test.go +++ b/aws/resource_aws_spot_fleet_request_test.go @@ -2763,7 +2763,7 @@ resource "aws_spot_fleet_request" "test" { } func testAccAWSSpotFleetRequestOnDemandTargetCapacityConfig(rName string, rInt int, validUntil string, targetCapacity int) string { - return testAccAWSSpotFleetRequestConfigBase(rName, rInt) + + return testAccAWSSpotFleetRequestConfigBase(rName) + fmt.Sprintf(` resource "aws_launch_template" "test" { name = %[1]q From 348e2525a8c0c21c82740b7390882c6760184904 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sat, 5 Dec 2020 21:05:31 +0200 Subject: [PATCH 13/21] rebase --- aws/resource_aws_spot_fleet_request_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_spot_fleet_request_test.go b/aws/resource_aws_spot_fleet_request_test.go index d1b5de2757e..85a3a70629d 100644 --- a/aws/resource_aws_spot_fleet_request_test.go +++ b/aws/resource_aws_spot_fleet_request_test.go @@ -2762,7 +2762,7 @@ resource "aws_spot_fleet_request" "test" { `, validUntil) } -func testAccAWSSpotFleetRequestOnDemandTargetCapacityConfig(rName string, rInt int, validUntil string, targetCapacity int) string { +func testAccAWSSpotFleetRequestOnDemandTargetCapacityConfig(rName string, validUntil string, targetCapacity int) string { return testAccAWSSpotFleetRequestConfigBase(rName) + fmt.Sprintf(` resource "aws_launch_template" "test" { From c99d2d8dad8dbb064e69820ae3138941fc4beeb9 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sat, 5 Dec 2020 21:08:21 +0200 Subject: [PATCH 14/21] validations --- aws/resource_aws_spot_fleet_request.go | 54 +++++++++----------------- 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index f70337df841..e009907697b 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -265,14 +265,10 @@ func resourceAwsSpotFleetRequest() *schema.Resource { ForceNew: true, }, "placement_tenancy": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - ec2.TenancyDefault, - ec2.TenancyDedicated, - ec2.TenancyHost, - }, false), + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice(ec2.Tenancy_Values(), false), }, "spot_price": { Type: schema.TypeString, @@ -409,15 +405,11 @@ func resourceAwsSpotFleetRequest() *schema.Resource { ForceNew: false, }, "allocation_strategy": { - Type: schema.TypeString, - Optional: true, - Default: ec2.AllocationStrategyLowestPrice, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - ec2.AllocationStrategyLowestPrice, - ec2.AllocationStrategyDiversified, - ec2.AllocationStrategyCapacityOptimized, - }, false), + Type: schema.TypeString, + Optional: true, + Default: ec2.AllocationStrategyLowestPrice, + ForceNew: true, + ValidateFunc: validation.StringInSlice(ec2.AllocationStrategy_Values(), false), }, "instance_pools_to_use_count": { Type: schema.TypeInt, @@ -437,15 +429,11 @@ func resourceAwsSpotFleetRequest() *schema.Resource { }, false), }, "instance_interruption_behaviour": { - Type: schema.TypeString, - Optional: true, - Default: ec2.InstanceInterruptionBehaviorTerminate, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - ec2.InstanceInterruptionBehaviorTerminate, - ec2.InstanceInterruptionBehaviorStop, - ec2.InstanceInterruptionBehaviorHibernate, - }, false), + Type: schema.TypeString, + Optional: true, + Default: ec2.InstanceInterruptionBehaviorTerminate, + ForceNew: true, + ValidateFunc: validation.StringInSlice(ec2.InstanceInterruptionBehavior_Values(), false), }, "spot_price": { Type: schema.TypeString, @@ -470,15 +458,11 @@ func resourceAwsSpotFleetRequest() *schema.Resource { ValidateFunc: validation.IsRFC3339Time, }, "fleet_type": { - Type: schema.TypeString, - Optional: true, - Default: ec2.FleetTypeMaintain, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - ec2.FleetTypeMaintain, - ec2.FleetTypeRequest, - ec2.FleetTypeInstant, - }, false), + Type: schema.TypeString, + Optional: true, + Default: ec2.FleetTypeMaintain, + ForceNew: true, + ValidateFunc: validation.StringInSlice(ec2.FleetType_Values(), false), }, "spot_maintenance_strategies": { Type: schema.TypeList, From b10dea815111dd6e989580eb8f63c73f28fc5d3e Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sat, 5 Dec 2020 21:21:42 +0200 Subject: [PATCH 15/21] add validations --- aws/resource_aws_spot_fleet_request.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index e009907697b..00aa56b8199 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -242,9 +242,10 @@ func resourceAwsSpotFleetRequest() *schema.Resource { ForceNew: true, }, "instance_type": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice(ec2.InstanceType_Values(), false), }, "key_name": { Type: schema.TypeString, @@ -362,9 +363,10 @@ func resourceAwsSpotFleetRequest() *schema.Resource { ForceNew: true, }, "instance_type": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice(ec2.InstanceType_Values(), false), }, "spot_price": { Type: schema.TypeString, @@ -489,12 +491,10 @@ func resourceAwsSpotFleetRequest() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "replacement_strategy": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - "launch", - }, false), + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice(ec2.ReplacementStrategy_Values(), false), }, }, }, From 265ce5160ea18fdb6e5803a5154d9e97e311b6e9 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Thu, 11 Mar 2021 01:32:02 +0200 Subject: [PATCH 16/21] changelog --- .changelog/13127.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changelog/13127.txt diff --git a/.changelog/13127.txt b/.changelog/13127.txt new file mode 100644 index 00000000000..896ccfac0de --- /dev/null +++ b/.changelog/13127.txt @@ -0,0 +1,7 @@ +```release-note:enhancement +resource/aws_spot_fleet_request: Add plan time validation `instance_type` +``` + +```release-note:enhancement +resource/aws_spot_fleet_request: Add `on_demand_allocation_strategy`, `on_demand_max_total_price`, and `on_demand_target_capacity` arguments +``` \ No newline at end of file From 5aee072610586554bd04a797818a9973fac9bb03 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sat, 27 Mar 2021 19:40:41 +0300 Subject: [PATCH 17/21] error check --- aws/resource_aws_spot_fleet_request_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aws/resource_aws_spot_fleet_request_test.go b/aws/resource_aws_spot_fleet_request_test.go index 85a3a70629d..518ac37d314 100644 --- a/aws/resource_aws_spot_fleet_request_test.go +++ b/aws/resource_aws_spot_fleet_request_test.go @@ -357,6 +357,7 @@ func TestAccAWSSpotFleetRequest_onDemandTargetCapacity(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID), Providers: testAccProviders, CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, Steps: []resource.TestStep{ @@ -399,6 +400,7 @@ func TestAccAWSSpotFleetRequest_onDemandMaxTotalPrice(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID), Providers: testAccProviders, CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, Steps: []resource.TestStep{ @@ -427,6 +429,7 @@ func TestAccAWSSpotFleetRequest_onDemandAllocationStrategy(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID), Providers: testAccProviders, CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, Steps: []resource.TestStep{ From 0add81265535fb4f50b4b1fad6686214fc5b60f7 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Mon, 7 Jun 2021 20:27:12 +0300 Subject: [PATCH 18/21] fmt --- aws/resource_aws_spot_fleet_request.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index 00aa56b8199..5d6c76c4232 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -1081,7 +1081,7 @@ func resourceAwsSpotFleetRequestCreate(d *schema.ResourceData, meta interface{}) } if err != nil { - return fmt.Errorf("Error requesting spot fleet: %s", err) + return fmt.Errorf("Error requesting spot fleet: %w", err) } d.SetId(aws.StringValue(resp.SpotFleetRequestId)) @@ -1132,7 +1132,7 @@ func resourceAwsSpotFleetRequestStateRefreshFunc(d *schema.ResourceData, meta in resp, err := conn.DescribeSpotFleetRequests(req) if err != nil { - log.Printf("Error on retrieving Spot Fleet Request when waiting: %s", err) + log.Printf("Error on retrieving Spot Fleet Request when waiting: %w", err) return nil, "", nil } @@ -1158,7 +1158,7 @@ func resourceAwsSpotFleetRequestFulfillmentRefreshFunc(id string, conn *ec2.EC2) resp, err := conn.DescribeSpotFleetRequests(req) if err != nil { - log.Printf("Error on retrieving Spot Fleet Request when waiting: %s", err) + log.Printf("Error on retrieving Spot Fleet Request when waiting: %w", err) return nil, "", nil } @@ -1298,7 +1298,7 @@ func resourceAwsSpotFleetRequestRead(d *schema.ResourceData, meta interface{}) e launchSpec, err := launchSpecsToSet(config.LaunchSpecifications, conn) if err != nil { - return fmt.Errorf("error occurred while reading launch specification: %s", err) + return fmt.Errorf("error occurred while reading launch specification: %w", err) } d.Set("replace_unhealthy_instances", config.ReplaceUnhealthyInstances) @@ -1318,20 +1318,20 @@ func resourceAwsSpotFleetRequestRead(d *schema.ResourceData, meta interface{}) e if len(config.LaunchTemplateConfigs) > 0 { if err := d.Set("launch_template_config", flattenFleetLaunchTemplateConfig(config.LaunchTemplateConfigs)); err != nil { - return fmt.Errorf("error setting launch_template_config: %s", err) + return fmt.Errorf("error setting launch_template_config: %w", err) } } if config.OnDemandTargetCapacity != nil { - d.Set("on_demand_target_capacity", aws.Int64Value(config.OnDemandTargetCapacity)) + d.Set("on_demand_target_capacity", config.OnDemandTargetCapacity) } if config.OnDemandAllocationStrategy != nil { - d.Set("on_demand_allocation_strategy", aws.StringValue(config.OnDemandAllocationStrategy)) + d.Set("on_demand_allocation_strategy", config.OnDemandAllocationStrategy) } if config.OnDemandMaxTotalPrice != nil { - d.Set("on_demand_max_total_price", aws.StringValue(config.OnDemandMaxTotalPrice)) + d.Set("on_demand_max_total_price", config.OnDemandMaxTotalPrice) } if config.LoadBalancersConfig != nil { @@ -1353,7 +1353,7 @@ func resourceAwsSpotFleetRequestRead(d *schema.ResourceData, meta interface{}) e flatTgs = append(flatTgs, tg.Arn) } if err := d.Set("target_group_arns", flattenStringSet(flatTgs)); err != nil { - return fmt.Errorf("error setting target_group_arns: %s", err) + return fmt.Errorf("error setting target_group_arns: %w", err) } } } From 2976f491d2a569a12b62d2f184abe0f0dbb21735 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Mon, 7 Jun 2021 20:28:55 +0300 Subject: [PATCH 19/21] fmt --- aws/resource_aws_spot_fleet_request.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index 5d6c76c4232..ec324fca79e 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -1132,7 +1132,7 @@ func resourceAwsSpotFleetRequestStateRefreshFunc(d *schema.ResourceData, meta in resp, err := conn.DescribeSpotFleetRequests(req) if err != nil { - log.Printf("Error on retrieving Spot Fleet Request when waiting: %w", err) + log.Printf("Error on retrieving Spot Fleet Request when waiting: %s", err) return nil, "", nil } @@ -1158,7 +1158,7 @@ func resourceAwsSpotFleetRequestFulfillmentRefreshFunc(id string, conn *ec2.EC2) resp, err := conn.DescribeSpotFleetRequests(req) if err != nil { - log.Printf("Error on retrieving Spot Fleet Request when waiting: %w", err) + log.Printf("Error on retrieving Spot Fleet Request when waiting: %s", err) return nil, "", nil } @@ -1343,7 +1343,7 @@ func resourceAwsSpotFleetRequestRead(d *schema.ResourceData, meta interface{}) e flatLbs = append(flatLbs, lb.Name) } if err := d.Set("load_balancers", flattenStringSet(flatLbs)); err != nil { - return fmt.Errorf("error setting load_balancers: %s", err) + return fmt.Errorf("error setting load_balancers: %w", err) } } @@ -1641,7 +1641,7 @@ 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) + return fmt.Errorf("error updating spot request (%s): %w", d.Id(), err) } log.Println("[INFO] Waiting for Spot Fleet Request to be modified") @@ -1663,7 +1663,7 @@ func resourceAwsSpotFleetRequestUpdate(d *schema.ResourceData, meta interface{}) if d.HasChange("tags_all") { o, n := d.GetChange("tags_all") if err := keyvaluetags.Ec2UpdateTags(conn, d.Id(), o, n); err != nil { - return fmt.Errorf("error updating tags: %s", err) + return fmt.Errorf("error updating tags: %w", err) } } @@ -1678,7 +1678,7 @@ func resourceAwsSpotFleetRequestDelete(d *schema.ResourceData, meta interface{}) log.Printf("[INFO] Cancelling spot fleet request: %s", d.Id()) err := deleteSpotFleetRequest(d.Id(), terminateInstances, d.Timeout(schema.TimeoutDelete), conn) if err != nil { - return fmt.Errorf("error deleting spot request (%s): %s", d.Id(), err) + return fmt.Errorf("error deleting spot request (%s): %w", d.Id(), err) } return nil @@ -1704,7 +1704,7 @@ func deleteSpotFleetRequest(spotFleetRequestID string, terminateInstances bool, }) if err != nil || resp == nil { - return 0, fmt.Errorf("error reading Spot Fleet Instances (%s): %s", spotFleetRequestID, err) + return 0, fmt.Errorf("error reading Spot Fleet Instances (%s): %w", spotFleetRequestID, err) } return len(resp.ActiveInstances), nil From 721cbbe3305ae222d158d377965ca9253e630572 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 16 Jun 2021 15:06:25 -0400 Subject: [PATCH 20/21] r/aws_spot_fleet_request: Don't validate EC2 instance types. --- .changelog/13127.txt | 4 ---- aws/resource_aws_spot_fleet_request.go | 14 ++++++-------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/.changelog/13127.txt b/.changelog/13127.txt index 896ccfac0de..519a42cb179 100644 --- a/.changelog/13127.txt +++ b/.changelog/13127.txt @@ -1,7 +1,3 @@ -```release-note:enhancement -resource/aws_spot_fleet_request: Add plan time validation `instance_type` -``` - ```release-note:enhancement resource/aws_spot_fleet_request: Add `on_demand_allocation_strategy`, `on_demand_max_total_price`, and `on_demand_target_capacity` arguments ``` \ No newline at end of file diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index ec324fca79e..dd35a8622b9 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -242,10 +242,9 @@ func resourceAwsSpotFleetRequest() *schema.Resource { ForceNew: true, }, "instance_type": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice(ec2.InstanceType_Values(), false), + Type: schema.TypeString, + Required: true, + ForceNew: true, }, "key_name": { Type: schema.TypeString, @@ -363,10 +362,9 @@ func resourceAwsSpotFleetRequest() *schema.Resource { ForceNew: true, }, "instance_type": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice(ec2.InstanceType_Values(), false), + Type: schema.TypeString, + Optional: true, + ForceNew: true, }, "spot_price": { Type: schema.TypeString, From 41715c9f24642aa7d85e7a47c02857edd71177b7 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 16 Jun 2021 15:41:46 -0400 Subject: [PATCH 21/21] r/aws_spot_fleet_request: Instead of 'd.GetOkExists', always set in value. Tidy up 'resourceAwsSpotFleetRequestUpdate'. --- aws/resource_aws_spot_fleet_request.go | 52 ++++++++------------------ 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index dd35a8622b9..2cd909d1ada 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -986,9 +986,7 @@ func resourceAwsSpotFleetRequestCreate(d *schema.ResourceData, meta interface{}) spotFleetConfig.SpotPrice = aws.String(v.(string)) } - if v, ok := d.GetOkExists("on_demand_target_capacity"); ok { - spotFleetConfig.OnDemandTargetCapacity = aws.Int64(int64(v.(int))) - } + spotFleetConfig.OnDemandTargetCapacity = aws.Int64(int64(d.Get("on_demand_target_capacity").(int))) if v, ok := d.GetOk("on_demand_allocation_strategy"); ok { spotFleetConfig.OnDemandAllocationStrategy = aws.String(v.(string)) @@ -1320,17 +1318,9 @@ func resourceAwsSpotFleetRequestRead(d *schema.ResourceData, meta interface{}) e } } - if config.OnDemandTargetCapacity != nil { - d.Set("on_demand_target_capacity", config.OnDemandTargetCapacity) - } - - if config.OnDemandAllocationStrategy != nil { - d.Set("on_demand_allocation_strategy", config.OnDemandAllocationStrategy) - } - - if config.OnDemandMaxTotalPrice != nil { - d.Set("on_demand_max_total_price", config.OnDemandMaxTotalPrice) - } + d.Set("on_demand_target_capacity", config.OnDemandTargetCapacity) + d.Set("on_demand_allocation_strategy", config.OnDemandAllocationStrategy) + d.Set("on_demand_max_total_price", config.OnDemandMaxTotalPrice) if config.LoadBalancersConfig != nil { lbConf := config.LoadBalancersConfig @@ -1608,35 +1598,25 @@ func resourceAwsSpotFleetRequestUpdate(d *schema.ResourceData, meta interface{}) // http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_ModifySpotFleetRequest.html conn := meta.(*AWSClient).ec2conn - req := &ec2.ModifySpotFleetRequestInput{ - SpotFleetRequestId: aws.String(d.Id()), - } - - updateFlag := false - - if d.HasChange("target_capacity") { - if val, ok := d.GetOkExists("target_capacity"); ok { - req.TargetCapacity = aws.Int64(int64(val.(int))) - updateFlag = true + if d.HasChangesExcept("tags", "tags_all") { + req := &ec2.ModifySpotFleetRequestInput{ + SpotFleetRequestId: aws.String(d.Id()), } - } - if d.HasChange("on_demand_target_capacity") { - if val, ok := d.GetOkExists("on_demand_target_capacity"); ok { - req.OnDemandTargetCapacity = aws.Int64(int64(val.(int))) - updateFlag = true + if d.HasChange("target_capacity") { + req.TargetCapacity = aws.Int64(int64(d.Get("target_capacity").(int))) } - } - if d.HasChange("excess_capacity_termination_policy") { - if val, ok := d.GetOk("excess_capacity_termination_policy"); ok { - req.ExcessCapacityTerminationPolicy = aws.String(val.(string)) + if d.HasChange("on_demand_target_capacity") { + req.OnDemandTargetCapacity = aws.Int64(int64(d.Get("on_demand_target_capacity").(int))) } - updateFlag = true - } + if d.HasChange("excess_capacity_termination_policy") { + if val, ok := d.GetOk("excess_capacity_termination_policy"); ok { + req.ExcessCapacityTerminationPolicy = aws.String(val.(string)) + } + } - 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): %w", d.Id(), err)