From 0f0894f9fda408b2ed4068a50e8705113a75364b Mon Sep 17 00:00:00 2001 From: Everton Macedo Date: Wed, 8 Jul 2020 17:24:40 +0200 Subject: [PATCH 1/2] #7310 state v2 to lowercase authorizationRules --- ...b_namespace_authorization_rule_resource.go | 7 +++- .../namespace_authorization_rule_v1.go | 41 +++++++++++++++++++ .../parse/namespace_authorization_rule.go | 2 +- .../namespace_authorization_rule_test.go | 6 +-- ...namespace_authorization_rule.html.markdown | 2 +- 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 azurerm/internal/services/eventhub/migration/namespace_authorization_rule_v1.go diff --git a/azurerm/internal/services/eventhub/eventhub_namespace_authorization_rule_resource.go b/azurerm/internal/services/eventhub/eventhub_namespace_authorization_rule_resource.go index 03b4d22a16ad..2a26651d4c7a 100644 --- a/azurerm/internal/services/eventhub/eventhub_namespace_authorization_rule_resource.go +++ b/azurerm/internal/services/eventhub/eventhub_namespace_authorization_rule_resource.go @@ -29,13 +29,18 @@ func resourceArmEventHubNamespaceAuthorizationRule() *schema.Resource { State: schema.ImportStatePassthrough, }, - SchemaVersion: 1, + SchemaVersion: 2, StateUpgraders: []schema.StateUpgrader{ { Type: migration.EventHubNamespaceAuthorizationRuleUpgradeV0Schema().CoreConfigSchema().ImpliedType(), Upgrade: migration.EventHubNamespaceAuthorizationRuleUpgradeV0ToV1, Version: 0, }, + { + Type: migration.EventHubNamespaceAuthorizationRuleUpgradeV1Schema().CoreConfigSchema().ImpliedType(), + Upgrade: migration.EventHubNamespaceAuthorizationRuleUpgradeV1ToV2, + Version: 1, + }, }, Timeouts: &schema.ResourceTimeout{ diff --git a/azurerm/internal/services/eventhub/migration/namespace_authorization_rule_v1.go b/azurerm/internal/services/eventhub/migration/namespace_authorization_rule_v1.go new file mode 100644 index 000000000000..605cebb0d03a --- /dev/null +++ b/azurerm/internal/services/eventhub/migration/namespace_authorization_rule_v1.go @@ -0,0 +1,41 @@ +package migration + +import ( + "log" + "strings" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +func EventHubNamespaceAuthorizationRuleUpgradeV1Schema() *schema.Resource { + return &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "namespace_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "resource_group_name": azure.SchemaResourceGroupName(), + }, + } +} + +func EventHubNamespaceAuthorizationRuleUpgradeV1ToV2(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { + oldId := rawState["id"].(string) + + newId := strings.Replace(rawState["id"].(string), "/AuthorizationRules/", "/authorizationRules/", 1) + + log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId) + + rawState["id"] = newId + + return rawState, nil +} diff --git a/azurerm/internal/services/eventhub/parse/namespace_authorization_rule.go b/azurerm/internal/services/eventhub/parse/namespace_authorization_rule.go index fe5c0442f964..c3453785a204 100644 --- a/azurerm/internal/services/eventhub/parse/namespace_authorization_rule.go +++ b/azurerm/internal/services/eventhub/parse/namespace_authorization_rule.go @@ -22,7 +22,7 @@ func NamespaceAuthorizationRuleID(input string) (*NamespaceAuthorizationRuleId, return nil, err } - if rule.Name, err = id.PopSegment("AuthorizationRules"); err != nil { + if rule.Name, err = id.PopSegment("authorizationRules"); err != nil { return nil, err } diff --git a/azurerm/internal/services/eventhub/parse/namespace_authorization_rule_test.go b/azurerm/internal/services/eventhub/parse/namespace_authorization_rule_test.go index 3723d78076fe..8525003c52f0 100644 --- a/azurerm/internal/services/eventhub/parse/namespace_authorization_rule_test.go +++ b/azurerm/internal/services/eventhub/parse/namespace_authorization_rule_test.go @@ -42,13 +42,13 @@ func TestNamespaceAuthorizationRuleID(t *testing.T) { Error: true, }, { - Name: "Missing AuthorizationRules Key", - Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/AuthorizationRules", + Name: "Missing authorizationRules Key", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/authorizationRules", Error: true, }, { Name: "Namespace Authorization Rule ID", - Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/AuthorizationRules/rule1", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/authorizationRules/rule1", Error: false, Expect: &NamespaceAuthorizationRuleId{ ResourceGroup: "group1", diff --git a/website/docs/r/eventhub_namespace_authorization_rule.html.markdown b/website/docs/r/eventhub_namespace_authorization_rule.html.markdown index ddc5ae515195..77e40c70b550 100644 --- a/website/docs/r/eventhub_namespace_authorization_rule.html.markdown +++ b/website/docs/r/eventhub_namespace_authorization_rule.html.markdown @@ -93,5 +93,5 @@ The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/d EventHub Namespace Authorization Rules can be imported using the `resource id`, e.g. ```shell -$ terraform import azurerm_eventhub_namespace_authorization_rule.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/AuthorizationRules/rule1 +$ terraform import azurerm_eventhub_namespace_authorization_rule.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/authorizationRules/rule1 ``` From 3636af7b1135f8ac1d2b7e29f5888adf448b5aba Mon Sep 17 00:00:00 2001 From: Everton Macedo Date: Wed, 8 Jul 2020 18:16:38 +0200 Subject: [PATCH 2/2] negative test --- .../eventhub/parse/namespace_authorization_rule_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/eventhub/parse/namespace_authorization_rule_test.go b/azurerm/internal/services/eventhub/parse/namespace_authorization_rule_test.go index 8525003c52f0..eae18be19847 100644 --- a/azurerm/internal/services/eventhub/parse/namespace_authorization_rule_test.go +++ b/azurerm/internal/services/eventhub/parse/namespace_authorization_rule_test.go @@ -58,7 +58,7 @@ func TestNamespaceAuthorizationRuleID(t *testing.T) { }, { Name: "Wrong Casing", - Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/authorizationRules/rule1", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.EventHub/namespaces/namespace1/AuthorizationRules/rule1", Error: true, }, }