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

Update the Cognitive SDK to use API Version 2024-10-01 from 2023-05-01 #27851

Merged
merged 8 commits into from
Nov 8, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2023-05-01/cognitiveservicesaccounts"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2024-10-01/cognitiveservicesaccounts"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
Expand Down
24 changes: 13 additions & 11 deletions internal/services/cognitive/ai_services_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2023-05-01/cognitiveservicesaccounts"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2024-10-01/cognitiveservicesaccounts"
"github.com/hashicorp/go-azure-sdk/sdk/environments"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
commonValidate "github.com/hashicorp/terraform-provider-azurerm/helpers/validate"
Expand Down Expand Up @@ -389,16 +389,6 @@ func (AzureAIServicesResource) Read() sdk.ResourceFunc {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

keys, err := client.AccountsListKeys(ctx, *id)
if err != nil {
return fmt.Errorf("listing the Keys for %s: %+v", id, err)
}

if model := keys.Model; model != nil {
state.PrimaryAccessKey = pointer.From(model.Key1)
state.SecondaryAccessKey = pointer.From(model.Key2)
}

state.Name = id.AccountName
state.ResourceGroupName = id.ResourceGroupName

Expand Down Expand Up @@ -427,6 +417,18 @@ func (AzureAIServicesResource) Read() sdk.ResourceFunc {
if props.DisableLocalAuth != nil {
localAuthEnabled = !*props.DisableLocalAuth
}

if localAuthEnabled {
keys, err := client.AccountsListKeys(ctx, *id)
if err != nil {
return fmt.Errorf("listing the Keys for %s: %+v", id, err)
}

if model := keys.Model; model != nil {
state.PrimaryAccessKey = pointer.From(model.Key1)
state.SecondaryAccessKey = pointer.From(model.Key2)
}
}
Comment on lines +421 to +431
Copy link
Member

Choose a reason for hiding this comment

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

Can you please update the documentation with a note below these properties with the explanation that these are only available if location_authentication_enabled is true

state.LocalAuthorizationEnabled = localAuthEnabled

customerManagedKey, err := flattenAzureAIServicesCustomerManagedKey(props.Encryption, env)
Expand Down
6 changes: 1 addition & 5 deletions internal/services/cognitive/ai_services_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2023-05-01/cognitiveservicesaccounts"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2024-10-01/cognitiveservicesaccounts"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -66,8 +66,6 @@ func TestAccCognitiveAzureAIServices_complete(t *testing.T) {
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
check.That(data.ResourceName).Key("tags.Acceptance").HasValue("Test"),
check.That(data.ResourceName).Key("primary_access_key").Exists(),
check.That(data.ResourceName).Key("secondary_access_key").Exists(),
),
},
data.ImportStep(),
Expand All @@ -94,8 +92,6 @@ func TestAccCognitiveAzureAIServices_update(t *testing.T) {
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
check.That(data.ResourceName).Key("tags.Acceptance").HasValue("Test"),
check.That(data.ResourceName).Key("primary_access_key").Exists(),
check.That(data.ResourceName).Key("secondary_access_key").Exists(),
),
},
})
Expand Down
4 changes: 2 additions & 2 deletions internal/services/cognitive/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package client
import (
"fmt"

"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/go-azure-sdk/resource-manager/cognitive/2024-10-01/cognitiveservicesaccounts"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2024-10-01/deployments"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2023-05-01/cognitiveservicesaccounts"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2024-10-01/cognitiveservicesaccounts"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2023-05-01/cognitiveservicesaccounts"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2024-10-01/cognitiveservicesaccounts"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down
37 changes: 25 additions & 12 deletions internal/services/cognitive/cognitive_account_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2023-05-01/cognitiveservicesaccounts"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2024-10-01/cognitiveservicesaccounts"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand All @@ -36,6 +36,11 @@ func dataSourceCognitiveAccount() *pluginsdk.Resource {

"location": commonschema.LocationComputed(),

"local_auth_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"kind": {
Type: pluginsdk.TypeString,
Computed: true,
Expand Down Expand Up @@ -90,19 +95,8 @@ func dataSourceCognitiveAccountRead(d *pluginsdk.ResourceData, meta interface{})
return fmt.Errorf("retrieving %s: %+v", id, err)
}

keys, err := client.AccountsListKeys(ctx, id)
if err != nil {
// TODO: gracefully fail here
return fmt.Errorf("retrieving Keys for %s: %+v", id, err)
}

d.SetId(id.ID())

if model := keys.Model; model != nil {
d.Set("primary_access_key", model.Key1)
d.Set("secondary_access_key", model.Key2)
}

if model := resp.Model; model != nil {
d.Set("location", location.NormalizeNilable(model.Location))
d.Set("kind", model.Kind)
Expand All @@ -115,6 +109,25 @@ func dataSourceCognitiveAccountRead(d *pluginsdk.ResourceData, meta interface{})
d.Set("qna_runtime_endpoint", apiProps.QnaRuntimeEndpoint)
}
d.Set("endpoint", props.Endpoint)

localAuthEnabled := true
if props.DisableLocalAuth != nil {
localAuthEnabled = !*props.DisableLocalAuth
}
d.Set("local_auth_enabled", localAuthEnabled)

if localAuthEnabled {
keys, err := client.AccountsListKeys(ctx, id)
if err != nil {
// note for the resource we shouldn't gracefully fail since we have permission to CRUD it
return fmt.Errorf("listing the Keys for %s: %+v", id, err)
}

if model := keys.Model; model != nil {
d.Set("primary_access_key", model.Key1)
d.Set("secondary_access_key", model.Key2)
}
}
}

