Skip to content

Commit

Permalink
add remote_function_options to bigquery_routine (#9893)
Browse files Browse the repository at this point in the history
* Add dataGovernanceType and remoteFunctionOptions to bigquery_routine

* add function-sources.zip to biguquery fixtures

* fix resource names in TestAccBigQueryRoutine

* add bigquery routine remote function example
  • Loading branch information
obada-ab authored Feb 23, 2024
1 parent 7beb4e6 commit 8365dfa
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 6 deletions.
50 changes: 44 additions & 6 deletions mmv1/products/bigquery/Routine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import_format:
['projects/{{project}}/datasets/{{dataset_id}}/routines/{{routine_id}}']
examples:
- !ruby/object:Provider::Terraform::Examples
name: 'big_query_routine_basic'
name: 'bigquery_routine_basic'
primary_resource_id: 'sproc'
primary_resource_name: "fmt.Sprintf(\"tf_test_dataset_id%s\",
context[\"random_suffix\"\
Expand All @@ -35,7 +35,7 @@ examples:
dataset_id: 'dataset_id'
routine_id: 'routine_id'
- !ruby/object:Provider::Terraform::Examples
name: 'big_query_routine_json'
name: 'bigquery_routine_json'
primary_resource_id: 'sproc'
primary_resource_name: "fmt.Sprintf(\"tf_test_dataset_id%s\",
context[\"random_suffix\"\
Expand All @@ -44,7 +44,7 @@ examples:
dataset_id: 'dataset_id'
routine_id: 'routine_id'
- !ruby/object:Provider::Terraform::Examples
name: 'big_query_routine_tvf'
name: 'bigquery_routine_tvf'
primary_resource_id: 'sproc'
primary_resource_name: "fmt.Sprintf(\"tf_test_dataset_id%s\",
context[\"random_suffix\"\
Expand All @@ -53,26 +53,34 @@ examples:
dataset_id: 'dataset_id'
routine_id: 'routine_id'
- !ruby/object:Provider::Terraform::Examples
name: 'big_query_routine_pyspark'
name: 'bigquery_routine_pyspark'
primary_resource_id: 'pyspark'
vars:
dataset_id: 'dataset_id'
connection_id: 'connection_id'
routine_id: 'routine_id'
- !ruby/object:Provider::Terraform::Examples
name: 'big_query_routine_pyspark_mainfile'
name: 'bigquery_routine_pyspark_mainfile'
primary_resource_id: 'pyspark_mainfile'
vars:
dataset_id: 'dataset_id'
connection_id: 'connection_id'
routine_id: 'routine_id'
- !ruby/object:Provider::Terraform::Examples
name: 'big_query_routine_spark_jar'
name: 'bigquery_routine_spark_jar'
primary_resource_id: 'spark_jar'
vars:
dataset_id: 'dataset_id'
connection_id: 'connection_id'
routine_id: 'routine_id'
- !ruby/object:Provider::Terraform::Examples
skip_test: true
name: 'bigquery_routine_remote_function'
primary_resource_id: 'remote_function'
vars:
dataset_id: 'dataset_id'
connection_id: 'connection_id'
routine_id: 'routine_id'
properties:
- !ruby/object:Api::Type::NestedObject
name: routineReference
Expand Down Expand Up @@ -283,3 +291,33 @@ properties:
description: |
The fully qualified name of a class in jarUris, for example, com.example.wordcount.
Exactly one of mainClass and main_jar_uri field should be set for Java/Scala language type.
- !ruby/object:Api::Type::NestedObject
name: 'remoteFunctionOptions'
description: Remote function specific options.
properties:
- !ruby/object:Api::Type::String
name: 'endpoint'
description: |
Endpoint of the user-provided remote service, e.g.
`https://us-east1-my_gcf_project.cloudfunctions.net/remote_add`
- !ruby/object:Api::Type::String
name: 'connection'
description: |
Fully qualified name of the user-provided connection object which holds
the authentication information to send requests to the remote service.
Format: "projects/{projectId}/locations/{locationId}/connections/{connectionId}"
- !ruby/object:Api::Type::KeyValuePairs
name: 'userDefinedContext'
description: |
User-defined context as a set of key/value pairs, which will be sent as function
invocation context together with batched arguments in the requests to the remote
service. The total number of bytes of keys and values must be less than 8KB.
An object containing a list of "key": value pairs. Example:
`{ "name": "wrench", "mass": "1.3kg", "count": "3" }`.
default_from_api: true
- !ruby/object:Api::Type::String
name: 'maxBatchingRows'
description: |
Max number of rows in each batch sent to the remote service. If absent or if 0,
BigQuery dynamically decides the number of rows in a batch.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
resource "google_bigquery_dataset" "test" {
dataset_id = "<%= ctx[:vars]['dataset_id'] %>"
}

resource "google_bigquery_connection" "test" {
connection_id = "<%= ctx[:vars]['connection_id'] %>"
location = "US"
cloud_resource { }
}

resource "google_bigquery_routine" "<%= ctx[:primary_resource_id] %>" {
dataset_id = google_bigquery_dataset.test.dataset_id
routine_id = "<%= ctx[:vars]['routine_id'] %>"
routine_type = "SCALAR_FUNCTION"
definition_body = ""

return_type = "{\"typeKind\" : \"STRING\"}"

remote_function_options {
endpoint = "https://us-east1-my_gcf_project.cloudfunctions.net/remote_add"
connection = google_bigquery_connection.test.name
max_batching_rows = "10"
user_defined_context = {
"z": "1.5",
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,171 @@ resource "google_bigquery_routine" "spark_jar" {
}
`, context)
}

func TestAccBigQueryRoutine_bigQueryRoutineRemoteFunction(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
"zip_path": "./test-fixtures/function-source.zip",
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckBigQueryRoutineDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryRoutine_bigQueryRoutineRemoteFunction(context),
},
{
ResourceName: "google_bigquery_routine.remote_function_routine",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccBigQueryRoutine_bigQueryRoutineRemoteFunction_Update(context),
},
{
ResourceName: "google_bigquery_routine.remote_function_routine",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccBigQueryRoutine_bigQueryRoutineRemoteFunction(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_storage_bucket" "default" {
name = "%{random_suffix}-gcf-source"
location = "US"
uniform_bucket_level_access = true
}
resource "google_storage_bucket_object" "object" {
name = "function-source.zip"
bucket = google_storage_bucket.default.name
source = "%{zip_path}"
}
resource "google_cloudfunctions2_function" "default" {
name = "function-v2-0"
location = "us-central1"
description = "a new function"
build_config {
runtime = "nodejs18"
entry_point = "helloHttp"
source {
storage_source {
bucket = google_storage_bucket.default.name
object = google_storage_bucket_object.object.name
}
}
}
service_config {
max_instance_count = 1
available_memory = "256M"
timeout_seconds = 60
}
}
resource "google_bigquery_connection" "test" {
connection_id = "tf_test_connection_id%{random_suffix}"
location = "US"
cloud_resource { }
}
resource "google_bigquery_dataset" "test" {
dataset_id = "tf_test_dataset_id%{random_suffix}"
}
resource "google_bigquery_routine" "remote_function_routine" {
dataset_id = "${google_bigquery_dataset.test.dataset_id}"
routine_id = "tf_test_routine_id%{random_suffix}"
routine_type = "SCALAR_FUNCTION"
definition_body = ""
return_type = "{\"typeKind\" : \"STRING\"}"
remote_function_options {
endpoint = google_cloudfunctions2_function.default.service_config[0].uri
connection = "${google_bigquery_connection.test.name}"
max_batching_rows = "10"
user_defined_context = {
"z": "1.5",
}
}
}
`, context)
}

func testAccBigQueryRoutine_bigQueryRoutineRemoteFunction_Update(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_storage_bucket" "default" {
name = "%{random_suffix}-gcf-source"
location = "US"
uniform_bucket_level_access = true
}
resource "google_storage_bucket_object" "object" {
name = "function-source.zip"
bucket = google_storage_bucket.default.name
source = "%{zip_path}"
}
resource "google_cloudfunctions2_function" "default2" {
name = "function-v2-1"
location = "us-central1"
description = "a new new function"
build_config {
runtime = "nodejs18"
entry_point = "helloHttp"
source {
storage_source {
bucket = google_storage_bucket.default.name
object = google_storage_bucket_object.object.name
}
}
}
service_config {
max_instance_count = 1
available_memory = "256M"
timeout_seconds = 60
}
}
resource "google_bigquery_connection" "test2" {
connection_id = "tf_test_connection2_id%{random_suffix}"
location = "US"
cloud_resource { }
}
resource "google_bigquery_dataset" "test" {
dataset_id = "tf_test_dataset_id%{random_suffix}"
}
resource "google_bigquery_routine" "remote_function_routine" {
dataset_id = "${google_bigquery_dataset.test.dataset_id}"
routine_id = "tf_test_routine_id%{random_suffix}"
routine_type = "SCALAR_FUNCTION"
definition_body = ""
return_type = "{\"typeKind\" : \"STRING\"}"
remote_function_options {
endpoint = google_cloudfunctions2_function.default2.service_config[0].uri
connection = "${google_bigquery_connection.test2.name}"
max_batching_rows = "5"
user_defined_context = {
"z": "1.2",
"w": "test",
}
}
}
`, context)
}
Binary file not shown.

0 comments on commit 8365dfa

Please sign in to comment.