Skip to content

Commit d7b88b7

Browse files
committed
Fix bq factory docs
1 parent 8708f49 commit d7b88b7

File tree

10 files changed

+94
-180
lines changed

10 files changed

+94
-180
lines changed

blueprints/factories/bigquery-factory/README.md

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,18 @@
11
# Google Cloud BQ Factory
22

3-
This module allows creation and management of BigQuery datasets and views as well as tables by defining them in well formatted `yaml` files.
3+
This module allows creation and management of BigQuery datasets tables and views by defining them in well-formatted YAML files. YAML abstraction for BQ can simplify users onboarding and also makes creation of tables easier compared to HCL.
44

5-
Yaml abstraction for BQ can simplify users onboarding and also makes creation of tables easier compared to HCL.
5+
This factory is based on the [BQ dataset module](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/tree/master/modules/bigquery-dataset) which currently only supports tables and views. As soon as external table and materialized view support is added, this factory will be enhanced accordingly.
66

7-
Subfolders distinguish between views and tables and ensures easier navigation for users.
8-
9-
This factory is based on the [BQ dataset module](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/tree/master/modules/bigquery-dataset) which currently only supports tables and views. As soon as external table and materialized view support is added, factory will be enhanced accordingly.
10-
11-
You can create as many files as you like, the code will loop through it and create the required variables in order to execute everything accordingly.
7+
You can create as many files as you like, the code will loop through it and create everything accordingly.
128

139
## Example
1410

1511
### Terraform code
1612

17-
```hcl
18-
module "bq" {
19-
source = "github.com/GoogleCloudPlatform/cloud-foundation-fabric/modules/bigquery-dataset"
20-
21-
for_each = local.output
22-
project_id = var.project_id
23-
id = each.key
24-
views = try(each.value.views, null)
25-
tables = try(each.value.tables, null)
26-
}
27-
# tftest skip
28-
```
29-
30-
### Configuration Structure
31-
13+
In this section we show how to create tables and views from a file structure simlar to the one shown below.
3214
```bash
33-
base_folder
15+
bigquery
3416
3517
├── tables
3618
│ ├── table_a.yaml
@@ -40,32 +22,43 @@ base_folder
4022
│ ├── view_b.yaml
4123
```
4224

43-
## YAML structure and definition formatting
25+
First we create the table definition in `bigquery/tables/countries.yaml`.
4426

45-
### Tables
27+
```yaml
28+
# tftest-file id=table path=bigquery/tables/countries.yaml
29+
dataset: my_dataset
30+
table: countries
31+
deletion_protection: true
32+
labels:
33+
env: prod
34+
schema:
35+
- name: country
36+
type: STRING
37+
- name: population
38+
type: INT64
39+
```
4640
47-
Table definition to be placed in a set of yaml files in the corresponding subfolder. Structure should look as following:
41+
And a view in `bigquery/views/population.yaml`.
4842

4943
```yaml
50-
51-
dataset: # required name of the dataset the table is to be placed in
52-
table: # required descriptive name of the table
53-
schema: # required schema in JSON FORMAT Example: [{name: "test", type: "STRING"},{name: "test2", type: "INT64"}]
54-
labels: # not required, defaults to {}, Example: {"a":"thisislabela","b":"thisislabelb"}
55-
use_legacy_sql: boolean # not required, defaults to false
56-
deletion_protection: boolean # not required, defaults to false
44+
# tftest-file id=view path=bigquery/views/population.yaml
45+
dataset: my_dataset
46+
view: department
47+
query: SELECT SUM(population) from my_dataset.countries
48+
labels:
49+
env: prod
5750
```
5851

59-
### Views
60-
View definition to be placed in a set of yaml files in the corresponding subfolder. Structure should look as following:
52+
With this file structure, we can use the factory as follows:
6153

