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

r/log_analytics_workspace: force-upgrading the resource id's #9853

Merged
merged 2 commits into from
Dec 16, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/loganalytics/migration"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/loganalytics/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/loganalytics/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
Expand All @@ -34,9 +35,11 @@ func resourceArmLogAnalyticsWorkspace() *schema.Resource {
return err
}),

SchemaVersion: 1,

MigrateState: WorkspaceMigrateState,
SchemaVersion: 2,
StateUpgraders: []schema.StateUpgrader{
migration.WorkspaceV0ToV1(),
migration.WorkspaceV1ToV2(),
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Expand Down
101 changes: 101 additions & 0 deletions azurerm/internal/services/loganalytics/migration/workspace_v0_to_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package migration

import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/loganalytics/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
)

func WorkspaceV0ToV1() schema.StateUpgrader {
return schema.StateUpgrader{
Version: 0,
Type: workspaceV0V1Schema().CoreConfigSchema().ImpliedType(),
Upgrade: workspaceUpgradeV0ToV1,
}
}

func workspaceV0V1Schema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"location": azure.SchemaLocation(),

"resource_group_name": azure.SchemaResourceGroupNameDiffSuppress(),

"internet_ingestion_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},

"internet_query_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},

"sku": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"retention_in_days": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},

"daily_quota_gb": {
Type: schema.TypeFloat,
Optional: true,
Default: -1.0,
},

"workspace_id": {
Type: schema.TypeString,
Computed: true,
},

"portal_url": {
Type: schema.TypeString,
Computed: true,
},

"primary_shared_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},

"secondary_shared_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},

"tags": tags.Schema(),
},
}
}

func workspaceUpgradeV0ToV1(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
subscriptionId := meta.(*clients.Client).Account.SubscriptionId

log.Printf("[DEBUG] Migrating IDs to correct casing for Log Analytics Workspace")
name := rawState["name"].(string)
resourceGroup := rawState["resource_group_name"].(string)
id := parse.NewLogAnalyticsWorkspaceID(subscriptionId, resourceGroup, name)

rawState["id"] = id.ID()
return rawState, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package migration

import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/loganalytics/parse"
)

func WorkspaceV1ToV2() schema.StateUpgrader {
// V1 to V2 is the same as v0 to v1 - to workaround a historical issue where `resource_group` was
// used in place of `resource_group_name` - ergo using the same schema is fine.
return schema.StateUpgrader{
Version: 1,
Type: workspaceV0V1Schema().CoreConfigSchema().ImpliedType(),
Upgrade: workspaceUpgradeV1ToV2,
}
}

func workspaceUpgradeV1ToV2(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
subscriptionId := meta.(*clients.Client).Account.SubscriptionId

log.Printf("[DEBUG] Migrating IDs to correct casing for Log Analytics Workspace")
name := rawState["name"].(string)
resourceGroup := rawState["resource_group_name"].(string)
id := parse.NewLogAnalyticsWorkspaceID(subscriptionId, resourceGroup, name)

rawState["id"] = id.ID()
return rawState, nil
}