Skip to content

Commit

Permalink
Merge pull request #3382 from justinsb/add_export_for_bigquery_table
Browse files Browse the repository at this point in the history
tests: add test coverage for exporting BigQueryTable
  • Loading branch information
google-oss-prow[bot] authored Dec 18, 2024
2 parents 4dffbd6 + 457d2c6 commit 95b3225
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: bigquery.cnrm.cloud.google.com/v1beta1
kind: BigQueryTable
metadata:
annotations:
cnrm.cloud.google.com/project-id: ${projectId}
labels:
cnrm-test: "true"
managed-by-cnrm: "true"
name: bigquerytablesample${uniqueId}
spec:
datasetRef:
external: bigquerydatasetsample${uniqueId}
externalDataConfiguration:
autodetect: true
compression: NONE
sourceFormat: CSV
sourceUris:
- gs://gcp-public-data-landsat/LC08/01/044/034/LC08_L1GT_044034_20130330_20170310_01_T2/LC08_L1GT_044034_20130330_20170310_01_T2_ANG.txt
friendlyName: bigquerytable-sample-updated
resourceID: bigquerytablesample${uniqueId}
schema: '[{"mode":"NULLABLE","name":"string_field_0","type":"STRING"},{"mode":"NULLABLE","name":"string_field_1","type":"STRING"},{"mode":"NULLABLE","name":"string_field_2","type":"STRING"},{"mode":"NULLABLE","name":"string_field_3","type":"STRING"},{"mode":"NULLABLE","name":"string_field_4","type":"STRING"},{"mode":"NULLABLE","name":"string_field_5","type":"STRING"},{"mode":"NULLABLE","name":"int64_field_6","type":"INTEGER"},{"mode":"NULLABLE","name":"int64_field_7","type":"INTEGER"},{"mode":"NULLABLE","name":"int64_field_8","type":"INTEGER"},{"mode":"NULLABLE","name":"int64_field_9","type":"INTEGER"},{"mode":"NULLABLE","name":"string_field_10","type":"STRING"},{"mode":"NULLABLE","name":"int64_field_11","type":"INTEGER"},{"mode":"NULLABLE","name":"int64_field_12","type":"INTEGER"},{"mode":"NULLABLE","name":"string_field_13","type":"STRING"}]'
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,159 @@ X-Xss-Protection: 0

---

POST https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/${datasetID}/tables/bigquerytablesample${uniqueId}:getIamPolicy?alt=json
Content-Type: application/json
User-Agent: Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

{
"options": {
"requestedPolicyVersion": 1
}
}

200 OK
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"etag": "abcdef0123A="
}

---

GET https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/${datasetID}/tables/bigquerytablesample${uniqueId}?alt=json&prettyPrint=false
User-Agent: google-api-go-client/0.5 Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

200 OK
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"creationTime": "123456789",
"etag": "abcdef0123A=",
"externalDataConfiguration": {
"autodetect": true,
"compression": "NONE",
"sourceFormat": "CSV",
"sourceUris": [
"gs://gcp-public-data-landsat/LC08/01/044/034/LC08_L1GT_044034_20130330_20170310_01_T2/LC08_L1GT_044034_20130330_20170310_01_T2_ANG.txt"
]
},
"friendlyName": "bigquerytable-sample-updated",
"id": "000000000000000000000",
"kind": "bigquery#table",
"labels": {
"cnrm-test": "true",
"managed-by-cnrm": "true"
},
"lastModifiedTime": "123456789",
"location": "US",
"numActiveLogicalBytes": "0",
"numBytes": "0",
"numLongTermBytes": "0",
"numLongTermLogicalBytes": "0",
"numRows": "0",
"numTotalLogicalBytes": "0",
"requirePartitionFilter": false,
"schema": {
"fields": [
{
"mode": "NULLABLE",
"name": "string_field_0",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "string_field_1",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "string_field_2",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "string_field_3",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "string_field_4",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "string_field_5",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "int64_field_6",
"type": "INTEGER"
},
{
"mode": "NULLABLE",
"name": "int64_field_7",
"type": "INTEGER"
},
{
"mode": "NULLABLE",
"name": "int64_field_8",
"type": "INTEGER"
},
{
"mode": "NULLABLE",
"name": "int64_field_9",
"type": "INTEGER"
},
{
"mode": "NULLABLE",
"name": "string_field_10",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "int64_field_11",
"type": "INTEGER"
},
{
"mode": "NULLABLE",
"name": "int64_field_12",
"type": "INTEGER"
},
{
"mode": "NULLABLE",
"name": "string_field_13",
"type": "STRING"
}
]
},
"selfLink": "https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/bigquerydatasetsample${uniqueId}/tables/bigquerytablesample${uniqueId}",
"tableReference": {
"datasetId": "bigquerydatasetsample${uniqueId}",
"projectId": "${projectId}",
"tableId": "bigquerytablesample${uniqueId}"
},
"type": "EXTERNAL"
}

