Skip to content

Commit

Permalink
azurerm_cognitive_deployment - support for the property `dynamic_th…
Browse files Browse the repository at this point in the history
…rottling_enabled` (hashicorp#28100)

* Support `dynamic_throttling_enabled` property in `azurerm_cognitive_deployment`

* Fix linter issue
  • Loading branch information
liuwuliuyun authored Dec 2, 2024
1 parent ce28eef commit c7481ab
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
35 changes: 23 additions & 12 deletions internal/services/cognitive/cognitive_deployment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import (
)

type cognitiveDeploymentModel struct {
Name string `tfschema:"name"`
CognitiveAccountId string `tfschema:"cognitive_account_id"`
Model []DeploymentModelModel `tfschema:"model"`
RaiPolicyName string `tfschema:"rai_policy_name"`
Sku []DeploymentSkuModel `tfschema:"sku"`
VersionUpgradeOption string `tfschema:"version_upgrade_option"`
Name string `tfschema:"name"`
CognitiveAccountId string `tfschema:"cognitive_account_id"`
DynamicThrottlingEnabled bool `tfschema:"dynamic_throttling_enabled"`
Model []DeploymentModelModel `tfschema:"model"`
RaiPolicyName string `tfschema:"rai_policy_name"`
Sku []DeploymentSkuModel `tfschema:"sku"`
VersionUpgradeOption string `tfschema:"version_upgrade_option"`
}

type DeploymentModelModel struct {
Expand Down Expand Up @@ -74,6 +75,11 @@ func (r CognitiveDeploymentResource) Arguments() map[string]*pluginsdk.Schema {
ValidateFunc: cognitiveservicesaccounts.ValidateAccountID,
},

"dynamic_throttling_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
},

"model": {
Type: pluginsdk.TypeList,
Required: true,
Expand Down Expand Up @@ -220,6 +226,10 @@ func (r CognitiveDeploymentResource) Create() sdk.ResourceFunc {
properties.Properties.RaiPolicyName = &model.RaiPolicyName
}

if model.DynamicThrottlingEnabled {
properties.Properties.DynamicThrottlingEnabled = &model.DynamicThrottlingEnabled
}

if model.VersionUpgradeOption != "" {
option := deployments.DeploymentModelVersionUpgradeOption(model.VersionUpgradeOption)
properties.Properties.VersionUpgradeOption = &option
Expand Down Expand Up @@ -266,6 +276,10 @@ func (r CognitiveDeploymentResource) Update() sdk.ResourceFunc {

properties := resp.Model

if metadata.ResourceData.HasChange("dynamic_throttling_enabled") {
properties.Properties.DynamicThrottlingEnabled = pointer.To(model.DynamicThrottlingEnabled)
}

if metadata.ResourceData.HasChange("sku.0.capacity") {
properties.Sku.Capacity = pointer.To(model.Sku[0].Capacity)
}
Expand Down Expand Up @@ -323,12 +337,9 @@ func (r CognitiveDeploymentResource) Read() sdk.ResourceFunc {
if properties := model.Properties; properties != nil {
state.Model = flattenDeploymentModelModel(properties.Model)

if v := properties.RaiPolicyName; v != nil {
state.RaiPolicyName = *v
}
if v := properties.VersionUpgradeOption; v != nil {
state.VersionUpgradeOption = string(*v)
}
state.DynamicThrottlingEnabled = pointer.From(properties.DynamicThrottlingEnabled)
state.RaiPolicyName = pointer.From(properties.RaiPolicyName)
state.VersionUpgradeOption = string(pointer.From(properties.VersionUpgradeOption))
}
if sku := flattenDeploymentSkuModel(model.Sku); sku != nil {
state.Sku = sku
Expand Down
26 changes: 7 additions & 19 deletions internal/services/cognitive/cognitive_deployment_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@ import (

type CognitiveDeploymentTestResource struct{}

func TestAccCognitiveDeploymentSequential(t *testing.T) {
// Only two OpenAI resources could be created per region, so run the tests sequentially.
// Refer to : https://learn.microsoft.com/en-us/azure/cognitive-services/openai/quotas-limits
acceptance.RunTestsInSequence(t, map[string]map[string]func(t *testing.T){
"deployment": {
"basic": TestAccCognitiveDeployment_basic,
"requiresImport": testAccCognitiveDeployment_requiresImport,
"complete": testAccCognitiveDeployment_complete,
"update": TestAccCognitiveDeployment_update,
},
})
}

func TestAccCognitiveDeployment_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_deployment", "test")
r := CognitiveDeploymentTestResource{}
Expand All @@ -47,7 +34,7 @@ func TestAccCognitiveDeployment_basic(t *testing.T) {
})
}

func testAccCognitiveDeployment_requiresImport(t *testing.T) {
func TestAccCognitiveDeployment_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_deployment", "test")

r := CognitiveDeploymentTestResource{}
Expand All @@ -62,14 +49,15 @@ func testAccCognitiveDeployment_requiresImport(t *testing.T) {
})
}

func testAccCognitiveDeployment_complete(t *testing.T) {
func TestAccCognitiveDeployment_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_deployment", "test")
r := CognitiveDeploymentTestResource{}
data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("dynamic_throttling_enabled").HasValue("true"),
),
},
data.ImportStep(),
Expand Down Expand Up @@ -211,9 +199,9 @@ func (r CognitiveDeploymentTestResource) complete(data acceptance.TestData) stri
%s
resource "azurerm_cognitive_deployment" "test" {
name = "acctest-cd-%d"
cognitive_account_id = azurerm_cognitive_account.test.id
name = "acctest-cd-%d"
cognitive_account_id = azurerm_cognitive_account.test.id
dynamic_throttling_enabled = true
model {
format = "OpenAI"
name = "text-embedding-ada-002"
Expand All @@ -222,7 +210,7 @@ resource "azurerm_cognitive_deployment" "test" {
sku {
name = "Standard"
}
rai_policy_name = "RAI policy"
rai_policy_name = "Microsoft.DefaultV2"
version_upgrade_option = "OnceNewDefaultVersionAvailable"
}
`, template, data.RandomInteger)
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/cognitive_deployment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ The following arguments are supported:

* `sku` - (Required) A `sku` block as defined below.

* `dynamic_throttling_enabled` - (Optional) Whether dynamic throttling is enabled.

* `rai_policy_name` - (Optional) The name of RAI policy.

* `version_upgrade_option` - (Optional) Deployment model version upgrade option. Possible values are `OnceNewDefaultVersionAvailable`, `OnceCurrentVersionExpired`, and `NoAutoUpgrade`. Defaults to `OnceNewDefaultVersionAvailable`.
Expand Down

0 comments on commit c7481ab

Please sign in to comment.