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 17, 2019
1 parent 5be1b29 commit 0de2a73
Show file tree
Hide file tree
Showing 18 changed files with 127 additions and 76 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
1 change: 1 addition & 0 deletions examples/basic_bq/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ tables = [
time_partitioning = null,
expiration_time = 2524604400000, # 2050/01/01
clustering = [],
view = null
labels = {
env = "devops"
billable = "true"
Expand Down
6 changes: 5 additions & 1 deletion examples/basic_bq/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ variable "tables" {
require_partition_filter = bool,
}),
expiration_time = string,
labels = map(string),
view = object({
query = string,
use_legacy_sql = bool,
}),
labels = map(string),
}))
}
1 change: 1 addition & 0 deletions examples/multiple_tables/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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 -->

Expand Down
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."
}
2 changes: 2 additions & 0 deletions examples/multiple_tables/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tables = [
billable = "true"
owner = "joedoe"
},
view = null
},
{
table_id = "bar",
Expand All @@ -34,5 +35,6 @@ tables = [
billable = "true"
owner = "joedoe"
},
view = null
}
]
6 changes: 5 additions & 1 deletion examples/multiple_tables/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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."
}
55 changes: 52 additions & 3 deletions test/fixtures/full/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,57 @@

module "example" {
source = "../../../examples/multiple_tables"
default_table_expiration_ms = var.default_table_expiration_ms
default_table_expiration_ms = 3600000
project_id = var.project_id
tables = var.tables
dataset_labels = var.dataset_labels
tables = [
{
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
},
{
table_id = "test_view",
schema = null,
time_partitioning = null,
expiration_time = 2524604400000, # 2050/01/01
clustering = [],
labels = {},
view = {
query = "select visitNumber from `${var.project_id}.foo.foo` limit 100"
use_legacy_sql = false
}

}
]
dataset_labels = {
env = "dev"
billable = "true"
owner = "janesmith"
}
}
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.

27 changes: 0 additions & 27 deletions test/fixtures/full/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,6 @@
* limitations under the License.
*/

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

variable "project_id" {
description = "Project where the dataset and table are created"
}

variable "dataset_labels" {
description = "Key value pairs in a map for dataset labels"
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 = []
type = list(object({
table_id = string,
schema = string,
clustering = list(string),
time_partitioning = object({
expiration_ms = string,
field = string,
type = string,
require_partition_filter = bool,
}),
expiration_time = string,
labels = map(string),
}))
}
8 changes: 8 additions & 0 deletions test/integration/full/controls/big_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
project_id = attribute('bigquery_dataset')['project']
dataset_name = attribute('bigquery_dataset')['friendly_name']
tables = attribute('bigquery_tables')
views = attribute('bigquery_views')

describe google_bigquery_dataset(project: "#{project_id}", name: "#{dataset_name}") do
it { should exist }
Expand All @@ -39,3 +40,10 @@
its('time_partitioning.type') { should be nil }
its('clustering') { should be nil }
end

describe google_bigquery_table(project: "#{project_id}", dataset: "#{dataset_name}", name: "#{views["test_view"]["friendly_name"]}") do
it { should exist }
its('friendly_name') { should eq "#{views["test_view"]["friendly_name"]}" }
its('type') { should eq "VIEW" }
its('view.use_legacy_sql') { should be false }
end
3 changes: 3 additions & 0 deletions test/integration/full/inspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ attributes:
- name: bigquery_tables
required: true
type: hash
- name: bigquery_views
required: true
type: hash
4 changes: 2 additions & 2 deletions test/setup/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ terraform {
}

provider "google" {
version = "~> 2.13.0"
version = "~> 2.12.0"
}

provider "google-beta" {
version = "~> 2.13.0"
version = "~> 2.12.0"
}
6 changes: 5 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ variable "tables" {
require_partition_filter = bool,
}),
expiration_time = string,
labels = map(string),
view = object({
query = string,
use_legacy_sql = bool,
}),
labels = map(string),
}))
}
3 changes: 0 additions & 3 deletions versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@

terraform {
required_version = "~> 0.12.6"
required_providers {
google = "~> 2.15"
}
}

0 comments on commit 0de2a73

Please sign in to comment.