fix and streamline bigtable module, add tests, align to new iam variable name

This commit is contained in:
Ludovico Magnocavallo 2020-11-06 08:11:07 +01:00
parent 3a4938874b
commit efc694ba01
8 changed files with 139 additions and 30 deletions

View File

@ -13,22 +13,21 @@ This module allows managing a single BigTable instance, including access configu
```hcl
module "big-table-instance" {
module "bigtable-instance" {
source = "./modules/bigtable-instance"
project_id = "my-project"
name = "instance"
cluster_id = "instance"
instance_type = "PRODUCTION"
zone = "europe-west1-b"
tables = {
test1 = { table_options = null },
test2 = { table_options = {
test1 = null,
test2 = {
split_keys = ["a", "b", "c"]
column_family = null
}
}
}
iam_members = {
viewer = ["user:viewer@testdomain.com"]
iam = {
"roles/bigtable.user" = ["user:viewer@testdomain.com"]
}
}
```
@ -44,12 +43,12 @@ module "big-table-instance" {
| *cluster_id* | The ID of the Cloud Bigtable cluster. | <code title="">string</code> | | <code title="">europe-west1</code> |
| *deletion_protection* | 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 title=""></code> | | <code title="">true</code> |
| *display_name* | The human-readable display name of the Bigtable instance. | <code title=""></code> | | <code title="">null</code> |
| *iam_members* | Authoritative for a given role. Updates the IAM policy to grant a role to a list of members. Other roles within the IAM policy for the instance are preserved. | <code title="map&#40;set&#40;string&#41;&#41;">map(set(string))</code> | | <code title="">{}</code> |
| *instance_type* | None | <code title="">string</code> | | <code title="">DEVELOPMENT</code> |
| *iam* | IAM bindings for topic in {ROLE => [MEMBERS]} format. | <code title="map&#40;list&#40;string&#41;&#41;">map(list(string))</code> | | <code title="">{}</code> |
| *instance_type* | (deprecated) The instance type to create. One of 'DEVELOPMENT' or 'PRODUCTION'. | <code title="">string</code> | | <code title="">null</code> |
| *num_nodes* | The number of nodes in your Cloud Bigtable cluster. | <code title="">number</code> | | <code title="">1</code> |
| *storage_type* | The storage type to use. | <code title="">string</code> | | <code title="">SSD</code> |
| *table_options_defaults* | 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({...})</code> | | <code title="&#123;&#10;split_keys &#61; &#91;&#93;&#10;column_family &#61; null&#10;&#125;">...</code> |
| *tables* | Tables to be created in the BigTable instance. | <code title="map&#40;object&#40;&#123;&#10;table_options &#61; object&#40;&#123;&#10;split_keys &#61; list&#40;string&#41;&#10;column_family &#61; string&#10;&#125;&#41;&#10;&#125;&#41;&#41;">map(object({...}))</code> | | <code title="">{}</code> |
| *tables* | 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(object({...}))</code> | | <code title="">{}</code> |
## Outputs

View File

@ -16,7 +16,7 @@
locals {
tables = {
for k, v in var.tables : k => v.table_options != null ? v.table_options : var.table_options_defaults
for k, v in var.tables : k => v != null ? v : var.table_options_defaults
}
}
@ -35,8 +35,7 @@ resource "google_bigtable_instance" "default" {
}
resource "google_bigtable_instance_iam_binding" "default" {
for_each = var.iam_members
for_each = var.iam
project = var.project_id
instance = google_bigtable_instance.default.name
role = each.key

View File

@ -18,8 +18,8 @@ output "id" {
description = "An identifier for the resource with format projects/{{project}}/instances/{{name}}."
value = google_bigtable_instance.default.id
depends_on = [
google_bigtable_instance_iam_binding,
google_bigtable_table
google_bigtable_instance_iam_binding.default,
google_bigtable_table.default
]
}
@ -27,8 +27,8 @@ output "instance" {
description = "BigTable intance."
value = google_bigtable_instance.default
depends_on = [
google_bigtable_instance_iam_binding,
google_bigtable_table
google_bigtable_instance_iam_binding.default,
google_bigtable_table.default
]
}

View File

@ -14,12 +14,6 @@
* limitations under the License.
*/
variable "iam_members" {
description = "Authoritative for a given role. Updates the IAM policy to grant a role to a list of members. Other roles within the IAM policy for the instance are preserved."
type = map(set(string))
default = {}
}
variable "cluster_id" {
description = "The ID of the Cloud Bigtable cluster."
type = string
@ -36,10 +30,16 @@ variable "display_name" {
default = null
}
variable "iam" {
description = "IAM bindings for topic in {ROLE => [MEMBERS]} format."
type = map(list(string))
default = {}
}
variable "instance_type" {
description = "The instance type to create. One of \"DEVELOPMENT\" or \"PRODUCTION\". Defaults to \"DEVELOPMENT\""
description = "(deprecated) The instance type to create. One of 'DEVELOPMENT' or 'PRODUCTION'."
type = string
default = "DEVELOPMENT"
default = null
}
variable "name" {
@ -65,12 +65,10 @@ variable "storage_type" {
}
variable "tables" {
description = "Tables to be created in the BigTable instance."
description = "Tables to be created in the BigTable instance, options can be null."
type = map(object({
table_options = object({
split_keys = list(string)
column_family = string
})
split_keys = list(string)
column_family = string
}))
default = {}
}

View File

@ -0,0 +1,13 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

View File

@ -0,0 +1,33 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
module "test" {
source = "../../../../modules/bigtable-instance"
project_id = "my-project"
name = "test"
iam = {
"roles/bigtable.user" = ["user:me@example.com"]
}
tables = {
test-1 = null,
test-2 = {
split_keys = ["a", "b", "c"]
column_family = null
}
}
zone = var.zone
}

View File

@ -0,0 +1,20 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
variable "zone" {
type = string
default = "europe-west1-b"
}

View File

@ -0,0 +1,47 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import pytest
FIXTURES_DIR = os.path.join(os.path.dirname(__file__), 'fixture')
@pytest.fixture
def resources(plan_runner):
_, resources = plan_runner(FIXTURES_DIR)
return resources
def test_resource_count(resources):
"Test number of resources created."
assert len(resources) == 4
def test_iam(resources):
"Test IAM binding resources."
bindings = [r['values'] for r in resources if r['type']
== 'google_bigtable_instance_iam_binding']
assert len(bindings) == 1
assert bindings[0]['role'] == 'roles/bigtable.user'
def test_tables(resources):
"Test table resources."
subs = [r['values'] for r in resources if r['type']
== 'google_bigtable_table']
assert len(subs) == 2
assert set(s['name'] for s in subs) == set(['test-1', 'test-2'])