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

Add support to a public access to cloudsql-instance #754

Merged
Show file tree
Hide file tree
Changes from 9 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
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
}
23 changes: 23 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,26 @@ 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 r['values']['settings'][0]['ip_configuration'][0]['ipv4_enabled'] == True
ludoo marked this conversation as resolved.
Show resolved Hide resolved


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
r = resources[1]
ludoo marked this conversation as resolved.
Show resolved Hide resolved
assert r['values']['settings'][0]['ip_configuration'][0]['ipv4_enabled'] == True