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

cognitive_account datasource: add identity block to attributes #24214

Merged
merged 4 commits into from
Dec 13, 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
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
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
Loading