refactor net-address modules for 1.3 (#840)

This commit is contained in:
Ludovico Magnocavallo 2022-09-28 14:10:05 +02:00 committed by GitHub
parent b983ae46f5
commit fcf71b983e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 86 deletions

View File

@ -27,22 +27,16 @@ module "addresses" {
project_id = var.project_id project_id = var.project_id
internal_addresses = { internal_addresses = {
ilb-1 = { ilb-1 = {
purpose = "SHARED_LOADBALANCER_VIP"
region = var.region region = var.region
subnetwork = var.subnet.self_link subnetwork = var.subnet.self_link
} }
ilb-2 = { ilb-2 = {
address = "10.0.0.2"
region = var.region region = var.region
subnetwork = var.subnet.self_link subnetwork = var.subnet.self_link
} }
} }
# optional configuration
internal_addresses_config = {
ilb-1 = {
address = null
purpose = "SHARED_LOADBALANCER_VIP"
tier = null
}
}
} }
# tftest modules=1 resources=2 # tftest modules=1 resources=2
``` ```
@ -89,13 +83,12 @@ module "addresses" {
| name | description | type | required | default | | name | description | type | required | default |
|---|---|:---:|:---:|:---:| |---|---|:---:|:---:|:---:|
| [project_id](variables.tf#L60) | Project where the addresses will be created. | <code>string</code> | ✓ | | | [project_id](variables.tf#L54) | Project where the addresses will be created. | <code>string</code> | ✓ | |
| [external_addresses](variables.tf#L17) | Map of external address regions, keyed by name. | <code>map&#40;string&#41;</code> | | <code>&#123;&#125;</code> | | [external_addresses](variables.tf#L17) | Map of external address regions, keyed by name. | <code>map&#40;string&#41;</code> | | <code>&#123;&#125;</code> |
| [global_addresses](variables.tf#L29) | List of global addresses to create. | <code>list&#40;string&#41;</code> | | <code>&#91;&#93;</code> | | [global_addresses](variables.tf#L29) | List of global addresses to create. | <code>list&#40;string&#41;</code> | | <code>&#91;&#93;</code> |
| [internal_addresses](variables.tf#L35) | Map of internal addresses to create, keyed by name. | <code title="map&#40;object&#40;&#123;&#10; region &#61; string&#10; subnetwork &#61; string&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> | | [internal_addresses](variables.tf#L35) | Map of internal addresses to create, keyed by name. | <code title="map&#40;object&#40;&#123;&#10; region &#61; string&#10; subnetwork &#61; string&#10; address &#61; optional&#40;string&#41;&#10; labels &#61; optional&#40;map&#40;string&#41;&#41;&#10; purpose &#61; optional&#40;string&#41;&#10; tier &#61; optional&#40;string&#41;&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [internal_addresses_config](variables.tf#L44) | Optional configuration for internal addresses, keyed by name. Unused options can be set to null. | <code title="map&#40;object&#40;&#123;&#10; address &#61; string&#10; purpose &#61; string&#10; tier &#61; string&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> | | [psa_addresses](variables.tf#L59) | Map of internal addresses used for Private Service Access. | <code title="map&#40;object&#40;&#123;&#10; address &#61; string&#10; network &#61; string&#10; prefix_length &#61; number&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [psa_addresses](variables.tf#L65) | Map of internal addresses used for Private Service Access. | <code title="map&#40;object&#40;&#123;&#10; address &#61; string&#10; network &#61; string&#10; prefix_length &#61; number&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> | | [psc_addresses](variables.tf#L69) | Map of internal addresses used for Private Service Connect. | <code title="map&#40;object&#40;&#123;&#10; address &#61; string&#10; network &#61; string&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [psc_addresses](variables.tf#L75) | Map of internal addresses used for Private Service Connect. | <code title="map&#40;object&#40;&#123;&#10; address &#61; string&#10; network &#61; string&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
## Outputs ## Outputs

View File

@ -39,10 +39,10 @@ resource "google_compute_address" "internal" {
address_type = "INTERNAL" address_type = "INTERNAL"
region = each.value.region region = each.value.region
subnetwork = each.value.subnetwork subnetwork = each.value.subnetwork
address = try(var.internal_addresses_config[each.key].address, null) address = each.value.address
network_tier = try(var.internal_addresses_config[each.key].tier, null) network_tier = each.value.tier
purpose = try(var.internal_addresses_config[each.key].purpose, null) purpose = each.value.purpose
# labels = lookup(var.internal_address_labels, each.key, {}) labels = coalesce(each.value.labels, {})
} }
resource "google_compute_global_address" "psc" { resource "google_compute_global_address" "psc" {

View File

@ -37,16 +37,10 @@ variable "internal_addresses" {
type = map(object({ type = map(object({
region = string region = string
subnetwork = string subnetwork = string
})) address = optional(string)
default = {} labels = optional(map(string))
} purpose = optional(string)
tier = optional(string)
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 = {} default = {}
} }

View File

@ -15,11 +15,10 @@
*/ */
module "test" { module "test" {
source = "../../../../modules/net-address" source = "../../../../modules/net-address"
external_addresses = var.external_addresses external_addresses = var.external_addresses
global_addresses = var.global_addresses global_addresses = var.global_addresses
internal_addresses = var.internal_addresses internal_addresses = var.internal_addresses
internal_addresses_config = var.internal_addresses_config psa_addresses = var.psa_addresses
psa_addresses = var.psa_addresses project_id = var.project_id
project_id = var.project_id
} }

View File

@ -15,29 +15,17 @@
*/ */
variable "external_addresses" { variable "external_addresses" {
type = map(string) type = any
default = {} default = {}
} }
variable "global_addresses" { variable "global_addresses" {
type = list(string) type = any
default = [] default = []
} }
variable "internal_addresses" { variable "internal_addresses" {
type = map(object({ type = any
region = string
subnetwork = string
}))
default = {}
}
variable "internal_addresses_config" {
type = map(object({
address = string
purpose = string
tier = string
}))
default = {} default = {}
} }
@ -47,10 +35,6 @@ variable "project_id" {
} }
variable "psa_addresses" { variable "psa_addresses" {
type = map(object({ type = any
address = string
network = string
prefix_length = number
}))
default = {} default = {}
} }

View File

@ -12,14 +12,15 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
def test_external_addresses(plan_runner): def test_external_addresses(plan_runner):
addresses = '{one = "europe-west1", two = "europe-west2"}' addresses = '{one = "europe-west1", two = "europe-west2"}'
_, resources = plan_runner(external_addresses=addresses) _, resources = plan_runner(external_addresses=addresses)
assert [r['values']['name'] for r in resources] == ['one', 'two'] assert [r['values']['name'] for r in resources] == ['one', 'two']
assert set(r['values']['address_type'] assert set(r['values']['address_type'] for r in resources) == set(
for r in resources) == set(['EXTERNAL']) ['EXTERNAL'])
assert [r['values']['region'] assert [r['values']['region'] for r in resources
for r in resources] == ['europe-west1', 'europe-west2'] ] == ['europe-west1', 'europe-west2']
def test_global_addresses(plan_runner): def test_global_addresses(plan_runner):
@ -29,42 +30,37 @@ def test_global_addresses(plan_runner):
def test_internal_addresses(plan_runner): def test_internal_addresses(plan_runner):
addresses = ( addresses = ('{one = {region = "europe-west1", subnetwork = "foobar"}, '
'{one = {region = "europe-west1", subnetwork = "foobar"}, ' 'two = {region = "europe-west2", subnetwork = "foobarz"}}')
'two = {region = "europe-west2", subnetwork = "foobarz"}}'
)
_, resources = plan_runner(internal_addresses=addresses) _, resources = plan_runner(internal_addresses=addresses)
assert [r['values']['name'] for r in resources] == ['one', 'two'] assert [r['values']['name'] for r in resources] == ['one', 'two']
assert set(r['values']['address_type'] assert set(r['values']['address_type'] for r in resources) == set(
for r in resources) == set(['INTERNAL']) ['INTERNAL'])
assert [r['values']['region'] assert [r['values']['region'] for r in resources
for r in resources] == ['europe-west1', 'europe-west2'] ] == ['europe-west1', 'europe-west2']
def test_internal_addresses_config(plan_runner): def test_internal_addresses_config(plan_runner):
addresses = ( addresses = '''{
'{one = {region = "europe-west1", subnetwork = "foobar"}, ' one = {
'two = {region = "europe-west2", subnetwork = "foobarz"}}' region = "europe-west1"
) subnetwork = "foobar"
config = ( address = "10.0.0.2"
'{one = {address = "10.0.0.2", purpose = "SHARED_LOADBALANCER_VIP", ' purpose = "SHARED_LOADBALANCER_VIP"
'tier=null}}' },
) two = {region = "europe-west2", subnetwork = "foobarz"}
_, resources = plan_runner(internal_addresses=addresses, }'''
internal_addresses_config=config) _, resources = plan_runner(internal_addresses=addresses)
assert [r['values']['name'] for r in resources] == ['one', 'two'] assert [r['values']['name'] for r in resources] == ['one', 'two']
assert set(r['values']['address_type'] assert set(r['values']['address_type'] for r in resources) == set(
for r in resources) == set(['INTERNAL']) ['INTERNAL'])
assert [r['values'].get('address') assert [r['values'].get('address') for r in resources] == ['10.0.0.2', None]
for r in resources] == ['10.0.0.2', None] assert [r['values'].get('purpose') for r in resources
assert [r['values'].get('purpose') ] == ['SHARED_LOADBALANCER_VIP', None]
for r in resources] == ['SHARED_LOADBALANCER_VIP', None]
def test_psa_config(plan_runner): def test_psa_config(plan_runner):
psa_addresses = '{cloudsql-mysql={address="10.199.0.0", network="foobar", prefix_length = 24}}' psa_addresses = '{cloudsql-mysql={address="10.199.0.0", network="foobar", prefix_length = 24}}'
_, resources = plan_runner(psa_addresses=psa_addresses) _, resources = plan_runner(psa_addresses=psa_addresses)
assert set(r['values']['purpose'] assert set(r['values']['purpose'] for r in resources) == set(['VPC_PEERING'])
for r in resources) == set(['VPC_PEERING']) assert set(r['values']['address'] for r in resources) == set(['10.199.0.0'])
assert set(r['values']['address']
for r in resources) == set(['10.199.0.0'])