62-
```yaml
63-
dataset: # required, name of the dataset the view is to be placed in
64-
view: # required, descriptive name of the view
65-
query: # required, SQL Query for the view in quotes
66-
labels: # not required, defaults to {}, Example: {"a":"thisislabela","b":"thisislabelb"}
67-
use_legacy_sql: bool # not required, defaults to false
68-
deletion_protection: bool # not required, defaults to false
54+
```hcl
55+
module "bq" {
56+
source = "./fabric/blueprints/factories/bigquery-factory"
57+
project_id = var.project_id
58+
tables_path = "bigquery/tables"
59+
views_path = "bigquery/views"
60+
}
61+
# tftest modules=2 resources=3 files=table,view inventory=simple.yaml
6962
```
7063
<!-- BEGIN TFDOC -->
7164

@@ -74,8 +67,8 @@ deletion_protection: bool # not required, defaults to false
7467
| name | description | type | required | default |
7568
|---|---|:---:|:---:|:---:|
7669
| [project_id](variables.tf#L17) | Project ID. | <code>string</code> | ✓ | |
77-
| [tables_dir](variables.tf#L22) | Relative path for the folder storing table data. | <code>string</code> | ✓ | |
78-
| [views_dir](variables.tf#L27) | Relative path for the folder storing view data. | <code>string</code> | ✓ | |
70+
| [tables_path](variables.tf#L22) | Relative path for the folder storing table data. | <code>string</code> | ✓ | |
71+
| [views_path](variables.tf#L27) | Relative path for the folder storing view data. | <code>string</code> | ✓ | |
7972

8073
<!-- END TFDOC -->
8174
## TODO

blueprints/factories/bigquery-factory/main.tf

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022 Google LLC
2+
* Copyright 2023 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,17 +16,22 @@
1616

1717
locals {
1818
views = {
19-
for f in fileset("${var.views_dir}", "**/*.yaml") :
20-
trimsuffix(f, ".yaml") => yamldecode(file("${var.views_dir}/${f}"))
19+
for f in fileset(var.views_path, "**/*.yaml") :
20+
trimsuffix(f, ".yaml") => yamldecode(file("${var.views_path}/${f}"))
2121
}
2222

2323
tables = {
24-
for f in fileset("${var.tables_dir}", "**/*.yaml") :
25-
trimsuffix(f, ".yaml") => yamldecode(file("${var.tables_dir}/${f}"))
24+
for f in fileset(var.tables_path, "**/*.yaml") :
25+
trimsuffix(f, ".yaml") => yamldecode(file("${var.tables_path}/${f}"))
2626
}
2727

28-
output = {
29-
for dataset in distinct([for v in values(merge(local.views, local.tables)) : v.dataset]) :
28+
all_datasets = distinct(concat(
29+
[for x in values(local.tables) : x.dataset],
30+
[for x in values(local.views) : x.dataset]
31+
))
32+
33+
datasets = {
34+
for dataset in local.all_datasets :
3035
dataset => {
3136
"views" = {
3237
for k, v in local.views :
@@ -57,9 +62,8 @@ locals {
5762
}
5863

5964
module "bq" {
60-
source = "../../../modules/bigquery-dataset"
61-
62-
for_each = local.output
65+
source = "../../../modules/bigquery-dataset"
66+
for_each = local.datasets
6367
project_id = var.project_id
6468
id = each.key
6569
views = try(each.value.views, null)

blueprints/factories/bigquery-factory/variables.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022 Google LLC
2+
* Copyright 2023 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,12 +19,12 @@ variable "project_id" {
1919
type = string
2020
}
2121

22-
variable "tables_dir" {
22+
variable "tables_path" {
2323
description = "Relative path for the folder storing table data."
2424
type = string
2525
}
2626

27-
variable "views_dir" {
27+
variable "views_path" {
2828
description = "Relative path for the folder storing view data."
2929
type = string
3030
}

tests/blueprints/factories/bigquery_factory/__init__.py

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
values:
16+
module.bq.module.bq["my_dataset"].google_bigquery_dataset.default:
17+
dataset_id: my_dataset
18+
project: project-id
19+
module.bq.module.bq["my_dataset"].google_bigquery_table.default["countries"]:
20+
dataset_id: my_dataset
21+
friendly_name: countries
22+
labels:
23+
env: prod
24+
project: project-id
25+
schema: '[{"name":"country","type":"STRING"},{"name":"population","type":"INT64"}]'
26+
table_id: countries
27+
module.bq.module.bq["my_dataset"].google_bigquery_table.views["department"]:
28+
dataset_id: my_dataset
29+
friendly_name: department
30+
labels:
31+
env: prod
32+
project: project-id
33+
table_id: department
34+
view:
35+
- query: SELECT SUM(population) from my_dataset.countries
36+
use_legacy_sql: false
37+
38+
counts:
39+
google_bigquery_dataset: 1
40+
google_bigquery_table: 2

tests/blueprints/factories/bigquery_factory/fixture/main.tf

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/blueprints/factories/bigquery_factory/fixture/tables/table_a.yaml

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/blueprints/factories/bigquery_factory/fixture/variables.tf

Lines changed: 0 additions & 34 deletions
This file was deleted.

tests/blueprints/factories/bigquery_factory/fixture/views/view_a.yaml

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/blueprints/factories/bigquery_factory/test_plan.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)