Skip to content

Commit

Permalink
New Resource: azurerm_eventhub_cluster (#7306)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry committed Jun 16, 2020
1 parent 2326ad0 commit f0e1d1c
Show file tree
Hide file tree
Showing 27 changed files with 3,148 additions and 451 deletions.
3 changes: 1 addition & 2 deletions azurerm/helpers/azure/eventhub.go
Expand Up @@ -5,10 +5,9 @@ import (
"log"
"regexp"

"github.com/Azure/azure-sdk-for-go/services/preview/eventhub/mgmt/2018-01-01-preview/eventhub"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
)

// validation
Expand Down
7 changes: 6 additions & 1 deletion azurerm/internal/services/eventhub/client/client.go
@@ -1,18 +1,22 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-sdk-for-go/services/preview/eventhub/mgmt/2018-01-01-preview/eventhub"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

type Client struct {
ClusterClient *eventhub.ClustersClient
ConsumerGroupClient *eventhub.ConsumerGroupsClient
DisasterRecoveryConfigsClient *eventhub.DisasterRecoveryConfigsClient
EventHubsClient *eventhub.EventHubsClient
NamespacesClient *eventhub.NamespacesClient
}

func NewClient(o *common.ClientOptions) *Client {
ClustersClient := eventhub.NewClustersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ClustersClient.Client, o.ResourceManagerAuthorizer)

EventHubsClient := eventhub.NewEventHubsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&EventHubsClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -26,6 +30,7 @@ func NewClient(o *common.ClientOptions) *Client {
o.ConfigureClient(&NamespacesClient.Client, o.ResourceManagerAuthorizer)

return &Client{
ClusterClient: &ClustersClient,
ConsumerGroupClient: &ConsumerGroupClient,
DisasterRecoveryConfigsClient: &DisasterRecoveryConfigsClient,
EventHubsClient: &EventHubsClient,
Expand Down
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-sdk-for-go/services/preview/eventhub/mgmt/2018-01-01-preview/eventhub"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
Expand Down
186 changes: 186 additions & 0 deletions azurerm/internal/services/eventhub/eventhub_cluster_resource.go
@@ -0,0 +1,186 @@
package eventhub

import (
"fmt"
"log"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/eventhub/mgmt/2018-01-01-preview/eventhub"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"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/tags"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmEventHubCluster() *schema.Resource {
return &schema.Resource{
Create: resourceArmEventHubClusterCreateUpdate,
Read: resourceArmEventHubClusterRead,
Update: resourceArmEventHubClusterCreateUpdate,
Delete: resourceArmEventHubClusterDelete,
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.ClusterID(id)
return err
}),

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
// You can't delete a cluster until at least 4 hours have passed from the initial creation.
Delete: schema.DefaultTimeout(300 * time.Minute),
},

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

"resource_group_name": azure.SchemaResourceGroupName(),

"location": azure.SchemaLocation(),

"sku_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"Dedicated_1",
}, false),
},

"tags": tags.Schema(),
},
}
}

func resourceArmEventHubClusterCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Eventhub.ClusterClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for Azure ARM EventHub Cluster creation.")

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

cluster := eventhub.Cluster{
Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))),
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
Sku: expandEventHubClusterSkuName(d.Get("sku_name").(string)),
}

