Skip to content

Commit 0e4cb96

Browse files
feat: Firestore datastore mode index support (#8713) (#16085)
Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Felipe Gonçalves de Castro <[email protected]>
1 parent e0e8a53 commit 0e4cb96

File tree

4 files changed

+120
-9
lines changed

4 files changed

+120
-9
lines changed

.changelog/8713.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
firestore: added `api_scope` field to `google_firestore_index` resource
3+
```

google/services/firestore/resource_firestore_index.go

+31-2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ Only one of 'order' and 'arrayConfig' can be specified. Possible values: ["ASCEN
134134
},
135135
},
136136
},
137+
"api_scope": {
138+
Type: schema.TypeString,
139+
Optional: true,
140+
ForceNew: true,
141+
ValidateFunc: verify.ValidateEnum([]string{"ANY_API", "DATASTORE_MODE_API", ""}),
142+
Description: `The API scope at which a query is run. Default value: "ANY_API" Possible values: ["ANY_API", "DATASTORE_MODE_API"]`,
143+
Default: "ANY_API",
144+
},
137145
"database": {
138146
Type: schema.TypeString,
139147
Optional: true,
@@ -145,8 +153,8 @@ Only one of 'order' and 'arrayConfig' can be specified. Possible values: ["ASCEN
145153
Type: schema.TypeString,
146154
Optional: true,
147155
ForceNew: true,
148-
ValidateFunc: verify.ValidateEnum([]string{"COLLECTION", "COLLECTION_GROUP", ""}),
149-
Description: `The scope at which a query is run. Default value: "COLLECTION" Possible values: ["COLLECTION", "COLLECTION_GROUP"]`,
156+
ValidateFunc: verify.ValidateEnum([]string{"COLLECTION", "COLLECTION_GROUP", "COLLECTION_RECURSIVE", ""}),
157+
Description: `The scope at which a query is run. Default value: "COLLECTION" Possible values: ["COLLECTION", "COLLECTION_GROUP", "COLLECTION_RECURSIVE"]`,
150158
Default: "COLLECTION",
151159
},
152160
"name": {
@@ -192,6 +200,12 @@ func resourceFirestoreIndexCreate(d *schema.ResourceData, meta interface{}) erro
192200
} else if v, ok := d.GetOkExists("query_scope"); !tpgresource.IsEmptyValue(reflect.ValueOf(queryScopeProp)) && (ok || !reflect.DeepEqual(v, queryScopeProp)) {
193201
obj["queryScope"] = queryScopeProp
194202
}
203+
apiScopeProp, err := expandFirestoreIndexApiScope(d.Get("api_scope"), d, config)
204+
if err != nil {
205+
return err
206+
} else if v, ok := d.GetOkExists("api_scope"); !tpgresource.IsEmptyValue(reflect.ValueOf(apiScopeProp)) && (ok || !reflect.DeepEqual(v, apiScopeProp)) {
207+
obj["apiScope"] = apiScopeProp
208+
}
195209
fieldsProp, err := expandFirestoreIndexFields(d.Get("fields"), d, config)
196210
if err != nil {
197211
return err
@@ -328,6 +342,9 @@ func resourceFirestoreIndexRead(d *schema.ResourceData, meta interface{}) error
328342
if err := d.Set("query_scope", flattenFirestoreIndexQueryScope(res["queryScope"], d, config)); err != nil {
329343
return fmt.Errorf("Error reading Index: %s", err)
330344
}
345+
if err := d.Set("api_scope", flattenFirestoreIndexApiScope(res["apiScope"], d, config)); err != nil {
346+
return fmt.Errorf("Error reading Index: %s", err)
347+
}
331348
if err := d.Set("fields", flattenFirestoreIndexFields(res["fields"], d, config)); err != nil {
332349
return fmt.Errorf("Error reading Index: %s", err)
333350
}
@@ -426,6 +443,14 @@ func flattenFirestoreIndexQueryScope(v interface{}, d *schema.ResourceData, conf
426443
return v
427444
}
428445

446+
func flattenFirestoreIndexApiScope(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
447+
if v == nil || tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
448+
return "ANY_API"
449+
}
450+
451+
return v
452+
}
453+
429454
func flattenFirestoreIndexFields(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
430455
if v == nil {
431456
return v
@@ -470,6 +495,10 @@ func expandFirestoreIndexQueryScope(v interface{}, d tpgresource.TerraformResour
470495
return v, nil
471496
}
472497

498+
func expandFirestoreIndexApiScope(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
499+
return v, nil
500+
}
501+
473502
func expandFirestoreIndexFields(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
474503
l := v.([]interface{})
475504
req := make([]interface{}, 0, len(l))

google/services/firestore/resource_firestore_index_generated_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,56 @@ resource "google_firestore_index" "my-index" {
7878
`, context)
7979
}
8080

