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

Fix missing arguments in spot_options block for aws_ec2_fleet resource #41272

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .changelog/41272.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_ec2_fleet: Implement the missing `spot_options` arguments that are mentioned in the provider documentation: `max_total_price`, `min_target_capacity`, `single_instance_type`, and `single_availability_zone`
```
48 changes: 48 additions & 0 deletions internal/service/ec2/ec2_fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,22 @@ func resourceFleet() *schema.Resource {
},
},
},
"max_total_price": {
Type: schema.TypeString,
Optional: true,
},
"min_target_capacity": {
Type: schema.TypeInt,
Optional: true,
},
"single_availability_zone": {
Type: schema.TypeBool,
Optional: true,
},
"single_instance_type": {
Type: schema.TypeBool,
Optional: true,
},
},
},
},
Expand Down Expand Up @@ -1152,6 +1168,22 @@ func expandSpotOptionsRequest(tfMap map[string]interface{}) *awstypes.SpotOption
apiObject.MaintenanceStrategies = expandFleetSpotMaintenanceStrategiesRequest(v[0].(map[string]interface{}))
}

if v, ok := tfMap["max_total_price"].(string); ok && v != "" {
apiObject.MaxTotalPrice = aws.String(v)
}

if v, ok := tfMap["min_target_capacity"].(int); ok {
apiObject.MinTargetCapacity = aws.Int32(int32(v))
}

if v, ok := tfMap["single_availability_zone"].(bool); ok {
apiObject.SingleAvailabilityZone = aws.Bool(v)
}

if v, ok := tfMap["single_instance_type"].(bool); ok {
apiObject.SingleInstanceType = aws.Bool(v)
}

return apiObject
}

Expand Down Expand Up @@ -1513,6 +1545,22 @@ func flattenSpotOptions(apiObject *awstypes.SpotOptions) map[string]interface{}
tfMap["maintenance_strategies"] = []interface{}{flattenFleetSpotMaintenanceStrategies(v)}
}

if v := apiObject.MaxTotalPrice; v != nil {
tfMap["max_total_price"] = aws.ToString(v)
}

if v := apiObject.MinTargetCapacity; v != nil {
tfMap["min_target_capacity"] = aws.ToInt32(v)
}

if v := apiObject.SingleAvailabilityZone; v != nil {
tfMap["single_availability_zone"] = aws.ToBool(v)
}

if v := apiObject.SingleInstanceType; v != nil {
tfMap["single_instance_type"] = aws.ToBool(v)
}

return tfMap
}

Expand Down
210 changes: 210 additions & 0 deletions internal/service/ec2/ec2_fleet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2725,6 +2725,102 @@ func TestAccEC2Fleet_SpotOptions_capacityRebalance(t *testing.T) {
})
}

func TestAccEC2Fleet_SpotOptions_MaxTotalPrice(t *testing.T) {
ctx := acctest.Context(t)
var fleet1 awstypes.FleetData
resourceName := "aws_ec2_fleet.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckFleet(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckFleetDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccFleetConfig_spotOptionsMaxTotalPrice(rName, "1.0"),
Check: resource.ComposeTestCheckFunc(
testAccCheckFleetExists(ctx, resourceName, &fleet1),
resource.TestCheckResourceAttr(resourceName, "spot_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spot_options.0.max_total_price", "1.0"),
),
},
},
})
}

func TestAccEC2Fleet_SpotOptions_MinTargetCapacity(t *testing.T) {
ctx := acctest.Context(t)
var fleet1 awstypes.FleetData
resourceName := "aws_ec2_fleet.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckFleet(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckFleetDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccFleetConfig_spotOptionsMinTargetCapacity(rName, "1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckFleetExists(ctx, resourceName, &fleet1),
resource.TestCheckResourceAttr(resourceName, "spot_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spot_options.0.min_target_capacity", "1"),
),
},
},
})
}

func TestAccEC2Fleet_SpotOptions_SingleAvailabilityZone(t *testing.T) {
ctx := acctest.Context(t)
var fleet1 awstypes.FleetData
resourceName := "aws_ec2_fleet.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckFleet(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckFleetDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccFleetConfig_spotOptionsSingleAvailabilityZone(rName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckFleetExists(ctx, resourceName, &fleet1),
resource.TestCheckResourceAttr(resourceName, "spot_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spot_options.0.single_availability_zone", acctest.CtTrue),
),
},
},
})
}

func TestAccEC2Fleet_SpotOptions_SingleInstanceType(t *testing.T) {
ctx := acctest.Context(t)
var fleet1 awstypes.FleetData
resourceName := "aws_ec2_fleet.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckFleet(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckFleetDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccFleetConfig_spotOptionsSingleInstanceType(rName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckFleetExists(ctx, resourceName, &fleet1),
resource.TestCheckResourceAttr(resourceName, "spot_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spot_options.0.single_instance_type", acctest.CtTrue),
),
},
},
})
}

func TestAccEC2Fleet_capacityRebalanceInvalidType(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down Expand Up @@ -4143,6 +4239,120 @@ resource "aws_ec2_fleet" "test" {
`, rName, allocationStrategy, replacementStrategy, terminationDelay))
}

