Let fields be sent raw in table schema definition

As per https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#TableFieldSchema
the `fields` attribute can recursively define more fields if the `type`
attribute is RECORD. There is no way to define recursive argument spec
in ansible modules, but if sent as `raw`:

> The raw type, performs no type validation or type casting, and maintains the type of the passed value.

(from https://docs.ansible.com/ansible/latest/dev_guide/developing_program_flow_modules.html#argument-spec)

Which works for what we're trying to accomplish here.

Also added integration test for this change
This commit is contained in:
Jorge Gallegos 2025-06-27 12:12:06 -06:00
commit 63d7025012
No known key found for this signature in database
3 changed files with 101 additions and 4 deletions

View file

@ -181,7 +181,7 @@ options:
fields:
description:
- Describes the nested schema fields if the type property is set to RECORD.
elements: str
elements: raw
required: false
type: list
mode:
@ -296,7 +296,7 @@ options:
description:
- Describes the nested schema fields if the type property is set to
RECORD .
elements: str
elements: raw
required: false
type: list
mode:
@ -1045,7 +1045,7 @@ def main():
elements='dict',
options=dict(
description=dict(type='str'),
fields=dict(type='list', elements='str'),
fields=dict(type='list', elements='raw'),
mode=dict(type='str'),
name=dict(type='str'),
type=dict(type='str'),
@ -1072,7 +1072,7 @@ def main():
elements='dict',
options=dict(
description=dict(type='str'),
fields=dict(type='list', elements='str'),
fields=dict(type='list', elements='raw'),
mode=dict(type='str'),
name=dict(type='str'),
type=dict(type='str'),

View file

@ -1,3 +1,8 @@
---
- name: Generated tests
ansible.builtin.include_tasks: autogen.yml
- name: Run nested test cases
ansible.builtin.include_tasks: nested.yml
vars:
dataset_name: "{{ resource_name | replace('-', '_') }}_nested"

View file

@ -0,0 +1,92 @@
---
- name: Run testcases
block:
- name: create dataset
google.cloud.gcp_bigquery_dataset:
name: "{{ dataset_name }}"
dataset_reference:
dataset_id: "{{ dataset_name }}"
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
state: present
register: _dataset
- name: Create table with defined fields
google.cloud.gcp_bigquery_table:
name: "{{ resource_name }}-fields"
dataset: "{{ dataset_name }}"
state: present
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
table_reference:
dataset_id: "{{ dataset_name }}"
project_id: "{{ gcp_project }}"
table_id: "{{ resource_name }}-fields"
schema:
fields:
- name: id
description: An Integer field
type: INTEGER
- name: name
description: A String field
type: STRING
- name: Create table with nested fields
google.cloud.gcp_bigquery_table:
name: "{{ resource_name }}-nested"
dataset: "{{ dataset_name }}"
state: present
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
table_reference:
dataset_id: "{{ dataset_name }}"
project_id: "{{ gcp_project }}"
table_id: "{{ resource_name }}-nested"
schema:
fields:
- name: id
description: An Integer field
type: INTEGER
- name: def
description: A Record field
type: RECORD
fields:
- name: id
description: A nested Integer field
type: INTEGER
- name: subdef
description: A nested Record field
type: RECORD
fields:
- name: id
description: A nested-nested Integer field
type: INTEGER
- name: desc
description: A nested-nested String field
type: STRING
always:
- name: Remove tables
google.cloud.gcp_bigquery_table:
name: "{{ resource_name }}-{{ item }}"
dataset: "{{ dataset_name }}"
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
state: absent
loop:
- fields
- nested
- name: Remove dataset
google.cloud.gcp_bigquery_dataset:
name: "{{ dataset_name }}"
dataset_reference:
dataset_id: "{{ dataset_name }}"
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
state: absent