Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor net-address module for 1.3 #840

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions modules/net-address/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,16 @@ module "addresses" {
project_id = var.project_id
internal_addresses = {
ilb-1 = {
purpose = "SHARED_LOADBALANCER_VIP"
region = var.region
subnetwork = var.subnet.self_link
}
ilb-2 = {
address = "10.0.0.2"
region = var.region
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
```
Expand Down Expand Up @@ -89,13 +83,12 @@ module "addresses" {

| 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> |
| [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_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#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#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> |
| [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> |
| [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> |
| [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> |

## Outputs

Expand Down
8 changes: 4 additions & 4 deletions modules/net-address/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ resource "google_compute_address" "internal" {
address_type = "INTERNAL"
region = each.value.region
subnetwork = each.value.subnetwork
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, {})
address = each.value.address
network_tier = each.value.tier
purpose = each.value.purpose
labels = coalesce(each.value.labels, {})
}

resource "google_compute_global_address" "psc" {
Expand Down
14 changes: 4 additions & 10 deletions modules/net-address/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,10 @@ variable "internal_addresses" {
type = map(object({
region = string
subnetwork = 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
address = optional(string)
labels = optional(map(string))
purpose = optional(string)
tier = optional(string)
}))
default = {}
}
Expand Down
13 changes: 6 additions & 7 deletions tests/modules/net_address/fixture/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
*/

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
psa_addresses = var.psa_addresses
project_id = var.project_id
source = "../../../../modules/net-address"
external_addresses = var.external_addresses
global_addresses = var.global_addresses
internal_addresses = var.internal_addresses
psa_addresses = var.psa_addresses
project_id = var.project_id
}
24 changes: 4 additions & 20 deletions tests/modules/net_address/fixture/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,17 @@
*/

variable "external_addresses" {
type = map(string)
type = any
default = {}
}

variable "global_addresses" {
type = list(string)
type = any
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
}))
type = any
default = {}
}

Expand All @@ -47,10 +35,6 @@ variable "project_id" {
}

variable "psa_addresses" {
type = map(object({
address = string
network = string
prefix_length = number
}))
type = any
default = {}
}
60 changes: 28 additions & 32 deletions tests/modules/net_address/test_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.


def test_external_addresses(plan_runner):
addresses = '{one = "europe-west1", two = "europe-west2"}'
_, resources = plan_runner(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']
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):
Expand All @@ -29,42 +30,37 @@ def test_global_addresses(plan_runner):


def test_internal_addresses(plan_runner):
addresses = (
'{one = {region = "europe-west1", subnetwork = "foobar"}, '
'two = {region = "europe-west2", subnetwork = "foobarz"}}'
)
addresses = ('{one = {region = "europe-west1", subnetwork = "foobar"}, '
'two = {region = "europe-west2", subnetwork = "foobarz"}}')
_, resources = plan_runner(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']
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(internal_addresses=addresses,
internal_addresses_config=config)
addresses = '''{
one = {
region = "europe-west1"
subnetwork = "foobar"
address = "10.0.0.2"
purpose = "SHARED_LOADBALANCER_VIP"
},
two = {region = "europe-west2", subnetwork = "foobarz"}
}'''
_, resources = plan_runner(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'].get('address')
for r in resources] == ['10.0.0.2', None]
assert [r['values'].get('purpose')
for r in resources] == ['SHARED_LOADBALANCER_VIP', None]
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]


def test_psa_config(plan_runner):
psa_addresses = '{cloudsql-mysql={address="10.199.0.0", network="foobar", prefix_length = 24}}'
_, resources = plan_runner(psa_addresses=psa_addresses)
assert set(r['values']['purpose']
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']['purpose'] for r in resources) == set(['VPC_PEERING'])
assert set(r['values']['address'] for r in resources) == set(['10.199.0.0'])