New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Resource: azurerm_eventhub_cluster
#7306
Changes from 5 commits
c319c7a
0cd8d06
9b6dba6
020bf3a
da20d62
4a246ba
3fb05c6
7e7931d
d2f46fc
a3e49c8
be10780
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,189 @@ | ||||||
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/tags" | ||||||
"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: &schema.ResourceImporter{ | ||||||
State: schema.ImportStatePassthrough, | ||||||
}, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we also check the ID before the import? |
||||||
|
||||||
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 err | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should put some custom error messages here? |
||||||
} | ||||||
|
||||||
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||||||
return fmt.Errorf("creating eventhub cluster: %+v", err) | ||||||
} | ||||||
|
||||||
read, err := client.Get(ctx, resourceGroup, name) | ||||||
if err != nil { | ||||||
return err | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should put some custom error messages here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||||||
} | ||||||
|
||||||
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 := azure.ParseAzureResourceID(d.Id()) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
resourceGroup := id.ResourceGroup | ||||||
name := id.Path["clusters"] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth a custom ID parser here? |
||||||
resp, err := client.Get(ctx, resourceGroup, 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", name, resourceGroup, err) | ||||||
} | ||||||
|
||||||
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 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 := azure.ParseAzureResourceID(d.Id()) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
resourceGroup := id.ResourceGroup | ||||||
name := id.Path["clusters"] | ||||||
|
||||||
// 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, resourceGroup, 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") { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth a 429 check here too given the length of time this operation could be running for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creating/deleting clusters is actually quite fast. It's just that once created you can't delete it for the first 4 hours. I don't think a case where TF would wait for 4 hours makes sense? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @favoretti |
||||||
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", name, resourceGroup, err)) | ||||||
} | ||||||
|
||||||
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||||||
return resource.NonRetryableError(fmt.Errorf("creating eventhub cluster: %+v", err)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo here?
Suggested change
|
||||||
} | ||||||
|
||||||
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) | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
) | ||
|
||
func TestAccAzureRMEventHubCluster_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_eventhub_cluster", "test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.PreCheck(t) }, | ||
Providers: acceptance.SupportedProviders, | ||
CheckDestroy: testCheckAzureRMEventHubClusterDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAzureRMEventHubCluster_basic(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMEventHubClusterExists(data.ResourceName), | ||
), | ||
}, | ||
data.ImportStep(), | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMEventHubCluster_update(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_eventhub_cluster", "test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.PreCheck(t) }, | ||
Providers: acceptance.SupportedProviders, | ||
CheckDestroy: testCheckAzureRMEventHubClusterDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAzureRMEventHubCluster_basic(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMEventHubClusterExists(data.ResourceName), | ||
), | ||
}, | ||
data.ImportStep(), | ||
{ | ||
Config: testAccAzureRMEventHubCluster_update(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMEventHubClusterExists(data.ResourceName), | ||
), | ||
}, | ||
data.ImportStep(), | ||
{ | ||
Config: testAccAzureRMEventHubCluster_basic(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMEventHubClusterExists(data.ResourceName), | ||
), | ||
}, | ||
data.ImportStep(), | ||
}, | ||
}) | ||
} | ||
|
||
func testCheckAzureRMEventHubClusterDestroy(s *terraform.State) error { | ||
conn := acceptance.AzureProvider.Meta().(*clients.Client).Eventhub.ClusterClient | ||
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "azurerm_eventhub_cluster" { | ||
continue | ||
} | ||
|
||
name := rs.Primary.Attributes["name"] | ||
resourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should use the parse id function here on the id |
||
|
||
resp, err := conn.Get(ctx, resourceGroup, name) | ||
|
||
if err != nil { | ||
return nil | ||
} | ||
|
||
if resp.StatusCode != http.StatusNotFound { | ||
return fmt.Errorf("EventHub Cluster still exists:\n%#v", resp.ClusterProperties) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testCheckAzureRMEventHubClusterExists(resourceName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
conn := acceptance.AzureProvider.Meta().(*clients.Client).Eventhub.ClusterClient | ||
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext | ||
|
||
// Ensure we have enough information in state to look up in API | ||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", resourceName) | ||
} | ||
|
||
name := rs.Primary.Attributes["name"] | ||
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and parse id again here |
||
if !hasResourceGroup { | ||
return fmt.Errorf("Bad: no resource group found in state for Event Hub Cluster: %s", name) | ||
} | ||
|
||
resp, err := conn.Get(ctx, resourceGroup, name) | ||
if err != nil { | ||
return fmt.Errorf("Bad: Get on clustersClient: %+v", err) | ||
} | ||
|
||
if resp.StatusCode == http.StatusNotFound { | ||
return fmt.Errorf("Bad: Event Hub Cluster %q (resource group: %q) does not exist", name, resourceGroup) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccAzureRMEventHubCluster_basic(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
|
||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-eventhub-%d" | ||
location = "%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.RandomInteger, data.Locations.Primary, data.RandomInteger) | ||
} | ||
|
||
func testAccAzureRMEventHubCluster_update(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
|
||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-eventhub-%d" | ||
location = "%s" | ||
} | ||
|
||
resource "azurerm_eventhub_cluster" "test" { | ||
name = "acctesteventhubcluster-%d" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can capitals be used here? |
||
resource_group_name = azurerm_resource_group.test.name | ||
location = azurerm_resource_group.test.location | ||
sku_name = "Dedicated_1" | ||
|
||
tags = { | ||
environment = "Production" | ||
} | ||
} | ||
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we can't upgrade the rest of the resources to this api version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call! The preview version was a little wonky when I started but it seems to have evened out with this latest version. It's been taken care of and tests look good