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

azurerm_cognitive_deployment - remove ForceNew tag for property 'capacity' #23251

Merged
merged 3 commits into from
Sep 26, 2023
Merged
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
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a test with a non OpenAI type to validate this works for other types of accounts too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I will test that today

Copy link
Contributor Author

@liuwuliuyun liuwuliuyun Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tombuildsstuff I am not able to perform this test because in azurerm_cognitive_deployment, the only supported format is OpenAI and it does not make sense to create a cognitive account of other type with a deployment of openai format. Link to the defination of format https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cognitive_deployment#format

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liuwuliuyun we should still add an update test for this property with deployment type of OpenAI so that we can track if anything changes/regresses in the API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephybun Thanks for the review. Agreed on adding the testcase.

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
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)
}
Loading