provider/azurerm: Event Hubs Consumer Groups (#9902)

* Documentation

* Scaffolding Consumer Groups

* Linting

* Adding a missing </li>

* User MetaData needs to explicitly be optional

* Typo

* Fixing the test syntax

* Removing the eventHubPath field since it appears to be deprecated

* Added a Complete import test

* WIP

* Updating to use SDK 7.0.1

* Calling the correct method

* Removing eventhubPath and updating the docs

* Fixing a typo
This commit is contained in:
Tom Harvey 2016-12-28 23:12:47 +00:00 committed by Paul Stack
parent 3dc3b88a78
commit cc09ac6b3f
8 changed files with 475 additions and 6 deletions

View File

@ -64,6 +64,7 @@ type ArmClient struct {
cdnEndpointsClient cdn.EndpointsClient cdnEndpointsClient cdn.EndpointsClient
eventHubClient eventhub.EventHubsClient eventHubClient eventhub.EventHubsClient
eventHubConsumerGroupClient eventhub.ConsumerGroupsClient
eventHubNamespacesClient eventhub.NamespacesClient eventHubNamespacesClient eventhub.NamespacesClient
providers resources.ProvidersClient providers resources.ProvidersClient
@ -226,6 +227,12 @@ func (c *Config) getArmClient() (*ArmClient, error) {
ehc.Sender = autorest.CreateSender(withRequestLogging()) ehc.Sender = autorest.CreateSender(withRequestLogging())
client.eventHubClient = ehc client.eventHubClient = ehc
chcgc := eventhub.NewConsumerGroupsClient(c.SubscriptionID)
setUserAgent(&chcgc.Client)
chcgc.Authorizer = spt
chcgc.Sender = autorest.CreateSender(withRequestLogging())
client.eventHubConsumerGroupClient = chcgc
ehnc := eventhub.NewNamespacesClient(c.SubscriptionID) ehnc := eventhub.NewNamespacesClient(c.SubscriptionID)
setUserAgent(&ehnc.Client) setUserAgent(&ehnc.Client)
ehnc.Authorizer = spt ehnc.Authorizer = spt

View File

@ -0,0 +1,58 @@
package azurerm
import (
"testing"
"fmt"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccAzureRMEventHubConsumerGroup_importBasic(t *testing.T) {
resourceName := "azurerm_eventhub_consumer_group.test"
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMEventHubConsumerGroup_basic, ri, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMEventHubConsumerGroupDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccAzureRMEventHubConsumerGroup_importComplete(t *testing.T) {
resourceName := "azurerm_eventhub_consumer_group.test"
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMEventHubConsumerGroup_complete, ri, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMEventHubConsumerGroupDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

View File

@ -56,6 +56,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_cdn_profile": resourceArmCdnProfile(), "azurerm_cdn_profile": resourceArmCdnProfile(),
"azurerm_eventhub": resourceArmEventHub(), "azurerm_eventhub": resourceArmEventHub(),
"azurerm_eventhub_consumer_group": resourceArmEventHubConsumerGroup(),
"azurerm_eventhub_namespace": resourceArmEventHubNamespace(), "azurerm_eventhub_namespace": resourceArmEventHubNamespace(),
"azurerm_lb": resourceArmLoadBalancer(), "azurerm_lb": resourceArmLoadBalancer(),

View File

@ -0,0 +1,152 @@
package azurerm
import (
"fmt"
"log"
"net/http"
"github.com/Azure/azure-sdk-for-go/arm/eventhub"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceArmEventHubConsumerGroup() *schema.Resource {
return &schema.Resource{
Create: resourceArmEventHubConsumerGroupCreateUpdate,
Read: resourceArmEventHubConsumerGroupRead,
Update: resourceArmEventHubConsumerGroupCreateUpdate,
Delete: resourceArmEventHubConsumerGroupDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"namespace_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"eventhub_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"resource_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"location": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"user_metadata": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceArmEventHubConsumerGroupCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
eventhubClient := client.eventHubConsumerGroupClient
log.Printf("[INFO] preparing arguments for Azure ARM EventHub Consumer Group creation.")
name := d.Get("name").(string)
namespaceName := d.Get("namespace_name").(string)
eventHubName := d.Get("eventhub_name").(string)
location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string)
userMetaData := d.Get("user_metadata").(string)
parameters := eventhub.ConsumerGroupCreateOrUpdateParameters{
Name: &name,
Location: &location,
ConsumerGroupProperties: &eventhub.ConsumerGroupProperties{
UserMetadata: &userMetaData,
},
}
_, err := eventhubClient.CreateOrUpdate(resGroup, namespaceName, eventHubName, name, parameters)
if err != nil {
return err
}
read, err := eventhubClient.Get(resGroup, namespaceName, eventHubName, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read EventHub Consumer Group %s (resource group %s) ID", name, resGroup)
}
d.SetId(*read.ID)
return resourceArmEventHubConsumerGroupRead(d, meta)
}
func resourceArmEventHubConsumerGroupRead(d *schema.ResourceData, meta interface{}) error {
eventhubClient := meta.(*ArmClient).eventHubConsumerGroupClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
namespaceName := id.Path["namespaces"]
eventHubName := id.Path["eventhubs"]
name := id.Path["consumergroups"]
resp, err := eventhubClient.Get(resGroup, namespaceName, eventHubName, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure EventHub Consumer Group %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
d.Set("name", name)
d.Set("eventhub_name", eventHubName)
d.Set("namespace_name", namespaceName)
d.Set("resource_group_name", resGroup)
d.Set("location", azureRMNormalizeLocation(*resp.Location))
d.Set("user_metadata", resp.ConsumerGroupProperties.UserMetadata)
return nil
}
func resourceArmEventHubConsumerGroupDelete(d *schema.ResourceData, meta interface{}) error {
eventhubClient := meta.(*ArmClient).eventHubConsumerGroupClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
namespaceName := id.Path["namespaces"]
eventHubName := id.Path["eventhubs"]
name := id.Path["consumergroups"]
resp, err := eventhubClient.Delete(resGroup, namespaceName, eventHubName, name)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Error issuing Azure ARM delete request of EventHub Consumer Group '%s': %s", name, err)
}
return nil
}

View File

@ -0,0 +1,171 @@
package azurerm
import (
"fmt"
"net/http"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAzureRMEventHubConsumerGroup_basic(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMEventHubConsumerGroup_basic, ri, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMEventHubConsumerGroupDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMEventHubConsumerGroupExists("azurerm_eventhub_consumer_group.test"),
),
},
},
})
}
func TestAccAzureRMEventHubConsumerGroup_complete(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMEventHubConsumerGroup_complete, ri, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMEventHubConsumerGroupDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMEventHubConsumerGroupExists("azurerm_eventhub_consumer_group.test"),
),
},
},
})
}
func testCheckAzureRMEventHubConsumerGroupDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).eventHubConsumerGroupClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_eventhub_consumer_group" {
continue
}
name := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
namespaceName := rs.Primary.Attributes["namespace_name"]
eventHubName := rs.Primary.Attributes["eventhub_name"]
resp, err := conn.Get(resourceGroup, namespaceName, eventHubName, name)
if err != nil {
return nil
}
if resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("EventHub Consumer Group still exists:\n%#v", resp.ConsumerGroupProperties)
}
}
return nil
}
func testCheckAzureRMEventHubConsumerGroupExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
name := rs.Primary.Attributes["name"]
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
if !hasResourceGroup {
return fmt.Errorf("Bad: no resource group found in state for Event Hub Consumer Group: %s", name)
}
conn := testAccProvider.Meta().(*ArmClient).eventHubConsumerGroupClient
namespaceName := rs.Primary.Attributes["namespace_name"]
eventHubName := rs.Primary.Attributes["eventhub_name"]
resp, err := conn.Get(resourceGroup, namespaceName, eventHubName, name)
if err != nil {
return fmt.Errorf("Bad: Get on eventHubConsumerGroupClient: %s", err)
}
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("Bad: Event Hub Consumer Group %q (resource group: %q) does not exist", name, resourceGroup)
}
return nil
}
}
var testAccAzureRMEventHubConsumerGroup_basic = `
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US"
}
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}"
location = "${azurerm_resource_group.test.location}"
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}"
location = "${azurerm_resource_group.test.location}"
}
`
var testAccAzureRMEventHubConsumerGroup_complete = `
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US"
}
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}"
location = "${azurerm_resource_group.test.location}"
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}"
location = "${azurerm_resource_group.test.location}"
user_metadata = "some-meta-data"
}
`

