Repository: GoogleCloudPlatform/terraform-validator
Pull request #380 (2021-11-16 17:45:41)

Add support for bigquery_table resource.

ankurdua: Magic modules PR:- https://github.com/GoogleCloudPlatform/magic-modules/pull/5458

Patch:
diff --git a/converters/google/resources/bigquery_table.go b/converters/google/resources/bigquery_table.go
index c0a158c2..5eaa6666 100644
--- a/converters/google/resources/bigquery_table.go
+++ b/converters/google/resources/bigquery_table.go
@@ -4,25 +4,34 @@ import (
 	"reflect"
 )
 
-func GetBigQueryTableCaiObject(d TerraformResourceData, config *Config) (Asset, error) {
+const BigQueryTableAssetType string = "bigquery.googleapis.com/Table"
+
+func resourceConverterBigQueryTable() ResourceConverter {
+	return ResourceConverter{
+		AssetType: BigQueryTableAssetType,
+		Convert:   GetBigQueryTableCaiObject,
+	}
+}
+
+func GetBigQueryTableCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) {
 	name, err := assetName(d, config, "//bigquery.googleapis.com/projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}}")
 
 	if err != nil {
-		return Asset{}, err
+		return []Asset{}, err
 	}
 	if obj, err := GetBigQueryTableApiObject(d, config); err == nil {
-		return Asset{
+		return []Asset{{
 			Name: name,
-			Type: "bigquery.googleapis.com/Table",
+			Type: BigQueryTableAssetType,
 			Resource: &AssetResource{
 				Version:              "v2",
 				DiscoveryDocumentURI: "https://www.googleapis.com/discovery/v1/apis/bigquery/v2/rest",
 				DiscoveryName:        "Table",
 				Data:                 obj,
 			},
-		}, nil
+		}}, nil
 	} else {
-		return Asset{}, err
+		return []Asset{}, err
 	}
 }
 
