Skip to content

Commit

Permalink
Added view options for BigQuery dataset table
Browse files Browse the repository at this point in the history
  • Loading branch information
kopachevsky committed Dec 18, 2019
1 parent 5be1b29 commit aa61b35
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 123 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ This module provisions a dataset and a list of tables with associated JSON schem
|------|-------------|
| bigquery\_dataset | Bigquery dataset resource. |
| bigquery\_tables | Map of bigquery table resources being provisioned. |
| bigquery\_views | Map of bigquery view resources being provisioned. |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

Expand Down
9 changes: 3 additions & 6 deletions examples/basic_bq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ The basic_bq example uses the root terraform-google-bigquery module to deploy a

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| dataset\_labels | A mapping of labels to assign to the table. | map(string) | n/a | yes |
| dataset\_labels | A mapping of labels to assign to the table. | map(string) | `<map>` | no |
| default\_table\_expiration\_ms | Default TTL of tables using the dataset in MS. | string | `"null"` | no |
| project\_id | Project where the dataset and table are created. | string | n/a | yes |
| project\_id | Project where the dataset and table are created. | string | `"example-project"` | no |
| tables | A list of maps that includes table_id, schema, clustering, time_partitioning, expiration_time, labels in each element. | object | `<list>` | no |

## Outputs
Expand All @@ -20,10 +20,7 @@ The basic_bq example uses the root terraform-google-bigquery module to deploy a

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

## Setup
Update the contents of `terraform.tfvars` to match your test environment.

## Run example
`terraform init`
`terraform plan`
`terraform apply -var-file terraform.tfvars`
`terraform apply`
22 changes: 0 additions & 22 deletions examples/basic_bq/terraform.tfvars

This file was deleted.

30 changes: 27 additions & 3 deletions examples/basic_bq/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,36 @@ variable "default_table_expiration_ms" {

variable "project_id" {
description = "Project where the dataset and table are created."
default = "example-project"
}

variable "dataset_labels" {
description = "A mapping of labels to assign to the table."
type = map(string)
default = {
env = "dev"
billable = "true"
owner = "janesmith"
}
type = map(string)
}

variable "tables" {
description = "A list of maps that includes table_id, schema, clustering, time_partitioning, expiration_time, labels in each element."
default = []
default = [
{
table_id = "bar",
schema = "sample_bq_schema.json",
time_partitioning = null,
expiration_time = 2524604400000, # 2050/01/01
clustering = [],
view = null
labels = {
env = "devops"
billable = "true"
owner = "joedoe"
},
}
]
type = list(object({
table_id = string,
schema = string,
Expand All @@ -42,6 +62,10 @@ variable "tables" {
require_partition_filter = bool,
}),
expiration_time = string,
labels = map(string),
view = object({
query = string,
use_legacy_sql = bool,
}),
labels = map(string),
}))
}
12 changes: 5 additions & 7 deletions examples/multiple_tables/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ This example is a good reference to understand and test the module usage.

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| dataset\_labels | A mapping of labels to assign to the table. | map(string) | n/a | yes |
| default\_table\_expiration\_ms | Default TTL of tables using the dataset in MS. | string | `"null"` | no |
| project\_id | Project where the dataset and table are created. | string | n/a | yes |
| dataset\_labels | A mapping of labels to assign to the table. | map(string) | `<map>` | no |
| default\_table\_expiration\_ms | Default TTL of tables using the dataset in MS. | string | `"3600000"` | no |
| project\_id | Project where the dataset and table are created. | string | `"example-project"` | no |
| tables | A list of maps that includes table_id, schema, clustering, time_partitioning, expiration_time, labels in each element. | object | `<list>` | no |

## Outputs
Expand All @@ -19,13 +19,11 @@ This example is a good reference to understand and test the module usage.
|------|-------------|
| bigquery\_dataset | Bigquery dataset resource. |
| bigquery\_tables | Map of bigquery table resources being provisioned. |
| bigquery\_views | Map of bigquery view resources being provisioned. |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

## Setup
Update the contents of `terraform.tfvars` to match your test environment.

