Skip to content

Commit

Permalink
New data source: azurerm_eventhub_cluster (hashicorp#11763)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom Harvey <[email protected]>
  • Loading branch information
favoretti and tombuildsstuff committed May 26, 2021
1 parent ee4003f commit 08c31a0
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
68 changes: 68 additions & 0 deletions azurerm/internal/services/eventhub/eventhub_cluster_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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/services/eventhub/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceEventHubCluster() *schema.Resource {
return &schema.Resource{
Read: dataSourceEventHubClusterRead,

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

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

"resource_group_name": azure.SchemaResourceGroupName(),

"location": azure.SchemaLocationForDataSource(),

"sku_name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

subscriptionId := meta.(*clients.Client).Account.SubscriptionId
id := parse.NewClusterID(subscriptionId, resourceGroup, name)
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("making Read request on Azure EventHub Cluster %q (Resource Group %q): %+v", name, resourceGroup, err)
}
d.SetId(id.ID())

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("sku_name", flattenEventHubClusterSkuName(resp.Sku))
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package eventhub_test

import (
"fmt"
"testing"

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

type EventHubClusterDataSource struct {
}

func TestAccEventHubClusterDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_eventhub_cluster", "test")
r := EventHubClusterDataSource{}

data.DataSourceTest(t, []resource.TestStep{
{
Config: r.basic(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("sku_name").HasValue("Dedicated_1"),
),
},
})
}

func (EventHubClusterDataSource) 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_cluster" "test" {
name = "acctesteventhubclusTER-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku_name = "Dedicated_1"
}
data "azurerm_eventhub_cluster" "test" {
name = azurerm_eventhub_cluster.test.name
resource_group_name = azurerm_resource_group.test.name
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
1 change: 1 addition & 0 deletions azurerm/internal/services/eventhub/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (r Registration) WebsiteCategories() []string {
func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_eventhub": dataSourceEventHub(),
"azurerm_eventhub_cluster": dataSourceEventHubCluster(),
"azurerm_eventhub_authorization_rule": EventHubAuthorizationRuleDataSource(),
"azurerm_eventhub_consumer_group": EventHubConsumerGroupDataSource(),
"azurerm_eventhub_namespace": EventHubNamespaceDataSource(),
Expand Down
48 changes: 48 additions & 0 deletions website/docs/d/eventhub_cluster.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
subcategory: "Messaging"
layout: "azurerm"
page_title: "Azure Resource Manager: Data Source: azurerm_eventhub_cluster"
description: |-
Gets information about an existing EventHub Cluster.
---

# Data Source: azurerm_eventhub_cluster

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

## Example Usage

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

## Arguments Reference

The following arguments are supported:

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

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

## Attributes Reference

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

* `id` - The ID of the EventHub Cluster.

* `sku_name` - SKU name of the EventHub Cluster.

* `location` - Location of the EventHub Cluster.

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

0 comments on commit 08c31a0

Please sign in to comment.