Skip to content

Commit

Permalink
azurerm_cognitive_deployment - remove ForceNew tag for property 'ca…
Browse files Browse the repository at this point in the history
…pacity' (#23251)

* remove ForceNew tag

* add update function

* format update
  • Loading branch information
liuwuliuyun authored Sep 26, 2023
1 parent 3d85505 commit 5707f88
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 10 deletions.
55 changes: 47 additions & 8 deletions internal/services/cognitive/cognitive_deployment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"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"
Expand Down Expand Up @@ -115,7 +116,6 @@ func (r CognitiveDeploymentResource) Arguments() map[string]*pluginsdk.Schema {
arguments["scale"] = &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
Expand Down Expand Up @@ -149,7 +149,6 @@ func (r CognitiveDeploymentResource) Arguments() map[string]*pluginsdk.Schema {
"capacity": {
Type: pluginsdk.TypeInt,
Optional: true,
ForceNew: true,
Default: 1,
ValidateFunc: validation.IntAtLeast(1),
},
Expand All @@ -161,7 +160,6 @@ func (r CognitiveDeploymentResource) Arguments() map[string]*pluginsdk.Schema {
arguments["sku"] = &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
Expand Down Expand Up @@ -195,7 +193,6 @@ func (r CognitiveDeploymentResource) Arguments() map[string]*pluginsdk.Schema {
"capacity": {
Type: pluginsdk.TypeInt,
Optional: true,
ForceNew: true,
Default: 1,
ValidateFunc: validation.IntAtLeast(1),
},
Expand All @@ -221,14 +218,13 @@ func (r CognitiveDeploymentResource) Create() sdk.ResourceFunc {

client := metadata.Client.Cognitive.DeploymentsClient
accountId, err := cognitiveservicesaccounts.ParseAccountID(model.CognitiveAccountId)

locks.ByID(accountId.ID())
defer locks.UnlockByID(accountId.ID())

if err != nil {
return err
}

locks.ByID(accountId.ID())
defer locks.UnlockByID(accountId.ID())

id := deployments.NewDeploymentID(accountId.SubscriptionId, accountId.ResourceGroupName, accountId.AccountName, model.Name)
existing, err := client.Get(ctx, id)
if err != nil && !response.WasNotFound(existing.HttpResponse) {
Expand Down Expand Up @@ -261,6 +257,49 @@ func (r CognitiveDeploymentResource) Create() sdk.ResourceFunc {
}
}

func (r CognitiveDeploymentResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var model cognitiveDeploymentModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

client := metadata.Client.Cognitive.DeploymentsClient
accountId, err := cognitiveservicesaccounts.ParseAccountID(model.CognitiveAccountId)
if err != nil {
return err
}

locks.ByID(accountId.ID())
defer locks.UnlockByID(accountId.ID())

id, err := deployments.ParseDeploymentID(metadata.ResourceData.Id())
if err != nil {
return err
}
resp, err := client.Get(ctx, *id)
if err != nil {
return err
}

properties := resp.Model

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

if err := client.CreateOrUpdateThenPoll(ctx, *id, *properties); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

metadata.SetID(id)
return nil
},
}
}

func (r CognitiveDeploymentResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Expand Down
54 changes: 52 additions & 2 deletions internal/services/cognitive/cognitive_deployment_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestAccCognitiveDeploymentSequential(t *testing.T) {
"basic": TestAccCognitiveDeployment_basic,
"requiresImport": testAccCognitiveDeployment_requiresImport,
"complete": testAccCognitiveDeployment_complete,
"update": TestAccCognitiveDeployment_update,
},
})
}
Expand Down Expand Up @@ -75,6 +76,30 @@ func testAccCognitiveDeployment_complete(t *testing.T) {
})
}

func TestAccCognitiveDeployment_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_deployment", "test")
r := CognitiveDeploymentTestResource{}

data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("scale.0.capacity").HasValue("1"),
),
},
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"),
),
},
data.ImportStep(),
})
}

func (r CognitiveDeploymentTestResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := deployments.ParseDeploymentID(state.ID)
if err != nil {
Expand Down Expand Up @@ -125,7 +150,7 @@ resource "azurerm_cognitive_deployment" "test" {
model {
format = "OpenAI"
name = "text-embedding-ada-002"
version = "1"
version = "2"
}
scale {
type = "Standard"
Expand All @@ -145,7 +170,7 @@ resource "azurerm_cognitive_deployment" "import" {
model {
format = "OpenAI"
name = "text-embedding-ada-002"
version = "1"
version = "2"
}
scale {
type = "Standard"
Expand Down Expand Up @@ -175,3 +200,28 @@ resource "azurerm_cognitive_deployment" "test" {
}
`, template, data.RandomInteger)
}

func (r CognitiveDeploymentTestResource) update(data acceptance.TestData) string {
template := r.template(data, "OpenAI")
return fmt.Sprintf(`
%s
resource "azurerm_cognitive_deployment" "test" {
name = "acctest-cd-%d"
cognitive_account_id = azurerm_cognitive_account.test.id
model {
format = "OpenAI"
name = "text-embedding-ada-002"
version = "2"
}
scale {
type = "Standard"
capacity = 2
}
}
`, template, data.RandomInteger)
}

0 comments on commit 5707f88

Please sign in to comment.