Skip to content

Commit 1a7b6d3

Browse files
Adds support for Filestore instance new feature: deletion protection. (#11602) (#8158)
[upstream:e51cc4e47b9daecece39bde05a692bdeee52569b] Signed-off-by: Modular Magician <[email protected]>
1 parent bbb3e58 commit 1a7b6d3

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed

Diff for: .changelog/11602.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
filestore: added `deletion_protection_enabled` and `deletion_protection_reason` fields to `google_filestore_instance`
3+
```

Diff for: google-beta/services/filestore/resource_filestore_instance.go

+64
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ addresses reserved for this instance.`,
217217
Description: `The service tier of the instance.
218218
Possible values include: STANDARD, PREMIUM, BASIC_HDD, BASIC_SSD, HIGH_SCALE_SSD, ZONAL, REGIONAL and ENTERPRISE`,
219219
},
220+
"deletion_protection_enabled": {
221+
Type: schema.TypeBool,
222+
Optional: true,
223+
Description: `Indicates whether the instance is protected against deletion.`,
224+
},
225+
"deletion_protection_reason": {
226+
Type: schema.TypeString,
227+
Optional: true,
228+
Description: `The reason for enabling deletion protection.`,
229+
},
220230
"description": {
221231
Type: schema.TypeString,
222232
Optional: true,
@@ -345,6 +355,18 @@ func resourceFilestoreInstanceCreate(d *schema.ResourceData, meta interface{}) e
345355
} else if v, ok := d.GetOkExists("kms_key_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(kmsKeyNameProp)) && (ok || !reflect.DeepEqual(v, kmsKeyNameProp)) {
346356
obj["kmsKeyName"] = kmsKeyNameProp
347357
}
358+
deletionProtectionEnabledProp, err := expandFilestoreInstanceDeletionProtectionEnabled(d.Get("deletion_protection_enabled"), d, config)
359+
if err != nil {
360+
return err
361+
} else if v, ok := d.GetOkExists("deletion_protection_enabled"); !tpgresource.IsEmptyValue(reflect.ValueOf(deletionProtectionEnabledProp)) && (ok || !reflect.DeepEqual(v, deletionProtectionEnabledProp)) {
362+
obj["deletionProtectionEnabled"] = deletionProtectionEnabledProp
363+
}
364+
deletionProtectionReasonProp, err := expandFilestoreInstanceDeletionProtectionReason(d.Get("deletion_protection_reason"), d, config)
365+
if err != nil {
366+
return err
367+
} else if v, ok := d.GetOkExists("deletion_protection_reason"); !tpgresource.IsEmptyValue(reflect.ValueOf(deletionProtectionReasonProp)) && (ok || !reflect.DeepEqual(v, deletionProtectionReasonProp)) {
368+
obj["deletionProtectionReason"] = deletionProtectionReasonProp
369+
}
348370
labelsProp, err := expandFilestoreInstanceEffectiveLabels(d.Get("effective_labels"), d, config)
349371
if err != nil {
350372
return err
@@ -506,6 +528,12 @@ func resourceFilestoreInstanceRead(d *schema.ResourceData, meta interface{}) err
506528
if err := d.Set("kms_key_name", flattenFilestoreInstanceKmsKeyName(res["kmsKeyName"], d, config)); err != nil {
507529
return fmt.Errorf("Error reading Instance: %s", err)
508530
}
531+
if err := d.Set("deletion_protection_enabled", flattenFilestoreInstanceDeletionProtectionEnabled(res["deletionProtectionEnabled"], d, config)); err != nil {
532+
return fmt.Errorf("Error reading Instance: %s", err)
533+
}
534+
if err := d.Set("deletion_protection_reason", flattenFilestoreInstanceDeletionProtectionReason(res["deletionProtectionReason"], d, config)); err != nil {
535+
return fmt.Errorf("Error reading Instance: %s", err)
536+
}
509537
if err := d.Set("terraform_labels", flattenFilestoreInstanceTerraformLabels(res["labels"], d, config)); err != nil {
510538
return fmt.Errorf("Error reading Instance: %s", err)
511539
}
@@ -544,6 +572,18 @@ func resourceFilestoreInstanceUpdate(d *schema.ResourceData, meta interface{}) e
544572
} else if v, ok := d.GetOkExists("file_shares"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, fileSharesProp)) {
545573
obj["fileShares"] = fileSharesProp
546574
}
575+
deletionProtectionEnabledProp, err := expandFilestoreInstanceDeletionProtectionEnabled(d.Get("deletion_protection_enabled"), d, config)
576+
if err != nil {
577+
return err
578+
} else if v, ok := d.GetOkExists("deletion_protection_enabled"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, deletionProtectionEnabledProp)) {
579+
obj["deletionProtectionEnabled"] = deletionProtectionEnabledProp
580+
}
581+
deletionProtectionReasonProp, err := expandFilestoreInstanceDeletionProtectionReason(d.Get("deletion_protection_reason"), d, config)
582+
if err != nil {
583+
return err
584+
} else if v, ok := d.GetOkExists("deletion_protection_reason"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, deletionProtectionReasonProp)) {
585+
obj["deletionProtectionReason"] = deletionProtectionReasonProp
586+
}
547587
labelsProp, err := expandFilestoreInstanceEffectiveLabels(d.Get("effective_labels"), d, config)
548588
if err != nil {
549589
return err
@@ -568,6 +608,14 @@ func resourceFilestoreInstanceUpdate(d *schema.ResourceData, meta interface{}) e
568608
updateMask = append(updateMask, "fileShares")
569609
}
570610

611+
if d.HasChange("deletion_protection_enabled") {
612+
updateMask = append(updateMask, "deletionProtectionEnabled")
613+
}
614+
615+
if d.HasChange("deletion_protection_reason") {
616+
updateMask = append(updateMask, "deletionProtectionReason")
617+
}
618+
571619
if d.HasChange("effective_labels") {
572620
updateMask = append(updateMask, "labels")
573621
}
@@ -895,6 +943,14 @@ func flattenFilestoreInstanceKmsKeyName(v interface{}, d *schema.ResourceData, c
895943
return v
896944
}
897945

946+
func flattenFilestoreInstanceDeletionProtectionEnabled(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
947+
return v
948+
}
949+
950+
func flattenFilestoreInstanceDeletionProtectionReason(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
951+
return v
952+
}
953+
898954
func flattenFilestoreInstanceTerraformLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
899955
if v == nil {
900956
return v
@@ -1125,6 +1181,14 @@ func expandFilestoreInstanceKmsKeyName(v interface{}, d tpgresource.TerraformRes
11251181
return v, nil
11261182
}
11271183

1184+
func expandFilestoreInstanceDeletionProtectionEnabled(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1185+
return v, nil
1186+
}
1187+
1188+
func expandFilestoreInstanceDeletionProtectionReason(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1189+
return v, nil
1190+
}
1191+
11281192
func expandFilestoreInstanceEffectiveLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
11291193
if v == nil {
11301194
return map[string]string{}, nil

Diff for: google-beta/services/filestore/resource_filestore_instance_test.go

+95
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,98 @@ resource "google_filestore_instance" "instance" {
192192
}
193193
`, name)
194194
}
195+
196+
func TestAccFilestoreInstance_deletionProtection_update(t *testing.T) {
197+
t.Parallel()
198+
199+
name := fmt.Sprintf("tf-test-%d", acctest.RandInt(t))
200+
location := "us-central1-a"
201+
tier := "ZONAL"
202+
203+
deletionProtection := true
204+
205+
acctest.VcrTest(t, resource.TestCase{
206+
PreCheck: func() { acctest.AccTestPreCheck(t) },
207+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
208+
CheckDestroy: testAccCheckFilestoreInstanceDestroyProducer(t),
209+
Steps: []resource.TestStep{
210+
{
211+
Config: testAccFilestoreInstance_deletionProtection_create(name, location, tier),
212+
},
213+
{
214+
ResourceName: "google_filestore_instance.instance",
215+
ImportState: true,
216+
ImportStateVerify: true,
217+
ImportStateVerifyIgnore: []string{"zone"},
218+
},
219+
{
220+
Config: testAccFilestoreInstance_deletionProtection_update(name, location, tier, deletionProtection),
221+
},
222+
{
223+
ResourceName: "google_filestore_instance.instance",
224+
ImportState: true,
225+
ImportStateVerify: true,
226+
ImportStateVerifyIgnore: []string{"zone"},
227+
},
228+
{
229+
Config: testAccFilestoreInstance_deletionProtection_update(name, location, tier, !deletionProtection),
230+
},
231+
{
232+
ResourceName: "google_filestore_instance.instance",
233+
ImportState: true,
234+
ImportStateVerify: true,
235+
ImportStateVerifyIgnore: []string{"zone"},
236+
},
237+
},
238+
})
239+
}
240+
241+
func testAccFilestoreInstance_deletionProtection_create(name, location, tier string) string {
242+
return fmt.Sprintf(`
243+
resource "google_filestore_instance" "instance" {
244+
name = "%s"
245+
zone = "%s"
246+
tier = "%s"
247+
description = "An instance created during testing."
248+
249+
file_shares {
250+
capacity_gb = 1024
251+
name = "share"
252+
}
253+
254+
networks {
255+
network = "default"
256+
modes = ["MODE_IPV4"]
257+
}
258+
}
259+
`, name, location, tier)
260+
}
261+
262+
func testAccFilestoreInstance_deletionProtection_update(name, location, tier string, deletionProtection bool) string {
263+
deletionProtectionReason := ""
264+
if deletionProtection {
265+
deletionProtectionReason = "A reason for deletion protection"
266+
}
267+
268+
return fmt.Sprintf(`
269+
resource "google_filestore_instance" "instance" {
270+
name = "%s"
271+
zone = "%s"
272+
tier = "%s"
273+
description = "An instance created during testing."
274+
275+
file_shares {
276+
capacity_gb = 1024
277+
name = "share"
278+
}
279+
280+
networks {
281+
network = "default"
282+
modes = ["MODE_IPV4"]
283+
}
284+
285+
deletion_protection_enabled = %t
286+
deletion_protection_reason = "%s"
287+
}
288+
`, name, location, tier, deletionProtection, deletionProtectionReason)
289+
}

Diff for: website/docs/r/filestore_instance.html.markdown

+8
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,14 @@ The following arguments are supported:
297297
(Optional)
298298
KMS key name used for data encryption.
299299

300+
* `deletion_protection_enabled` -
301+
(Optional)
302+
Indicates whether the instance is protected against deletion.
303+
304+
* `deletion_protection_reason` -
305+
(Optional)
306+
The reason for enabling deletion protection.
307+
300308
* `zone` -
301309
(Optional, Deprecated)
302310
The name of the Filestore zone of the instance.

0 commit comments

Comments
 (0)