From 326fd056408bb491871ca7330488f5d03d7e96a6 Mon Sep 17 00:00:00 2001 From: patst Date: Wed, 13 Dec 2023 12:28:52 +0100 Subject: [PATCH] `cognitive_account` datasource: add identity block to attributes (#24214) * add the identity attribute to the `cognitive_account_data_source` * add the identity attribute to the `cognitive_account_data_source` * fix goimports for file * Update internal/services/cognitive/cognitive_account_data_source.go Co-authored-by: stephybun --------- Co-authored-by: stephybun --- .../cognitive_account_data_source.go | 12 +++++ .../cognitive_account_data_source_test.go | 45 +++++++++++++++++++ .../docs/d/cognitive_account.html.markdown | 14 ++++++ 3 files changed, 71 insertions(+) diff --git a/internal/services/cognitive/cognitive_account_data_source.go b/internal/services/cognitive/cognitive_account_data_source.go index 9be617f93d23..df11fbab67be 100644 --- a/internal/services/cognitive/cognitive_account_data_source.go +++ b/internal/services/cognitive/cognitive_account_data_source.go @@ -7,6 +7,8 @@ import ( "fmt" "time" + "github.com/hashicorp/go-azure-helpers/resourcemanager/identity" + "github.com/hashicorp/go-azure-helpers/lang/response" "github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" "github.com/hashicorp/go-azure-helpers/resourcemanager/location" @@ -67,6 +69,8 @@ func dataSourceCognitiveAccount() *pluginsdk.Resource { Sensitive: true, }, + "identity": commonschema.SystemAssignedUserAssignedIdentityComputed(), + "tags": commonschema.Tags(), }, } @@ -114,6 +118,14 @@ func dataSourceCognitiveAccountRead(d *pluginsdk.ResourceData, meta interface{}) d.Set("endpoint", props.Endpoint) } + flattenedIdentity, err := identity.FlattenSystemAndUserAssignedMap(model.Identity) + if err != nil { + return fmt.Errorf("flattening `identity`: %+v", err) + } + if err := d.Set("identity", flattenedIdentity); err != nil { + return fmt.Errorf("setting `identity`: %+v", err) + } + return tags.FlattenAndSet(d, model.Tags) } return nil diff --git a/internal/services/cognitive/cognitive_account_data_source_test.go b/internal/services/cognitive/cognitive_account_data_source_test.go index 09a5922aae09..db242ebd6e4e 100644 --- a/internal/services/cognitive/cognitive_account_data_source_test.go +++ b/internal/services/cognitive/cognitive_account_data_source_test.go @@ -30,6 +30,24 @@ func TestAccCognitiveAccountDataSource_basic(t *testing.T) { }) } +func TestAccCognitiveAccountDataSource_identity(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_cognitive_account", "test") + r := CognitiveAccountDataSource{} + + data.DataSourceTest(t, []acceptance.TestStep{ + { + Config: r.identity(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).Key("kind").HasValue("Face"), + check.That(data.ResourceName).Key("tags.%").HasValue("1"), + 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(), + ), + }, + }) +} + func (CognitiveAccountDataSource) basic(data acceptance.TestData) string { return fmt.Sprintf(` %s @@ -53,6 +71,33 @@ data "azurerm_cognitive_account" "test" { `, CognitiveAccountDataSource{}.template(data), data.RandomInteger) } +func (CognitiveAccountDataSource) identity(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_cognitive_account" "test" { + name = "acctestcogacc-%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + kind = "Face" + sku_name = "S0" + + identity { + type = "SystemAssigned" + } + + tags = { + Acceptance = "Test" + } +} + +data "azurerm_cognitive_account" "test" { + name = azurerm_cognitive_account.test.name + resource_group_name = azurerm_cognitive_account.test.resource_group_name +} +`, CognitiveAccountDataSource{}.template(data), data.RandomInteger) +} + func (CognitiveAccountDataSource) template(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { diff --git a/website/docs/d/cognitive_account.html.markdown b/website/docs/d/cognitive_account.html.markdown index 3bb791d8b17e..c8dd365dbbba 100644 --- a/website/docs/d/cognitive_account.html.markdown +++ b/website/docs/d/cognitive_account.html.markdown @@ -35,6 +35,8 @@ The following arguments are supported: The following attributes are exported: +* `identity` - A `identity` block as defined below. + * `location` - The Azure location where the Cognitive Services Account exists * `kind` - The kind of the Cognitive Services Account @@ -51,6 +53,18 @@ The following attributes are exported: * `tags` - A mapping of tags to assigned to the resource. +--- + +An `identity` block exports the following: + +* `type` - The type of Managed Service Identity that is configured on this Cognitive Account. + +* `principal_id` - The Principal ID of the System Assigned Managed Service Identity that is configured on this Cognitive Account. + +* `tenant_id` - The Tenant ID of the System Assigned Managed Service Identity that is configured on this Cognitive Account. + +* `identity_ids` - The list of User Assigned Managed Identity IDs assigned to this Cognitive Account. + ## Timeouts The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions: