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

New data source: 'azurerm_eventhub_authorization_rule' #5805

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
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 dataSourceEventHubAuthorizationRule() *schema.Resource {
return &schema.Resource{
Read: dataSourceEventHubAuthorizationRuleRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},

Schema: azure.EventHubAuthorizationRuleSchemaFrom(map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateEventHubAuthorizationRuleName(),
},

"namespace_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateEventHubNamespaceName(),
},

"eventhub_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateEventHubName(),
},

"primary_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},

"secondary_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},

"primary_connection_string": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},

"secondary_connection_string": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"location": azure.SchemaLocationForDataSource(),
}),
}
}

func dataSourceEventHubAuthorizationRuleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Eventhub.EventHubsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
eventHubName := d.Get("eventhub_name").(string)
namespaceName := d.Get("namespace_name").(string)

resp, err := client.GetAuthorizationRule(ctx, resourceGroup, namespaceName, eventHubName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: EventHub Authorization Rule %q (Resource Group %q) was not found", name, resourceGroup)
}
return fmt.Errorf("Error: EventHub Authorization Rule %s: %+v", name, err)
}

d.SetId(*resp.ID)

d.Set("name", name)
d.Set("eventhub_name", eventHubName)
d.Set("namespace_name", namespaceName)
d.Set("resource_group_name", resourceGroup)

keysResp, err := client.ListKeys(ctx, resourceGroup, namespaceName, eventHubName, 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
}
1 change: 1 addition & 0 deletions azurerm/internal/services/eventhub/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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_authorization_rule": dataSourceEventHubAuthorizationRule(),
"azurerm_eventhub_consumer_group": dataSourceEventHubConsumerGroup(),
"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,48 @@
package tests

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
)

func TestAccDataSourceAzureRMEventHubAuthorizationRule(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_eventhub_authorization_rule", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMEventHubAuthorizationRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMEventHubAuthorizationRule_base(data, true, true, true),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMEventHubAuthorizationRuleExists(data.ResourceName),
resource.TestCheckResourceAttrSet(data.ResourceName, "name"),
resource.TestCheckResourceAttrSet(data.ResourceName, "namespace_name"),
resource.TestCheckResourceAttrSet(data.ResourceName, "eventhub_name"),
resource.TestCheckResourceAttrSet(data.ResourceName, "primary_key"),
resource.TestCheckResourceAttrSet(data.ResourceName, "secondary_key"),
resource.TestCheckResourceAttrSet(data.ResourceName, "primary_connection_string"),
resource.TestCheckResourceAttrSet(data.ResourceName, "secondary_connection_string"),
),
},
},
})
}

func testAccDataSourceAzureRMEventHubAuthorizationRule_base(data acceptance.TestData, listen, send, manage bool) string {
template := testAccAzureRMEventHubAuthorizationRule_base(data, listen, send, manage)
return fmt.Sprintf(`
%s

data "azurerm_eventhub_authorization_rule" "test" {
name = "${azurerm_eventhub_authorization_rule.test.name}"
namespace_name = "${azurerm_eventhub_namespace.test.name}"
eventhub_name = "${azurerm_eventhub.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, template)
}
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@
<a href="/docs/providers/azurerm/d/disk_encryption_set.html">azurerm_disk_encryption_set</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/eventhub_authorization_rule.html">azurerm_eventhub_authorization_rule</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/eventhub_consumer_group.html">azurerm_eventhub_consumer_group</a>
</li>
Expand Down
52 changes: 52 additions & 0 deletions website/docs/d/eventhub_authorization_rule.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
subcategory: "Messaging"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_eventhub_authorization_rule"
description: |-
Gets information about an Event Hubs Authorization Rule within an Event Hub.
---

# azurerm_eventhub_authorization_rule

Use this data source to access information about an existing Event Hubs Authorization Rule within an Event Hub.

## Example Usage

```hcl
data "azurerm_eventhub_authorization_rule" "test" {
name = "test"
namespace_name = "${azurerm_eventhub_namespace.test.name}"
eventhub_name = "${azurerm_eventhub.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
```

## Argument Reference

* `name` - Specifies the name of the EventHub Authorization Rule resource. be created.

* `namespace_name` - Specifies the name of the grandparent EventHub Namespace.

* `eventhub_name` - Specifies the name of the EventHub.

* `resource_group_name` - The name of the resource group in which the EventHub Authorization Rule's grandparent Namespace exists.

## Attributes Reference

The following attributes are exported:

* `id` - The EventHub ID.

* `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.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when retrieving the EventHub Authorization Rule.