Skip to content

Commit 177437e

Browse files
authored
Add new fields supporting BQ Python UDF to bigquery_routine resource. (#15786)
1 parent d0cef3c commit 177437e

File tree

3 files changed

+225
-0
lines changed

3 files changed

+225
-0
lines changed

mmv1/products/bigquery/Routine.yaml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ examples:
7979
connection_id: 'connection_id'
8080
routine_id: 'routine_id'
8181
exclude_test: true
82+
- name: 'bigquery_routine_python_function'
83+
primary_resource_id: 'python_function'
84+
vars:
85+
dataset_id: 'dataset_id'
86+
routine_id: 'routine_id'
87+
min_version: beta
8288
parameters:
8389
properties:
8490
- name: 'routineReference'
@@ -331,3 +337,60 @@ properties:
331337
description: |
332338
Max number of rows in each batch sent to the remote service. If absent or if 0,
333339
BigQuery dynamically decides the number of rows in a batch.
340+
- name: 'pythonOptions'
341+
type: NestedObject
342+
description: |
343+
Options for a user-defined Python function.
344+
min_version: beta
345+
properties:
346+
- name: 'entryPoint'
347+
type: String
348+
description: |
349+
The name of the function defined in Python code as the entry point when the
350+
Python UDF is invoked.
351+
required: true
352+
- name: 'packages'
353+
type: Array
354+
description: |
355+
A list of Python package names along with versions to be installed.
356+
Example: ["pandas>=2.1", "google-cloud-translate==3.11"]. For more
357+
information, see [Use third-party
358+
packages](https://cloud.google.com/bigquery/docs/user-defined-functions-python#third-party-packages).
359+
item_type:
360+
type: String
361+
- name: 'externalRuntimeOptions'
362+
type: NestedObject
363+
description: |
364+
Options for the runtime of the external system.
365+
This field is only applicable for Python UDFs.
366+
min_version: beta
367+
properties:
368+
- name: 'containerMemory'
369+
type: String
370+
description: |
371+
Amount of memory provisioned for a Python UDF container instance. Format:
372+
{number}{unit} where unit is one of "M", "G", "Mi" and "Gi" (e.g. 1G,
373+
512Mi). If not specified, the default value is 512Mi. For more information,
374+
see [Configure container limits for Python
375+
UDFs](https://cloud.google.com/bigquery/docs/user-defined-functions-python#configure-container-limits)
376+
- name: 'containerCpu'
377+
type: Double
378+
description: |
379+
Amount of CPU provisioned for a Python UDF container instance. For more
380+
information, see [Configure container limits for Python
381+
UDFs](https://cloud.google.com/bigquery/docs/user-defined-functions-python#configure-container-limits)
382+
- name: 'runtimeConnection'
383+
type: String
384+
description: |
385+
Fully qualified name of the connection whose service account will be used
386+
to execute the code in the container. Format:
387+
`"projects/{project_id}/locations/{location_id}/connections/{connection_id}"`
388+
- name: 'maxBatchingRows'
389+
type: String
390+
description: |
391+
Maximum number of rows in each batch sent to the external runtime. If
392+
absent or if 0, BigQuery dynamically decides the number of rows in a batch.
393+
- name: 'runtimeVersion'
394+
type: String
395+
description: |
396+
Language runtime version. Example: `python-3.11`.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
resource "google_bigquery_dataset" "test" {
2+
provider = google-beta
3+
dataset_id = "{{index $.Vars "dataset_id"}}"
4+
}
5+
6+
resource "google_bigquery_routine" "{{$.PrimaryResourceId}}" {
7+
provider = google-beta
8+
dataset_id = google_bigquery_dataset.test.dataset_id
9+
routine_id = "{{index $.Vars "routine_id"}}"
10+
routine_type = "SCALAR_FUNCTION"
11+
language = "PYTHON"
12+
arguments {
13+
name = "x"
14+
data_type = "{\"typeKind\" : \"FLOAT64\"}"
15+
}
16+
arguments {
17+
name = "y"
18+
data_type = "{\"typeKind\" : \"FLOAT64\"}"
19+
}
20+
definition_body = <<-EOS
21+
def multiply(x, y):
22+
return x * y
23+
EOS
24+
return_type = "{\"typeKind\" : \"FLOAT64\"}"
25+
python_options {
26+
entry_point = "multiply"
27+
}
28+
external_runtime_options {
29+
container_memory = "512Mi"
30+
container_cpu = 0.5
31+
runtime_version = "python-3.11"
32+
}
33+
34+
}

mmv1/third_party/terraform/services/bigquery/resource_bigquery_routine_test.go renamed to mmv1/third_party/terraform/services/bigquery/resource_bigquery_routine_test.go.tmpl

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"testing"
66

77
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
8+
{{- if ne $.TargetVersionName "ga" }}
9+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
10+
{{- end }}
811
"github.com/hashicorp/terraform-provider-google/google/acctest"
912
)
1013

@@ -343,3 +346,128 @@ resource "google_bigquery_routine" "remote_function_routine" {
343346
}
344347
`, context)
345348
}
349+
350+
{{if ne $.TargetVersionName "ga" }}
351+
func TestAccBigQueryRoutine_bigqueryRoutinePythonFunction_update(t *testing.T) {
352+
t.Parallel()
353+
354+
context := map[string]interface{}{
355+
"random_suffix": acctest.RandString(t, 10),
356+
}
357+
358+
acctest.VcrTest(t, resource.TestCase{
359+
PreCheck: func() { acctest.AccTestPreCheck(t) },
360+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
361+
CheckDestroy: testAccCheckBigQueryRoutineDestroyProducer(t),
362+
Steps: []resource.TestStep{
363+
{
364+
Config: testAccBigQueryRoutine_bigqueryRoutinePythonFunction_basic(context),
365+
},
366+
{
367+
ResourceName: "google_bigquery_routine.python_function",
368+
ImportState: true,
369+
ImportStateVerify: true,
370+
},
371+
{
372+
Config: testAccBigQueryRoutine_bigqueryRoutinePythonFunction_update(context),
373+
ConfigPlanChecks: resource.ConfigPlanChecks{
374+
PreApply: []plancheck.PlanCheck{
375+
plancheck.ExpectResourceAction("google_bigquery_routine.python_function", plancheck.ResourceActionUpdate),
376+
},
377+
},
378+
},
379+
{
380+
ResourceName: "google_bigquery_routine.python_function",
381+
ImportState: true,
382+
ImportStateVerify: true,
383+
},
384+
},
385+
})
386+
}
387+
388+
func testAccBigQueryRoutine_bigqueryRoutinePythonFunction_basic(context map[string]interface{}) string {
389+
return acctest.Nprintf(`
390+
resource "google_bigquery_dataset" "test" {
391+
provider = google-beta
392+
dataset_id = "tf_test_dataset_id%{random_suffix}"
393+
}
394+
395+
resource "google_bigquery_routine" "python_function" {
396+
provider = google-beta
397+
dataset_id = google_bigquery_dataset.test.dataset_id
398+
routine_id = "tf_test_routine_id%{random_suffix}"
399+
routine_type = "SCALAR_FUNCTION"
400+
language = "PYTHON"
401+
arguments {
402+
name = "x"
403+
data_type = "{\"typeKind\" : \"FLOAT64\"}"
404+
}
405+
arguments {
406+
name = "y"
407+
data_type = "{\"typeKind\" : \"FLOAT64\"}"
408+
}
409+
definition_body = <<-EOS
410+
def multiply(x, y):
411+
return x * y
412+
EOS
413+
return_type = "{\"typeKind\" : \"FLOAT64\"}"
414+
python_options {
415+
entry_point = "multiply"
416+
}
417+
external_runtime_options {
418+
runtime_version = "python-3.11"
419+
}
420+
421+
}
422+
`, context)
423+
}
424+
425+
func testAccBigQueryRoutine_bigqueryRoutinePythonFunction_update(context map[string]interface{}) string {
426+
return acctest.Nprintf(`
427+
resource "google_bigquery_dataset" "test" {
428+
provider = google-beta
429+
dataset_id = "tf_test_dataset_id%{random_suffix}"
430+
}
431+
432+
resource "google_bigquery_connection" "test" {
433+
provider = google-beta
434+
connection_id = "tf_test_connection_id%{random_suffix}"
435+
location = "US"
436+
cloud_resource {}
437+
}
438+
439+
resource "google_bigquery_routine" "python_function" {
440+
provider = google-beta
441+
dataset_id = google_bigquery_dataset.test.dataset_id
442+
routine_id = "tf_test_routine_id%{random_suffix}"
443+
routine_type = "SCALAR_FUNCTION"
444+
language = "PYTHON"
445+
arguments {
446+
name = "x"
447+
data_type = "{\"typeKind\" : \"FLOAT64\"}"
448+
}
449+
arguments {
450+
name = "y"
451+
data_type = "{\"typeKind\" : \"FLOAT64\"}"
452+
}
453+
definition_body = <<-EOS
454+
def multiply(x, y):
455+
return x * y
456+
EOS
457+
return_type = "{\"typeKind\" : \"FLOAT64\"}"
458+
python_options {
459+
entry_point = "multiply"
460+
packages = ["requests"]
461+
}
462+
external_runtime_options {
463+
container_memory = "512Mi"
464+
container_cpu = 0.5
465+
runtime_connection = "${google_bigquery_connection.test.name}"
466+
runtime_version = "python-3.11"
467+
max_batching_rows = "10"
468+
}
469+
470+
}
471+
`, context)
472+
}
473+
{{- end }}

0 commit comments

Comments
 (0)