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

r/monitor_diagnostic_setting: handling a difference in casing the EHNamespace Authorization Rule #10104

Merged
merged 7 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,59 @@ func NamespaceAuthorizationRuleID(input string) (*NamespaceAuthorizationRuleId,

return &resourceId, nil
}

// NamespaceAuthorizationRuleIDInsensitively parses an NamespaceAuthorizationRule ID into an NamespaceAuthorizationRuleId struct, insensitively
// This should only be used to parse an ID for rewriting, the NamespaceAuthorizationRuleID
// method should be used instead for validation etc.
//
// Whilst this may seem strange, this enables Terraform have consistent casing
// which works around issues in Core, whilst handling broken API responses.
func NamespaceAuthorizationRuleIDInsensitively(input string) (*NamespaceAuthorizationRuleId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := NamespaceAuthorizationRuleId{
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")
}

// find the correct casing for the 'namespaces' segment
namespacesKey := "namespaces"
for key := range id.Path {
if strings.EqualFold(key, namespacesKey) {
namespacesKey = key
break
}
}
if resourceId.NamespaceName, err = id.PopSegment(namespacesKey); err != nil {
return nil, err
}

// find the correct casing for the 'authorizationRules' segment
authorizationRulesKey := "authorizationRules"
for key := range id.Path {
if strings.EqualFold(key, authorizationRulesKey) {
authorizationRulesKey = key
break
}
}
if resourceId.AuthorizationRuleName, err = id.PopSegment(authorizationRulesKey); err != nil {
return nil, err
}

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

return &resourceId, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,139 @@ func TestNamespaceAuthorizationRuleID(t *testing.T) {
}
}
}

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

{
// 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 AuthorizationRuleName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/",
Error: true,
},

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

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/authorizationRules/rule1",
Expected: &NamespaceAuthorizationRuleId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "group1",
NamespaceName: "namespace1",
AuthorizationRuleName: "rule1",
},
},

{
// lower-cased segment names
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/authorizationrules/rule1",
Expected: &NamespaceAuthorizationRuleId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "group1",
NamespaceName: "namespace1",
AuthorizationRuleName: "rule1",
},
},

{
// upper-cased segment names
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/NAMESPACES/namespace1/AUTHORIZATIONRULES/rule1",
Expected: &NamespaceAuthorizationRuleId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "group1",
NamespaceName: "namespace1",
AuthorizationRuleName: "rule1",
},
},

{
// mixed-cased segment names
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/NaMeSpAcEs/namespace1/AuThOrIzAtIoNrUlEs/rule1",
Expected: &NamespaceAuthorizationRuleId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "group1",
NamespaceName: "namespace1",
AuthorizationRuleName: "rule1",
},
},
}

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

actual, err := NamespaceAuthorizationRuleIDInsensitively(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.AuthorizationRuleName != v.Expected.AuthorizationRuleName {
t.Fatalf("Expected %q but got %q for AuthorizationRuleName", v.Expected.AuthorizationRuleName, actual.AuthorizationRuleName)
}
}
}
2 changes: 1 addition & 1 deletion azurerm/internal/services/eventhub/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ 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=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 -path=./ -name=NamespaceAuthorizationRule -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/authorizationRules/rule1
//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
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func resourceMonitorDiagnosticSettingRead(d *schema.ResourceData, meta interface
d.Set("eventhub_name", resp.EventHubName)
eventhubAuthorizationRuleId := ""
if resp.EventHubAuthorizationRuleID != nil && *resp.EventHubAuthorizationRuleID != "" {
parsedId, err := eventhubParse.NamespaceAuthorizationRuleID(*resp.EventHubAuthorizationRuleID)
parsedId, err := eventhubParse.NamespaceAuthorizationRuleIDInsensitively(*resp.EventHubAuthorizationRuleID)
if err != nil {
return err
}
Expand Down