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 resource "azurerm_kusto_eventgrid_data_connection" #10712

Merged
merged 4 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
65 changes: 27 additions & 38 deletions azurerm/internal/services/eventhub/eventhub_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"log"
"time"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/eventhub/validate"

"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/tf"
"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/services/eventhub/validate"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand All @@ -26,9 +27,11 @@ func resourceEventHub() *schema.Resource {
Read: resourceEventHubRead,
Update: resourceEventHubCreateUpdate,
Delete: resourceEventHubDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.EventHubID(id)
return err
}),

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Expand Down Expand Up @@ -152,24 +155,24 @@ func resourceEventHub() *schema.Resource {

func resourceEventHubCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Eventhub.EventHubsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
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)
id := parse.NewEventHubID(subscriptionId, d.Get("resource_group_name").(string), d.Get("namespace_name").(string), d.Get("name").(string))

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

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

Expand All @@ -187,20 +190,11 @@ func resourceEventHubCreateUpdate(d *schema.ResourceData, meta interface{}) erro
parameters.Properties.CaptureDescription = expandEventHubCaptureDescription(d)
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, namespaceName, name, parameters); err != nil {
if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.NamespaceName, id.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)
d.SetId(id.ID())

return resourceEventHubRead(d, meta)
}
Expand All @@ -210,26 +204,23 @@ func resourceEventHubRead(d *schema.ResourceData, meta interface{}) error {
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.EventHubID(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)
resp, err := client.Get(ctx, id.ResourceGroup, id.NamespaceName, id.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)
return fmt.Errorf("Error making Read request on %s: %+v", id, err)
}

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

if props := resp.Properties; props != nil {
d.Set("partition_count", props.PartitionCount)
Expand All @@ -249,21 +240,19 @@ func resourceEventHubDelete(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())

id, err := parse.EventHubID(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)
resp, err := client.Delete(ctx, id.ResourceGroup, id.NamespaceName, id.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 fmt.Errorf("deleting %s: %+v", id, err)
}

return nil
Expand Down
12 changes: 5 additions & 7 deletions azurerm/internal/services/eventhub/eventhub_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"strconv"
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/eventhub/parse"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/eventhub/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"

Expand Down Expand Up @@ -324,17 +325,14 @@ func TestAccEventHub_messageRetentionUpdate(t *testing.T) {
}

func (EventHubResource) Exists(ctx context.Context, clients *clients.Client, state *terraform.InstanceState) (*bool, error) {
id, err := azure.ParseAzureResourceID(state.ID)
id, err := parse.EventHubID(state.ID)
if err != nil {
return nil, err
}

namespaceName := id.Path["namespaces"]
name := id.Path["eventhubs"]

resp, err := clients.Eventhub.EventHubsClient.Get(ctx, id.ResourceGroup, namespaceName, name)
resp, err := clients.Eventhub.EventHubsClient.Get(ctx, id.ResourceGroup, id.NamespaceName, id.Name)
if err != nil {
return nil, fmt.Errorf("retrieving Event Hub %q (namespace %q / resource group: %q): %v", name, namespaceName, id.ResourceGroup, err)
return nil, fmt.Errorf("retrieving %s: %v", id, err)
}

return utils.Bool(resp.Properties != nil), nil
Expand Down
75 changes: 75 additions & 0 deletions azurerm/internal/services/eventhub/parse/event_hub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"
"strings"

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

type EventHubId struct {
SubscriptionId string
ResourceGroup string
NamespaceName string
Name string
}

func NewEventHubID(subscriptionId, resourceGroup, namespaceName, name string) EventHubId {
return EventHubId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
NamespaceName: namespaceName,
Name: name,
}
}

func (id EventHubId) String() string {
segments := []string{
fmt.Sprintf("Name %q", id.Name),
fmt.Sprintf("Namespace Name %q", id.NamespaceName),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Event Hub", segmentsStr)
}

func (id EventHubId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventHub/namespaces/%s/eventhubs/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.NamespaceName, id.Name)
}

// EventHubID parses a EventHub ID into an EventHubId struct
func EventHubID(input string) (*EventHubId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := EventHubId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}

if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

if resourceId.NamespaceName, err = id.PopSegment("namespaces"); err != nil {
return nil, err
}
if resourceId.Name, err = id.PopSegment("eventhubs"); err != nil {
return nil, err
}

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

return &resourceId, nil
}
128 changes: 128 additions & 0 deletions azurerm/internal/services/eventhub/parse/event_hub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = EventHubId{}

func TestEventHubIDFormatter(t *testing.T) {
actual := NewEventHubID("12345678-1234-9876-4563-123456789012", "group1", "namespace1", "eventhub1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/eventhubs/eventhub1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestEventHubID(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *EventHubId
}{

{
// empty
Input: "",
Error: true,
},

{
// missing SubscriptionId
Input: "/",
Error: true,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Error: true,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Error: true,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Error: true,
},

{
// missing NamespaceName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/",
Error: true,
},

{
// missing value for NamespaceName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/",
Error: true,
},

{
// missing Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/",
Error: true,
},

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/eventhubs/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/eventhubs/eventhub1",
Expected: &EventHubId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "group1",
NamespaceName: "namespace1",
Name: "eventhub1",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/GROUP1/PROVIDERS/MICROSOFT.EVENTHUB/NAMESPACES/NAMESPACE1/EVENTHUBS/EVENTHUB1",
Error: true,
},
}

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

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

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.SubscriptionId != v.Expected.SubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId)
}
if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup)
}
if actual.NamespaceName != v.Expected.NamespaceName {
t.Fatalf("Expected %q but got %q for NamespaceName", v.Expected.NamespaceName, actual.NamespaceName)
}
if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}
}
}
1 change: 1 addition & 0 deletions azurerm/internal/services/eventhub/resourceids.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eventhub

//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Cluster -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/clusters/cluster1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=EventHub -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/eventhubs/eventhub1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=EventHubConsumerGroup -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/eventhubs/eventhub1/consumergroups/consumergroup1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Namespace -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1
//go:generate go run ../../tools/generator-resource-id/main.go -rewrite=true -path=./ -name=NamespaceAuthorizationRule -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/authorizationRules/rule1
Loading