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

Added new data source azurerm_eventhub_namespace_authorization_rule #5489

Merged
merged 12 commits into from
Jan 29, 2020
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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(),
},
tracypholmes marked this conversation as resolved.
Show resolved Hide resolved

"namespace_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateEventHubNamespaceName(),
},
tracypholmes marked this conversation as resolved.
Show resolved Hide resolved

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
},
}
}

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)
tracypholmes marked this conversation as resolved.
Show resolved Hide resolved

return nil
}
3 changes: 2 additions & 1 deletion azurerm/internal/services/eventhub/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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(`
data "azurerm_eventhub_namespace_authorization_rule" "test" {
tracypholmes marked this conversation as resolved.
Show resolved Hide resolved
name = "${azurerm_eventhub_namespace_authorization_rule.test.name}"
namespace_name = "${azurerm_eventhub_namespace.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}

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.RandomInteger, data.Locations.Primary, listen, send, manage)
}
56 changes: 56 additions & 0 deletions website/docs/d/eventhub_namespace_authorization_rule.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
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 = "${azurerm_eventhub_authorization_rule.example.resource_group_name}"
tracypholmes marked this conversation as resolved.
Show resolved Hide resolved
}

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.