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

Support cloud sql private ip (incorporating previous PR feedback) #145

Merged
merged 11 commits into from
Nov 21, 2018
22 changes: 19 additions & 3 deletions google-beta/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"google.golang.org/api/sqladmin/v1beta4"
)

const privateNetworkLinkRegex = "projects/(" + ProjectRegex + ")/global/networks/((?:[a-z](?:[-a-z0-9]*[a-z0-9])?))$"

var sqlDatabaseAuthorizedNetWorkSchemaElem *schema.Resource = &schema.Resource{
Schema: map[string]*schema.Schema{
"expiration_time": &schema.Schema{
Expand Down Expand Up @@ -180,6 +182,12 @@ func resourceSqlDatabaseInstance() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"private_network": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateRegexp(privateNetworkLinkRegex),
DiffSuppressFunc: compareSelfLinkRelativePaths,
},
},
},
},
Expand Down Expand Up @@ -265,6 +273,10 @@ func resourceSqlDatabaseInstance() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"time_to_retire": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -614,10 +626,13 @@ func expandIpConfiguration(configured []interface{}) *sqladmin.IpConfiguration {
}

_ipConfiguration := configured[0].(map[string]interface{})

return &sqladmin.IpConfiguration{
Ipv4Enabled: _ipConfiguration["ipv4_enabled"].(bool),
RequireSsl: _ipConfiguration["require_ssl"].(bool),
PrivateNetwork: _ipConfiguration["private_network"].(string),
AuthorizedNetworks: expandAuthorizedNetworks(_ipConfiguration["authorized_networks"].(*schema.Set).List()),
ForceSendFields: []string{"Ipv4Enabled"},
}
}
func expandAuthorizedNetworks(configured []interface{}) []*sqladmin.AclEntry {
Expand Down Expand Up @@ -696,7 +711,6 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
if err := d.Set("replica_configuration", flattenReplicaConfiguration(instance.ReplicaConfiguration, d)); err != nil {
log.Printf("[WARN] Failed to set SQL Database Instance Replica Configuration")
}

ipAddresses := flattenIpAddresses(instance.IpAddresses)
if err := d.Set("ip_address", ipAddresses); err != nil {
log.Printf("[WARN] Failed to set SQL Database Instance IP Addresses")
Expand Down Expand Up @@ -870,8 +884,9 @@ func flattenDatabaseFlags(databaseFlags []*sqladmin.DatabaseFlags) []map[string]

func flattenIpConfiguration(ipConfiguration *sqladmin.IpConfiguration) interface{} {
data := map[string]interface{}{
"ipv4_enabled": ipConfiguration.Ipv4Enabled,
"require_ssl": ipConfiguration.RequireSsl,
"ipv4_enabled": ipConfiguration.Ipv4Enabled,
"private_network": ipConfiguration.PrivateNetwork,
"require_ssl": ipConfiguration.RequireSsl,
}

if ipConfiguration.AuthorizedNetworks != nil {
Expand Down Expand Up @@ -950,6 +965,7 @@ func flattenIpAddresses(ipAddresses []*sqladmin.IpMapping) []map[string]interfac
for _, ip := range ipAddresses {
data := map[string]interface{}{
"ip_address": ip.IpAddress,
"type": ip.Type,
"time_to_retire": ip.TimeToRetire,
}

Expand Down
60 changes: 60 additions & 0 deletions google-beta/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,30 @@ func TestAccSqlDatabaseInstance_basic_with_user_labels(t *testing.T) {
})
}

func TestAccSqlDatabaseInstance_withPrivateNetwork(t *testing.T) {
t.Parallel()

databaseName := "tf-test-" + acctest.RandString(10)
networkName := "tf-test-" + acctest.RandString(10)
addressName := "tf-test-" + acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccSqlDatabaseInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccSqlDatabaseInstance_withPrivateNetwork(databaseName, networkName, addressName),
},
resource.TestStep{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccSqlDatabaseInstanceDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
config := testAccProvider.Meta().(*Config)
Expand Down Expand Up @@ -714,6 +738,42 @@ resource "google_sql_database_instance" "instance-failover" {
`, instanceName, failoverName)
}

func testAccSqlDatabaseInstance_withPrivateNetwork(databaseName, networkName, addressRangeName string) string {
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I think the test will run faster if you do auto_create_subnetworks = false (unless you need them for the test, in which case carry on)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

name = "%s"
auto_create_subnetworks = false
}

resource "google_compute_global_address" "foobar" {
name = "%s"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = "${google_compute_network.foobar.self_link}"
}

resource "google_service_networking_connection" "foobar" {
network = "${google_compute_network.foobar.self_link}"
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = ["${google_compute_global_address.foobar.name}"]
}

resource "google_sql_database_instance" "instance" {
depends_on = ["google_service_networking_connection.foobar"]
name = "%s"
region = "us-central1"
settings {
tier = "db-f1-micro"
ip_configuration {
ipv4_enabled = "false"
private_network = "${google_compute_network.foobar.self_link}"
}
}
}
`, networkName, addressRangeName, databaseName)
}

var testGoogleSqlDatabaseInstance_settings = `
resource "google_sql_database_instance" "instance" {
name = "tf-lw-%d"
Expand Down