Skip to content

Commit e0d4c9f

Browse files
Added updation capability in google_data_loss_prevention_stored_info_type resource (#7601) (#14207)
* Added updation capability in data_loss_prevention_stored_info_type resource * Adjusted the extra whitespaces in the data_loss_prevention_stored_info_type_test file Signed-off-by: Modular Magician <[email protected]>
1 parent cf6bc6b commit e0d4c9f

3 files changed

+339
-4
lines changed

.changelog/7601.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
dlp: Changed `dictionary`, `regex`, `regex.group_indexes` and `large_custom_dictionary` fields in `google_data_loss_prevention_stored_info_type` to be update-in-place
3+
```

google/resource_data_loss_prevention_stored_info_type.go

+56-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package google
1616

1717
import (
18+
"context"
1819
"fmt"
1920
"log"
2021
"reflect"
@@ -24,6 +25,29 @@ import (
2425
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2526
)
2627

28+
func storedInfoTypeCustomizeDiffFunc(diff TerraformResourceDiff) error {
29+
oldDict, newDict := diff.GetChange("dictionary")
30+
oldRegex, newRegex := diff.GetChange("regex")
31+
oldLargeCD, newLargeCD := diff.GetChange("large_custom_dictionary")
32+
if !isEmptyValue(reflect.ValueOf(oldDict)) && isEmptyValue(reflect.ValueOf(newDict)) {
33+
diff.ForceNew("dictionary")
34+
return nil
35+
}
36+
if !isEmptyValue(reflect.ValueOf(oldRegex)) && isEmptyValue(reflect.ValueOf(newRegex)) {
37+
diff.ForceNew("regex")
38+
return nil
39+
}
40+
if !isEmptyValue(reflect.ValueOf(oldLargeCD)) && isEmptyValue(reflect.ValueOf(newLargeCD)) {
41+
diff.ForceNew("large_custom_dictionary")
42+
return nil
43+
}
44+
return nil
45+
}
46+
47+
func storedInfoTypeCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
48+
return storedInfoTypeCustomizeDiffFunc(diff)
49+
}
50+
2751
func ResourceDataLossPreventionStoredInfoType() *schema.Resource {
2852
return &schema.Resource{
2953
Create: resourceDataLossPreventionStoredInfoTypeCreate,
@@ -41,6 +65,8 @@ func ResourceDataLossPreventionStoredInfoType() *schema.Resource {
4165
Delete: schema.DefaultTimeout(20 * time.Minute),
4266
},
4367

68+
CustomizeDiff: storedInfoTypeCustomizeDiff,
69+
4470
Schema: map[string]*schema.Schema{
4571
"parent": {
4672
Type: schema.TypeString,
@@ -61,7 +87,6 @@ func ResourceDataLossPreventionStoredInfoType() *schema.Resource {
6187
"dictionary": {
6288
Type: schema.TypeList,
6389
Optional: true,
64-
ForceNew: true,
6590
Description: `Dictionary which defines the rule.`,
6691
MaxItems: 1,
6792
Elem: &schema.Resource{
@@ -114,7 +139,6 @@ phrase and every phrase must contain at least 2 characters that are letters or d
114139
"large_custom_dictionary": {
115140
Type: schema.TypeList,
116141
Optional: true,
117-
ForceNew: true,
118142
Description: `Dictionary which defines the rule.`,
119143
MaxItems: 1,
120144
Elem: &schema.Resource{
@@ -209,7 +233,6 @@ If any of these artifacts are modified, the dictionary is considered invalid and
209233
"regex": {
210234
Type: schema.TypeList,
211235
Optional: true,
212-
ForceNew: true,
213236
Description: `Regular expression which defines the rule.`,
214237
MaxItems: 1,
215238
Elem: &schema.Resource{
@@ -223,7 +246,6 @@ Its syntax (https://github.com/google/re2/wiki/Syntax) can be found under the go
223246
"group_indexes": {
224247
Type: schema.TypeList,
225248
Optional: true,
226-
ForceNew: true,
227249
Description: `The index of the submatch to extract as findings. When not specified, the entire match is returned. No more than 3 may be included.`,
228250
Elem: &schema.Schema{
229251
Type: schema.TypeInt,
@@ -442,6 +464,24 @@ func resourceDataLossPreventionStoredInfoTypeUpdate(d *schema.ResourceData, meta
442464
} else if v, ok := d.GetOkExists("display_name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, displayNameProp)) {
443465
obj["displayName"] = displayNameProp
444466
}
467+
regexProp, err := expandDataLossPreventionStoredInfoTypeRegex(d.Get("regex"), d, config)
468+
if err != nil {
469+
return err
470+
} else if v, ok := d.GetOkExists("regex"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, regexProp)) {
471+
obj["regex"] = regexProp
472+
}
473+
dictionaryProp, err := expandDataLossPreventionStoredInfoTypeDictionary(d.Get("dictionary"), d, config)
474+
if err != nil {
475+
return err
476+
} else if v, ok := d.GetOkExists("dictionary"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, dictionaryProp)) {
477+
obj["dictionary"] = dictionaryProp
478+
}
479+
largeCustomDictionaryProp, err := expandDataLossPreventionStoredInfoTypeLargeCustomDictionary(d.Get("large_custom_dictionary"), d, config)
480+
if err != nil {
481+
return err
482+
} else if v, ok := d.GetOkExists("large_custom_dictionary"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, largeCustomDictionaryProp)) {
483+
obj["largeCustomDictionary"] = largeCustomDictionaryProp
484+
}
445485

446486
obj, err = resourceDataLossPreventionStoredInfoTypeEncoder(d, meta, obj)
447487
if err != nil {
@@ -463,6 +503,18 @@ func resourceDataLossPreventionStoredInfoTypeUpdate(d *schema.ResourceData, meta
463503
if d.HasChange("display_name") {
464504
updateMask = append(updateMask, "displayName")
465505
}
506+
507+
if d.HasChange("regex") {
508+
updateMask = append(updateMask, "regex")
509+
}
510+
511+
if d.HasChange("dictionary") {
512+
updateMask = append(updateMask, "dictionary")
513+
}
514+
515+
if d.HasChange("large_custom_dictionary") {
516+
updateMask = append(updateMask, "largeCustomDictionary")
517+
}
466518
// updateMask is a URL parameter but not present in the schema, so replaceVars
467519
// won't set it
468520
url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})

0 commit comments

Comments
 (0)