Skip to content
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 resources: azurerm_eventhub_namespace_dedicated and azurerm_eventhub_dedicated #7347

Closed
wants to merge 48 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
39b309f
Adjust partition count validator
favoretti Jun 16, 2020
cec7b0f
Update tests
favoretti Jun 16, 2020
f8f4f58
Up retention limits to fit eventhub clusters
favoretti Jun 16, 2020
977375b
WIP: dedicated eventhubs
favoretti Jun 22, 2020
44934a3
Fix tests
favoretti Jun 22, 2020
9680d01
Revert error messages to what they should be
favoretti Jun 22, 2020
50dd95a
Add tests
favoretti Jun 22, 2020
b8d1041
Fix typos, add registration
favoretti Jun 22, 2020
2401a49
More typoes, set locks for both normal and dedicated hubs and namespaces
favoretti Jun 22, 2020
d049c1c
Fix last linter error
favoretti Jun 22, 2020
1b2f139
Added documentation
favoretti Jun 22, 2020
6b42455
Update CHANGELOG
favoretti Jun 22, 2020
5689261
Sigh, more typoes
favoretti Jun 22, 2020
2d0372e
Update website/docs/r/eventhub_dedicated.html.markdown
favoretti Jun 24, 2020
ec54ba7
Update website/docs/r/eventhub_dedicated.html.markdown
favoretti Jun 24, 2020
a626c83
Update azurerm/internal/services/eventhub/eventhub_dedicated_resource.go
favoretti Jun 24, 2020
b65f3c0
Update website/docs/r/eventhub_dedicated.html.markdown
favoretti Jun 24, 2020
0b18f1e
Update website/docs/r/eventhub_namespace_dedicated.html.markdown
favoretti Jun 24, 2020
25f72f5
Revert "Update CHANGELOG"
favoretti Jun 24, 2020
1a6cdfe
Address review feedback
favoretti Jun 24, 2020
14c3576
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 24, 2020
5c15c31
Update azurerm/internal/services/eventhub/tests/eventhub_dedicated_re…
favoretti Jun 24, 2020
f623029
Update azurerm/internal/services/eventhub/tests/eventhub_dedicated_re…
favoretti Jun 24, 2020
9d12d6d
Address feedback batch 2
favoretti Jun 24, 2020
dbce63e
Update azurerm/internal/services/eventhub/eventhub_dedicated_resource.go
favoretti Jun 27, 2020
8917af5
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
5452ca4
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
0005649
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
953dbfa
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
8d3c2cf
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
6dbe1e9
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
a662eb4
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
4a3a4e0
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
7cc7328
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
202b886
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
3953e9b
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
33597bf
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
1c33705
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
ca31414
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
0d8452a
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
2f51ac6
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
baff0ed
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
0841593
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
cca9342
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
e3ab34b
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
32c188b
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
8d3bd89
Update azurerm/internal/services/eventhub/eventhub_namespace_dedicate…
favoretti Jun 27, 2020
b4b2a98
Address feedback batch 3
favoretti Jun 29, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ func resourceArmEventHubAuthorizationRuleCreateUpdate(d *schema.ResourceData, me
locks.ByName(namespaceName, eventHubNamespaceResourceName)
defer locks.UnlockByName(namespaceName, eventHubNamespaceResourceName)

locks.ByName(eventHubName, eventHubDedicatedResourceName)
defer locks.UnlockByName(eventHubName, eventHubDedicatedResourceName)

locks.ByName(namespaceName, eventHubNamespaceDedicatedResourceName)
defer locks.UnlockByName(namespaceName, eventHubNamespaceDedicatedResourceName)

Comment on lines +98 to +103
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I missed on the first pass, but we'll need this locking in the resourceArmEventHubAuthorizationRuleDelete func too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@favoretti & @jackofallops - do you think azurerm_eventhub_namespace_dedicated would be more aptly named azurerm_eventhub_dedicated_namespace? after some internal discussion and that these belong to a cluster i think azurerm_eventhub_cluster_hub and azurerm_eventhub_cluster_[hub_]namespace would make the most sense?

MSFT isn't super clear on naming either it seems. Lots of documentation calls them dedicated eventhubs, however portal calls them Event Hubs Cluster.

parameters := eventhub.AuthorizationRule{
Name: &name,
AuthorizationRuleProperties: &eventhub.AuthorizationRuleProperties{
Expand Down Expand Up @@ -197,6 +203,12 @@ func resourceArmEventHubAuthorizationRuleDelete(d *schema.ResourceData, meta int
locks.ByName(namespaceName, eventHubNamespaceResourceName)
defer locks.UnlockByName(namespaceName, eventHubNamespaceResourceName)

locks.ByName(eventHubName, eventHubDedicatedResourceName)
defer locks.UnlockByName(eventHubName, eventHubDedicatedResourceName)

locks.ByName(namespaceName, eventHubNamespaceDedicatedResourceName)
defer locks.UnlockByName(namespaceName, eventHubNamespaceDedicatedResourceName)

if resp, err := eventhubClient.DeleteAuthorizationRule(ctx, resourceGroup, namespaceName, eventHubName, name); err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error issuing Azure ARM delete request of EventHub Authorization Rule '%s': %+v", name, err)
Expand Down
289 changes: 289 additions & 0 deletions azurerm/internal/services/eventhub/eventhub_dedicated_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
package eventhub

import (
"fmt"
"log"
"time"

"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"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"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"
)

var eventHubDedicatedResourceName = "azurerm_eventhub_dedicated"

func resourceArmEventHubDedicated() *schema.Resource {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

taking a look through this appears to be the same as the azurerm_eventhub resource, so I think we can remove this and reuse that instead?

return &schema.Resource{
Create: resourceArmEventHubDedicatedCreateUpdate,
Read: resourceArmEventHubDedicatedRead,
Update: resourceArmEventHubDedicatedCreateUpdate,
Delete: resourceArmEventHubDedicatedDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

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

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

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

"resource_group_name": azure.SchemaResourceGroupName(),

"partition_count": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: ValidateEventHubDedicatedPartitionCount,
},

"message_retention": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: ValidateEventHubDedicatedMessageRetentionCount,
},

"capture_description": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Required: true,
},
"skip_empty_archives": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"encoding": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
string(eventhub.Avro),
string(eventhub.AvroDeflate),
}, true),
},
"interval_in_seconds": {
Type: schema.TypeInt,
Optional: true,
Default: 300,
ValidateFunc: validation.IntBetween(60, 900),
},
"size_limit_in_bytes": {
Type: schema.TypeInt,
Optional: true,
Default: 314572800,
ValidateFunc: validation.IntBetween(10485760, 524288000),
},
"destination": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"EventHubArchive.AzureBlockBlob",
// TODO: support `EventHubArchive.AzureDataLake` once supported in the Swagger / SDK
// https://github.com/Azure/azure-rest-api-specs/issues/2255
// BlobContainerName & StorageAccountID can then become Optional
}, false),
},
"archive_name_format": {
Type: schema.TypeString,
Required: true,
ValidateFunc: ValidateEventHubArchiveNameFormat,
},
"blob_container_name": {
Type: schema.TypeString,
Required: true,
},
"storage_account_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateResourceID,
},
},
},
},
},
},
},