View File

@ -66,7 +66,7 @@ The following attributes are exported:
## Import ## Import
EventHub's can be imported using the `resource id`, e.g. EventHubs can be imported using the `resource id`, e.g.
``` ```
terraform import azurerm_eventhub.eventhub1 /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/eventhubs/eventhub1 terraform import azurerm_eventhub.eventhub1 /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/eventhubs/eventhub1

View File

@ -0,0 +1,78 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_eventhub_consumer_group"
sidebar_current: "docs-azurerm-resource-eventhub-consumer-group"
description: |-
Creates a new Event Hub Consumer Group as a nested resource within an Event Hub.
---
# azurerm\_eventhub\_consumer\_group
Creates a new Event Hub Consumer Group as a nested resource within an Event Hub.
## Example Usage
```
resource "azurerm_resource_group" "test" {
name = "resourceGroup1"
location = "West US"
}
resource "azurerm_eventhub_namespace" "test" {
name = "acceptanceTestEventHubNamespace"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Basic"
capacity = 2
tags {
environment = "Production"
}
}
resource "azurerm_eventhub" "test" {
name = "acceptanceTestEventHub"
namespace_name = "${azurerm_eventhub_namespace.test.name}"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
partition_count = 2
message_retention = 2
}
resource "azurerm_eventhub_consumer_group" "test" {
name = "acceptanceTestEventHubConsumerGroup"
namespace_name = "${azurerm_eventhub_namespace.test.name}"
eventhub_name = "${azurerm_eventhub.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
user_metadata = "some-meta-data"
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) Specifies the name of the EventHub Consumer Group resource. Changing this forces a new resource to be created.
* `namespace_name` - (Required) Specifies the name of the grandparent EventHub Namespace. Changing this forces a new resource to be created.
* `resource_group_name` - (Required) The name of the resource group in which the EventHub Consumer Group's grandparent Namespace exists. Changing this forces a new resource to be created.
* `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created.
* `user_metadata` - (Optional) Specifies the user metadata.
## Attributes Reference
The following attributes are exported:
* `id` - The EventHub Consumer Group ID.
## Import
EventHub Consumer Groups can be imported using the `resource id`, e.g.
```
terraform import azurerm_eventhub_consumer_group.consumerGroup1 /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/eventhubs/eventhub1/consumergroups/consumerGroup1
```

