Skip to content

Commit

Permalink
Merge pull request #6841 from robselway/feature/eventhub_data_source
Browse files Browse the repository at this point in the history
New data source azurerm_eventhub
  • Loading branch information
tombuildsstuff committed May 11, 2020
2 parents 6c4ce43 + bed3d85 commit a1ad233
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 0 deletions.
79 changes: 79 additions & 0 deletions azurerm/internal/services/eventhub/eventhub_data_source.go
@@ -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 dataSourceEventHub() *schema.Resource {
return &schema.Resource{
Read: dataSourceEventHubRead,

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

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"namespace_name": {
Type: schema.TypeString,
Required: true,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"partition_count": {
Type: schema.TypeInt,
Computed: true,
},

"partition_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceEventHubRead(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)
namespaceName := d.Get("namespace_name").(string)

resp, err := client.Get(ctx, resourceGroup, namespaceName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: EventHub %q (Resource Group %q / Namespace Name %q) was not found", name, resourceGroup, namespaceName)
}

return fmt.Errorf("Error making Read request on EventHub %q (Resource Group %q / Namespace Name %q): %+v", name, resourceGroup, namespaceName, err)
}

d.SetId(*resp.ID)

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

if props := resp.Properties; props != nil {
d.Set("partition_count", props.PartitionCount)
d.Set("partition_ids", props.PartitionIds)
}

return nil
}
1 change: 1 addition & 0 deletions azurerm/internal/services/eventhub/registration.go
Expand Up @@ -21,6 +21,7 @@ func (r Registration) WebsiteCategories() []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": dataSourceEventHub(),
"azurerm_eventhub_authorization_rule": dataSourceEventHubAuthorizationRule(),
"azurerm_eventhub_consumer_group": dataSourceEventHubConsumerGroup(),
"azurerm_eventhub_namespace": dataSourceEventHubNamespace(),
Expand Down
@@ -0,0 +1,62 @@
package tests

import (
"fmt"
"testing"

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

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceEventHub_basic(data),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(data.ResourceName, "partition_count", "2"),
resource.TestCheckResourceAttr(data.ResourceName, "partition_ids.#", "2"),
),
},
},
})
}

func testAccDataSourceEventHub_basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
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 = "Basic"
}
resource "azurerm_eventhub" "test" {
name = "acctest-eh-%[1]d"
resource_group_name = azurerm_resource_group.test.name
namespace_name = azurerm_eventhub_namespace.test.name
partition_count = 2
message_retention = 1
}
data "azurerm_eventhub" "test" {
name = azurerm_eventhub.test.name
namespace_name = azurerm_eventhub_namespace.test.name
resource_group_name = azurerm_resource_group.test.name
}
`, data.RandomInteger, data.Locations.Primary)
}
4 changes: 4 additions & 0 deletions website/azurerm.erb
Expand Up @@ -210,6 +210,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.html">azurerm_eventhub</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/eventhub_authorization_rule.html">azurerm_eventhub_authorization_rule</a>
</li>
Expand Down
51 changes: 51 additions & 0 deletions website/docs/d/eventhub.html.markdown
@@ -0,0 +1,51 @@
---
subcategory: "Messaging"
layout: "azurerm"
page_title: "Azure Resource Manager: Data Source: azurerm_eventhub"
description: |-
Gets information about an existing EventHub.
---

# Data Source: azurerm_eventhub

Use this data source to access information about an existing EventHub.

## Example Usage

```hcl
data "azurerm_eventhub" "example" {
name = "search-eventhub"
resource_group_name = "search-service"
namespace_name = "search-eventhubns"
}
output "eventhub_id" {
value = data.azurerm_eventhub.example.id
}
```

## Arguments Reference

The following arguments are supported:

* `name` - (Required) The name of this EventHub.

* `resource_group_name` - (Required) The name of the Resource Group where the EventHub exists.

* `namespace_name` - (Required) The name of the EventHub Namespace where the EventHub exists.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:

* `id` - The ID of the EventHub.

* `partition_count` - The number of partitions in the EventHub.

* `partition_ids` - The identifiers for the partitions of this EventHub.

## 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.

0 comments on commit a1ad233

Please sign in to comment.