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

firestore index diff suppress on fields #3528

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/5058.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
firestore: removed diff for server generated field `__name__` on `google_firestore_index`
```
44 changes: 41 additions & 3 deletions google-beta/resource_firestore_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,43 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

/*
* FirestoreIndex api apends __name__ as an item to the
* end of the fields list if not present. We are suppressing
* this server generated field.
*/
func firestoreIFieldsDiffSuppressFunc(k, old, new string, d TerraformResourceDataChange) bool {
kLength := "fields.#"
oldLength, newLength := d.GetChange(kLength)
oldInt, ok := oldLength.(int)
if !ok {
return false
}
newInt, ok := newLength.(int)
if !ok {
return false
}

if oldInt == newInt+1 {
kold := fmt.Sprintf("fields.%v.field_path", oldInt-1)
knew := fmt.Sprintf("fields.%v.field_path", newInt-1)

oldLastIndexName, _ := d.GetChange(kold)
_, newLastIndexName := d.GetChange(knew)
if oldLastIndexName == "__name__" && newLastIndexName != "__name__" {
oldBase := fmt.Sprintf("fields.%v", oldInt-1)
if strings.HasPrefix(k, oldBase) || k == kLength {
return true
}
}
}
return false
}

func firestoreIFieldsDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
return firestoreIFieldsDiffSuppressFunc(k, old, new, d)
}

func resourceFirestoreIndex() *schema.Resource {
return &schema.Resource{
Create: resourceFirestoreIndexCreate,
Expand All @@ -48,9 +85,10 @@ func resourceFirestoreIndex() *schema.Resource {
Description: `The collection being indexed.`,
},
"fields": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Type: schema.TypeList,
Required: true,
ForceNew: true,
DiffSuppressFunc: firestoreIFieldsDiffSuppress,
Description: `The fields supported by this index. The last field entry is always for
the field path '__name__'. If, on creation, '__name__' was not
specified as the last field, it will be added automatically with the
Expand Down
4 changes: 0 additions & 4 deletions google-beta/resource_firestore_index_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ resource "google_firestore_index" "my-index" {
order = "DESCENDING"
}

fields {
field_path = "__name__"
order = "DESCENDING"
}
}
`, context)
}
Expand Down
130 changes: 130 additions & 0 deletions google-beta/resource_firestore_index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package google

import (
"fmt"
"testing"
)

func TestUnitFirestoreIndex_firestoreIFieldsDiffSuppress(t *testing.T) {
for _, tc := range firestoreIndexDiffSuppressTestCases {
tc.Test(t)
}
}

type FirestoreIndexDiffSuppressTestCase struct {
Name string
KeysToSuppress []string
Before map[string]interface{}
After map[string]interface{}
}

var firestoreIndexDiffSuppressTestCases = []FirestoreIndexDiffSuppressTestCase{
{
Name: "working_as_intended",
KeysToSuppress: []string{"fields.#", "fields.2.field_path", "fields.2.banana"},
Before: map[string]interface{}{
"fields.#": 3,
"fields.0.field_path": "a",
"fields.1.field_path": "b",
"fields.2.field_path": "__name__",
"fields.2.banana": "sdc",
},
After: map[string]interface{}{
"fields.#": 2,
"fields.0.field_path": "a",
"fields.1.field_path": "b",
},
},
{
Name: "same_size_array",
KeysToSuppress: []string{},
Before: map[string]interface{}{
"fields.#": 3,
"fields.0.field_path": "a",
"fields.1.field_path": "b",
"fields.2.field_path": "__name__",
"fields.2.banana": "sdc",
},
After: map[string]interface{}{
"fields.#": 3,
"fields.0.field_path": "a",
"fields.1.field_path": "b",
"fields.2.field_path": "__name__",
"fields.2.banana": "sdc",
},
},
{
Name: "new_array_larger",
KeysToSuppress: []string{},
Before: map[string]interface{}{
"fields.#": 3,
"fields.0.field_path": "a",
"fields.1.field_path": "b",
"fields.2.field_path": "beep",
"fields.2.banana": "sdc",
},
After: map[string]interface{}{
"fields.#": 4,
"fields.0.field_path": "a",
"fields.1.field_path": "b",
"fields.2.field_path": "__name__",
"fields.2.banana": "sdc",
},
},
{
Name: "does_not_clear_other_fields",
KeysToSuppress: []string{"fields.#", "fields.2.field_path", "fields.2.banana"},
Before: map[string]interface{}{
"fields.#": 3,
"fields.0.field_path": "a",
"fields.1.field_path": "b",
"fields.2.field_path": "__name__",
"fields.2.banana": "sdc",
},
After: map[string]interface{}{
"fields.#": 2,
"fields.0.field_path": "b",
"fields.1.field_path": "b",
},
},
}

func (tc *FirestoreIndexDiffSuppressTestCase) Test(t *testing.T) {
mockResourceDiff := &ResourceDiffMock{
Before: tc.Before,
After: tc.After,
}

keySuppressionMap := map[string]bool{}
for key := range tc.Before {
keySuppressionMap[key] = false
}
for key := range tc.After {
keySuppressionMap[key] = false
}

for _, key := range tc.KeysToSuppress {
keySuppressionMap[key] = true
}

for key, tcSuppress := range keySuppressionMap {
oldValue, ok := tc.Before[key]
if !ok {
oldValue = ""
}
newValue, ok := tc.After[key]
if !ok {
newValue = ""
}
suppressed := firestoreIFieldsDiffSuppressFunc(key, fmt.Sprintf("%v", oldValue), fmt.Sprintf("%v", newValue), mockResourceDiff)
if suppressed != tcSuppress {
var expectation string
if tcSuppress {
expectation = "be"
} else {
expectation = "not be"
}
t.Errorf("Test %s: expected key `%s` to %s suppressed", tc.Name, key, expectation)
}
}
}
4 changes: 4 additions & 0 deletions google-beta/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"google.golang.org/api/googleapi"
)

type TerraformResourceDataChange interface {
GetChange(string) (interface{}, interface{})
}

type TerraformResourceData interface {
HasChange(string) bool
GetOkExists(string) (interface{}, bool)
Expand Down
4 changes: 0 additions & 4 deletions website/docs/r/firestore_index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ resource "google_firestore_index" "my-index" {
order = "DESCENDING"
}

fields {
field_path = "__name__"
order = "DESCENDING"
}
}
```

Expand Down