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

add diff suppress for big query table schema #3751

Merged
merged 2 commits into from
Jul 16, 2020
Merged
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
39 changes: 38 additions & 1 deletion third_party/terraform/resources/resource_bigquery_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,49 @@ import (
"errors"
"fmt"
"log"
"reflect"
"sort"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"google.golang.org/api/bigquery/v2"
)

// JSONBytesEqual compares the JSON in two byte slices.
// Reference: https://stackoverflow.com/questions/32408890/how-to-compare-two-json-requests
func JSONBytesEqual(a, b []byte) (bool, error) {
var j, j2 interface{}
if err := json.Unmarshal(a, &j); err != nil {
return false, err
}
jList := j.([]interface{})
sort.Slice(jList, func(i, k int) bool {
return jList[i].(map[string]interface{})["name"].(string) < jList[k].(map[string]interface{})["name"].(string)
})
if err := json.Unmarshal(b, &j2); err != nil {
return false, err
}
j2List := j2.([]interface{})
sort.Slice(j2List, func(i, k int) bool {
return j2List[i].(map[string]interface{})["name"].(string) < j2List[k].(map[string]interface{})["name"].(string)
})
return reflect.DeepEqual(j2List, jList), nil
}

// Compare the JSON strings are equal
func bigQueryTableSchemaDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
oldBytes := []byte(old)
newBytes := []byte(new)

eq, err := JSONBytesEqual(oldBytes, newBytes)
if err != nil {
log.Printf("[DEBUG] Error comparing JSON bytes: %v, %v", old, new)
}

return eq
}

func resourceBigQueryTable() *schema.Resource {
return &schema.Resource{
Create: resourceBigQueryTableCreate,
Expand Down Expand Up @@ -299,7 +335,8 @@ func resourceBigQueryTable() *schema.Resource {
json, _ := structure.NormalizeJsonString(v)
return json
},
Description: `A JSON schema for the table.`,
DiffSuppressFunc: bigQueryTableSchemaDiffSuppress,
Description: `A JSON schema for the table.`,
},

// View: [Optional] If specified, configures this table as a view.
Expand Down