From 223092bfa160f97d0554e5a2d39aba13fb08f9f1 Mon Sep 17 00:00:00 2001 From: Israel Herraiz Date: Thu, 22 Dec 2022 15:42:06 +0100 Subject: [PATCH] A table can have more than one column family. This commit fixes #1064 by allowing to add more than one column family. Split keys are also now optional, and there is no possibility to set defaults for all tables (since keys and column families are related to the data and schema for each table, it is difficult that several tables share the same values). Also, declaring a table with no split keys nor column families requires initializing the table to an empty map, instead of using null. --- modules/bigtable-instance/README.md | 32 ++++++++++++++++++++++---- modules/bigtable-instance/main.tf | 9 +++----- modules/bigtable-instance/variables.tf | 19 +++------------ 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/modules/bigtable-instance/README.md b/modules/bigtable-instance/README.md index 10c782ae8f..076526c7b1 100644 --- a/modules/bigtable-instance/README.md +++ b/modules/bigtable-instance/README.md @@ -22,10 +22,9 @@ module "bigtable-instance" { cluster_id = "instance" zone = "europe-west1-b" tables = { - test1 = null, + test1 = {}, test2 = { split_keys = ["a", "b", "c"] - column_family = null } } iam = { @@ -35,6 +34,30 @@ module "bigtable-instance" { # tftest modules=1 resources=4 ``` +### Instance with tables and column families + +```hcl + +module "bigtable-instance" { + source = "./fabric/modules/bigtable-instance" + project_id = "my-project" + name = "instance" + cluster_id = "instance" + zone = "europe-west1-b" + tables = { + test1 = {}, + test2 = { + split_keys = ["a", "b", "c"] + column_families = ["cf1", "cf2", "cf3"] + } + test3 = { + column_families = ["cf1"] + } + } +} +# tftest modules=1 resources=4 +``` + ### Instance with static number of nodes If you are not using autoscaling settings, you must set a specific number of nodes with the variable `num_nodes`. @@ -101,7 +124,8 @@ module "bigtable-instance" { |---|---|:---:|:---:|:---:| | [name](variables.tf#L56) | The name of the Cloud Bigtable instance. | string | ✓ | | | [project_id](variables.tf#L67) | Id of the project where datasets will be created. | string | ✓ | | -| [zone](variables.tf#L99) | The zone to create the Cloud Bigtable cluster in. | string | ✓ | | +| [tables](variables.tf#L78) | Tables to be created in the BigTable instance. | map(object({…})) | ✓ | | +| [zone](variables.tf#L86) | The zone to create the Cloud Bigtable cluster in. | string | ✓ | | | [autoscaling_config](variables.tf#L17) | Settings for autoscaling of the instance. If you set this variable, the variable num_nodes is ignored. | object({…}) | | null | | [cluster_id](variables.tf#L28) | The ID of the Cloud Bigtable cluster. | string | | "europe-west1" | | [deletion_protection](variables.tf#L34) | Whether or not to allow Terraform to destroy the instance. Unless this field is set to false in Terraform state, a terraform destroy or terraform apply that would delete the instance will fail. | | | true | @@ -110,8 +134,6 @@ module "bigtable-instance" { | [instance_type](variables.tf#L50) | (deprecated) The instance type to create. One of 'DEVELOPMENT' or 'PRODUCTION'. | string | | null | | [num_nodes](variables.tf#L61) | The number of nodes in your Cloud Bigtable cluster. This value is ignored if you are using autoscaling. | number | | 1 | | [storage_type](variables.tf#L72) | The storage type to use. | string | | "SSD" | -| [table_options_defaults](variables.tf#L78) | Default option of tables created in the BigTable instance. | object({…}) | | {…} | -| [tables](variables.tf#L90) | Tables to be created in the BigTable instance, options can be null. | map(object({…})) | | {} | ## Outputs diff --git a/modules/bigtable-instance/main.tf b/modules/bigtable-instance/main.tf index b5764c3493..ce56c15f76 100644 --- a/modules/bigtable-instance/main.tf +++ b/modules/bigtable-instance/main.tf @@ -15,9 +15,6 @@ */ locals { - tables = { - for k, v in var.tables : k => v != null ? v : var.table_options_defaults - } num_nodes = var.autoscaling_config == null ? var.num_nodes : null } @@ -54,17 +51,17 @@ resource "google_bigtable_instance_iam_binding" "default" { } resource "google_bigtable_table" "default" { - for_each = local.tables + for_each = var.tables project = var.project_id instance_name = google_bigtable_instance.default.name name = each.key split_keys = each.value.split_keys dynamic "column_family" { - for_each = each.value.column_family != null ? [""] : [] + for_each = each.value.column_families content { - family = each.value.column_family + family = column_family.value } } } diff --git a/modules/bigtable-instance/variables.tf b/modules/bigtable-instance/variables.tf index 84a6013b60..b8ee42b1bd 100644 --- a/modules/bigtable-instance/variables.tf +++ b/modules/bigtable-instance/variables.tf @@ -75,25 +75,12 @@ variable "storage_type" { default = "SSD" } -variable "table_options_defaults" { - description = "Default option of tables created in the BigTable instance." - type = object({ - split_keys = list(string) - column_family = string - }) - default = { - split_keys = [] - column_family = null - } -} - variable "tables" { - description = "Tables to be created in the BigTable instance, options can be null." + description = "Tables to be created in the BigTable instance." type = map(object({ - split_keys = list(string) - column_family = string + split_keys = optional(list(string), []) + column_families = optional(list(string), []) })) - default = {} } variable "zone" {