-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
azurerm_app_configuration_key
- id parse bugfix (#19722)
fix #19711
- Loading branch information
Showing
3 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
internal/services/appconfiguration/migration/key_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package migration | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appconfiguration/parse" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
) | ||
|
||
var _ pluginsdk.StateUpgrade = KeyResourceV0ToV1{} | ||
|
||
type KeyResourceV0ToV1 struct{} | ||
|
||
func (KeyResourceV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc { | ||
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { | ||
// old: | ||
// /subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/resourceGroup1/providers/Microsoft.AppConfiguration/configurationStores/appConf1/AppConfigurationKey/key%3Aname%2Ftest/Label/test%3Alabel%2Fname | ||
// new: | ||
// /subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/resourceGroup1/providers/Microsoft.AppConfiguration/configurationStores/appConf1/AppConfigurationKey/key:name/test/Label/test:label/name | ||
oldId := rawState["id"].(string) | ||
oldKeyNames := regexp.MustCompile(`AppConfigurationKey\/(.+)\/Label`).FindStringSubmatch(oldId) | ||
if len(oldKeyNames) == 2 { | ||
decodedName, err := url.QueryUnescape(oldKeyNames[1]) | ||
if err != nil { | ||
return rawState, err | ||
} | ||
oldId = strings.Replace(oldId, oldKeyNames[1], decodedName, 1) | ||
} | ||
oldLabelNames := regexp.MustCompile(`AppConfigurationKey\/.+\/Label\/(.+)`).FindStringSubmatch(oldId) | ||
if len(oldLabelNames) == 2 { | ||
decodedName, err := url.QueryUnescape(oldLabelNames[1]) | ||
if err != nil { | ||
return rawState, err | ||
} | ||
oldId = strings.Replace(oldId, oldLabelNames[1], decodedName, 1) | ||
} | ||
parsedNewId, err := parse.KeyId(oldId) | ||
if err != nil { | ||
return rawState, fmt.Errorf("parsing existing Key Resource %q: %+v", oldId, err) | ||
} | ||
rawState["id"] = parsedNewId.ID() | ||
|
||
return rawState, nil | ||
} | ||
} | ||
|
||
func (KeyResourceV0ToV1) Schema() map[string]*pluginsdk.Schema { | ||
return KeyResourceSchemaForV0AndV1() | ||
} | ||
|
||
func KeyResourceSchemaForV0AndV1() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"configuration_store_id": { | ||
Required: true, | ||
Type: pluginsdk.TypeString, | ||
}, | ||
"content_type": { | ||
Optional: true, | ||
Type: pluginsdk.TypeString, | ||
}, | ||
"etag": { | ||
Optional: true, | ||
Type: pluginsdk.TypeString, | ||
}, | ||
"key": { | ||
Required: true, | ||
Type: pluginsdk.TypeString, | ||
}, | ||
"label": { | ||
Optional: true, | ||
Type: pluginsdk.TypeString, | ||
}, | ||
"locked": { | ||
Optional: true, | ||
Type: pluginsdk.TypeBool, | ||
}, | ||
"tags": { | ||
Elem: &pluginsdk.Schema{Type: pluginsdk.TypeString}, | ||
Optional: true, | ||
Type: pluginsdk.TypeMap, | ||
}, | ||
"type": { | ||
Optional: true, | ||
Type: pluginsdk.TypeString, | ||
}, | ||
"value": { | ||
Optional: true, | ||
Type: pluginsdk.TypeString, | ||
}, | ||
"vault_key_reference": { | ||
Optional: true, | ||
Type: pluginsdk.TypeString, | ||
}, | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
internal/services/appconfiguration/migration/key_resource_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package migration | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-provider-azurerm/utils" | ||
) | ||
|
||
func TestKeyResourceV0ToV1(t *testing.T) { | ||
testData := []struct { | ||
name string | ||
input map[string]interface{} | ||
expected *string | ||
}{ | ||
{ | ||
name: "old id (normal)", | ||
input: map[string]interface{}{ | ||
"id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourcegroups/resourceGroup1/providers/Microsoft.AppConfiguration/configurationStores/appConf1/AppConfigurationKey/keyName/Label/labelName", | ||
}, | ||
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourcegroups/resourceGroup1/providers/Microsoft.AppConfiguration/configurationStores/appConf1/AppConfigurationKey/keyName/Label/labelName"), | ||
}, | ||
{ | ||
name: "old id (encoded)", | ||
input: map[string]interface{}{ | ||
"id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourcegroups/resourceGroup1/providers/Microsoft.AppConfiguration/configurationStores/appConf1/AppConfigurationKey/key%3Aname%2Ftest/Label/test%3Alabel%2Fname", | ||
}, | ||
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourcegroups/resourceGroup1/providers/Microsoft.AppConfiguration/configurationStores/appConf1/AppConfigurationKey/key:name/test/Label/test:label/name"), | ||
}, | ||
{ | ||
name: "new id", | ||
input: map[string]interface{}{ | ||
"id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourcegroups/resourceGroup1/providers/Microsoft.AppConfiguration/configurationStores/appConf1/AppConfigurationKey/key:name/test/Label/test:label/name", | ||
}, | ||
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourcegroups/resourceGroup1/providers/Microsoft.AppConfiguration/configurationStores/appConf1/AppConfigurationKey/key:name/test/Label/test:label/name"), | ||
}, | ||
} | ||
for _, test := range testData { | ||
t.Logf("Testing %q...", test.name) | ||
result, err := KeyResourceV0ToV1{}.UpgradeFunc()(context.TODO(), test.input, nil) | ||
if err != nil && test.expected == nil { | ||
continue | ||
} else { | ||
if err == nil && test.expected == nil { | ||
t.Fatalf("Expected an error but didn't get one") | ||
} else if err != nil && test.expected != nil { | ||
t.Fatalf("Expected no error but got: %+v", err) | ||
} | ||
} | ||
|
||
actualId := result["id"].(string) | ||
if *test.expected != actualId { | ||
t.Fatalf("expected %q but got %q!", *test.expected, actualId) | ||
} | ||
} | ||
} |