Skip to content

Commit

Permalink
add state migration for app insights component id (#25628)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephybun authored Apr 16, 2024
1 parent bc67011 commit d337f98
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ func resourceApplicationInsights() *pluginsdk.Resource {
return err
}),

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

Timeouts: &pluginsdk.ResourceTimeout{
Expand Down
129 changes: 129 additions & 0 deletions internal/services/applicationinsights/migration/component_v1_to_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package migration

import (
"context"
"log"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
components "github.com/hashicorp/go-azure-sdk/resource-manager/applicationinsights/2020-02-02/componentsapis"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

var _ pluginsdk.StateUpgrade = ComponentUpgradeV1ToV2{}

type ComponentUpgradeV1ToV2 struct{}

func (ComponentUpgradeV1ToV2) Schema() map[string]*pluginsdk.Schema {
return componentSchemaForV1AndV2()
}

func (ComponentUpgradeV1ToV2) 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 application insights
// resources with the incorrect casing could still be imported and exist within some user's state
oldIdRaw := rawState["id"].(string)
id, err := components.ParseComponentIDInsensitively(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 componentSchemaForV1AndV2() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
},

"resource_group_name": commonschema.ResourceGroupName(),

"location": commonschema.Location(),

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

"workspace_id": {
Type: pluginsdk.TypeString,
Optional: true,
},

"retention_in_days": {
Type: pluginsdk.TypeInt,
Optional: true,
},

"sampling_percentage": {
Type: pluginsdk.TypeFloat,
Optional: true,
},

"disable_ip_masking": {
Type: pluginsdk.TypeBool,
Optional: true,
},

"tags": tags.Schema(),

"daily_data_cap_in_gb": {
Type: pluginsdk.TypeFloat,
Optional: true,
Computed: true,
},

"daily_data_cap_notifications_disabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Computed: true,
},

"app_id": {
Type: pluginsdk.TypeString,
Computed: true,
},

"instrumentation_key": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},

"connection_string": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},

"local_authentication_disabled": {
Type: pluginsdk.TypeBool,
Optional: true,
},

"internet_ingestion_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
},

"internet_query_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
},
"force_customer_storage_for_profiler": {
Type: pluginsdk.TypeBool,
Optional: true,
},
}
}
Original file line number Diff line number Diff line change
@@ -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 TestComponentV1ToV2(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",
},
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/components/component1"),
},
{
name: "old id - mixed case",
input: map[string]interface{}{
"id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourcegroups/group1/providers/microsoft.insights/Components/component1",
},
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/components/component1"),
},
{
name: "new id",
input: map[string]interface{}{
"id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/components/component1",
},
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/components/component1"),
},
}
for _, test := range testData {
t.Logf("Testing %q...", test.name)
result, err := ComponentUpgradeV1ToV2{}.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)
}
}
}

0 comments on commit d337f98

Please sign in to comment.