Skip to content

Commit

Permalink
A table can have more than one column family.
Browse files Browse the repository at this point in the history
This commit fixes GoogleCloudPlatform#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.
  • Loading branch information
iht committed Dec 22, 2022
1 parent ebc1104 commit dd0a104
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
32 changes: 27 additions & 5 deletions modules/bigtable-instance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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`.
Expand Down Expand Up @@ -101,7 +124,7 @@ module "bigtable-instance" {
|---|---|:---:|:---:|:---:|
| [name](variables.tf#L56) | The name of the Cloud Bigtable instance. | <code>string</code> || |
| [project_id](variables.tf#L67) | Id of the project where datasets will be created. | <code>string</code> || |
| [zone](variables.tf#L99) | The zone to create the Cloud Bigtable cluster in. | <code>string</code> || |
| [zone](variables.tf#L87) | The zone to create the Cloud Bigtable cluster in. | <code>string</code> || |
| [autoscaling_config](variables.tf#L17) | Settings for autoscaling of the instance. If you set this variable, the variable num_nodes is ignored. | <code title="object&#40;&#123;&#10; min_nodes &#61; number&#10; max_nodes &#61; number&#10; cpu_target &#61; number,&#10; storage_target &#61; optional&#40;number, null&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [cluster_id](variables.tf#L28) | The ID of the Cloud Bigtable cluster. | <code>string</code> | | <code>&#34;europe-west1&#34;</code> |
| [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. | <code></code> | | <code>true</code> |
Expand All @@ -110,8 +133,7 @@ module "bigtable-instance" {
| [instance_type](variables.tf#L50) | (deprecated) The instance type to create. One of 'DEVELOPMENT' or 'PRODUCTION'. | <code>string</code> | | <code>null</code> |
| [num_nodes](variables.tf#L61) | The number of nodes in your Cloud Bigtable cluster. This value is ignored if you are using autoscaling. | <code>number</code> | | <code>1</code> |
| [storage_type](variables.tf#L72) | The storage type to use. | <code>string</code> | | <code>&#34;SSD&#34;</code> |
| [table_options_defaults](variables.tf#L78) | Default option of tables created in the BigTable instance. | <code title="object&#40;&#123;&#10; split_keys &#61; list&#40;string&#41;&#10; column_family &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; split_keys &#61; &#91;&#93;&#10; column_family &#61; null&#10;&#125;">&#123;&#8230;&#125;</code> |
| [tables](variables.tf#L90) | Tables to be created in the BigTable instance, options can be null. | <code title="map&#40;object&#40;&#123;&#10; split_keys &#61; list&#40;string&#41;&#10; column_family &#61; string&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [tables](variables.tf#L78) | Tables to be created in the BigTable instance, options can be null or ignored. | <code title="map&#40;object&#40;&#123;&#10; split_keys &#61; optional&#40;list&#40;string&#41;&#41;&#10; column_families &#61; optional&#40;list&#40;string&#41;&#41;&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |

## Outputs

Expand Down
9 changes: 3 additions & 6 deletions modules/bigtable-instance/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}
}
19 changes: 3 additions & 16 deletions modules/bigtable-instance/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit dd0a104

Please sign in to comment.