Skip to content

Commit 7f431f7

Browse files
Fix Firestore field deletion for wildcard fields (#12856) (#21034)
[upstream:7d08e89ac8686de13d9fdc30dc1f557925c3004d] Signed-off-by: Modular Magician <[email protected]>
1 parent cb9f05c commit 7f431f7

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

.changelog/12856.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
firestore: fixed deletion of wildcard fields in `google_firestore_field`
3+
```

google/services/firestore/resource_firestore_field.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,16 @@ func resourceFirestoreFieldDelete(d *schema.ResourceData, meta interface{}) erro
430430
return err
431431
}
432432

433-
updateMask := []string{"indexConfig", "ttlConfig"}
434-
433+
// For wildcard fields, do not add ttlConfig to the update mask (unsupported)
434+
updateMask := []string{"indexConfig"}
435+
re := regexp.MustCompile("^projects/([^/]+)/databases/([^/]+)/collectionGroups/([^/]+)/fields/(.+)$")
436+
match := re.FindStringSubmatch(d.Get("name").(string))
437+
if len(match) < 5 {
438+
return fmt.Errorf("Error parsing field name for App")
439+
}
440+
if fieldName := match[4]; fieldName != "*" {
441+
updateMask = append(updateMask, "ttlConfig")
442+
}
435443
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
436444
if err != nil {
437445
return err

google/services/firestore/resource_firestore_field_generated_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,64 @@ resource "google_firestore_field" "match_override" {
204204
`, context)
205205
}
206206

207+
func TestAccFirestoreField_firestoreFieldWildcardExample(t *testing.T) {
208+
t.Parallel()
209+
210+
context := map[string]interface{}{
211+
"project_id": envvar.GetTestProjectFromEnv(),
212+
"delete_protection_state": "DELETE_PROTECTION_DISABLED",
213+
"random_suffix": acctest.RandString(t, 10),
214+
}
215+
216+
acctest.VcrTest(t, resource.TestCase{
217+
PreCheck: func() { acctest.AccTestPreCheck(t) },
218+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
219+
CheckDestroy: testAccCheckFirestoreFieldDestroyProducer(t),
220+
Steps: []resource.TestStep{
221+
{
222+
Config: testAccFirestoreField_firestoreFieldWildcardExample(context),
223+
},
224+
{
225+
ResourceName: "google_firestore_field.wildcard",
226+
ImportState: true,
227+
ImportStateVerify: true,
228+
ImportStateVerifyIgnore: []string{"collection", "database", "field"},
229+
},
230+
},
231+
})
232+
}
233+
234+
func testAccFirestoreField_firestoreFieldWildcardExample(context map[string]interface{}) string {
235+
return acctest.Nprintf(`
236+
resource "google_firestore_database" "database" {
237+
project = "%{project_id}"
238+
name = "tf-test-database-id%{random_suffix}"
239+
location_id = "nam5"
240+
type = "FIRESTORE_NATIVE"
241+
242+
delete_protection_state = "%{delete_protection_state}"
243+
deletion_policy = "DELETE"
244+
}
245+
246+
resource "google_firestore_field" "wildcard" {
247+
project = "%{project_id}"
248+
database = google_firestore_database.database.name
249+
collection = "chatrooms_%{random_suffix}"
250+
field = "*"
251+
252+
index_config {
253+
indexes {
254+
order = "ASCENDING"
255+
query_scope = "COLLECTION_GROUP"
256+
}
257+
indexes {
258+
array_config = "CONTAINS"
259+
}
260+
}
261+
}
262+
`, context)
263+
}
264+
207265
func testAccCheckFirestoreFieldDestroyProducer(t *testing.T) func(s *terraform.State) error {
208266
return func(s *terraform.State) error {
209267
for name, rs := range s.RootModule().Resources {

website/docs/r/firestore_field.html.markdown

+31
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,37 @@ resource "google_firestore_field" "match_override" {
126126
}
127127
}
128128
```
129+
## Example Usage - Firestore Field Wildcard
130+
131+
132+
```hcl
133+
resource "google_firestore_database" "database" {
134+
project = "my-project-name"
135+
name = "database-id"
136+
location_id = "nam5"
137+
type = "FIRESTORE_NATIVE"
138+
139+
delete_protection_state = "DELETE_PROTECTION_ENABLED"
140+
deletion_policy = "DELETE"
141+
}
142+
143+
resource "google_firestore_field" "wildcard" {
144+
project = "my-project-name"
145+
database = google_firestore_database.database.name
146+
collection = "chatrooms_%{random_suffix}"
147+
field = "*"
148+
149+
index_config {
150+
indexes {
151+
order = "ASCENDING"
152+
query_scope = "COLLECTION_GROUP"
153+
}
154+
indexes {
155+
array_config = "CONTAINS"
156+
}
157+
}
158+
}
159+
```
129160

130161
## Argument Reference
131162

0 commit comments

Comments
 (0)