Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data.azurerm_sentinel_alert_rule_template and azurerm_sentinel_automation_rule - a state migration to work around the previously incorrect id casing #19487

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package migration

import (
"context"
"log"

"github.com/hashicorp/terraform-provider-azurerm/internal/services/sentinel/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type SentinelAlertRuleTemplateV0ToV1 struct{}

func (s SentinelAlertRuleTemplateV0ToV1) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
},

"display_name": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
},

"log_analytics_workspace_id": {
Type: pluginsdk.TypeString,
Required: true,
},

"scheduled_template": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"description": {
Type: pluginsdk.TypeString,
Computed: true,
},
"tactics": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"severity": {
Type: pluginsdk.TypeString,
Computed: true,
},
"query": {
Type: pluginsdk.TypeString,
Computed: true,
},
"query_frequency": {
Type: pluginsdk.TypeString,
Computed: true,
},
"query_period": {
Type: pluginsdk.TypeString,
Computed: true,
},
"trigger_operator": {
Type: pluginsdk.TypeString,
Computed: true,
},
"trigger_threshold": {
Type: pluginsdk.TypeInt,
Computed: true,
},
},
},
},

"security_incident_template": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"description": {
Type: pluginsdk.TypeString,
Computed: true,
},
"product_filter": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},

"nrt_template": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"description": {
Type: pluginsdk.TypeString,
Computed: true,
},
"tactics": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"severity": {
Type: pluginsdk.TypeString,
Computed: true,
},
"query": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},
}
}

func (s SentinelAlertRuleTemplateV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldId := rawState["id"].(string)
newId, err := parse.SentinelAlertRuleTemplateIDInsensitively(oldId)
if err != nil {
return nil, err
}

log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId)

rawState["id"] = newId.ID()
return rawState, nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package migration

import (
"context"
"log"

"github.com/hashicorp/terraform-provider-azurerm/internal/services/sentinel/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type SentinelAutomationRuleV0ToV1 struct{}

func (s SentinelAutomationRuleV0ToV1) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"log_analytics_workspace_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"display_name": {
Type: pluginsdk.TypeString,
Required: true,
},

"order": {
Type: pluginsdk.TypeInt,
Required: true,
},

"enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"expiration": {
Type: pluginsdk.TypeString,
Optional: true,
},

"condition": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"property": {
Type: pluginsdk.TypeString,
Required: true,
},

"operator": {
Type: pluginsdk.TypeString,
Required: true,
},

"values": {
Type: pluginsdk.TypeList,
Required: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},

"action_incident": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"order": {
Type: pluginsdk.TypeInt,
Required: true,
},

"status": {
Type: pluginsdk.TypeString,
Optional: true,
},

"classification": {
Type: pluginsdk.TypeString,
Optional: true,
},

"classification_comment": {
Type: pluginsdk.TypeString,
Optional: true,
},

"labels": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"owner_id": {
Type: pluginsdk.TypeString,
Optional: true,
},

"severity": {
Type: pluginsdk.TypeString,
Optional: true,
},
},
},
},

"action_playbook": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"order": {
Type: pluginsdk.TypeInt,
Required: true,
},

"logic_app_id": {
Type: pluginsdk.TypeString,
Required: true,
},

"tenant_id": {
Type: pluginsdk.TypeString,
// We'll use the current tenant id if this property is absent.
Optional: true,
Computed: true,
},
},
},
},
}
}

func (s SentinelAutomationRuleV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldId := rawState["id"].(string)
newId, err := parse.AutomationRuleIDInsensitively(oldId)
if err != nil {
return nil, err
}

log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId)

rawState["id"] = newId.ID()
return rawState, nil
}
}
60 changes: 58 additions & 2 deletions internal/services/sentinel/parse/automation_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (id AutomationRuleId) String() string {
}

func (id AutomationRuleId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.OperationalInsights/workspaces/%s/providers/Microsoft.SecurityInsights/AutomationRules/%s"
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.OperationalInsights/workspaces/%s/providers/Microsoft.SecurityInsights/automationRules/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.WorkspaceName, id.Name)
}

Expand All @@ -63,7 +63,63 @@ func AutomationRuleID(input string) (*AutomationRuleId, error) {
if resourceId.WorkspaceName, err = id.PopSegment("workspaces"); err != nil {
return nil, err
}
if resourceId.Name, err = id.PopSegment("AutomationRules"); err != nil {
if resourceId.Name, err = id.PopSegment("automationRules"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &resourceId, nil
}

// AutomationRuleIDInsensitively parses an AutomationRule ID into an AutomationRuleId struct, insensitively
// This should only be used to parse an ID for rewriting, the AutomationRuleID
// method should be used instead for validation etc.
//
// Whilst this may seem strange, this enables Terraform have consistent casing
// which works around issues in Core, whilst handling broken API responses.
func AutomationRuleIDInsensitively(input string) (*AutomationRuleId, error) {
id, err := resourceids.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := AutomationRuleId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}

if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

// find the correct casing for the 'workspaces' segment
workspacesKey := "workspaces"
for key := range id.Path {
if strings.EqualFold(key, workspacesKey) {
workspacesKey = key
break
}
}
if resourceId.WorkspaceName, err = id.PopSegment(workspacesKey); err != nil {
return nil, err
}

// find the correct casing for the 'automationRules' segment
automationRulesKey := "automationRules"
for key := range id.Path {
if strings.EqualFold(key, automationRulesKey) {
automationRulesKey = key
break
}
}
if resourceId.Name, err = id.PopSegment(automationRulesKey); err != nil {
return nil, err
}

Expand Down
Loading