-
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.
add state migration for app insights component id (#25628)
- Loading branch information
Showing
5 changed files
with
190 additions
and
1 deletion.
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
File renamed without changes.
File renamed without changes.
129 changes: 129 additions & 0 deletions
129
internal/services/applicationinsights/migration/component_v1_to_v2.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,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, | ||
}, | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
internal/services/applicationinsights/migration/component_v1_to_v2_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,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) | ||
} | ||
} | ||
} |