@@ -36,7 +45,7 @@ func GetBigQueryTableApiObject(d TerraformResourceData, config *Config) (map[str
 		obj["tableReference"] = tableReferenceProp
 	}
 
-    descriptionProp, err := expandBigQueryTableDescription(d.Get("description"), d, config)
+	descriptionProp, err := expandBigQueryTableDescription(d.Get("description"), d, config)
 	if err != nil {
 		return nil, err
 	} else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
@@ -49,7 +58,7 @@ func GetBigQueryTableApiObject(d TerraformResourceData, config *Config) (map[str
 	} else if v, ok := d.GetOkExists("friendly_name"); !isEmptyValue(reflect.ValueOf(friendlyNameProp)) && (ok || !reflect.DeepEqual(v, friendlyNameProp)) {
 		obj["friendlyName"] = friendlyNameProp
 	}
-	
+
 	labelsProp, err := expandBigQueryTableLabels(d.Get("labels"), d, config)
 	if err != nil {
 		return nil, err
@@ -63,7 +72,7 @@ func GetBigQueryTableApiObject(d TerraformResourceData, config *Config) (map[str
 	} else if v, ok := d.GetOkExists("location"); !isEmptyValue(reflect.ValueOf(locationProp)) && (ok || !reflect.DeepEqual(v, locationProp)) {
 		obj["location"] = locationProp
 	}
-	
+
 	expirationTimeProp, err := expandBigQueryTableExpirationTime(nil, d, config)
 	if err != nil {
 		return nil, err
@@ -90,7 +99,7 @@ func GetBigQueryTableApiObject(d TerraformResourceData, config *Config) (map[str
 
 func expandBigQueryTableTableReference(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
 	transformed := make(map[string]interface{})
-	
+
 	transformedProjectId, err := expandBigQueryTableTableReferenceProjectId(d.Get("project"), d, config)
 	if err != nil {
 		return nil, err
@@ -109,7 +118,7 @@ func expandBigQueryTableTableReference(v interface{}, d TerraformResourceData, c
 	if err != nil {
 		return nil, err
 	} else if val := reflect.ValueOf(transformedTableId); val.IsValid() && !isEmptyValue(val) {
-		transformed["tableId"] = transformedDatasetId
+		transformed["tableId"] = transformedTableId
 	}
 
 	return transformed, nil
@@ -117,7 +126,7 @@ func expandBigQueryTableTableReference(v interface{}, d TerraformResourceData, c
 
 func expandBigQueryTableExpirationTime(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
 	transformed := make(map[string]interface{})
-	
+
 	transformedExpirationTimeValue, err := expandBigQueryTableExpirationTimeValue(d.Get("expiration_time"), d, config)
 
 	if err != nil {
@@ -139,7 +148,7 @@ func expandBigQueryTableEncyptionConfiguration(v interface{}, d TerraformResourc
 	transformed := make(map[string]interface{})
 
 	transformedKmsKeyName, err := expandBigQueryTableEncyptionConfigurationKmsKeyName(original["kms_key_name"], d, config)
-	
+
 	if err != nil {
 		return nil, err
 	} else if val := reflect.ValueOf(transformedKmsKeyName); val.IsValid() && !isEmptyValue(val) {
@@ -178,7 +187,7 @@ func expandBigQueryTableTimePartition(v interface{}, d TerraformResourceData, co
 	transformed := make(map[string]interface{})
 
 	transformedType, err := expandBigQueryTableTimePartitionType(original["type"], d, config)
-	
+
 	if err != nil {
 		return nil, err
 	} else if val := reflect.ValueOf(transformedType); val.IsValid() && !isEmptyValue(val) {
@@ -186,7 +195,7 @@ func expandBigQueryTableTimePartition(v interface{}, d TerraformResourceData, co
 	}
 
 	transformedExpirationMs, err := expandBigQueryTableTimePartitionExpirationMs(original["expiration_ms"], d, config)
-	
+
 	if err != nil {
 		return nil, err
 	} else if val := reflect.ValueOf(transformedExpirationMs); val.IsValid() && !isEmptyValue(val) {
@@ -194,15 +203,15 @@ func expandBigQueryTableTimePartition(v interface{}, d TerraformResourceData, co
 	}
 
 	transformedField, err := expandBigQueryTableTimeField(original["field"], d, config)
-	
+
 	if err != nil {
 		return nil, err
 	} else if val := reflect.ValueOf(transformedField); val.IsValid() && !isEmptyValue(val) {
 		transformed["field"] = transformedField
 	}
-	
+
 	transformedRequirePartitionFilter, err := expandBigQueryTableRequirePartitionFilter(original["require_partition_filter"], d, config)
-	
+
 	if err != nil {
 		return nil, err
 	} else if val := reflect.ValueOf(transformedRequirePartitionFilter); val.IsValid() && !isEmptyValue(val) {
@@ -221,7 +230,7 @@ func expandBigQueryTableTimePartitionExpirationMs(v interface{}, d TerraformReso
 
 func expandBigQueryTableTimeField(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
 	transformed := make(map[string]interface{})
-	
+
 	transformedValue, err := expandBigQueryTableTimeFieldValue(v, d, config)
 
 	if err != nil {
diff --git a/converters/google/resources/resource_converters.go b/converters/google/resources/resource_converters.go
index 9553cce5..fc251e57 100644
--- a/converters/google/resources/resource_converters.go
+++ b/converters/google/resources/resource_converters.go
@@ -50,6 +50,7 @@ func ResourceConverters() map[string][]ResourceConverter {
 		"google_access_context_manager_service_perimeter":  {resourceConverterAccessContextManagerServicePerimeter()},
 		"google_cloud_run_service":                         {resourceConverterCloudRunService()},
 		"google_cloud_run_domain_mapping":                  {resourceConverterCloudRunDomainMapping()},
+    "google_bigquery_table":                            {resourceConverterBigQueryTable()},
 		"google_bigquery_table_iam_policy":                 {resourceConverterBigQueryTableIamPolicy()},
 		"google_bigquery_table_iam_binding":                {resourceConverterBigQueryTableIamBinding()},
 		"google_bigquery_table_iam_member":                 {resourceConverterBigQueryTableIamMember()},
diff --git a/docs/supported_resources.md b/docs/supported_resources.md
index d5c37763..d1276b57 100644
--- a/docs/supported_resources.md
+++ b/docs/supported_resources.md
@@ -12,6 +12,7 @@ google_bigquery_dataset                          | bigquery.googleapis.com/Datas
 google_bigquery_dataset_iam_binding              | bigquery.googleapis.com/Dataset
 google_bigquery_dataset_iam_member               | bigquery.googleapis.com/Dataset
 google_bigquery_dataset_iam_policy               | bigquery.googleapis.com/Dataset
+google_bigquery_table                            | bigquery.googleapis.com/Table
 google_bigquery_table_iam_binding                | bigquery.googleapis.com/Table
 google_bigquery_table_iam_member                 | bigquery.googleapis.com/Table
 google_bigquery_table_iam_policy                 | bigquery.googleapis.com/Table


Test patch:
diff --git a/test/cli_test.go b/test/cli_test.go
index eeba465e..ccb12528 100644
--- a/test/cli_test.go
+++ b/test/cli_test.go
@@ -64,6 +64,7 @@ func TestCLI(t *testing.T) {
 		{name: "example_bigquery_dataset_iam_policy"},
 		{name: "example_bigquery_dataset_iam_binding"},
 		{name: "example_bigquery_dataset_iam_member"},
+		{name: "example_bigquery_table"},
 		{name: "example_bigtable_instance"},
 		{name: "example_cloud_run_mapping"},
 		{name: "example_cloud_run_service"},
diff --git a/test/read_test.go b/test/read_test.go
index 619061d3..b5e15a3e 100644
--- a/test/read_test.go
+++ b/test/read_test.go
@@ -25,6 +25,7 @@ func TestReadPlannedAssetsCoverage(t *testing.T) {
 		{name: "example_bigquery_dataset_iam_policy"},
 		{name: "example_bigquery_dataset_iam_binding"},
 		{name: "example_bigquery_dataset_iam_member"},
+		{name: "example_bigquery_table"},
 		{name: "example_bigtable_instance"},
 		{name: "example_cloud_run_mapping"},
 		{name: "example_cloud_run_service"},
diff --git a/testdata/templates/example_bigquery_table.json b/testdata/templates/example_bigquery_table.json
new file mode 100644
index 00000000..79f96366
--- /dev/null
+++ b/testdata/templates/example_bigquery_table.json
@@ -0,0 +1,21 @@
+[
+  {
+    "name": "//bigquery.googleapis.com/projects/{{.Provider.project}}/datasets/dataset/tables/table",
+    "asset_type": "bigquery.googleapis.com/Table",
+    "ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}",
+    "resource": {
+      "version": "v2",
+      "discovery_document_uri": "https://www.googleapis.com/discovery/v1/apis/bigquery/v2/rest",
+      "discovery_name": "Table",
+      "parent": "//cloudresourcemanager.googleapis.com/projects/{{.Provider.project}}",
+      "data": {
+        "friendlyName": "friendly table",
+        "tableReference": {
+          "datasetId": "dataset",
+          "project": "{{.Provider.project}}",
+          "tableId": "table"
+        }
+      }
+    }
+  }
+]
diff --git a/testdata/templates/example_bigquery_table.tf b/testdata/templates/example_bigquery_table.tf
new file mode 100644
index 00000000..0f49f7ac
--- /dev/null
+++ b/testdata/templates/example_bigquery_table.tf
@@ -0,0 +1,51 @@
+/**
+ * Copyright 2021 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+terraform {
+  required_providers {
+    google = {
+      source = "hashicorp/google"
+      version = "~> {{.Provider.version}}"
+    }
+  }
+}
+
+provider "google" {
+  {{if .Provider.credentials }}credentials = "{{.Provider.credentials}}"{{end}}
+}
+
+resource "google_bigquery_table" "table" {
+dataset_id          = "dataset"
+project             = "{{.Provider.project}}"
+table_id            = "table"
+friendly_name       = "friendly table"
+
+schema = <<EOF
+[
+  {
+    "name": "Card_Number",
+    "mode": "NULLABLE",
+    "type": "STRING"
+  },
+  {
+    "name": "Card_Holders_Name",
+    "mode": "NULLABLE",
+    "type": "STRING"
+  }
+]
+EOF
+
+}
\ No newline at end of file
diff --git a/testdata/templates/example_bigquery_table.tfplan.json b/testdata/templates/example_bigquery_table.tfplan.json
new file mode 100644
index 00000000..236c78e4
--- /dev/null
+++ b/testdata/templates/example_bigquery_table.tfplan.json
@@ -0,0 +1,117 @@
+{
+  "format_version": "0.1",
+  "terraform_version": "0.13.5",
+  "planned_values": {
+    "root_module": {
+      "resources": [
+        {
+          "address": "google_bigquery_table.table",
+          "mode": "managed",
+          "type": "google_bigquery_table",
+          "name": "table",
+          "provider_name": "registry.terraform.io/hashicorp/google",
+          "schema_version": 0,
+          "values": {
+            "clustering": null,
+            "dataset_id": "dataset",
+            "deletion_protection": true,
+            "description": null,
+            "encryption_configuration": [],
+            "external_data_configuration": [],
+            "friendly_name": "friendly table",
+            "labels": null,
+            "materialized_view": [],
+            "project": "{{.Provider.project}}",
+            "range_partitioning": [],
+            "schema": "[{\"mode\":\"NULLABLE\",\"name\":\"Card_Number\",\"type\":\"STRING\"},{\"mode\":\"NULLABLE\",\"name\":\"Card_Holders_Name\",\"type\":\"STRING\"}]",
+            "table_id": "table",
+            "time_partitioning": [],
+            "view": []
+          }
+        }
+      ]
+    }
+  },
+  "resource_changes": [
+    {
+      "address": "google_bigquery_table.table",
+      "mode": "managed",
+      "type": "google_bigquery_table",
+      "name": "table",
+      "provider_name": "registry.terraform.io/hashicorp/google",
+      "change": {
+        "actions": [
+          "create"
+        ],
+        "before": null,
+        "after": {
+          "clustering": null,
+          "dataset_id": "dataset",
+          "deletion_protection": true,
+          "description": null,
+          "encryption_configuration": [],
+          "external_data_configuration": [],
+          "friendly_name": "friendly table",
+          "labels": null,
+          "materialized_view": [],
+          "project": "{{.Provider.project}}",
+          "range_partitioning": [],
+          "schema": "[{\"mode\":\"NULLABLE\",\"name\":\"Card_Number\",\"type\":\"STRING\"},{\"mode\":\"NULLABLE\",\"name\":\"Card_Holders_Name\",\"type\":\"STRING\"}]",
+          "table_id": "table",
+          "time_partitioning": [],
+          "view": []
+        },
+        "after_unknown": {
+          "creation_time": true,
+          "encryption_configuration": [],
+          "etag": true,
+          "expiration_time": true,
+          "external_data_configuration": [],
+          "id": true,
+          "last_modified_time": true,
+          "location": true,
+          "materialized_view": [],
+          "num_bytes": true,
+          "num_long_term_bytes": true,
+          "num_rows": true,
+          "range_partitioning": [],
+          "self_link": true,
+          "time_partitioning": [],
+          "type": true,
+          "view": []
+        }
+      }
+    }
+  ],
+  "configuration": {
+    "root_module": {
+      "resources": [
+        {
+          "address": "google_bigquery_table.table",
+          "mode": "managed",
+          "type": "google_bigquery_table",
+          "name": "table",
+          "provider_config_key": "google",
+          "expressions": {
+            "dataset_id": {
+              "constant_value": "dataset"
+            },
+            "friendly_name": {
+              "constant_value": "friendly table"
+            },
+            "project": {
+              "constant_value": "{{.Provider.project}}"
+            },
+            "schema": {
+              "constant_value": "[\n  {\n    \"name\": \"Card_Number\",\n    \"mode\": \"NULLABLE\",\n    \"type\": \"STRING\"\n  },\n  {\n    \"name\": \"Card_Holders_Name\",\n    \"mode\": \"NULLABLE\",\n    \"type\": \"STRING\"\n  }\n]\n"
+            },
+            "table_id": {
+              "constant_value": "table"
+            }
+          },
+          "schema_version": 0
+        }
+      ]
+    }
+  }
+}
\ No newline at end of file