Skip to content

Commit

Permalink
Merge pull request #5518 from brunhil/eventhub_consumer_group_datasource
Browse files Browse the repository at this point in the history
New Data Source: 'azurerm_eventhub_consumer_group'
  • Loading branch information
tombuildsstuff authored Feb 11, 2020
2 parents 195e82c + 5aaf923 commit 977fb0c
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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 dataSourceEventHubConsumerGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmEventHubConsumerGroupRead,

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

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

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

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

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"location": azure.SchemaLocationForDataSource(),

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

func dataSourceArmEventHubConsumerGroupRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Eventhub.ConsumerGroupClient
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.Get(ctx, resourceGroup, namespaceName, eventHubName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: EventHub Consumer Group %q (Resource Group %q) was not found", name, resourceGroup)
}
return fmt.Errorf("Error: EventHub Consumer Group %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)

if resp.ConsumerGroupProperties != nil {
d.Set("user_metadata", resp.ConsumerGroupProperties.UserMetadata)
}

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_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
Expand Up @@ -147,7 +147,10 @@ func resourceArmEventHubConsumerGroupRead(d *schema.ResourceData, meta interface
d.Set("eventhub_name", eventHubName)
d.Set("namespace_name", namespaceName)
d.Set("resource_group_name", resGroup)
d.Set("user_metadata", resp.ConsumerGroupProperties.UserMetadata)

if resp.ConsumerGroupProperties != nil {
d.Set("user_metadata", resp.ConsumerGroupProperties.UserMetadata)
}

return nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package tests

import (
"fmt"
"testing"

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

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMEventHubConsumerGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMEventHubConsumerGroup_complete(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMEventHubConsumerGroupExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "user_metadata", "some-meta-data"),
),
},
},
})
}

func testAccDataSourceAzureRMEventHubConsumerGroup_complete(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_eventhub_namespace" "test" {
name = "acctesteventhubnamespace-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Standard"
}
resource "azurerm_eventhub" "test" {
name = "acctesteventhub-%d"
namespace_name = "${azurerm_eventhub_namespace.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
partition_count = 2
message_retention = 7
}
resource "azurerm_eventhub_consumer_group" "test" {
name = "acctesteventhubcg-%d"
namespace_name = "${azurerm_eventhub_namespace.test.name}"
eventhub_name = "${azurerm_eventhub.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
user_metadata = "some-meta-data"
}
data "azurerm_eventhub_consumer_group" "test" {
name = "${azurerm_eventhub_consumer_group.test.name}"
namespace_name = "${azurerm_eventhub_namespace.test.name}"
eventhub_name = "${azurerm_eventhub.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,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_consumer_group.html">azurerm_eventhub_consumer_group</a>
</li>

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

# Data Source: azurerm_eventhub_consumer_group

Use this data source to access information about an existing Event Hubs Consumer Group within an Event Hub.

## Example Usage

```hcl
data "azurerm_eventhub_consumer_group" "test" {
name = "${azurerm_eventhub_consumer_group.test.name}"
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 Consumer Group resource.

* `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 Consumer Group's grandparent Namespace exists.

## Attributes Reference

The following attributes are exported:

* `id` - The EventHub Consumer Group ID.

* `user_metadata` - Specifies the user metadata.

0 comments on commit 977fb0c

Please sign in to comment.