Skip to content

Commit

Permalink
Merge pull request #10104 from andrey-dubnik/dev-fix-eh-diag-validation
Browse files Browse the repository at this point in the history
r/monitor_diagnostic_setting: handling a difference in casing the EHNamespace Authorization Rule
  • Loading branch information
tombuildsstuff committed Jan 12, 2021
2 parents 06fa82d + ba0404c commit 7b3d15b
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 2 deletions.
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
1 change: 1 addition & 0 deletions azurerm/internal/tools/generator-resource-id/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ func (id ResourceIdGenerator) ValidatorCode() string {
import (
"fmt"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/%[2]s/parse"
)
Expand Down

0 comments on commit 7b3d15b

Please sign in to comment.