From 17af1c181bec3b3febbd1fc8a69a8f7378eb3046 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Thu, 14 Jan 2021 14:03:42 +0100 Subject: [PATCH] r/advanced_threat_protection: adding a state migration to ensure the ID has a `/` prefix Fixes #10179 --- .../advanced_threat_protection_resource.go | 6 +++ .../advanced_threat_protection_v0_to_v1.go | 54 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 azurerm/internal/services/securitycenter/migration/advanced_threat_protection_v0_to_v1.go diff --git a/azurerm/internal/services/securitycenter/advanced_threat_protection_resource.go b/azurerm/internal/services/securitycenter/advanced_threat_protection_resource.go index 802346897783..25cc42e77297 100644 --- a/azurerm/internal/services/securitycenter/advanced_threat_protection_resource.go +++ b/azurerm/internal/services/securitycenter/advanced_threat_protection_resource.go @@ -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" @@ -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), diff --git a/azurerm/internal/services/securitycenter/migration/advanced_threat_protection_v0_to_v1.go b/azurerm/internal/services/securitycenter/migration/advanced_threat_protection_v0_to_v1.go new file mode 100644 index 000000000000..3749de6d3960 --- /dev/null +++ b/azurerm/internal/services/securitycenter/migration/advanced_threat_protection_v0_to_v1.go @@ -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 +}