Skip to content

Commit

Permalink
cognitive_account datasource: add identity block to attributes (#24214
Browse files Browse the repository at this point in the history
)

* 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 <[email protected]>

---------

Co-authored-by: stephybun <[email protected]>
  • Loading branch information
patst and stephybun authored Dec 13, 2023
1 parent ce72874 commit 326fd05
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/services/cognitive/cognitive_account_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -67,6 +69,8 @@ func dataSourceCognitiveAccount() *pluginsdk.Resource {
Sensitive: true,
},

"identity": commonschema.SystemAssignedUserAssignedIdentityComputed(),

"tags": commonschema.Tags(),
},
}
Expand Down Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions internal/services/cognitive/cognitive_account_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" {
Expand Down
14 changes: 14 additions & 0 deletions website/docs/d/cognitive_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 326fd05

Please sign in to comment.