refactor GCS module and tests (#1066)

This commit is contained in:
Ludovico Magnocavallo
2022-12-22 12:27:09 +01:00
committed by GitHub
parent ebc110409c
commit 082c63dfc5
12 changed files with 215 additions and 241 deletions

View File

@@ -0,0 +1,13 @@
force_destroy = true
labels = { environment = "test" }
logging_config = {
log_bucket = "foo"
}
name = "test"
project_id = "test-project"
retention_policy = {
retention_period = 5
is_locked = false
}
storage_class = "MULTI_REGIONAL"
versioning = true

View File

@@ -1,29 +0,0 @@
/**
* Copyright 2022 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/gcs"
project_id = "my-project"
uniform_bucket_level_access = var.uniform_bucket_level_access
force_destroy = var.force_destroy
iam = var.iam
labels = var.labels
logging_config = var.logging_config
name = "bucket-a"
prefix = var.prefix
retention_policy = var.retention_policy
versioning = var.versioning
}

View File

@@ -1,77 +0,0 @@
/**
* Copyright 2022 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 "uniform_bucket_level_access" {
type = bool
default = false
}
variable "force_destroy" {
type = bool
default = true
}
variable "iam" {
type = map(list(string))
default = {}
}
variable "labels" {
type = map(string)
default = { environment = "test" }
}
variable "logging_config" {
type = object({
log_bucket = string
log_object_prefix = string
})
default = {
log_bucket = "foo"
log_object_prefix = null
}
}
variable "prefix" {
type = string
default = null
}
variable "project_id" {
type = string
default = "my-project"
}
variable "retention_policy" {
type = object({
retention_period = number
is_locked = bool
})
default = {
retention_period = 5
is_locked = false
}
}
variable "storage_class" {
type = string
default = "MULTI_REGIONAL"
}
variable "versioning" {
type = bool
default = true
}

View File

@@ -0,0 +1,3 @@
iam = {
"roles/storage.admin" = ["user:a@example.org"]
}

View File

@@ -0,0 +1,30 @@
# Copyright 2022 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.
values:
google_storage_bucket.bucket:
name: test
google_storage_bucket_iam_binding.bindings["roles/storage.admin"]:
bucket: test
condition: []
members:
- user:a@example.org
role: roles/storage.admin
counts:
google_storage_bucket: 1
google_storage_bucket_iam_binding: 1
modules: 0
resources: 2

View File

@@ -0,0 +1 @@
prefix = "foo"

View File

@@ -0,0 +1,44 @@
# Copyright 2022 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.
values:
google_storage_bucket.bucket:
force_destroy: true
labels:
environment: test
location: EU
logging:
- log_bucket: foo
name: foo-test
project: test-project
retention_policy:
- is_locked: false
retention_period: 5
storage_class: MULTI_REGIONAL
uniform_bucket_level_access: true
versioning:
- enabled: true
counts:
google_storage_bucket: 1
modules: 0
resources: 1
outputs:
bucket: __missing__
id: foo-test
name: foo-test
notification: null
topic: null
url: __missing__

View File

@@ -1,54 +0,0 @@
# Copyright 2022 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.
def test_buckets(plan_runner):
"Test bucket resources."
_, resources = plan_runner()
assert len(resources) == 1
r = resources[0]
assert r['type'] == 'google_storage_bucket'
assert r['values']['name'] == 'bucket-a'
assert r['values']['project'] == 'my-project'
def test_prefix(plan_runner):
"Test bucket name when prefix is set."
_, resources = plan_runner(prefix='foo')
assert resources[0]['values']['name'] == 'foo-bucket-a'
def test_config_values(plan_runner):
"Test that variables set the correct attributes on buckets."
variables = dict(
uniform_bucket_level_access='true',
force_destroy='true',
versioning='true'
)
_, resources = plan_runner(**variables)
assert len(resources) == 1
r = resources[0]
assert r['values']['uniform_bucket_level_access'] is True
assert r['values']['force_destroy'] is True
assert r['values']['versioning'] == [{'enabled': True}]
assert r['values']['logging'] == [{'log_bucket': 'foo'}]
assert r['values']['retention_policy'] == [
{'is_locked': False, 'retention_period': 5}
]
def test_iam(plan_runner):
"Test bucket resources with iam roles and members."
iam = '{ "roles/storage.admin" = ["user:a@b.com"] }'
_, resources = plan_runner(iam=iam)
assert len(resources) == 2

View File

@@ -0,0 +1,20 @@
# Copyright 2022 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: modules/gcs
common_tfvars:
- common.tfvars
tests:
prefix:
iam: