Skip to content

Commit

Permalink
r/eventhub_namespace_authorization_rule: adding a second state migrat…
Browse files Browse the repository at this point in the history
…ion (#7622)

fixes #7310
  • Loading branch information
evertonmc authored Jul 9, 2020
1 parent 851899c commit 313c7e3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit 313c7e3

Please sign in to comment.