diff --git a/modules/gcs/README.md b/modules/gcs/README.md index 1f31a40fd..d5f793f5d 100644 --- a/modules/gcs/README.md +++ b/modules/gcs/README.md @@ -93,9 +93,9 @@ module "buckets" { | *labels* | Labels to be attached to all buckets. | map(string) | | {} | | *location* | Bucket location. | string | | EU | | *prefix* | Prefix used to generate the bucket name. | string | | null | +| *retention_policies* | Per-bucket retention policy. | map(map(string)) | | {} | | *storage_class* | Bucket storage class. | string | | MULTI_REGIONAL | | *versioning* | Optional map to set versioning keyed by name, defaults to false. | map(bool) | | {} | -| *retention_policies* | Optional map to set up retention policy keyed by bucket name. | map(map(string)) | | {} | ## Outputs diff --git a/modules/net-address/README.md b/modules/net-address/README.md index 9c1169b5b..eb3e1168a 100644 --- a/modules/net-address/README.md +++ b/modules/net-address/README.md @@ -1,14 +1,46 @@ # Net Address Reservation Module -## Example +This module allows reserving Compute Engine external, global, and internal addresses. + +## Examples + +### External and global addresses ```hcl module "addresses" { source = "./modules/net-address" project_id = local.projects.host external_addresses = { - nat-1 = module.vpc.subnet_regions["default"], - vpn-remote = module.vpc.subnet_regions["default"], + nat-1 = var.region + vpn-remote = var.region + } + global_addresses = ["app-1", "app-2"] +} +``` + +### Internal addresses + +```hcl +module "addresses" { + source = "./modules/net-address" + project_id = local.projects.host + internal_addresses = { + ilb-1 = { + region = var.region + subnetwork = module.vpc.subnet_self_links["${var.region}-test"] + } + ilb-2 = { + region = var.region + subnetwork = module.vpc.subnet_self_links["${var.region}-test"] + } + } + # optional configuration + internal_addresses_config = { + ilb-1 = { + address = null + purpose = "SHARED_LOADBALANCER_VIP" + tier = null + } } } ``` @@ -21,9 +53,8 @@ module "addresses" { | project_id | Project where the addresses will be created. | string | ✓ | | | *external_addresses* | Map of external address regions, keyed by name. | map(string) | | {} | | *global_addresses* | List of global addresses to create. | list(string) | | [] | -| *internal_address_addresses* | Optional explicit addresses for internal addresses, keyed by name. | map(string) | | {} | -| *internal_address_tiers* | Optional network tiers for internal addresses, keyed by name. | map(string) | | {} | | *internal_addresses* | Map of internal addresses to create, keyed by name. | map(object({...})) | | {} | +| *internal_addresses_config* | Optional configuration for internal addresses, keyed by name. Unused options can be set to null. | map(object({...})) | | {} | ## Outputs diff --git a/modules/net-address/main.tf b/modules/net-address/main.tf index b752f2aa9..ae43174ef 100644 --- a/modules/net-address/main.tf +++ b/modules/net-address/main.tf @@ -31,6 +31,7 @@ resource "google_compute_address" "external" { } resource "google_compute_address" "internal" { + provider = google-beta for_each = var.internal_addresses project = var.project_id name = each.key @@ -38,7 +39,8 @@ resource "google_compute_address" "internal" { address_type = "INTERNAL" region = each.value.region subnetwork = each.value.subnetwork - address = lookup(var.internal_address_addresses, each.key, null) - network_tier = lookup(var.internal_address_tiers, each.key, null) + address = try(var.internal_addresses_config[each.key].address, null) + network_tier = try(var.internal_addresses_config[each.key].tier, null) + purpose = try(var.internal_addresses_config[each.key].purpose, null) # labels = lookup(var.internal_address_labels, each.key, {}) } diff --git a/modules/net-address/outputs.tf b/modules/net-address/outputs.tf index 7d26158a6..188e88c1c 100644 --- a/modules/net-address/outputs.tf +++ b/modules/net-address/outputs.tf @@ -31,7 +31,6 @@ output "global_addresses" { address.name => { address = address.address self_link = address.self_link - status = address.status } } } diff --git a/modules/net-address/variables.tf b/modules/net-address/variables.tf index 02b85f68b..e5eda9457 100644 --- a/modules/net-address/variables.tf +++ b/modules/net-address/variables.tf @@ -41,16 +41,14 @@ variable "internal_addresses" { default = {} } -variable "internal_address_addresses" { - description = "Optional explicit addresses for internal addresses, keyed by name." - type = map(string) - default = {} -} - -variable "internal_address_tiers" { - description = "Optional network tiers for internal addresses, keyed by name." - type = map(string) - default = {} +variable "internal_addresses_config" { + description = "Optional configuration for internal addresses, keyed by name. Unused options can be set to null." + type = map(object({ + address = string + purpose = string + tier = string + })) + default = {} } # variable "internal_address_labels" { diff --git a/modules/net-address/versions.tf b/modules/net-address/versions.tf index ce6918e09..ef2d34645 100644 --- a/modules/net-address/versions.tf +++ b/modules/net-address/versions.tf @@ -16,4 +16,7 @@ terraform { required_version = ">= 0.12.6" + required_providers { + google-beta = "~> 3.28.0" + } } diff --git a/tests/modules/net_address/__init__.py b/tests/modules/net_address/__init__.py new file mode 100644 index 000000000..6913f02e3 --- /dev/null +++ b/tests/modules/net_address/__init__.py @@ -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. diff --git a/tests/modules/net_address/fixture/main.tf b/tests/modules/net_address/fixture/main.tf new file mode 100644 index 000000000..e10bf7d29 --- /dev/null +++ b/tests/modules/net_address/fixture/main.tf @@ -0,0 +1,24 @@ +/** + * 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/net-address" + external_addresses = var.external_addresses + global_addresses = var.global_addresses + internal_addresses = var.internal_addresses + internal_addresses_config = var.internal_addresses_config + project_id = var.project_id +} diff --git a/tests/modules/net_address/fixture/outputs.tf b/tests/modules/net_address/fixture/outputs.tf new file mode 100644 index 000000000..77b8211fa --- /dev/null +++ b/tests/modules/net_address/fixture/outputs.tf @@ -0,0 +1,19 @@ +/** + * 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. + */ + +output "module" { + value = module.test +} diff --git a/tests/modules/net_address/fixture/variables.tf b/tests/modules/net_address/fixture/variables.tf new file mode 100644 index 000000000..9d3508192 --- /dev/null +++ b/tests/modules/net_address/fixture/variables.tf @@ -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. + */ + +variable "external_addresses" { + type = map(string) + default = {} +} + +variable "global_addresses" { + type = list(string) + default = [] +} + +variable "internal_addresses" { + type = map(object({ + region = string + subnetwork = string + })) + default = {} +} + +variable "internal_addresses_config" { + type = map(object({ + address = string + purpose = string + tier = string + })) + default = {} +} + +variable "project_id" { + type = string + default = "my-project" +} diff --git a/tests/modules/net_address/test_plan.py b/tests/modules/net_address/test_plan.py new file mode 100644 index 000000000..968f05dc6 --- /dev/null +++ b/tests/modules/net_address/test_plan.py @@ -0,0 +1,70 @@ +# 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') + + +def test_external_addresses(plan_runner): + addresses = '{one = "europe-west1", two = "europe-west2"}' + _, resources = plan_runner(FIXTURES_DIR, external_addresses=addresses) + assert [r['values']['name'] for r in resources] == ['one', 'two'] + assert set(r['values']['address_type'] + for r in resources) == set(['EXTERNAL']) + assert [r['values']['region'] + for r in resources] == ['europe-west1', 'europe-west2'] + + +def test_global_addresses(plan_runner): + _, resources = plan_runner(FIXTURES_DIR, global_addresses='["one", "two"]') + assert [r['values']['name'] for r in resources] == ['one', 'two'] + assert set(r['values']['address_type'] for r in resources) == set([None]) + + +def test_internal_addresses(plan_runner): + addresses = ( + '{one = {region = "europe-west1", subnetwork = "foobar"}, ' + 'two = {region = "europe-west2", subnetwork = "foobarz"}}' + ) + _, resources = plan_runner(FIXTURES_DIR, internal_addresses=addresses) + assert [r['values']['name'] for r in resources] == ['one', 'two'] + assert set(r['values']['address_type'] + for r in resources) == set(['INTERNAL']) + assert [r['values']['region'] + for r in resources] == ['europe-west1', 'europe-west2'] + + +def test_internal_addresses_config(plan_runner): + addresses = ( + '{one = {region = "europe-west1", subnetwork = "foobar"}, ' + 'two = {region = "europe-west2", subnetwork = "foobarz"}}' + ) + config = ( + '{one = {address = "10.0.0.2", purpose = "SHARED_LOADBALANCER_VIP", ' + 'tier=null}}' + ) + _, resources = plan_runner(FIXTURES_DIR, + internal_addresses=addresses, + internal_addresses_config=config) + assert [r['values']['name'] for r in resources] == ['one', 'two'] + assert set(r['values']['address_type'] + for r in resources) == set(['INTERNAL']) + assert [r['values'].get('address') + for r in resources] == ['10.0.0.2', None] + assert [r['values'].get('purpose') + for r in resources] == ['SHARED_LOADBALANCER_VIP', None]