hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.32k stars 1.73k forks source link

google_firestore_index collection field needs forward slashes "/" escaped. #16979

Open MaitreyaBuddha opened 8 months ago

MaitreyaBuddha commented 8 months ago

Community Note

Terraform Version

Terraform v1.6.4 on darwin_arm64

Your version of Terraform is out of date! The latest version is 1.6.6. You can update by downloading from https://www.terraform.io/downloads.html

Affected Resource(s)

google_firestore_index

Terraform Configuration Files

Ex:


resource "google_firestore_index" "foo_index" {
  for_each    = toset(var.project_ids)
  project     = each.value
  collection  = "people/${var.person}/collection"
  query_scope = "COLLECTION"

  fields {
    field_path = "user_id"
    order      = "ASCENDING"
  }
  fields {
    field_path = "timestamp"
    order      = "DESCENDING"
  }
}

Debug Output

Panic Output

Expected Behavior

Works for nested collections.

Actual Behavior

Get a 404 from /v1/projects/***/databases/(default)/collectionGroups/people/john-doe/collection/indexes

β”‚ Error: Error creating Index: googleapi: got HTTP response code 404 with body: <!DOCTYPE html>
β”‚ <html lang=en>
β”‚   <meta charset=utf-8>
β”‚   <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
β”‚   <title>Error 404 (Not Found)!!1</title>
β”‚   <style>
β”‚     *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
β”‚   </style>
β”‚   <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
β”‚   <p><b>404.</b> <ins>That’s an error.</ins>
β”‚   <p>The requested URL <code>/v1/projects/***/databases/(default)/collectionGroups/people/john-doe/collection/indexes?alt=json</code> was not found on this server.  <ins>That’s all we know.</ins>
β”‚

Steps to Reproduce

  1. terraform apply

Important Factoids

Workaround (and fix?) is to use what firestore uses in the URL, ex:

collection = "~2Fpeople~2F${var.coach_id}~2Fcollection"

References

b/319998569

pcostell commented 2 months ago

In Firestore, indexes are defined for all collections of the same id (the query scope then defines if you want to query within a collection or across the entire collection group).

In this case, defining the collection as collection, would create the index for the sub-collection people/<person>/collection.

Defining an index on the collection ~2Fpeople~2F${var.coach_id}~2Fcollection won't create an index on the subcollection, but rather on a collection named "people/${var.coach_id}/collection".