---

DELETE https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/${datasetID}/tables/bigquerytablesample${uniqueId}?alt=json&prettyPrint=false
User-Agent: google-api-go-client/0.5 Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: bigquery.cnrm.cloud.google.com/v1beta1
kind: BigQueryTable
metadata:
annotations:
cnrm.cloud.google.com/project-id: ${projectId}
labels:
cnrm-test: "true"
managed-by-cnrm: "true"
name: bigquerytable-resourceid-${uniqueId}
spec:
datasetRef:
external: bigquerydataset_resourceid_${uniqueId}
friendlyName: bigquerytable-sample-updated
resourceID: bigquerytable_resourceid_${uniqueId}
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,76 @@ X-Xss-Protection: 0

---

POST https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/${datasetID}/tables/bigquerytable_resourceid_${uniqueId}:getIamPolicy?alt=json
Content-Type: application/json
User-Agent: Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

{
"options": {
"requestedPolicyVersion": 1
}
}

200 OK
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"etag": "abcdef0123A="
}

---

GET https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/${datasetID}/tables/bigquerytable_resourceid_${uniqueId}?alt=json&prettyPrint=false
User-Agent: google-api-go-client/0.5 Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

200 OK
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"creationTime": "123456789",
"etag": "abcdef0123A=",
"friendlyName": "bigquerytable-sample-updated",
"id": "000000000000000000000",
"kind": "bigquery#table",
"labels": {
"cnrm-test": "true",
"managed-by-cnrm": "true"
},
"lastModifiedTime": "123456789",
"location": "US",
"numActiveLogicalBytes": "0",
"numBytes": "0",
"numLongTermBytes": "0",
"numLongTermLogicalBytes": "0",
"numRows": "0",
"numTotalLogicalBytes": "0",
"selfLink": "https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/bigquerydataset_resourceid_${uniqueId}/tables/bigquerytable_resourceid_${uniqueId}",
"tableReference": {
"datasetId": "bigquerydataset_resourceid_${uniqueId}",
"projectId": "${projectId}",
"tableId": "bigquerytable_resourceid_${uniqueId}"
},
"type": "TABLE"
}

---

DELETE https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/${datasetID}/tables/bigquerytable_resourceid_${uniqueId}?alt=json&prettyPrint=false
User-Agent: google-api-go-client/0.5 Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

Expand Down
8 changes: 8 additions & 0 deletions tests/e2e/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func exportResource(h *create.Harness, obj *unstructured.Unstructured, expectati
if !found && expectations.Location {
h.T.Error("expected to find location or region in obj but did not find it")
}

statusSelfLink, _, _ := unstructured.NestedString(obj.Object, "status", "selfLink")

// This list should match https://cloud.google.com/asset-inventory/docs/resource-name-format
gvk := obj.GroupVersionKind()
switch gvk.GroupKind() {
Expand All @@ -55,6 +58,11 @@ func exportResource(h *create.Harness, obj *unstructured.Unstructured, expectati

case schema.GroupKind{Group: "bigquery.cnrm.cloud.google.com", Kind: "BigQueryDataset"}:
exportURI = "//bigquery.googleapis.com/projects/" + projectID + "/datasets/" + resourceID
case schema.GroupKind{Group: "bigquery.cnrm.cloud.google.com", Kind: "BigQueryTable"}:
if statusSelfLink == "" {
h.T.Errorf("status.selfLink not set in BigQueryTable object")
}
exportURI = strings.ReplaceAll(statusSelfLink, "https://bigquery.googleapis.com/bigquery/v2/", "//bigquery.googleapis.com/")

case schema.GroupKind{Group: "discoveryengine.cnrm.cloud.google.com", Kind: "DiscoveryEngineDataStore"}:
exportURI = "//discoveryengine.googleapis.com/projects/{projectID}/locations/{.spec.location}/collections/{.spec.collection}/dataStores/{resourceID}"
Expand Down

0 comments on commit 95b3225

Please sign in to comment.