diff --git a/azurerm/internal/services/eventhub/data_source_eventhub_namespace_authorization_rule.go b/azurerm/internal/services/eventhub/data_source_eventhub_namespace_authorization_rule.go new file mode 100644 index 000000000000..c0e2c9b8b181 --- /dev/null +++ b/azurerm/internal/services/eventhub/data_source_eventhub_namespace_authorization_rule.go @@ -0,0 +1,118 @@ +package eventhub + +import ( + "fmt" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceEventHubNamespaceAuthorizationRule() *schema.Resource { + return &schema.Resource{ + Read: dataSourceEventHubNamespaceAuthorizationRuleRead, + + Timeouts: &schema.ResourceTimeout{ + Read: schema.DefaultTimeout(5 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: azure.ValidateEventHubAuthorizationRuleName(), + }, + + "namespace_name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: azure.ValidateEventHubNamespaceName(), + }, + + "resource_group_name": azure.SchemaResourceGroupNameForDataSource(), + + "listen": { + Type: schema.TypeBool, + Computed: true, + }, + + "send": { + Type: schema.TypeBool, + Computed: true, + }, + + "manage": { + Type: schema.TypeBool, + Computed: true, + }, + + "primary_key": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + }, + + "primary_connection_string": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + }, + + "secondary_key": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + }, + + "secondary_connection_string": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + }, + }, + } +} + +func dataSourceEventHubNamespaceAuthorizationRuleRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).Eventhub.NamespacesClient + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + name := d.Get("name").(string) + resourceGroup := d.Get("resource_group_name").(string) + namespaceName := d.Get("namespace_name").(string) + + resp, err := client.GetAuthorizationRule(ctx, resourceGroup, namespaceName, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Error: Azure EventHub Authorization Rule %q (Resource Group %q / Namespace Name %q) was not found", name, resourceGroup, namespaceName) + } + return fmt.Errorf("Error making Read request on Azure EventHub Authorization Rule %s: %+v", name, err) + } + + d.Set("name", name) + d.Set("namespace_name", namespaceName) + d.Set("resource_group_name", resourceGroup) + + if props := resp.AuthorizationRuleProperties; props != nil { + listen, send, manage := azure.FlattenEventHubAuthorizationRuleRights(props.Rights) + d.Set("manage", manage) + d.Set("listen", listen) + d.Set("send", send) + } + + keysResp, err := client.ListKeys(ctx, resourceGroup, namespaceName, name) + if err != nil { + return fmt.Errorf("Error making Read request on Azure EventHub Authorization Rule List Keys %s: %+v", name, err) + } + + d.Set("primary_key", keysResp.PrimaryKey) + d.Set("secondary_key", keysResp.SecondaryKey) + d.Set("primary_connection_string", keysResp.PrimaryConnectionString) + d.Set("secondary_connection_string", keysResp.SecondaryConnectionString) + + return nil +} diff --git a/azurerm/internal/services/eventhub/registration.go b/azurerm/internal/services/eventhub/registration.go index 508b5a8bbfaf..2513826f993d 100644 --- a/azurerm/internal/services/eventhub/registration.go +++ b/azurerm/internal/services/eventhub/registration.go @@ -14,7 +14,8 @@ func (r Registration) Name() string { // SupportedDataSources returns the supported Data Sources supported by this Service func (r Registration) SupportedDataSources() map[string]*schema.Resource { return map[string]*schema.Resource{ - "azurerm_eventhub_namespace": dataSourceEventHubNamespace(), + "azurerm_eventhub_namespace": dataSourceEventHubNamespace(), + "azurerm_eventhub_namespace_authorization_rule": dataSourceEventHubNamespaceAuthorizationRule(), } } diff --git a/azurerm/internal/services/eventhub/tests/data_source_eventhub_namespace_authorization_rule_test.go b/azurerm/internal/services/eventhub/tests/data_source_eventhub_namespace_authorization_rule_test.go new file mode 100644 index 000000000000..2a747b475668 --- /dev/null +++ b/azurerm/internal/services/eventhub/tests/data_source_eventhub_namespace_authorization_rule_test.go @@ -0,0 +1,57 @@ +package tests + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" +) + +func TestAccDataSourceAzureRMEventHubNamespaceAuthorizationRule_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "data.eventhub_namespace_authorization_rule", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceEventHubNamespaceAuthorizationRule_basic(data, true, true, true), + Check: resource.ComposeTestCheckFunc(), + }, + }, + }) +} + +func testAccDataSourceEventHubNamespaceAuthorizationRule_basic(data acceptance.TestData, listen, send, manage bool) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-eventhub-%[1]d" + location = "%[2]s" +} + +resource "azurerm_eventhub_namespace" "test" { + name = "acctest-EHN-%[1]d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + sku = "Standard" +} + +resource "azurerm_eventhub_namespace_authorization_rule" "test" { + name = "acctest-EHN-AR%[1]d" + namespace_name = "${azurerm_eventhub_namespace.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" + + listen = %[3]t + send = %[4]t + manage = %[5]t +} + +data "azurerm_eventhub_namespace_authorization_rule" "test" { + name = "${azurerm_eventhub_namespace_authorization_rule.test.name}" + namespace_name = "${azurerm_eventhub_namespace.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" +} +`, data.RandomInteger, data.Locations.Primary, listen, send, manage) +} diff --git a/website/docs/d/eventhub_namespace_authorization_rule.html.markdown b/website/docs/d/eventhub_namespace_authorization_rule.html.markdown new file mode 100644 index 000000000000..95a70a87d30c --- /dev/null +++ b/website/docs/d/eventhub_namespace_authorization_rule.html.markdown @@ -0,0 +1,55 @@ +--- +subcategory: "Messaging" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_eventhub_namespace_authorization_rule" +description: |- + Gets information about an Authorization Rule for an Event Hub Namespace. +--- + +# Data Source: azurerm_eventhub_namespace_authorization_rule + +Use this data source to access information about an Authorization Rule for an Event Hub Namespace. + +## Example Usage + +```hcl +data "azurerm_eventhub_namespace_authorization_rule" "example" { + name = "navi" + resource_group_name = "example-resources" +} + +output "eventhub_authorization_rule_id" { + value = "${data.azurem_eventhub_namespace_authorization_rule.example.id}" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the EventHub Authorization Rule resource. + +* `resource_group_name` - (Required) The name of the resource group in which the EventHub Namespace exists. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The EventHub ID. + +* `namespace_name` - The name of the EventHub Namespace. + +* `listen` - Does this Authorization Rule have permissions to Listen to the Event Hub? + +* `send` - Does this Authorization Rule have permissions to Send to the Event Hub? + +* `manage` - Does this Authorization Rule have permissions to Manage to the Event Hub? + +* `primary_key` - The Primary Key for the Event Hubs authorization Rule. + +* `primary_connection_string` - The Primary Connection String for the Event Hubs authorization Rule. + +* `secondary_key` - The Secondary Key for the Event Hubs authorization Rule. + +* `secondary_connection_string` - The Secondary Connection String for the Event Hubs authorization Rule. +