diff --git a/castai/resource_commitments.go b/castai/resource_commitments.go index b80f5c1d5..249710375 100644 --- a/castai/resource_commitments.go +++ b/castai/resource_commitments.go @@ -225,6 +225,12 @@ func resourceCommitments() *schema.Resource { Description: "Scaling strategy of the commitment in CAST AI. One of: Default, CPUBased, RamBased", ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"Default", "CPUBased", "RamBased"}, false)), }, + "auto_assignment": { + Type: schema.TypeBool, + Optional: true, + Default: true, + Description: "If enabled, the commitment is automatically assigned to all clusters in the matching region. When disabled, only explicitly listed cluster assignments are used.", + }, }), }, }, diff --git a/castai/resource_commitments_mapping.go b/castai/resource_commitments_mapping.go index 06a1750a8..04fd1879a 100644 --- a/castai/resource_commitments_mapping.go +++ b/castai/resource_commitments_mapping.go @@ -9,9 +9,10 @@ import ( "strings" "time" - "github.com/samber/lo" "slices" + "github.com/samber/lo" + "github.com/castai/terraform-provider-castai/castai/reservations" "github.com/castai/terraform-provider-castai/castai/sdk" ) @@ -76,6 +77,7 @@ type ( AllowedUsage *float64 `mapstructure:"allowed_usage,omitempty"` Assignments []*commitmentAssignmentResource `mapstructure:"assignments,omitempty"` ScalingStrategy *string `mapstructure:"scaling_strategy,omitempty"` + AutoAssignment *bool `mapstructure:"auto_assignment,omitempty"` } commitmentConfigMatcherResource struct { Name string `mapstructure:"name"` @@ -511,6 +513,7 @@ func mapCommitmentImportWithConfigToUpdateRequest( Prioritization: c.Commitment.Prioritization, Status: c.Commitment.Status, ScalingStrategy: c.Commitment.ScalingStrategy, + AutoAssignment: c.Commitment.AutoAssignment, } if c.Config != nil { if c.Config.AllowedUsage != nil { @@ -525,6 +528,9 @@ func mapCommitmentImportWithConfigToUpdateRequest( if c.Config.ScalingStrategy != nil { req.ScalingStrategy = (*sdk.CastaiInventoryV1beta1CommitmentScalingStrategy)(c.Config.ScalingStrategy) } + if c.Config.AutoAssignment != nil { + req.AutoAssignment = c.Config.AutoAssignment + } } return req } diff --git a/castai/resource_commitments_test.go b/castai/resource_commitments_test.go index f77a2756e..7525e1a51 100644 --- a/castai/resource_commitments_test.go +++ b/castai/resource_commitments_test.go @@ -88,6 +88,7 @@ func TestCommitmentsResourceCreateAndUpdate(t *testing.T) { commitmentImport: gcpImport, expectCommitmentUpdate: sdk.CommitmentsAPIUpdateCommitmentJSONRequestBody{ AllowedUsage: lo.ToPtr[float32](0.6), + AutoAssignment: lo.ToPtr(true), Prioritization: lo.ToPtr(true), ScalingStrategy: lo.ToPtr(sdk.CPUBased), Status: lo.ToPtr(sdk.CastaiInventoryV1beta1CommitmentStatusActive), @@ -157,6 +158,7 @@ test,3b3de39c-bc44-4d69-be2d-69527dfe9958,630226bb-5170-4b95-90b0-f222757130c1,S commitmentImport: commitmentImport, expectCommitmentUpdate: sdk.CommitmentsAPIUpdateCommitmentJSONRequestBody{ AllowedUsage: lo.ToPtr[float32](0.7), + AutoAssignment: lo.ToPtr(true), Prioritization: lo.ToPtr(true), ScalingStrategy: lo.ToPtr(sdk.Default), Status: lo.ToPtr(sdk.CastaiInventoryV1beta1CommitmentStatusActive), @@ -363,10 +365,11 @@ test,3b3de39c-bc44-4d69-be2d-69527dfe9958,630226bb-5170-4b95-90b0-f222757130c1,S "priority": 1, }, }, - "prioritization": true, - "status": "Active", - "allowed_usage": 0.7, - "scaling_strategy": "Default", + "prioritization": true, + "status": "Active", + "allowed_usage": 0.7, + "scaling_strategy": "Default", + "auto_assignment": false, }, }, }) @@ -427,6 +430,7 @@ test,3b3de39c-bc44-4d69-be2d-69527dfe9958,630226bb-5170-4b95-90b0-f222757130c1,S mockClient.EXPECT().CommitmentsAPIUpdateCommitmentWithResponse( gomock.Any(), commitmentID.String(), sdk.CommitmentsAPIUpdateCommitmentJSONRequestBody{ AllowedUsage: lo.ToPtr[float32](0.7), + AutoAssignment: lo.ToPtr(false), Prioritization: lo.ToPtr(true), ScalingStrategy: lo.ToPtr(sdk.Default), Status: lo.ToPtr(sdk.CastaiInventoryV1beta1CommitmentStatusActive), diff --git a/docs/resources/commitments.md b/docs/resources/commitments.md index d451c1166..80a139f3c 100644 --- a/docs/resources/commitments.md +++ b/docs/resources/commitments.md @@ -87,6 +87,7 @@ Optional: - `allowed_usage` (Number) Allowed usage of the commitment. The value is between 0 (0%) and 1 (100%). - `assignments` (Block List) List of assigned clusters for the commitment. If prioritization is enabled, the order of the assignments indicates the priority. The first assignment has the highest priority. (see [below for nested schema](#nestedblock--commitment_configs--assignments)) +- `auto_assignment` (Boolean) If enabled, the commitment is automatically assigned to all clusters in the matching region. When disabled, only explicitly listed cluster assignments are used. - `prioritization` (Boolean) If enabled, it's possible to assign priorities to the assigned clusters. - `scaling_strategy` (String) Scaling strategy of the commitment in CAST AI. One of: Default, CPUBased, RamBased - `status` (String) Status of the commitment in CAST AI.