Skip to content

Commit 5f1940c

Browse files
modular-magicianrileykarson
authored andcommitted
Revert recreating bigquery tables
Signed-off-by: Modular Magician <[email protected]>
1 parent d7f51a8 commit 5f1940c

File tree

4 files changed

+13
-283
lines changed

4 files changed

+13
-283
lines changed

google-beta/resource_bigquery_table.go

Lines changed: 13 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package google
22

33
import (
4-
"context"
54
"encoding/json"
65
"errors"
76
"fmt"
87
"log"
98

10-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
119
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1210
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
1311
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -91,14 +89,20 @@ func bigQueryTableMapKeyOverride(key string, objectA, objectB map[string]interfa
9189
valB := objectB[key]
9290
switch key {
9391
case "mode":
94-
eq := bigQueryTableModeEq(valA, valB)
92+
equivelentSet := []interface{}{nil, "NULLABLE"}
93+
eq := valueIsInArray(valA, equivelentSet) && valueIsInArray(valB, equivelentSet)
9594
return eq
9695
case "description":
97-
equivalentSet := []interface{}{nil, ""}
98-
eq := valueIsInArray(valA, equivalentSet) && valueIsInArray(valB, equivalentSet)
96+
equivelentSet := []interface{}{nil, ""}
97+
eq := valueIsInArray(valA, equivelentSet) && valueIsInArray(valB, equivelentSet)
9998
return eq
10099
case "type":
101-
return bigQueryTableTypeEq(valA, valB)
100+
equivelentSet1 := []interface{}{"INTEGER", "INT64"}
101+
equivelentSet2 := []interface{}{"FLOAT", "FLOAT64"}
102+
eq1 := valueIsInArray(valA, equivelentSet1) && valueIsInArray(valB, equivelentSet1)
103+
eq2 := valueIsInArray(valA, equivelentSet2) && valueIsInArray(valB, equivelentSet2)
104+
eq := eq1 || eq2
105+
return eq
102106
}
103107

104108
// otherwise rely on default behavior
@@ -128,141 +132,6 @@ func bigQueryTableSchemaDiffSuppress(_, old, new string, _ *schema.ResourceData)
128132
return eq
129133
}
130134

131-
func bigQueryTableTypeEq(old, new interface{}) bool {
132-
equivalentSet1 := []interface{}{"INTEGER", "INT64"}
133-
equivalentSet2 := []interface{}{"FLOAT", "FLOAT64"}
134-
equivalentSet3 := []interface{}{"BOOLEAN", "BOOL"}
135-
eq0 := old == new
136-
eq1 := valueIsInArray(old, equivalentSet1) && valueIsInArray(new, equivalentSet1)
137-
eq2 := valueIsInArray(old, equivalentSet2) && valueIsInArray(new, equivalentSet2)
138-
eq3 := valueIsInArray(old, equivalentSet3) && valueIsInArray(new, equivalentSet3)
139-
eq := eq0 || eq1 || eq2 || eq3
140-
return eq
141-
}
142-
143-
func bigQueryTableModeEq(old, new interface{}) bool {
144-
equivalentSet := []interface{}{nil, "NULLABLE"}
145-
eq0 := old == new
146-
eq1 := valueIsInArray(old, equivalentSet) && valueIsInArray(new, equivalentSet)
147-
eq := eq0 || eq1
148-
return eq
149-
}
150-
151-
func bigQueryTableModeIsForceNew(old, new interface{}) bool {
152-
eq := bigQueryTableModeEq(old, new)
153-
reqToNull := old == "REQUIRED" && new == "NULLABLE"
154-
return !eq && !reqToNull
155-
}
156-
157-
// Compares two existing schema implementations and decides if
158-
// it is changeable.. pairs with a force new on not changeable
159-
func resourceBigQueryTableSchemaIsChangeable(old, new interface{}) (bool, error) {
160-
switch old.(type) {
161-
case []interface{}:
162-
arrayOld := old.([]interface{})
163-
arrayNew, ok := new.([]interface{})
164-
if !ok {
165-
// if not both arrays not changeable
166-
return false, nil
167-
}
168-
if len(arrayOld) > len(arrayNew) {
169-
// if not growing not changeable
170-
return false, nil
171-
}
172-
for i := range arrayOld {
173-
if isChangable, err :=
174-
resourceBigQueryTableSchemaIsChangeable(arrayOld[i], arrayNew[i]); err != nil || !isChangable {
175-
return false, err
176-
}
177-
}
178-
return true, nil
179-
case map[string]interface{}:
180-
objectOld := old.(map[string]interface{})
181-
objectNew, ok := new.(map[string]interface{})
182-
if !ok {
183-
// if both aren't objects
184-
return false, nil
185-
}
186-
187-
var unionOfKeys map[string]bool = make(map[string]bool)
188-
for key := range objectOld {
189-
unionOfKeys[key] = true
190-
}
191-
for key := range objectNew {
192-
unionOfKeys[key] = true
193-
}
194-
195-
for key := range unionOfKeys {
196-
valOld := objectOld[key]
197-
valNew := objectNew[key]
198-
switch key {
199-
case "name":
200-
if valOld != valNew {
201-
return false, nil
202-
}
203-
case "type":
204-
if !bigQueryTableTypeEq(valOld, valNew) {
205-
return false, nil
206-
}
207-
case "mode":
208-
if bigQueryTableModeIsForceNew(valOld, valNew) {
209-
return false, nil
210-
}
211-
case "fields":
212-
return resourceBigQueryTableSchemaIsChangeable(valOld, valNew)
213-
214-
// other parameters: description, policyTags and
215-
// policyTags.names[] are changeable
216-
}
217-
}
218-
return true, nil
219-
case string, float64, bool, nil:
220-
// realistically this shouldn't hit
221-
log.Printf("[DEBUG] comparison of generics hit... not expected")
222-
return old == new, nil
223-
default:
224-
log.Printf("[DEBUG] tried to iterate through json but encountered a non native type to json deserialization... please ensure you are passing a json object from json.Unmarshall")
225-
return false, errors.New("unable to compare values")
226-
}
227-
}
228-
229-
func resourceBigQueryTableSchemaCustomizeDiffFunc(d TerraformResourceDiff) error {
230-
if _, hasSchema := d.GetOk("schema"); hasSchema {
231-
oldSchema, newSchema := d.GetChange("schema")
232-
oldSchemaText := oldSchema.(string)
233-
newSchemaText := newSchema.(string)
234-
if oldSchemaText == "null" {
235-
// The API can return an empty schema which gets encoded to "null" during read.
236-
oldSchemaText = "[]"
237-
}
238-
var old, new interface{}
239-
if err := json.Unmarshal([]byte(oldSchemaText), &old); err != nil {
240-
// don't return error, its possible we are going from no schema to schema
241-
// this case will be cover on the conparision regardless.
242-
log.Printf("[DEBUG] unable to unmarshal json customized diff - %v", err)
243-
}
244-
if err := json.Unmarshal([]byte(newSchemaText), &new); err != nil {
245-
// same as above
246-
log.Printf("[DEBUG] unable to unmarshal json customized diff - %v", err)
247-
}
248-
isChangeable, err := resourceBigQueryTableSchemaIsChangeable(old, new)
249-
if err != nil {
250-
return err
251-
}
252-
if !isChangeable {
253-
if err := d.ForceNew("schema"); err != nil {
254-
return err
255-
}
256-
}
257-
return nil
258-
}
259-
return nil
260-
}
261-
262-
func resourceBigQueryTableSchemaCustomizeDiff(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
263-
return resourceBigQueryTableSchemaCustomizeDiffFunc(d)
264-
}
265-
266135
func resourceBigQueryTable() *schema.Resource {
267136
return &schema.Resource{
268137
Create: resourceBigQueryTableCreate,
@@ -272,9 +141,6 @@ func resourceBigQueryTable() *schema.Resource {
272141
Importer: &schema.ResourceImporter{
273142
State: resourceBigQueryTableImport,
274143
},
275-
CustomizeDiff: customdiff.All(
276-
resourceBigQueryTableSchemaCustomizeDiff,
277-
),
278144
Schema: map[string]*schema.Schema{
279145
// TableId: [Required] The ID of the table. The ID must contain only
280146
// letters (a-z, A-Z), numbers (0-9), or underscores (_). The maximum
@@ -556,6 +422,7 @@ func resourceBigQueryTable() *schema.Resource {
556422
DiffSuppressFunc: bigQueryTableSchemaDiffSuppress,
557423
Description: `A JSON schema for the table.`,
558424
},
425+
559426
// View: [Optional] If specified, configures this table as a view.
560427
"view": {
561428
Type: schema.TypeList,
@@ -895,6 +762,7 @@ func resourceTable(d *schema.ResourceData, meta interface{}) (*bigquery.Table, e
895762
if err != nil {
896763
return nil, err
897764
}
765+
898766
table.Schema = schema
899767
}
900768

@@ -1081,6 +949,7 @@ func resourceBigQueryTableRead(d *schema.ResourceData, meta interface{}) error {
1081949
if err != nil {
1082950
return err
1083951
}
952+
1084953
if err := d.Set("schema", schema); err != nil {
1085954
return fmt.Errorf("Error setting schema: %s", err)
1086955
}

google-beta/resource_bigquery_table_test.go

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -471,135 +471,12 @@ func TestUnitBigQueryDataTable_jsonEquivalency(t *testing.T) {
471471
}
472472
}
473473

474-
func TestUnitBigQueryDataTable_schemaIsChangable(t *testing.T) {
475-
t.Parallel()
476-
for _, testcase := range testUnitBigQueryDataTableIsChangableTestCases {
477-
testcase.check(t)
478-
testcaseNested := &testUnitBigQueryDataTableJSONChangeableTestCase{
479-
testcase.name + "Nested",
480-
fmt.Sprintf("[{\"name\": \"someValue\", \"type\" : \"INTEGER\", \"fields\" : %s }]", testcase.jsonOld),
481-
fmt.Sprintf("[{\"name\": \"someValue\", \"type\" : \"INT64\", \"fields\" : %s }]", testcase.jsonNew),
482-
testcase.changeable,
483-
}
484-
testcaseNested.check(t)
485-
}
486-
}
487-
488474
type testUnitBigQueryDataTableJSONEquivalencyTestCase struct {
489475
jsonA string
490476
jsonB string
491477
equivalent bool
492478
}
493479

494-
type testUnitBigQueryDataTableJSONChangeableTestCase struct {
495-
name string
496-
jsonOld string
497-
jsonNew string
498-
changeable bool
499-
}
500-
501-
func (testcase *testUnitBigQueryDataTableJSONChangeableTestCase) check(t *testing.T) {
502-
var old, new interface{}
503-
if err := json.Unmarshal([]byte(testcase.jsonOld), &old); err != nil {
504-
t.Fatalf("unable to unmarshal json - %v", err)
505-
}
506-
if err := json.Unmarshal([]byte(testcase.jsonNew), &new); err != nil {
507-
t.Fatalf("unable to unmarshal json - %v", err)
508-
}
509-
changeable, err := resourceBigQueryTableSchemaIsChangeable(old, new)
510-
if err != nil {
511-
t.Errorf("%s failed unexpectedly: %s", testcase.name, err)
512-
}
513-
if changeable != testcase.changeable {
514-
t.Errorf("expected changeable result of %v but got %v for testcase %s", testcase.changeable, changeable, testcase.name)
515-
}
516-
517-
d := &ResourceDiffMock{
518-
Before: map[string]interface{}{},
519-
After: map[string]interface{}{},
520-
}
521-
522-
d.Before["schema"] = testcase.jsonOld
523-
d.After["schema"] = testcase.jsonNew
524-
525-
err = resourceBigQueryTableSchemaCustomizeDiffFunc(d)
526-
if err != nil {
527-
t.Errorf("error on testcase %s - %w", testcase.name, err)
528-
}
529-
if !testcase.changeable != d.IsForceNew {
530-
t.Errorf("%s: expected d.IsForceNew to be %v, but was %v", testcase.name, !testcase.changeable, d.IsForceNew)
531-
}
532-
}
533-
534-
var testUnitBigQueryDataTableIsChangableTestCases = []testUnitBigQueryDataTableJSONChangeableTestCase{
535-
{
536-
name: "defaultEquality",
537-
jsonOld: "[{\"name\": \"someValue\", \"type\" : \"INTEGER\", \"mode\" : \"NULLABLE\", \"description\" : \"someVal\" }]",
538-
jsonNew: "[{\"name\": \"someValue\", \"type\" : \"INTEGER\", \"mode\" : \"NULLABLE\", \"description\" : \"someVal\" }]",
539-
changeable: true,
540-
},
541-
{
542-
name: "arraySizeIncreases",
543-
jsonOld: "[{\"name\": \"someValue\", \"type\" : \"INTEGER\", \"mode\" : \"NULLABLE\", \"description\" : \"someVal\" }]",
544-
jsonNew: "[{\"name\": \"someValue\", \"type\" : \"INTEGER\", \"mode\" : \"NULLABLE\", \"description\" : \"someVal\" }, {\"name\": \"someValue\", \"type\" : \"INTEGER\", \"mode\" : \"NULLABLE\", \"description\" : \"someVal\" }]",
545-
changeable: true,
546-
},
547-
{
548-
name: "arraySizeDecreases",
549-
jsonOld: "[{\"name\": \"someValue\", \"type\" : \"INTEGER\", \"mode\" : \"NULLABLE\", \"description\" : \"someVal\" }, {\"name\": \"someValue\", \"type\" : \"INTEGER\", \"mode\" : \"NULLABLE\", \"description\" : \"someVal\" }]",
550-
jsonNew: "[{\"name\": \"someValue\", \"type\" : \"INTEGER\", \"mode\" : \"NULLABLE\", \"description\" : \"someVal\" }]",
551-
changeable: false,
552-
},
553-
{
554-
name: "descriptionChanges",
555-
jsonOld: "[{\"name\": \"someValue\", \"type\" : \"INTEGER\", \"mode\" : \"NULLABLE\", \"description\" : \"someVal\" }]",
556-
jsonNew: "[{\"name\": \"someValue\", \"type\" : \"INTEGER\", \"mode\" : \"NULLABLE\", \"description\" : \"some new value\" }]",
557-
changeable: true,
558-
},
559-
{
560-
name: "typeInteger",
561-
jsonOld: "[{\"name\": \"someValue\", \"type\" : \"INTEGER\", \"mode\" : \"NULLABLE\", \"description\" : \"someVal\" }]",
562-
jsonNew: "[{\"name\": \"someValue\", \"type\" : \"INT64\", \"mode\" : \"NULLABLE\", \"description\" : \"some new value\" }]",
563-
changeable: true,
564-
},
565-
{
566-
name: "typeFloat",
567-
jsonOld: "[{\"name\": \"someValue\", \"type\" : \"FLOAT\", \"mode\" : \"NULLABLE\", \"description\" : \"someVal\" }]",
568-
jsonNew: "[{\"name\": \"someValue\", \"type\" : \"FLOAT64\", \"mode\" : \"NULLABLE\", \"description\" : \"some new value\" }]",
569-
changeable: true,
570-
},
571-
{
572-
name: "typeBool",
573-
jsonOld: "[{\"name\": \"someValue\", \"type\" : \"BOOLEAN\", \"mode\" : \"NULLABLE\", \"description\" : \"someVal\" }]",
574-
jsonNew: "[{\"name\": \"someValue\", \"type\" : \"BOOL\", \"mode\" : \"NULLABLE\", \"description\" : \"some new value\" }]",
575-
changeable: true,
576-
},
577-
{
578-
name: "typeChangeIncompatible",
579-
jsonOld: "[{\"name\": \"someValue\", \"type\" : \"BOOLEAN\", \"mode\" : \"NULLABLE\", \"description\" : \"someVal\" }]",
580-
jsonNew: "[{\"name\": \"someValue\", \"type\" : \"DATETIME\", \"mode\" : \"NULLABLE\", \"description\" : \"some new value\" }]",
581-
changeable: false,
582-
},
583-
{
584-
name: "typeModeReqToNull",
585-
jsonOld: "[{\"name\": \"someValue\", \"type\" : \"BOOLEAN\", \"mode\" : \"REQUIRED\", \"description\" : \"someVal\" }]",
586-
jsonNew: "[{\"name\": \"someValue\", \"type\" : \"BOOLEAN\", \"mode\" : \"NULLABLE\", \"description\" : \"some new value\" }]",
587-
changeable: true,
588-
},
589-
{
590-
name: "typeModeIncompatible",
591-
jsonOld: "[{\"name\": \"someValue\", \"type\" : \"BOOLEAN\", \"mode\" : \"REQUIRED\", \"description\" : \"someVal\" }]",
592-
jsonNew: "[{\"name\": \"someValue\", \"type\" : \"BOOLEAN\", \"mode\" : \"REPEATED\", \"description\" : \"some new value\" }]",
593-
changeable: false,
594-
},
595-
{
596-
name: "typeModeOmission",
597-
jsonOld: "[{\"name\": \"someValue\", \"type\" : \"BOOLEAN\", \"mode\" : \"REQUIRED\", \"description\" : \"someVal\" }]",
598-
jsonNew: "[{\"name\": \"someValue\", \"type\" : \"BOOLEAN\", \"description\" : \"some new value\" }]",
599-
changeable: false,
600-
},
601-
}
602-
603480
var testUnitBigQueryDataTableJSONEquivalencyTestCases = []testUnitBigQueryDataTableJSONEquivalencyTestCase{
604481
{
605482
"[{\"someKey\": \"someValue\", \"anotherKey\" : \"anotherValue\", \"finalKey\" : {} }]",
@@ -651,16 +528,6 @@ var testUnitBigQueryDataTableJSONEquivalencyTestCases = []testUnitBigQueryDataTa
651528
"[{\"someKey\": \"someValue\", \"anotherKey\" : \"anotherValue\" }]",
652529
false,
653530
},
654-
{
655-
"[{\"someKey\": \"someValue\", \"anotherKey\" : \"anotherValue\", \"type\": \"BOOLEAN\" }]",
656-
"[{\"someKey\": \"someValue\", \"anotherKey\" : \"anotherValue\", \"type\": \"BOOL\" }]",
657-
true,
658-
},
659-
{
660-
"[{\"someKey\": \"someValue\", \"anotherKey\" : \"anotherValue\", \"type\": \"BOOLEAN\" }]",
661-
"[{\"someKey\": \"someValue\", \"anotherKey\" : \"anotherValue\" }]",
662-
false,
663-
},
664531
{
665532
"[1,2,3]",
666533
"[1,2,3]",

google-beta/test_utils.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ func (d *ResourceDiffMock) Get(key string) interface{} {
9191
return d.After[key]
9292
}
9393

94-
func (d *ResourceDiffMock) GetOk(key string) (interface{}, bool) {
95-
v, ok := d.After[key]
96-
return v, ok
97-
}
98-
9994
func (d *ResourceDiffMock) Clear(key string) error {
10095
if d.Cleared == nil {
10196
d.Cleared = map[string]struct{}{}

google-beta/utils.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ type TerraformResourceDiff interface {
3131
HasChange(string) bool
3232
GetChange(string) (interface{}, interface{})
3333
Get(string) interface{}
34-
GetOk(string) (interface{}, bool)
3534
Clear(string) error
3635
ForceNew(string) error
3736
}

0 commit comments

Comments
 (0)