flattenedIdentity, err := identity.FlattenSystemAndUserAssignedMap(model.Identity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestAccCognitiveAccountDataSource_basic(t *testing.T) {
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("kind").HasValue("Face"),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
check.That(data.ResourceName).Key("local_auth_enabled").HasValue("true"),
check.That(data.ResourceName).Key("primary_access_key").Exists(),
check.That(data.ResourceName).Key("secondary_access_key").Exists(),
),
Expand All @@ -40,6 +41,7 @@ func TestAccCognitiveAccountDataSource_identity(t *testing.T) {
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("kind").HasValue("Face"),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
check.That(data.ResourceName).Key("local_auth_enabled").HasValue("true"),
check.That(data.ResourceName).Key("primary_access_key").Exists(),
check.That(data.ResourceName).Key("secondary_access_key").Exists(),
check.That(data.ResourceName).Key("identity.0.principal_id").Exists(),
Expand Down
26 changes: 14 additions & 12 deletions internal/services/cognitive/cognitive_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2023-05-01/cognitiveservicesaccounts"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2024-10-01/cognitiveservicesaccounts"
search "github.com/hashicorp/go-azure-sdk/resource-manager/search/2022-09-01/services"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
Expand Down Expand Up @@ -518,17 +518,6 @@ func resourceCognitiveAccountRead(d *pluginsdk.ResourceData, meta interface{}) e
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

keys, err := client.AccountsListKeys(ctx, *id)
if err != nil {
// note for the resource we shouldn't gracefully fail since we have permission to CRUD it
return fmt.Errorf("listing the Keys for %s: %+v", *id, err)
}

if model := keys.Model; model != nil {
d.Set("primary_access_key", model.Key1)
d.Set("secondary_access_key", model.Key2)
}

d.Set("name", id.AccountName)
d.Set("resource_group_name", id.ResourceGroupName)

Expand Down Expand Up @@ -591,6 +580,19 @@ func resourceCognitiveAccountRead(d *pluginsdk.ResourceData, meta interface{}) e
}
d.Set("local_auth_enabled", localAuthEnabled)

if localAuthEnabled {
keys, err := client.AccountsListKeys(ctx, *id)
if err != nil {
// note for the resource we shouldn't gracefully fail since we have permission to CRUD it
return fmt.Errorf("listing the Keys for %s: %+v", *id, err)
}

if model := keys.Model; model != nil {
d.Set("primary_access_key", model.Key1)
d.Set("secondary_access_key", model.Key2)
}
}
Comment on lines +583 to +594
Copy link
Member

Choose a reason for hiding this comment

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

Can you please update the documentation with a note below these properties with the explanation that these are only available if location_auth_enabled is true


Comment on lines +583 to +595
Copy link
Member

Choose a reason for hiding this comment

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

Technically a breaking change since these were being populated when using 2023-05-01 even when local_authentication_enabled = false

Whether those keys were actually useful though is another question. Pointing this out for awareness.

customerManagedKey, err := flattenCognitiveAccountCustomerManagedKey(props.Encryption)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"regexp"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2023-05-01/cognitiveservicesaccounts"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2024-10-01/cognitiveservicesaccounts"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -133,8 +133,6 @@ func TestAccCognitiveAccount_complete(t *testing.T) {
check.That(data.ResourceName).Key("kind").HasValue("Face"),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
check.That(data.ResourceName).Key("tags.Acceptance").HasValue("Test"),
check.That(data.ResourceName).Key("primary_access_key").Exists(),
check.That(data.ResourceName).Key("secondary_access_key").Exists(),
),
},
data.ImportStep(),
Expand Down Expand Up @@ -163,8 +161,6 @@ func TestAccCognitiveAccount_update(t *testing.T) {
check.That(data.ResourceName).Key("kind").HasValue("Face"),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
check.That(data.ResourceName).Key("tags.Acceptance").HasValue("Test"),
check.That(data.ResourceName).Key("primary_access_key").Exists(),
check.That(data.ResourceName).Key("secondary_access_key").Exists(),
),
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"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"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2024-10-01/cognitiveservicesaccounts"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2024-10-01/deployments"
"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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2023-05-01/deployments"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2024-10-01/deployments"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading