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

Add Firestore deletion protection #15878

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
3 changes: 3 additions & 0 deletions .changelog/8906.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
firestore: added `delete_protection_state` field to `google_firestore_database` resource.
```
34 changes: 34 additions & 0 deletions google/services/firestore/resource_firestore_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ for information about how to choose. Possible values: ["FIRESTORE_NATIVE", "DATA
ValidateFunc: verify.ValidateEnum([]string{"OPTIMISTIC", "PESSIMISTIC", "OPTIMISTIC_WITH_ENTITY_GROUPS", ""}),
Description: `The concurrency control mode to use for this database. Possible values: ["OPTIMISTIC", "PESSIMISTIC", "OPTIMISTIC_WITH_ENTITY_GROUPS"]`,
},
"delete_protection_state": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ValidateFunc: verify.ValidateEnum([]string{"DELETE_PROTECTION_STATE_UNSPECIFIED", "DELETE_PROTECTION_ENABLED", "DELETE_PROTECTION_DISABLED", ""}),
Description: `State of delete protection for the database. Possible values: ["DELETE_PROTECTION_STATE_UNSPECIFIED", "DELETE_PROTECTION_ENABLED", "DELETE_PROTECTION_DISABLED"]`,
},
"point_in_time_recovery_enablement": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -200,6 +207,12 @@ func resourceFirestoreDatabaseCreate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("point_in_time_recovery_enablement"); !tpgresource.IsEmptyValue(reflect.ValueOf(pointInTimeRecoveryEnablementProp)) && (ok || !reflect.DeepEqual(v, pointInTimeRecoveryEnablementProp)) {
obj["pointInTimeRecoveryEnablement"] = pointInTimeRecoveryEnablementProp
}
deleteProtectionStateProp, err := expandFirestoreDatabaseDeleteProtectionState(d.Get("delete_protection_state"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("delete_protection_state"); !tpgresource.IsEmptyValue(reflect.ValueOf(deleteProtectionStateProp)) && (ok || !reflect.DeepEqual(v, deleteProtectionStateProp)) {
obj["deleteProtectionState"] = deleteProtectionStateProp
}
etagProp, err := expandFirestoreDatabaseEtag(d.Get("etag"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -336,6 +349,9 @@ func resourceFirestoreDatabaseRead(d *schema.ResourceData, meta interface{}) err
if err := d.Set("key_prefix", flattenFirestoreDatabaseKeyPrefix(res["key_prefix"], d, config)); err != nil {
return fmt.Errorf("Error reading Database: %s", err)
}
if err := d.Set("delete_protection_state", flattenFirestoreDatabaseDeleteProtectionState(res["deleteProtectionState"], d, config)); err != nil {
return fmt.Errorf("Error reading Database: %s", err)
}
if err := d.Set("etag", flattenFirestoreDatabaseEtag(res["etag"], d, config)); err != nil {
return fmt.Errorf("Error reading Database: %s", err)
}
Expand Down Expand Up @@ -398,6 +414,12 @@ func resourceFirestoreDatabaseUpdate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("point_in_time_recovery_enablement"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, pointInTimeRecoveryEnablementProp)) {
obj["pointInTimeRecoveryEnablement"] = pointInTimeRecoveryEnablementProp
}
deleteProtectionStateProp, err := expandFirestoreDatabaseDeleteProtectionState(d.Get("delete_protection_state"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("delete_protection_state"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, deleteProtectionStateProp)) {
obj["deleteProtectionState"] = deleteProtectionStateProp
}
etagProp, err := expandFirestoreDatabaseEtag(d.Get("etag"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -429,6 +451,10 @@ func resourceFirestoreDatabaseUpdate(d *schema.ResourceData, meta interface{}) e
updateMask = append(updateMask, "pointInTimeRecoveryEnablement")
}

if d.HasChange("delete_protection_state") {
updateMask = append(updateMask, "deleteProtectionState")
}

if d.HasChange("etag") {
updateMask = append(updateMask, "etag")
}
Expand Down Expand Up @@ -531,6 +557,10 @@ func flattenFirestoreDatabaseKeyPrefix(v interface{}, d *schema.ResourceData, co
return v
}

func flattenFirestoreDatabaseDeleteProtectionState(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenFirestoreDatabaseEtag(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
Expand Down Expand Up @@ -579,6 +609,10 @@ func expandFirestoreDatabasePointInTimeRecoveryEnablement(v interface{}, d tpgre
return v, nil
}

func expandFirestoreDatabaseDeleteProtectionState(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandFirestoreDatabaseEtag(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,42 @@ func TestAccFirestoreDatabase_updatePitrEnablement(t *testing.T) {
})
}

func TestAccFirestoreDatabase_updateDeleteProtectionState(t *testing.T) {
t.Parallel()

orgId := envvar.GetTestOrgFromEnv(t)
billingAccount := envvar.GetTestBillingAccountFromEnv(t)
randomSuffix := acctest.RandString(t, 10)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
ExternalProviders: map[string]resource.ExternalProvider{
"time": {},
},
Steps: []resource.TestStep{
{
Config: testAccFirestoreDatabase_deleteProtectionState(orgId, billingAccount, randomSuffix, "DELETE_PROTECTION_ENABLED"),
},
{
ResourceName: "google_firestore_database.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"etag", "project"},
},
{
Config: testAccFirestoreDatabase_deleteProtectionState(orgId, billingAccount, randomSuffix, "DELETE_PROTECTION_DISABLED"),
},
{
ResourceName: "google_firestore_database.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"etag", "project"},
},
},
})
}

func testAccFirestoreDatabase_basicDependencies(orgId, billingAccount string, randomSuffix string) string {
return fmt.Sprintf(`
resource "google_project" "default" {
Expand Down Expand Up @@ -139,3 +175,19 @@ resource "google_firestore_database" "default" {
}
`, pointInTimeRecoveryEnablement)
}

func testAccFirestoreDatabase_deleteProtectionState(orgId, billingAccount string, randomSuffix string, deleteProtectionState string) string {
return testAccFirestoreDatabase_basicDependencies(orgId, billingAccount, randomSuffix) + fmt.Sprintf(`

resource "google_firestore_database" "default" {
name = "(default)"
type = "DATASTORE_MODE"
location_id = "nam5"
delete_protection_state = "%s"

project = google_project.default.project_id

depends_on = [google_project_service.firestore]
}
`, deleteProtectionState)
}
23 changes: 23 additions & 0 deletions website/docs/r/firestore_database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ resource "google_firestore_database" "database" {
depends_on = [google_project_service.firestore]
}
```
## Example Usage - Firestore Database With Delete Protection


```hcl
resource "google_firestore_database" "database" {
project = google_project.project.project_id
name = "my-database"
location_id = "nam5"
type = "FIRESTORE_NATIVE"

# Prevents accidental deletion of the database.
# To delete the database, first set this field to `DELETE_PROTECTION_DISABLED`, apply the changes.
# Then delete the database resource and apply the changes again.
delete_protection_state = "DELETE_PROTECTION_ENABLED"

depends_on = [google_project_service.firestore]
}
```

## Argument Reference

Expand Down Expand Up @@ -221,6 +239,11 @@ The following arguments are supported:
Default value is `POINT_IN_TIME_RECOVERY_DISABLED`.
Possible values are: `POINT_IN_TIME_RECOVERY_ENABLED`, `POINT_IN_TIME_RECOVERY_DISABLED`.

* `delete_protection_state` -
(Optional)
State of delete protection for the database.
Possible values are: `DELETE_PROTECTION_STATE_UNSPECIFIED`, `DELETE_PROTECTION_ENABLED`, `DELETE_PROTECTION_DISABLED`.

* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the provider project is used.

Expand Down