Skip to content

Commit

Permalink
r/advanced_threat_protection: adding a state migration to ensure the …
Browse files Browse the repository at this point in the history
…ID has a `/` prefix

Fixes #10179
  • Loading branch information
tombuildsstuff committed Jan 14, 2021
1 parent 55826a2 commit 17af1c1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"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/securitycenter/migration"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/securitycenter/parse"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
Expand All @@ -28,6 +29,11 @@ func resourceAdvancedThreatProtection() *schema.Resource {
return err
}),

SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
migration.AdvancedThreatProtectionV0ToV1(),
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package migration

import (
"fmt"
"log"
"strings"

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

func AdvancedThreatProtectionV0ToV1() schema.StateUpgrader {
return schema.StateUpgrader{
Version: 0,
Type: advancedThreatProtectionV0Schema().CoreConfigSchema().ImpliedType(),
Upgrade: advancedThreadProtectionV0toV1Upgrade,
}
}

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

"enabled": {
Type: schema.TypeBool,
Required: true,
},
},
}
}

func advancedThreadProtectionV0toV1Upgrade(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldId := rawState["id"].(string)

// remove the existing `/` if it's present (2.42+) which'll do nothing if it wasn't (2.38)
newId := strings.TrimPrefix(oldId, "/")
newId = fmt.Sprintf("/%s", oldId)

parsedId, err := parse.AdvancedThreatProtectionID(newId)
if err != nil {
return nil, err
}

newId = parsedId.ID()

log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId)
rawState["id"] = newId
return rawState, nil
}

0 comments on commit 17af1c1

Please sign in to comment.