func testAccFleetConfig_spotOptionsMaxTotalPrice(rName, maxTotalPrice string) string {
return acctest.ConfigCompose(testAccFleetConfig_BaseLaunchTemplate(rName), fmt.Sprintf(`
resource "aws_ec2_fleet" "test" {
launch_template_config {
launch_template_specification {
launch_template_id = aws_launch_template.test.id
version = aws_launch_template.test.latest_version
}
}

spot_options {
max_total_price = %[2]q
}

target_capacity_specification {
default_target_capacity_type = "spot"
total_target_capacity = 0
}

tags = {
Name = %[1]q
}
}
`, rName, maxTotalPrice))
}

func testAccFleetConfig_spotOptionsMinTargetCapacity(rName, minTargetcapcity string) string {
return acctest.ConfigCompose(testAccFleetConfig_BaseLaunchTemplate(rName), fmt.Sprintf(`
resource "aws_ec2_fleet" "test" {
launch_template_config {
launch_template_specification {
launch_template_id = aws_launch_template.test.id
version = aws_launch_template.test.latest_version
}
}

spot_options {
min_target_capacity = %[2]s
single_availability_zone = true
}

target_capacity_specification {
default_target_capacity_type = "spot"
total_target_capacity = 0
}

terminate_instances = true
type = "instant"

tags = {
Name = %[1]q
}
}
`, rName, minTargetcapcity))
}

func testAccFleetConfig_spotOptionsSingleAvailabilityZone(rName string, singleAZ bool) string {
return acctest.ConfigCompose(testAccFleetConfig_BaseLaunchTemplate(rName), fmt.Sprintf(`
resource "aws_ec2_fleet" "test" {
launch_template_config {
launch_template_specification {
launch_template_id = aws_launch_template.test.id
version = aws_launch_template.test.latest_version
}
}

spot_options {
single_availability_zone = %[2]t
}

target_capacity_specification {
default_target_capacity_type = "spot"
total_target_capacity = 0
}

terminate_instances = true
type = "instant"

tags = {
Name = %[1]q
}
}
`, rName, singleAZ))
}

func testAccFleetConfig_spotOptionsSingleInstanceType(rName string, singleInstanceType bool) string {
return acctest.ConfigCompose(testAccFleetConfig_BaseLaunchTemplate(rName), fmt.Sprintf(`
resource "aws_ec2_fleet" "test" {
launch_template_config {
launch_template_specification {
launch_template_id = aws_launch_template.test.id
version = aws_launch_template.test.latest_version
}
}

spot_options {
single_instance_type = %[2]t
}

target_capacity_specification {
default_target_capacity_type = "spot"
total_target_capacity = 0
}

terminate_instances = true
type = "instant"

tags = {
Name = %[1]q
}
}
`, rName, singleInstanceType))
}

func testAccFleetConfig_invalidTypeForCapacityRebalance(rName string) string {
return acctest.ConfigCompose(testAccFleetConfig_BaseLaunchTemplate(rName), fmt.Sprintf(`
resource "aws_ec2_fleet" "test" {
Expand Down