## Run example
`terraform init`
`terraform plan`
`terraform apply -var-file terraform.tfvars`
`terraform apply`
5 changes: 5 additions & 0 deletions examples/multiple_tables/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ output "bigquery_tables" {
value = module.bigquery.bigquery_tables
description = "Map of bigquery table resources being provisioned."
}

output "bigquery_views" {
value = module.bigquery.bigquery_views
description = "Map of bigquery view resources being provisioned."
}
38 changes: 0 additions & 38 deletions examples/multiple_tables/terraform.tfvars

This file was deleted.

50 changes: 46 additions & 4 deletions examples/multiple_tables/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,59 @@

variable "default_table_expiration_ms" {
description = "Default TTL of tables using the dataset in MS."
default = null
default = 3600000
}

variable "project_id" {
description = "Project where the dataset and table are created."
default = "example-project"
}

variable "dataset_labels" {
description = "A mapping of labels to assign to the table."
type = map(string)
default = {
env = "dev"
billable = "true"
owner = "janesmith"
}
type = map(string)
}

variable "tables" {
description = "A list of maps that includes table_id, schema, clustering, time_partitioning, expiration_time, labels in each element."
default = []
default = [
{
table_id = "foo",
schema = "sample_bq_schema.json",
time_partitioning = {
type = "DAY",
field = null,
require_partition_filter = false,
expiration_ms = null,
},
expiration_time = null,
clustering = ["fullVisitorId", "visitId"],
labels = {
env = "dev"
billable = "true"
owner = "joedoe"
},
view = null
},
{
table_id = "bar",
schema = "sample_bq_schema.json",
time_partitioning = null,
expiration_time = 2524604400000, # 2050/01/01
clustering = [],
labels = {
env = "devops"
billable = "true"
owner = "joedoe"
},
view = null
}
]
type = list(object({
table_id = string,
schema = string,
Expand All @@ -42,6 +80,10 @@ variable "tables" {
require_partition_filter = bool,
}),
expiration_time = string,
labels = map(string),
view = object({
query = string,
use_legacy_sql = bool,
}),
labels = map(string),
}))
}
28 changes: 27 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/

locals {
tables = { for table in var.tables : table["table_id"] => table }
tables = { for table in var.tables : table["table_id"] => table if lookup(table, "view", null) == null }
views = { for table in var.tables : table["table_id"] => table if lookup(table, "view", null) != null }
}

resource "google_bigquery_dataset" "main" {
Expand Down Expand Up @@ -49,3 +50,28 @@ resource "google_bigquery_table" "main" {
}
}
}

resource "google_bigquery_table" "view" {
for_each = local.views
dataset_id = google_bigquery_dataset.main.dataset_id
friendly_name = each.key
table_id = each.key
labels = each.value["labels"]
clustering = each.value["clustering"]
expiration_time = each.value["expiration_time"]
project = var.project_id
depends_on = [google_bigquery_table.main]
view {
query = each.value["view"]["query"]
use_legacy_sql = each.value["view"]["use_legacy_sql"]
}
dynamic "time_partitioning" {
for_each = each.value["time_partitioning"] != null ? [each.value["time_partitioning"]] : []
content {
type = time_partitioning.value["type"]
expiration_ms = time_partitioning.value["expiration_ms"]
field = time_partitioning.value["field"]
require_partition_filter = time_partitioning.value["require_partition_filter"]
}
}
}
5 changes: 5 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ output "bigquery_tables" {
value = google_bigquery_table.main
description = "Map of bigquery table resources being provisioned."
}

output "bigquery_views" {
value = google_bigquery_table.view
description = "Map of bigquery view resources being provisioned."
}
5 changes: 5 additions & 0 deletions test/fixtures/full/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ output "bigquery_tables" {
value = module.example.bigquery_tables
description = "Map of bigquery table resources being provisioned."
}

output "bigquery_views" {
value = module.example.bigquery_views
description = "Map of bigquery view resources being provisioned."
}
37 changes: 0 additions & 37 deletions test/fixtures/full/terraform.tfvars

This file was deleted.

0 comments on commit aa61b35

Please sign in to comment.