Skip to content

Commit

Permalink
Add support to a public access to cloudsql-instance (#754)
Browse files Browse the repository at this point in the history
* feat: add support to a public access to cloudsql-instance

* doc: update cloudsql-instance doc changing the ipv4_enabled parameter description

* test: fix assertion test to ipv4_enabled
  • Loading branch information
alefmreis authored Aug 9, 2022
1 parent c9735bc commit 97104c0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions modules/cloudsql-instance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ module "db" {
| [disk_type](variables.tf#L73) | The type of data disk: `PD_SSD` or `PD_HDD`. | <code>string</code> | | <code>&#34;PD_SSD&#34;</code> |
| [encryption_key_name](variables.tf#L79) | The full path to the encryption key used for the CMEK disk encryption of the primary instance. | <code>string</code> | | <code>null</code> |
| [flags](variables.tf#L85) | Map FLAG_NAME=>VALUE for database-specific tuning. | <code>map&#40;string&#41;</code> | | <code>null</code> |
| [ipv4_enabled](variables.tf#L143) | Add a public IP address to database instance. | <code>bool</code> | | <code>false</code> |
| [labels](variables.tf#L91) | Labels to be attached to all instances. | <code>map&#40;string&#41;</code> | | <code>null</code> |
| [prefix](variables.tf#L107) | Prefix used to generate instance names. | <code>string</code> | | <code>null</code> |
| [replicas](variables.tf#L123) | Map of NAME=> {REGION, KMS_KEY} for additional read replicas. Set to null to disable replica creation. | <code title="map&#40;object&#40;&#123;&#10; region &#61; string&#10; encryption_key_name &#61; string&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
Expand Down
4 changes: 2 additions & 2 deletions modules/cloudsql-instance/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ resource "google_sql_database_instance" "primary" {
user_labels = var.labels

ip_configuration {
ipv4_enabled = false
ipv4_enabled = var.ipv4_enabled
private_network = var.network
dynamic "authorized_networks" {
for_each = var.authorized_networks != null ? var.authorized_networks : {}
Expand Down Expand Up @@ -124,7 +124,7 @@ resource "google_sql_database_instance" "replicas" {
user_labels = var.labels

ip_configuration {
ipv4_enabled = false
ipv4_enabled = var.ipv4_enabled
private_network = var.network
dynamic "authorized_networks" {
for_each = var.authorized_networks != null ? var.authorized_networks : {}
Expand Down
6 changes: 6 additions & 0 deletions modules/cloudsql-instance/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,9 @@ variable "users" {
type = map(string)
default = null
}

variable "ipv4_enabled" {
description = "Add a public IP address to database instance."
type = bool
default = false
}
1 change: 1 addition & 0 deletions tests/modules/cloudsql_instance/fixture/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ module "test" {
users = var.users
tier = var.tier
deletion_protection = var.deletion_protection
ipv4_enabled = var.ipv4_enabled
}
5 changes: 5 additions & 0 deletions tests/modules/cloudsql_instance/fixture/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,8 @@ variable "deletion_protection" {
type = bool
default = false
}

variable "ipv4_enabled" {
type = bool
default = false
}
22 changes: 22 additions & 0 deletions tests/modules/cloudsql_instance/test_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,25 @@ def test_databases(plan_runner):
assert len(resources) == 2
assert all(r['values']['instance'] == "db" for r in resources)
assert sorted(r['values']['name'] for r in resources) == ["db1", "db2"]


def test_simple_instance_ipv4_enable(plan_runner):
"Test instance ipv4_enabled."

_, resources = plan_runner(ipv4_enabled="true")
assert len(resources) == 1
assert resources[0]['values']['settings'][0]['ip_configuration'][0]['ipv4_enabled']


def test_replicas_ipv4_enable(plan_runner):
"Test replicas ipv4_enabled."

replicas = """{
replica1 = { region = "europe-west3", encryption_key_name = null }
}"""

_, resources = plan_runner(replicas=replicas, ipv4_enabled="true")

assert len(resources) == 2
assert all([r['values']['settings'][0]['ip_configuration'][0]['ipv4_enabled'] for r in resources])

0 comments on commit 97104c0

Please sign in to comment.