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 (#10190)

Co-authored-by: jackofallops <[email protected]>
  • Loading branch information
tombuildsstuff and jackofallops authored Jan 14, 2021
1 parent 09bd111 commit 7e3c0bf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ 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"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand All @@ -22,8 +24,14 @@ func resourceAdvancedThreatProtection() *schema.Resource {
Update: resourceAdvancedThreatProtectionCreateUpdate,
Delete: resourceAdvancedThreatProtectionDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.AdvancedThreatProtectionID(id)
return err
}),

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

Timeouts: &schema.ResourceTimeout{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 := fmt.Sprintf("/%s", strings.TrimPrefix(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 7e3c0bf

Please sign in to comment.