Skip to content

Commit 26474df

Browse files
authored
chore: CPS-6447 update visionone_crm_profile 'exceptions' handling (#109) (#51)
1 parent 46d0410 commit 26474df

3 files changed

Lines changed: 24 additions & 25 deletions

File tree

internal/trendmicro/cloud_risk_management/utils/profile_converters_from_dto.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
2826
func 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 {

internal/trendmicro/cloud_risk_management/utils/profile_converters_to_dto.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,21 @@ func ConvertScanRulesToDTO(_ context.Context, rules []ScanRuleModel) ([]cloud_ri
9292
RiskLevel: rule.RiskLevel.ValueString(),
9393
}
9494

95-
// Convert exceptions - only create if there are actual values
96-
if rule.Exceptions != nil && (len(rule.Exceptions.FilterTags) > 0 || len(rule.Exceptions.ResourceIds) > 0) {
95+
// Convert exceptions - send to API if user specified them (even if empty)
96+
if rule.Exceptions != nil {
9797
result[i].Exceptions = &cloud_risk_management_dto.RuleExceptions{}
9898

99-
if len(rule.Exceptions.FilterTags) > 0 {
99+
// Send FilterTags if it's not nil (user specified it)
100+
if rule.Exceptions.FilterTags != nil {
100101
filterTags := make([]string, len(rule.Exceptions.FilterTags))
101102
for j, ft := range rule.Exceptions.FilterTags {
102103
filterTags[j] = ft.ValueString()
103104
}
104105
result[i].Exceptions.FilterTags = filterTags
105106
}
106107

107-
if len(rule.Exceptions.ResourceIds) > 0 {
108+
// Send ResourceIds if it's not nil (user specified it)
109+
if rule.Exceptions.ResourceIds != nil {
108110
resourceIds := make([]string, len(rule.Exceptions.ResourceIds))
109111
for j, rid := range rule.Exceptions.ResourceIds {
110112
resourceIds[j] = rid.ValueString()

pkg/dto/cloud_risk_management/profile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ type ScanRule struct {
2121

2222
// RuleExceptions represents exceptions for a scan rule
2323
type RuleExceptions struct {
24-
FilterTags []string `json:"tags,omitempty"`
25-
ResourceIds []string `json:"resourceIds,omitempty"`
24+
FilterTags []string `json:"tags"`
25+
ResourceIds []string `json:"resourceIds"`
2626
}
2727

2828
// RuleExtraSetting represents additional configuration for a scan rule

0 commit comments

Comments
 (0)