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

Fix Firestore field deletion for wildcard fields #9140

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/12856.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
firestore: fixed deletion of wildcard fields in `google_firestore_field`
```
12 changes: 10 additions & 2 deletions google-beta/services/firestore/resource_firestore_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,16 @@ func resourceFirestoreFieldDelete(d *schema.ResourceData, meta interface{}) erro
return err
}

updateMask := []string{"indexConfig", "ttlConfig"}

// For wildcard fields, do not add ttlConfig to the update mask (unsupported)
updateMask := []string{"indexConfig"}
re := regexp.MustCompile("^projects/([^/]+)/databases/([^/]+)/collectionGroups/([^/]+)/fields/(.+)$")
match := re.FindStringSubmatch(d.Get("name").(string))
if len(match) < 5 {
return fmt.Errorf("Error parsing field name for App")
}
if fieldName := match[4]; fieldName != "*" {
updateMask = append(updateMask, "ttlConfig")
}
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,64 @@ resource "google_firestore_field" "match_override" {
`, context)
}

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

context := map[string]interface{}{
"project_id": envvar.GetTestProjectFromEnv(),
"delete_protection_state": "DELETE_PROTECTION_DISABLED",
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckFirestoreFieldDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccFirestoreField_firestoreFieldWildcardExample(context),
},
{
ResourceName: "google_firestore_field.wildcard",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"collection", "database", "field"},
},
},
})
}

func testAccFirestoreField_firestoreFieldWildcardExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_firestore_database" "database" {
project = "%{project_id}"
name = "tf-test-database-id%{random_suffix}"
location_id = "nam5"
type = "FIRESTORE_NATIVE"

delete_protection_state = "%{delete_protection_state}"
deletion_policy = "DELETE"
}

resource "google_firestore_field" "wildcard" {
project = "%{project_id}"
database = google_firestore_database.database.name
collection = "chatrooms_%{random_suffix}"
field = "*"

index_config {
indexes {
order = "ASCENDING"
query_scope = "COLLECTION_GROUP"
}
indexes {
array_config = "CONTAINS"
}
}
}
`, context)
}

func testAccCheckFirestoreFieldDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
31 changes: 31 additions & 0 deletions website/docs/r/firestore_field.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,37 @@ resource "google_firestore_field" "match_override" {
}
}
```
## Example Usage - Firestore Field Wildcard


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

delete_protection_state = "DELETE_PROTECTION_ENABLED"
deletion_policy = "DELETE"
}

resource "google_firestore_field" "wildcard" {
project = "my-project-name"
database = google_firestore_database.database.name
collection = "chatrooms_%{random_suffix}"
field = "*"

index_config {
indexes {
order = "ASCENDING"
query_scope = "COLLECTION_GROUP"
}
indexes {
array_config = "CONTAINS"
}
}
}
```

## Argument Reference

Expand Down
Loading