From fc70de5702e271f567e1e40254307e5cb3539365 Mon Sep 17 00:00:00 2001 From: markrzasa Date: Fri, 15 Sep 2023 02:50:42 -0400 Subject: [PATCH] add identity block to eventgrid domain data source (#23274) * add identity to eventgrid domain data source * fix terraform format errors --- .../eventgrid/eventgrid_domain_data_source.go | 11 ++++ .../eventgrid_domain_data_source_test.go | 59 +++++++++++++++++++ website/docs/d/eventgrid_domain.html.markdown | 14 +++++ 3 files changed, 84 insertions(+) diff --git a/internal/services/eventgrid/eventgrid_domain_data_source.go b/internal/services/eventgrid/eventgrid_domain_data_source.go index d84260a74454..59cf02d48121 100644 --- a/internal/services/eventgrid/eventgrid_domain_data_source.go +++ b/internal/services/eventgrid/eventgrid_domain_data_source.go @@ -9,6 +9,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/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/eventgrid/2022-06-15/domains" @@ -138,6 +139,8 @@ func dataSourceEventGridDomain() *pluginsdk.Resource { }, "tags": commonschema.TagsDataSource(), + + "identity": commonschema.SystemOrUserAssignedIdentityComputed(), }, } } @@ -202,6 +205,14 @@ func dataSourceEventGridDomainRead(d *pluginsdk.ResourceData, meta interface{}) if err := d.Set("inbound_ip_rule", inboundIPRules); err != nil { return fmt.Errorf("setting `inbound_ip_rule` in %s: %+v", id, err) } + + 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) + } } if err := tags.FlattenAndSet(d, model.Tags); err != nil { diff --git a/internal/services/eventgrid/eventgrid_domain_data_source_test.go b/internal/services/eventgrid/eventgrid_domain_data_source_test.go index 50923b8ae5ee..9fc6dc297504 100644 --- a/internal/services/eventgrid/eventgrid_domain_data_source_test.go +++ b/internal/services/eventgrid/eventgrid_domain_data_source_test.go @@ -35,6 +35,43 @@ func TestAccEventGridDomainDataSource_basic(t *testing.T) { check.That(data.ResourceName).Key("inbound_ip_rule.1.ip_mask").Exists(), check.That(data.ResourceName).Key("inbound_ip_rule.0.action").Exists(), check.That(data.ResourceName).Key("inbound_ip_rule.1.action").Exists(), + check.That(data.ResourceName).Key("identity.#").HasValue("0"), + ), + }, + }) +} + +func TestAccEventGridDomainDataSource_systemAssignedIdentity(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_eventgrid_domain", "test") + r := EventGridDomainDataSource{} + + data.DataSourceTest(t, []acceptance.TestStep{ + { + Config: r.basicWithSystemManagedIdentity(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).Key("identity.#").HasValue("1"), + check.That(data.ResourceName).Key("identity.0.type").HasValue("SystemAssigned"), + check.That(data.ResourceName).Key("identity.0.identity_ids.#").HasValue("0"), + check.That(data.ResourceName).Key("identity.0.principal_id").Exists(), + check.That(data.ResourceName).Key("identity.0.tenant_id").Exists(), + ), + }, + }) +} + +func TestAccEventGridDomainDataSource_userAssignedIdentity(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_eventgrid_domain", "test") + r := EventGridDomainDataSource{} + + data.DataSourceTest(t, []acceptance.TestStep{ + { + Config: r.basicWithUserAssignedManagedIdentity(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).Key("identity.#").HasValue("1"), + check.That(data.ResourceName).Key("identity.0.type").HasValue("UserAssigned"), + check.That(data.ResourceName).Key("identity.0.identity_ids.#").HasValue("1"), + check.That(data.ResourceName).Key("identity.0.principal_id").IsEmpty(), + check.That(data.ResourceName).Key("identity.0.tenant_id").IsEmpty(), ), }, }) @@ -50,3 +87,25 @@ data "azurerm_eventgrid_domain" "test" { } `, EventGridDomainResource{}.complete(data)) } + +func (EventGridDomainDataSource) basicWithSystemManagedIdentity(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +data "azurerm_eventgrid_domain" "test" { + name = azurerm_eventgrid_domain.test.name + resource_group_name = azurerm_resource_group.test.name +} +`, EventGridDomainResource{}.basicWithSystemManagedIdentity(data)) +} + +func (EventGridDomainDataSource) basicWithUserAssignedManagedIdentity(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +data "azurerm_eventgrid_domain" "test" { + name = azurerm_eventgrid_domain.test.name + resource_group_name = azurerm_resource_group.test.name +} +`, EventGridDomainResource{}.basicWithUserAssignedManagedIdentity(data)) +} diff --git a/website/docs/d/eventgrid_domain.html.markdown b/website/docs/d/eventgrid_domain.html.markdown index 08bd714a9ff0..d1aac45cb526 100644 --- a/website/docs/d/eventgrid_domain.html.markdown +++ b/website/docs/d/eventgrid_domain.html.markdown @@ -58,6 +58,8 @@ The following attributes are exported: * `tags` - A mapping of tags assigned to the EventGrid Domain. +* `identity` - An `identity` block as documented below. + --- A `input_mapping_fields` supports the following: @@ -92,6 +94,18 @@ A `inbound_ip_rule` block supports the following: * `action` - The action to take when the rule is matched. Possible values are `Allow`. +--- + +An `identity` block exports the following: + +* `type` - The type of Managed Service Identity that is configured on this EventGrid Domain. + +* `principal_id` - The Principal ID of the System Assigned Managed Service Identity. + +* `tenant_id` - The Tenant ID of the System Assigned Managed Service Identity. + +* `identity_ids` - The list of User Assigned Managed Identity IDs assigned to this EventGrid Domain. + ## Timeouts The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions: