Skip to content

Commit

Permalink
Merge pull request #9853 from terraform-providers/b/log-analytics-wor…
Browse files Browse the repository at this point in the history
…kspace-state-migration

r/log_analytics_workspace: force-upgrading the resource id's
  • Loading branch information
tombuildsstuff authored Dec 16, 2020
2 parents 0fcb0a8 + bbcf7ad commit 0174958
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 39 deletions.

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
}

0 comments on commit 0174958

Please sign in to comment.