Skip to content

Commit

Permalink
add identity block to eventgrid domain data source (hashicorp#23274)
Browse files Browse the repository at this point in the history
* add identity to eventgrid domain data source

* fix terraform format errors
  • Loading branch information
markrzasa authored Sep 15, 2023
1 parent 661cd00 commit fc70de5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/services/eventgrid/eventgrid_domain_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -138,6 +139,8 @@ func dataSourceEventGridDomain() *pluginsdk.Resource {
},

"tags": commonschema.TagsDataSource(),

"identity": commonschema.SystemOrUserAssignedIdentityComputed(),
},
}
}
Expand Down Expand Up @@ -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 {
Expand Down
59 changes: 59 additions & 0 deletions internal/services/eventgrid/eventgrid_domain_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
),
},
})
Expand All @@ -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))
}
14 changes: 14 additions & 0 deletions website/docs/d/eventgrid_domain.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit fc70de5

Please sign in to comment.