Skip to content

Commit

Permalink
azurerm_cognitive_deployment - fix-up partial 4.0 deprecation for c…
Browse files Browse the repository at this point in the history
…hanging to supported SKU (#27173)

* fixup partial deprecation for changing to supported SKU block from legacy scale block

* missed path check in update

* put sequential test back
  • Loading branch information
jackofallops authored Aug 23, 2024
1 parent 71abf94 commit 72472af
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 125 deletions.
153 changes: 52 additions & 101 deletions internal/services/cognitive/cognitive_deployment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2023-05-01/cognitiveservicesaccounts"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2023-05-01/deployments"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand All @@ -21,12 +20,12 @@ import (
)

type cognitiveDeploymentModel struct {
Name string `tfschema:"name"`
CognitiveAccountId string `tfschema:"cognitive_account_id"`
Model []DeploymentModelModel `tfschema:"model"`
RaiPolicyName string `tfschema:"rai_policy_name"`
ScaleSettings []DeploymentScaleSettingsModel `tfschema:"scale"`
VersionUpgradeOption string `tfschema:"version_upgrade_option"`
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"`
}

type DeploymentModelModel struct {
Expand All @@ -35,12 +34,12 @@ type DeploymentModelModel struct {
Version string `tfschema:"version"`
}

type DeploymentScaleSettingsModel struct {
ScaleType string `tfschema:"type"`
Tier string `tfschema:"tier"`
Size string `tfschema:"size"`
Family string `tfschema:"family"`
Capacity int64 `tfschema:"capacity"`
type DeploymentSkuModel struct {
Name string `tfschema:"name"`
Tier string `tfschema:"tier"`
Size string `tfschema:"size"`
Family string `tfschema:"family"`
Capacity int64 `tfschema:"capacity"`
}

type CognitiveDeploymentResource struct{}
Expand All @@ -60,7 +59,7 @@ func (r CognitiveDeploymentResource) IDValidationFunc() pluginsdk.SchemaValidate
}

func (r CognitiveDeploymentResource) Arguments() map[string]*pluginsdk.Schema {
arguments := map[string]*pluginsdk.Schema{
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
Expand Down Expand Up @@ -106,79 +105,24 @@ func (r CognitiveDeploymentResource) Arguments() map[string]*pluginsdk.Schema {
},
},

"rai_policy_name": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"version_upgrade_option": {
Type: pluginsdk.TypeString,
Optional: true,
Default: string(deployments.DeploymentModelVersionUpgradeOptionOnceNewDefaultVersionAvailable),
ValidateFunc: validation.StringInSlice([]string{
string(deployments.DeploymentModelVersionUpgradeOptionOnceCurrentVersionExpired),
string(deployments.DeploymentModelVersionUpgradeOptionOnceNewDefaultVersionAvailable),
string(deployments.DeploymentModelVersionUpgradeOptionNoAutoUpgrade),
}, false),
},
}
if !features.FourPointOh() {
arguments["scale"] = &pluginsdk.Schema{
"sku": {
Type: pluginsdk.TypeList,
Required: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"type": {
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},
"tier": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(deployments.SkuTierFree),
string(deployments.SkuTierBasic),
string(deployments.SkuTierStandard),
string(deployments.SkuTierPremium),
string(deployments.SkuTierEnterprise),
"Standard",
"GlobalBatch",
"GlobalStandard",
"ProvisionedManaged",
}, false),
},
"size": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
},
"family": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
},
"capacity": {
Type: pluginsdk.TypeInt,
Optional: true,
Default: 1,
ValidateFunc: validation.IntAtLeast(1),
},
},
},
}
} else {
//TODO: 4.0 - add corresponding field in cognitiveDeploymentModel struct
arguments["sku"] = &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Required: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"tier": {
Type: pluginsdk.TypeString,
Optional: true,
Expand All @@ -191,16 +135,19 @@ func (r CognitiveDeploymentResource) Arguments() map[string]*pluginsdk.Schema {
string(deployments.SkuTierEnterprise),
}, false),
},

"size": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
},

"family": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
},

"capacity": {
Type: pluginsdk.TypeInt,
Optional: true,
Expand All @@ -209,9 +156,25 @@ func (r CognitiveDeploymentResource) Arguments() map[string]*pluginsdk.Schema {
},
},
},
}
},

"rai_policy_name": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"version_upgrade_option": {
Type: pluginsdk.TypeString,
Optional: true,
Default: string(deployments.DeploymentModelVersionUpgradeOptionOnceNewDefaultVersionAvailable),
ValidateFunc: validation.StringInSlice([]string{
string(deployments.DeploymentModelVersionUpgradeOptionOnceCurrentVersionExpired),
string(deployments.DeploymentModelVersionUpgradeOptionOnceNewDefaultVersionAvailable),
string(deployments.DeploymentModelVersionUpgradeOptionNoAutoUpgrade),
}, false),
},
}
return arguments
}

func (r CognitiveDeploymentResource) Attributes() map[string]*pluginsdk.Schema {
Expand Down Expand Up @@ -261,7 +224,7 @@ func (r CognitiveDeploymentResource) Create() sdk.ResourceFunc {
properties.Properties.VersionUpgradeOption = &option
}

properties.Sku = expandDeploymentSkuModel(model.ScaleSettings)
properties.Sku = expandDeploymentSkuModel(model.Sku)

if err := client.CreateOrUpdateThenPoll(ctx, id, *properties); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
Expand Down Expand Up @@ -302,8 +265,8 @@ func (r CognitiveDeploymentResource) Update() sdk.ResourceFunc {

properties := resp.Model

if metadata.ResourceData.HasChange("scale.0.capacity") {
properties.Sku.Capacity = pointer.To(model.ScaleSettings[0].Capacity)
if metadata.ResourceData.HasChange("sku.0.capacity") {
properties.Sku.Capacity = pointer.To(model.Sku[0].Capacity)
}

if metadata.ResourceData.HasChange("rai_policy_name") {
Expand Down Expand Up @@ -366,10 +329,9 @@ func (r CognitiveDeploymentResource) Read() sdk.ResourceFunc {
if v := properties.VersionUpgradeOption; v != nil {
state.VersionUpgradeOption = string(*v)
}
state.ScaleSettings = flattenDeploymentScaleSettingsModel(properties.ScaleSettings)
}
if scale := flattenDeploymentSkuModel(model.Sku); scale != nil {
state.ScaleSettings = scale
if sku := flattenDeploymentSkuModel(model.Sku); sku != nil {
state.Sku = sku
}
return metadata.Encode(&state)
},
Expand Down Expand Up @@ -423,13 +385,13 @@ func expandDeploymentModelModel(inputList []DeploymentModelModel) *deployments.D
return &output
}

func expandDeploymentSkuModel(inputList []DeploymentScaleSettingsModel) *deployments.Sku {
func expandDeploymentSkuModel(inputList []DeploymentSkuModel) *deployments.Sku {
if len(inputList) == 0 {
return nil
}
input := inputList[0]
s := &deployments.Sku{
Name: input.ScaleType,
Name: input.Name,
}
if input.Capacity != 0 {
s.Capacity = utils.Int64(input.Capacity)
Expand Down Expand Up @@ -475,23 +437,12 @@ func flattenDeploymentModelModel(input *deployments.DeploymentModel) []Deploymen
return append(outputList, output)
}

func flattenDeploymentScaleSettingsModel(input *deployments.DeploymentScaleSettings) []DeploymentScaleSettingsModel {
if input == nil || input.ScaleType == nil {
return nil
}

output := DeploymentScaleSettingsModel{
ScaleType: string(*input.ScaleType),
}
return []DeploymentScaleSettingsModel{output}
}

func flattenDeploymentSkuModel(input *deployments.Sku) []DeploymentScaleSettingsModel {
func flattenDeploymentSkuModel(input *deployments.Sku) []DeploymentSkuModel {
if input == nil {
return nil
}
output := DeploymentScaleSettingsModel{
ScaleType: input.Name,
output := DeploymentSkuModel{
Name: input.Name,
}
if input.Capacity != nil {
output.Capacity = *input.Capacity
Expand All @@ -505,5 +456,5 @@ func flattenDeploymentSkuModel(input *deployments.Sku) []DeploymentScaleSettings
if input.Family != nil {
output.Family = *input.Family
}
return []DeploymentScaleSettingsModel{output}
return []DeploymentSkuModel{output}
}
32 changes: 12 additions & 20 deletions internal/services/cognitive/cognitive_deployment_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,49 +85,41 @@ func TestAccCognitiveDeployment_update(t *testing.T) {
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("scale.0.capacity").HasValue("1"),
check.That(data.ResourceName).Key("rai_policy_name").HasValue("RAI policy"),
),
},
data.ImportStep(),
{
Config: r.update(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("scale.0.capacity").HasValue("2"),
check.That(data.ResourceName).Key("rai_policy_name").HasValue("Microsoft.Default"),
),
},
data.ImportStep(),
{
Config: r.updateVersion(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("model.0.version").HasValue("1"),
),
},
data.ImportStep(),
{
Config: r.versionUpgradeOption(data, "OnceNewDefaultVersionAvailable"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("version_upgrade_option").HasValue("OnceNewDefaultVersionAvailable"),
),
},
data.ImportStep(),
{
Config: r.versionUpgradeOption(data, "OnceCurrentVersionExpired"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("version_upgrade_option").HasValue("OnceCurrentVersionExpired"),
),
},
data.ImportStep(),
{
Config: r.updateVersion(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("version_upgrade_option").HasValue("OnceNewDefaultVersionAvailable"),
),
},
data.ImportStep(),
Expand Down Expand Up @@ -183,8 +175,8 @@ resource "azurerm_cognitive_deployment" "test" {
format = "OpenAI"
name = "text-embedding-ada-002"
}
scale {
type = "Standard"
sku {
name = "Standard"
}
lifecycle {
ignore_changes = [model.0.version]
Expand All @@ -206,8 +198,8 @@ resource "azurerm_cognitive_deployment" "import" {
name = "text-embedding-ada-002"
version = "2"
}
scale {
type = "Standard"
sku {
name = "Standard"
}
}
`, config)
Expand All @@ -227,8 +219,8 @@ resource "azurerm_cognitive_deployment" "test" {
name = "text-embedding-ada-002"
version = "2"
}
scale {
type = "Standard"
sku {
name = "Standard"
}
rai_policy_name = "RAI policy"
version_upgrade_option = "OnceNewDefaultVersionAvailable"
Expand All @@ -250,8 +242,8 @@ resource "azurerm_cognitive_deployment" "test" {
name = "text-embedding-ada-002"
version = "2"
}
scale {
type = "Standard"
sku {
name = "Standard"
capacity = 2
}
}
Expand All @@ -272,8 +264,8 @@ resource "azurerm_cognitive_deployment" "test" {
name = "text-embedding-ada-002"
version = "1"
}
scale {
type = "Standard"
sku {
name = "Standard"
capacity = 2
}
}
Expand All @@ -295,8 +287,8 @@ resource "azurerm_cognitive_deployment" "test" {
name = "text-embedding-ada-002"
version = "1"
}
scale {
type = "Standard"
sku {
name = "Standard"
capacity = 2
}
}
Expand Down
4 changes: 4 additions & 0 deletions website/docs/guides/4.0-upgrade-guide.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,10 @@ The deprecated data source has been superseded by `azurerm_mssql_server` and has
* The deprecated `health_probes_enabled` property has been removed in favour of the `enabled` property.
* The `enabled` property now defaults to `true`.

### `azurerm_cognitive_account`

* The `scale` block is no longer supported by the service and has been removed and replaced with the `sku` block.

### `azurerm_consumption_budget_management_group`

* The deprecated `filter.not` block has been removed.
Expand Down
Loading

0 comments on commit 72472af

Please sign in to comment.