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

azurerm_application_insights_api_key - parse API Key ID insensitively #25567

Merged
merged 4 commits into from Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -30,9 +30,10 @@ func resourceApplicationInsightsAPIKey() *pluginsdk.Resource {
return err
}),

SchemaVersion: 1,
SchemaVersion: 2,
StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{
0: migration.ApiKeyUpgradeV0ToV1{},
1: migration.ApiKeyUpgradeV1ToV2{},
}),

Timeouts: &pluginsdk.ResourceTimeout{
Expand Down
@@ -0,0 +1,77 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package migration

import (
"context"
"log"

apikeys "github.com/hashicorp/go-azure-sdk/resource-manager/applicationinsights/2015-05-01/componentapikeysapis"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

var _ pluginsdk.StateUpgrade = ApiKeyUpgradeV1ToV2{}

type ApiKeyUpgradeV1ToV2 struct{}

func (ApiKeyUpgradeV1ToV2) Schema() map[string]*pluginsdk.Schema {
return apiKeySchemaForV1AndV2()
}

func (ApiKeyUpgradeV1ToV2) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
// This state migration is identical to v0 -> v1, however we need to apply it again because the resource
// previously only normalised the `apiKeys` segment instead of all the static segments, so IDs with incorrect
// casing were still present and being created in a user's state
oldIdRaw := rawState["id"].(string)
id, err := apikeys.ParseApiKeyIDInsensitively(oldIdRaw)
if err != nil {
return rawState, err
}

newId := id.ID()
log.Printf("[DEBUG] Updating ID from %q to %q", oldIdRaw, newId)
rawState["id"] = newId

return rawState, nil
}
}

func apiKeySchemaForV1AndV2() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
},

"application_insights_id": {
Type: pluginsdk.TypeString,
Required: true,
},

"read_permissions": {
Type: pluginsdk.TypeSet,
Optional: true,
Set: pluginsdk.HashString,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"write_permissions": {
Type: pluginsdk.TypeSet,
Optional: true,
Set: pluginsdk.HashString,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"api_key": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},
}
}
@@ -0,0 +1,59 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package migration

import (
"context"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func TestApiKeyV1ToV2(t *testing.T) {
testData := []struct {
name string
input map[string]interface{}
expected *string
}{
{
name: "old id",
input: map[string]interface{}{
"id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourcegroups/group1/providers/microsoft.insights/components/component1/apikeys/key1",
},
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/components/component1/apiKeys/key1"),
},
{
name: "old id - mixed case",
input: map[string]interface{}{
"id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourcegroups/group1/providers/microsoft.insights/components/component1/Apikeys/key1",
},
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/components/component1/apiKeys/key1"),
},
{
name: "new id",
input: map[string]interface{}{
"id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/components/component1/apiKeys/key1",
},
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/components/component1/apiKeys/key1"),
},
}
for _, test := range testData {
t.Logf("Testing %q...", test.name)
result, err := ApiKeyUpgradeV1ToV2{}.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)
}
}
}