@@ -23,18 +23,10 @@ const (
2323)
2424
2525// UpdatePlanFromProfile updates the Terraform plan/state model with data from the API response.
26- // It preserves original plan values where the API doesn't return certain fields (like exceptions)
27- // and maintains consistency between the user's configuration and the actual state.
2826func UpdatePlanFromProfile (plan * ProfileResourceModel , profile * cloud_risk_management_dto.Profile ) {
29- // Create a map of original plan's scan rules by ID for reference
30- originalExceptions := make (map [string ]* RuleExceptionsModel )
3127 // Create a map of original extra_settings by rule ID -> setting name
3228 originalExtraSettings := make (map [string ]map [string ]* ExtraSettingModel )
3329 for _ , rule := range plan .ScanRules {
34- // Preserve exceptions to maintain user's config structure (even if empty)
35- if rule .Exceptions != nil {
36- originalExceptions [rule .ID .ValueString ()] = rule .Exceptions
37- }
3830 if len (rule .ExtraSettings ) > 0 {
3931 ruleID := rule .ID .ValueString ()
4032 originalExtraSettings [ruleID ] = make (map [string ]* ExtraSettingModel )
@@ -60,34 +52,39 @@ func UpdatePlanFromProfile(plan *ProfileResourceModel, profile *cloud_risk_manag
6052 RiskLevel : types .StringValue (rule .RiskLevel ),
6153 }
6254
63- // Convert exceptions: start with user's original (preserves nil vs empty), then override with API values
64- if originalExc := originalExceptions [rule .ID ]; originalExc != nil {
65- plan .ScanRules [i ].Exceptions = & RuleExceptionsModel {
66- FilterTags : originalExc .FilterTags ,
67- ResourceIds : originalExc .ResourceIds ,
68- }
69- }
70- if rule .Exceptions != nil {
71- if plan .ScanRules [i ].Exceptions == nil {
72- plan .ScanRules [i ].Exceptions = & RuleExceptionsModel {}
73- }
55+ // Convert exceptions directly from API response
56+ if rule .Exceptions != nil {
57+ plan .ScanRules [i ].Exceptions = & RuleExceptionsModel {}
7458
59+ // Only set FilterTags if the API returned it (even if empty)
60+ // nil means the field was not sent/returned, [] means it was sent as empty
61+ if rule .Exceptions .FilterTags != nil {
7562 if len (rule .Exceptions .FilterTags ) > 0 {
7663 filterTags := make ([]types.String , len (rule .Exceptions .FilterTags ))
7764 for j , ft := range rule .Exceptions .FilterTags {
7865 filterTags [j ] = types .StringValue (ft )
7966 }
8067 plan .ScanRules [i ].Exceptions .FilterTags = filterTags
68+ } else {
69+ // Empty array was explicitly sent/returned
70+ plan .ScanRules [i ].Exceptions .FilterTags = []types.String {}
8171 }
72+ }
8273
74+ // Only set ResourceIds if the API returned it (even if empty)
75+ if rule .Exceptions .ResourceIds != nil {
8376 if len (rule .Exceptions .ResourceIds ) > 0 {
8477 resourceIds := make ([]types.String , len (rule .Exceptions .ResourceIds ))
8578 for j , rid := range rule .Exceptions .ResourceIds {
8679 resourceIds [j ] = types .StringValue (rid )
8780 }
8881 plan .ScanRules [i ].Exceptions .ResourceIds = resourceIds
82+ } else {
83+ // Empty array was explicitly sent/returned
84+ plan .ScanRules [i ].Exceptions .ResourceIds = []types.String {}
8985 }
9086 }
87+ }
9188
9289 // Convert extra settings - always convert to match plan structure
9390 if len (rule .ExtraSettings ) > 0 {
0 commit comments