View File

@ -83,13 +83,15 @@
</ul> </ul>
</li> </li>
<li<%= sidebar_current(/^docs-azurerm-resource-eventhub/) %>> <li<%= sidebar_current(/^docs-azurerm-resource-eventhub/) %>>
<a href="#">Event Hubs</a> <a href="#">Event Hubs</a>
<ul class="nav nav-visible"> <ul class="nav nav-visible">
<li<%= sidebar_current("docs-azurerm-resource-eventhub") %>> <li<%= sidebar_current("docs-azurerm-resource-eventhub") %>>
<a href="/docs/providers/azurerm/r/eventhub.html">azurerm_eventhub</a> <a href="/docs/providers/azurerm/r/eventhub.html">azurerm_eventhub</a>
</li> </li>
<li<%= sidebar_current("docs-azurerm-resource-eventhub-consumer-group") %>>
<a href="/docs/providers/azurerm/r/eventhub_consumer_group.html">azurerm_eventhub_consumer_group</a>
</li>
<li<%= sidebar_current("docs-azurerm-resource-eventhub-namespace") %>> <li<%= sidebar_current("docs-azurerm-resource-eventhub-namespace") %>>
<a href="/docs/providers/azurerm/r/eventhub_namespace.html">azurerm_eventhub_namespace</a> <a href="/docs/providers/azurerm/r/eventhub_namespace.html">azurerm_eventhub_namespace</a>
</li> </li>