-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
r/advanced_threat_protection: adding a state migration to ensure the …
…ID has a `/` prefix (#10190) Co-authored-by: jackofallops <[email protected]>
- Loading branch information
1 parent
09bd111
commit 7e3c0bf
Showing
2 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
azurerm/internal/services/securitycenter/migration/advanced_threat_protection_v0_to_v1.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |