diff --git a/builtin/providers/azurerm/config.go b/builtin/providers/azurerm/config.go index 48d50f9f5..d72363f6d 100644 --- a/builtin/providers/azurerm/config.go +++ b/builtin/providers/azurerm/config.go @@ -63,8 +63,9 @@ type ArmClient struct { cdnProfilesClient cdn.ProfilesClient cdnEndpointsClient cdn.EndpointsClient - eventHubClient eventhub.EventHubsClient - eventHubNamespacesClient eventhub.NamespacesClient + eventHubClient eventhub.EventHubsClient + eventHubConsumerGroupClient eventhub.ConsumerGroupsClient + eventHubNamespacesClient eventhub.NamespacesClient providers resources.ProvidersClient resourceGroupClient resources.GroupsClient @@ -226,6 +227,12 @@ func (c *Config) getArmClient() (*ArmClient, error) { ehc.Sender = autorest.CreateSender(withRequestLogging()) 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) setUserAgent(&ehnc.Client) ehnc.Authorizer = spt diff --git a/builtin/providers/azurerm/import_arm_eventhub_consumer_group_test.go b/builtin/providers/azurerm/import_arm_eventhub_consumer_group_test.go new file mode 100644 index 000000000..eb40cb76f --- /dev/null +++ b/builtin/providers/azurerm/import_arm_eventhub_consumer_group_test.go @@ -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, + }, + }, + }) +} diff --git a/builtin/providers/azurerm/provider.go b/builtin/providers/azurerm/provider.go index ab545b28c..d1fa7d682 100644 --- a/builtin/providers/azurerm/provider.go +++ b/builtin/providers/azurerm/provider.go @@ -55,8 +55,9 @@ func Provider() terraform.ResourceProvider { "azurerm_cdn_endpoint": resourceArmCdnEndpoint(), "azurerm_cdn_profile": resourceArmCdnProfile(), - "azurerm_eventhub": resourceArmEventHub(), - "azurerm_eventhub_namespace": resourceArmEventHubNamespace(), + "azurerm_eventhub": resourceArmEventHub(), + "azurerm_eventhub_consumer_group": resourceArmEventHubConsumerGroup(), + "azurerm_eventhub_namespace": resourceArmEventHubNamespace(), "azurerm_lb": resourceArmLoadBalancer(), "azurerm_lb_backend_address_pool": resourceArmLoadBalancerBackendAddressPool(), diff --git a/builtin/providers/azurerm/resource_arm_eventhub_consumer_group.go b/builtin/providers/azurerm/resource_arm_eventhub_consumer_group.go new file mode 100644 index 000000000..648fc761c --- /dev/null +++ b/builtin/providers/azurerm/resource_arm_eventhub_consumer_group.go @@ -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 +} diff --git a/builtin/providers/azurerm/resource_arm_eventhub_consumer_group_test.go b/builtin/providers/azurerm/resource_arm_eventhub_consumer_group_test.go new file mode 100644 index 000000000..206f00c0f --- /dev/null +++ b/builtin/providers/azurerm/resource_arm_eventhub_consumer_group_test.go @@ -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" +} +` diff --git a/website/source/docs/providers/azurerm/r/eventhub.html.markdown b/website/source/docs/providers/azurerm/r/eventhub.html.markdown index 498299627..4a78c3b64 100644 --- a/website/source/docs/providers/azurerm/r/eventhub.html.markdown +++ b/website/source/docs/providers/azurerm/r/eventhub.html.markdown @@ -66,7 +66,7 @@ The following attributes are exported: ## 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 diff --git a/website/source/docs/providers/azurerm/r/eventhub_consumer_group.html.markdown b/website/source/docs/providers/azurerm/r/eventhub_consumer_group.html.markdown new file mode 100644 index 000000000..7f3d11ff9 --- /dev/null +++ b/website/source/docs/providers/azurerm/r/eventhub_consumer_group.html.markdown @@ -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 +``` diff --git a/website/source/layouts/azurerm.erb b/website/source/layouts/azurerm.erb index 5a1fff5b1..d66b04391 100644 --- a/website/source/layouts/azurerm.erb +++ b/website/source/layouts/azurerm.erb @@ -83,13 +83,15 @@ - > Event Hubs