81+
func TestAccFirestoreIndex_firestoreIndexDatastoreModeExample(t *testing.T) {
82+
t.Parallel()
83+
84+
context := map[string]interface{}{
85+
"project_id": envvar.GetTestFirestoreProjectFromEnv(t),
86+
"random_suffix": acctest.RandString(t, 10),
87+
}
88+
89+
acctest.VcrTest(t, resource.TestCase{
90+
PreCheck: func() { acctest.AccTestPreCheck(t) },
91+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
92+
CheckDestroy: testAccCheckFirestoreIndexDestroyProducer(t),
93+
Steps: []resource.TestStep{
94+
{
95+
Config: testAccFirestoreIndex_firestoreIndexDatastoreModeExample(context),
96+
},
97+
{
98+
ResourceName: "google_firestore_index.my-datastore-mode-index",
99+
ImportState: true,
100+
ImportStateVerify: true,
101+
ImportStateVerifyIgnore: []string{"database", "collection"},
102+
},
103+
},
104+
})
105+
}
106+
107+
func testAccFirestoreIndex_firestoreIndexDatastoreModeExample(context map[string]interface{}) string {
108+
return acctest.Nprintf(`
109+
resource "google_firestore_index" "my-datastore-mode-index" {
110+
project = "%{project_id}"
111+
112+
collection = "chatrooms"
113+
114+
query_scope = "COLLECTION_RECURSIVE"
115+
api_scope = "DATASTORE_MODE_API"
116+
117+
fields {
118+
field_path = "name"
119+
order = "ASCENDING"
120+
}
121+
122+
fields {
123+
field_path = "description"
124+
order = "DESCENDING"
125+
}
126+
127+
}
128+
`, context)
129+
}
130+
81131
func testAccCheckFirestoreIndexDestroyProducer(t *testing.T) func(s *terraform.State) error {
82132
return func(s *terraform.State) error {
83133
for name, rs := range s.RootModule().Resources {

website/docs/r/firestore_index.html.markdown

+36-7
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ To get more information about Index, see:
3232

3333
~> **Warning:** This resource creates a Firestore Index on a project that already has
3434
a Firestore database. If you haven't already created it, you may
35-
create a `google_firestore_database` resource with `type` set to
36-
`"FIRESTORE_NATIVE"` and `location_id` set to your chosen location.
37-
If you wish to use App Engine, you may instead create a
38-
`google_app_engine_application` resource with `database_type` set to
39-
`"CLOUD_FIRESTORE"`. Your Firestore location will be the same as
40-
the App Engine location specified.
35+
create a `google_firestore_database` resource and `location_id` set
36+
to your chosen location. If you wish to use App Engine, you may
37+
instead create a `google_app_engine_application` resource with
38+
`database_type` set to `"CLOUD_FIRESTORE"`. Your Firestore location
39+
will be the same as the App Engine location specified.
4140

4241
## Example Usage - Firestore Index Basic
4342

@@ -58,6 +57,30 @@ resource "google_firestore_index" "my-index" {
5857
order = "DESCENDING"
5958
}
6059
60+
}
61+
```
62+
## Example Usage - Firestore Index Datastore Mode
63+
64+
65+
```hcl
66+
resource "google_firestore_index" "my-datastore-mode-index" {
67+
project = "my-project-name"
68+
69+
collection = "chatrooms"
70+
71+
query_scope = "COLLECTION_RECURSIVE"
72+
api_scope = "DATASTORE_MODE_API"
73+
74+
fields {
75+
field_path = "name"
76+
order = "ASCENDING"
77+
}
78+
79+
fields {
80+
field_path = "description"
81+
order = "DESCENDING"
82+
}
83+
6184
}
6285
```
6386

@@ -110,7 +133,13 @@ The following arguments are supported:
110133
(Optional)
111134
The scope at which a query is run.
112135
Default value is `COLLECTION`.
113-
Possible values are: `COLLECTION`, `COLLECTION_GROUP`.
136+
Possible values are: `COLLECTION`, `COLLECTION_GROUP`, `COLLECTION_RECURSIVE`.
137+
138+
* `api_scope` -
139+
(Optional)
140+
The API scope at which a query is run.
141+
Default value is `ANY_API`.
142+
Possible values are: `ANY_API`, `DATASTORE_MODE_API`.
114143

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

0 commit comments

Comments
 (0)