Skip to content

Commit

Permalink
Added new data source `azurerm_eventhub_namespace_authorization… (#5489)
Browse files Browse the repository at this point in the history
Fixes #3577
  • Loading branch information
tracypholmes authored Jan 29, 2020
1 parent fd6db94 commit 77ebbc4
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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
}
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(`
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)
}
55 changes: 55 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,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.

0 comments on commit 77ebbc4

Please sign in to comment.