future, err := client.Put(ctx, resourceGroup, name, cluster)
if err != nil {
return fmt.Errorf("creating EventHub Cluster %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation of EventHub Cluster %q (Resource Group %q): %+v", name, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("making Read request on Azure EventHub Cluster %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if read.ID == nil || *read.ID == "" {
return fmt.Errorf("cannot read EventHub Cluster %s (Resource Group %s) ID", name, resourceGroup)
}

d.SetId(*read.ID)

return resourceArmEventHubClusterRead(d, meta)
}

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

id, err := parse.ClusterID(d.Id())
if err != nil {
return err
}
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("making Read request on Azure EventHub Cluster %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

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

return tags.FlattenAndSet(d, resp.Tags)
}

func resourceArmEventHubClusterDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Eventhub.ClusterClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.ClusterID(d.Id())
if err != nil {
return err
}

// The EventHub Cluster can't be deleted until four hours after creation so we'll keep retrying until it can be deleted.
return resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
if strings.Contains(err.Error(), "Cluster cannot be deleted until four hours after its creation time") || future.Response().StatusCode == 429 {
return resource.RetryableError(fmt.Errorf("expected eventhub cluster to be deleted but was in pending creation state, retrying"))
}
return resource.NonRetryableError(fmt.Errorf("issuing delete request for EventHub Cluster %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err))
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return resource.NonRetryableError(fmt.Errorf("deleting EventHub Cluster %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err))
}

return nil
})
}

func expandEventHubClusterSkuName(skuName string) *eventhub.ClusterSku {
if len(skuName) == 0 {
return nil
}

name, capacity, err := azure.SplitSku(skuName)
if err != nil {
return nil
}

return &eventhub.ClusterSku{
Name: utils.String(name),
Capacity: utils.Int32(capacity),
}
}

func flattenEventHubClusterSkuName(input *eventhub.ClusterSku) string {
if input == nil || input.Name == nil {
return ""
}

return fmt.Sprintf("%s_%d", *input.Name, *input.Capacity)
}
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-sdk-for-go/services/preview/eventhub/mgmt/2018-01-01-preview/eventhub"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
Expand Down
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-sdk-for-go/services/preview/eventhub/mgmt/2018-01-01-preview/eventhub"
"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/helpers/tf"
Expand Down
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"time"

"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-sdk-for-go/services/preview/eventhub/mgmt/2018-01-01-preview/eventhub"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
Expand Down
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"
"time"

"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-sdk-for-go/services/preview/eventhub/mgmt/2018-01-01-preview/eventhub"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down
2 changes: 1 addition & 1 deletion azurerm/internal/services/eventhub/eventhub_resource.go
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-sdk-for-go/services/preview/eventhub/mgmt/2018-01-01-preview/eventhub"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
Expand Down
29 changes: 29 additions & 0 deletions azurerm/internal/services/eventhub/parse/cluster.go
@@ -0,0 +1,29 @@
package parse

import "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"

type ClusterId struct {
ResourceGroup string
Name string
}

func ClusterID(input string) (*ClusterId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

cluster := ClusterId{
ResourceGroup: id.ResourceGroup,
}

if cluster.Name, err = id.PopSegment("clusters"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &cluster, nil
}
75 changes: 75 additions & 0 deletions azurerm/internal/services/eventhub/parse/cluster_test.go
@@ -0,0 +1,75 @@
package parse

import (
"testing"
)

func TestClusterID(t *testing.T) {
testData := []struct {
Name string
Input string
Error bool
Expect *ClusterId
}{
{
Name: "Empty",
Input: "",
Error: true,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Error: true,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Error: true,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Error: true,
},
{
Name: "Missing Clusters Key",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.EventHub/clusters/",
Error: true,
},
{
Name: "Clusters ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.EventHub/clusters/cluster1",
Error: false,
Expect: &ClusterId{
ResourceGroup: "group1",
Name: "cluster1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.EventHub/Clusters/cluster1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := ClusterID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expect.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expect.Name, actual.Name)
}

if actual.ResourceGroup != v.Expect.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expect.ResourceGroup, actual.ResourceGroup)
}
}
}
1 change: 1 addition & 0 deletions azurerm/internal/services/eventhub/registration.go
Expand Up @@ -33,6 +33,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
func (r Registration) SupportedResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_eventhub_authorization_rule": resourceArmEventHubAuthorizationRule(),
"azurerm_eventhub_cluster": resourceArmEventHubCluster(),
"azurerm_eventhub_consumer_group": resourceArmEventHubConsumerGroup(),
"azurerm_eventhub_namespace_authorization_rule": resourceArmEventHubNamespaceAuthorizationRule(),
"azurerm_eventhub_namespace_disaster_recovery_config": resourceArmEventHubNamespaceDisasterRecoveryConfig(),
Expand Down

0 comments on commit f0e1d1c

Please sign in to comment.