Skip to content

Commit

Permalink
Merge pull request #6958 from jrauschenbusch/fix-eventgrid-subscripti…
Browse files Browse the repository at this point in the history
…on-id-parser

fix: eventgrid id parsers
  • Loading branch information
tombuildsstuff committed May 20, 2020
2 parents a636e11 + c69699f commit 41c524e
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 9 deletions.
53 changes: 53 additions & 0 deletions azurerm/internal/services/eventgrid/parse/eventgrid_domain_test.go
@@ -0,0 +1,53 @@
package parse

import (
"testing"
)

func TestEventGridDomainId(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *EventGridDomainId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Domain",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.EventGrid/topics/topic1",
Expected: nil,
},
{
Name: "EventGrid Domain ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.EventGrid/domains/domain1",
Expected: &EventGridDomainId{
Name: "domain1",
ResourceGroup: "resGroup1",
},
},
}

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

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

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

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

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}
@@ -0,0 +1,53 @@
package parse

import (
"testing"
)

func TestEventGridDomainTopicId(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *EventGridDomainTopicId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "Missing Domain",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/providers/Microsoft.EventGrid/topics/topic1",
Expected: nil,
},
{
Name: "Domain Topic ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.EventGrid/domains/domain1/topics/topic1",
Expected: &EventGridDomainTopicId{
Name: "topic1",
ResourceGroup: "resGroup1",
},
},
}

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

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

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

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

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}
Expand Up @@ -13,7 +13,7 @@ type EventGridEventSubscriptionId struct {
}

func EventGridEventSubscriptionID(input string) (*EventGridEventSubscriptionId, error) {
id, err := azure.ParseAzureResourceID(input)
_, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse EventGrid Event Subscription ID %q: %+v", input, err)
}
Expand All @@ -25,14 +25,7 @@ func EventGridEventSubscriptionID(input string) (*EventGridEventSubscriptionId,

eventSubscription := EventGridEventSubscriptionId{
Scope: segments[0],
}

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

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
Name: segments[1],
}

return &eventSubscription, nil
Expand Down
@@ -0,0 +1,85 @@
package parse

import (
"testing"
)

func TestEventGridEventSubscriptionId(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *EventGridEventSubscriptionId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/providers/Microsoft.EventGrid/eventSubscriptions/subscription1",
Expected: nil,
},
{
Name: "Subscription Scope",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.EventGrid/eventSubscriptions/subscription1",
Expected: &EventGridEventSubscriptionId{
Name: "subscription1",
Scope: "/subscriptions/00000000-0000-0000-0000-000000000000",
},
},
{
Name: "Resource Group",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.EventGrid/eventSubscriptions/subscription1",
Expected: &EventGridEventSubscriptionId{
Name: "subscription1",
Scope: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1",
},
},
{
Name: "Storage Account Scope",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storage1/providers/Microsoft.EventGrid/eventSubscriptions/subscription1",
Expected: &EventGridEventSubscriptionId{
Name: "subscription1",
Scope: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storage1",
},
},
{
Name: "Event Grid Domain Scope",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.EventGrid/domains/domain1/providers/Microsoft.EventGrid/eventSubscriptions/subscription1",
Expected: &EventGridEventSubscriptionId{
Name: "subscription1",
Scope: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.EventGrid/domains/domain1",
},
},
{
Name: "Event Grid Topic Scope",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.EventGrid/topics/topic1/providers/Microsoft.EventGrid/eventSubscriptions/subscription1",
Expected: &EventGridEventSubscriptionId{
Name: "subscription1",
Scope: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.EventGrid/topics/topic1",
},
},
}

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

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

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

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

if actual.Scope != v.Expected.Scope {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.Scope, actual.Scope)
}
}
}
53 changes: 53 additions & 0 deletions azurerm/internal/services/eventgrid/parse/eventgrid_topic_test.go
@@ -0,0 +1,53 @@
package parse

import (
"testing"
)

func TestEventGridTopicId(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *EventGridTopicId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Topic",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/providers/Microsoft.EventGrid/domains/domain1",
Expected: nil,
},
{
Name: "EventGrid Topic ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.EventGrid/topics/topic1",
Expected: &EventGridTopicId{
Name: "topic1",
ResourceGroup: "resGroup1",
},
},
}

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

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

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

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

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

0 comments on commit 41c524e

Please sign in to comment.