"partition_ids": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Computed: true,
},
},
}
}

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

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

if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, namespaceName, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Dedicated EventHub %q (Namespace %q / Resource Group %q): %s", name, namespaceName, resourceGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_eventhub", *existing.ID)
}
}

partitionCount := int64(d.Get("partition_count").(int))
messageRetention := int64(d.Get("message_retention").(int))

parameters := eventhub.Model{
Properties: &eventhub.Properties{
PartitionCount: &partitionCount,
MessageRetentionInDays: &messageRetention,
},
}

if _, ok := d.GetOk("capture_description"); ok {
parameters.Properties.CaptureDescription = expandEventHubCaptureDescription(d)
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, namespaceName, name, parameters); err != nil {
return err
}

read, err := client.Get(ctx, resourceGroup, namespaceName, name)
if err != nil {
return err
}

if read.ID == nil {
return fmt.Errorf("Cannot read EventHub %s (resource group %s) ID", name, resourceGroup)
}

d.SetId(*read.ID)

return resourceArmEventHubDedicatedRead(d, meta)
}

func resourceArmEventHubDedicatedRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Eventhub.EventHubsClient
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
namespaceName := id.Path["namespaces"]
name := id.Path["eventhubs"]
resp, err := client.Get(ctx, resourceGroup, namespaceName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure EventHub %q (resource group %q): %+v", name, resourceGroup, err)
}

d.Set("name", resp.Name)
d.Set("namespace_name", namespaceName)
d.Set("resource_group_name", resourceGroup)

if props := resp.Properties; props != nil {
d.Set("partition_count", props.PartitionCount)
d.Set("message_retention", props.MessageRetentionInDays)
d.Set("partition_ids", props.PartitionIds)

captureDescription := flattenEventHubCaptureDescription(props.CaptureDescription)
if err := d.Set("capture_description", captureDescription); err != nil {
return err
}
}

return nil
}

func resourceArmEventHubDedicatedDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Eventhub.EventHubsClient
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
namespaceName := id.Path["namespaces"]
name := id.Path["eventhubs"]
resp, err := client.Delete(ctx, resourceGroup, namespaceName, name)

if err != nil {
if utils.ResponseWasNotFound(resp) {
return nil
}

return fmt.Errorf("Error issuing delete request for EventHub %q (resource group %q): %+v", name, resourceGroup, err)
}

return nil
}

func ValidateEventHubDedicatedPartitionCount(v interface{}, _ string) (warnings []string, errors []error) {
value := v.(int)

if !(1024 >= value && value >= 1) {
errors = append(errors, fmt.Errorf("EventHub Partition Count has to be between 1 and 1024"))
}

return warnings, errors
}

func ValidateEventHubDedicatedMessageRetentionCount(v interface{}, _ string) (warnings []string, errors []error) {
value := v.(int)

if !(90 >= value && value >= 1) {
errors = append(errors, fmt.Errorf("EventHub Retention Count has to be between 1 and 90"))
}

return warnings, errors
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ func resourceArmEventHubNamespaceAuthorizationRuleDelete(d *schema.ResourceData,
return err
}

locks.ByName(id.NamespaceName, eventHubNamespaceDedicatedResourceName)
defer locks.UnlockByName(id.NamespaceName, eventHubNamespaceDedicatedResourceName)

locks.ByName(id.NamespaceName, eventHubNamespaceResourceName)
defer locks.UnlockByName(id.NamespaceName, eventHubNamespaceResourceName)

Expand Down
Loading