Skip to content

Commit 4e7b2bd

Browse files
authored
Merge pull request hashicorp#145 from craigatgoogle/pr-46
Support cloud sql private ip (incorporating previous PR feedback)
2 parents 8c1ec01 + cfa9ff0 commit 4e7b2bd

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

google-beta/resource_sql_database_instance.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"google.golang.org/api/sqladmin/v1beta4"
1717
)
1818

19+
const privateNetworkLinkRegex = "projects/(" + ProjectRegex + ")/global/networks/((?:[a-z](?:[-a-z0-9]*[a-z0-9])?))$"
20+
1921
var sqlDatabaseAuthorizedNetWorkSchemaElem *schema.Resource = &schema.Resource{
2022
Schema: map[string]*schema.Schema{
2123
"expiration_time": &schema.Schema{
@@ -180,6 +182,12 @@ func resourceSqlDatabaseInstance() *schema.Resource {
180182
Type: schema.TypeBool,
181183
Optional: true,
182184
},
185+
"private_network": &schema.Schema{
186+
Type: schema.TypeString,
187+
Optional: true,
188+
ValidateFunc: validateRegexp(privateNetworkLinkRegex),
189+
DiffSuppressFunc: compareSelfLinkRelativePaths,
190+
},
183191
},
184192
},
185193
},
@@ -265,6 +273,10 @@ func resourceSqlDatabaseInstance() *schema.Resource {
265273
Type: schema.TypeString,
266274
Computed: true,
267275
},
276+
"type": &schema.Schema{
277+
Type: schema.TypeString,
278+
Computed: true,
279+
},
268280
"time_to_retire": &schema.Schema{
269281
Type: schema.TypeString,
270282
Optional: true,
@@ -614,10 +626,13 @@ func expandIpConfiguration(configured []interface{}) *sqladmin.IpConfiguration {
614626
}
615627

616628
_ipConfiguration := configured[0].(map[string]interface{})
629+
617630
return &sqladmin.IpConfiguration{
618631
Ipv4Enabled: _ipConfiguration["ipv4_enabled"].(bool),
619632
RequireSsl: _ipConfiguration["require_ssl"].(bool),
633+
PrivateNetwork: _ipConfiguration["private_network"].(string),
620634
AuthorizedNetworks: expandAuthorizedNetworks(_ipConfiguration["authorized_networks"].(*schema.Set).List()),
635+
ForceSendFields: []string{"Ipv4Enabled"},
621636
}
622637
}
623638
func expandAuthorizedNetworks(configured []interface{}) []*sqladmin.AclEntry {
@@ -696,7 +711,6 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
696711
if err := d.Set("replica_configuration", flattenReplicaConfiguration(instance.ReplicaConfiguration, d)); err != nil {
697712
log.Printf("[WARN] Failed to set SQL Database Instance Replica Configuration")
698713
}
699-
700714
ipAddresses := flattenIpAddresses(instance.IpAddresses)
701715
if err := d.Set("ip_address", ipAddresses); err != nil {
702716
log.Printf("[WARN] Failed to set SQL Database Instance IP Addresses")
@@ -870,8 +884,9 @@ func flattenDatabaseFlags(databaseFlags []*sqladmin.DatabaseFlags) []map[string]
870884

871885
func flattenIpConfiguration(ipConfiguration *sqladmin.IpConfiguration) interface{} {
872886
data := map[string]interface{}{
873-
"ipv4_enabled": ipConfiguration.Ipv4Enabled,
874-
"require_ssl": ipConfiguration.RequireSsl,
887+
"ipv4_enabled": ipConfiguration.Ipv4Enabled,
888+
"private_network": ipConfiguration.PrivateNetwork,
889+
"require_ssl": ipConfiguration.RequireSsl,
875890
}
876891

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

google-beta/resource_sql_database_instance_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,30 @@ func TestAccSqlDatabaseInstance_basic_with_user_labels(t *testing.T) {
594594
})
595595
}
596596

597+
func TestAccSqlDatabaseInstance_withPrivateNetwork(t *testing.T) {
598+
t.Parallel()
599+
600+
databaseName := "tf-test-" + acctest.RandString(10)
601+
networkName := "tf-test-" + acctest.RandString(10)
602+
addressName := "tf-test-" + acctest.RandString(10)
603+
604+
resource.Test(t, resource.TestCase{
605+
PreCheck: func() { testAccPreCheck(t) },
606+
Providers: testAccProviders,
607+
CheckDestroy: testAccSqlDatabaseInstanceDestroy,
608+
Steps: []resource.TestStep{
609+
resource.TestStep{
610+
Config: testAccSqlDatabaseInstance_withPrivateNetwork(databaseName, networkName, addressName),
611+
},
612+
resource.TestStep{
613+
ResourceName: "google_sql_database_instance.instance",
614+
ImportState: true,
615+
ImportStateVerify: true,
616+
},
617+
},
618+
})
619+
}
620+
597621
func testAccSqlDatabaseInstanceDestroy(s *terraform.State) error {
598622
for _, rs := range s.RootModule().Resources {
599623
config := testAccProvider.Meta().(*Config)
@@ -714,6 +738,42 @@ resource "google_sql_database_instance" "instance-failover" {
714738
`, instanceName, failoverName)
715739
}
716740

741+
func testAccSqlDatabaseInstance_withPrivateNetwork(databaseName, networkName, addressRangeName string) string {
742+
return fmt.Sprintf(`
743+
resource "google_compute_network" "foobar" {
744+
name = "%s"
745+
auto_create_subnetworks = false
746+
}
747+
748+
resource "google_compute_global_address" "foobar" {
749+
name = "%s"
750+
purpose = "VPC_PEERING"
751+
address_type = "INTERNAL"
752+
prefix_length = 16
753+
network = "${google_compute_network.foobar.self_link}"
754+
}
755+
756+
resource "google_service_networking_connection" "foobar" {
757+
network = "${google_compute_network.foobar.self_link}"
758+
service = "servicenetworking.googleapis.com"
759+
reserved_peering_ranges = ["${google_compute_global_address.foobar.name}"]
760+
}
761+
762+
resource "google_sql_database_instance" "instance" {
763+
depends_on = ["google_service_networking_connection.foobar"]
764+
name = "%s"
765+
region = "us-central1"
766+
settings {
767+
tier = "db-f1-micro"
768+
ip_configuration {
769+
ipv4_enabled = "false"
770+
private_network = "${google_compute_network.foobar.self_link}"
771+
}
772+
}
773+
}
774+
`, networkName, addressRangeName, databaseName)
775+
}
776+
717777
var testGoogleSqlDatabaseInstance_settings = `
718778
resource "google_sql_database_instance" "instance" {
719779
name = "tf-lw-%d"

website/docs/r/sql_database_instance.html.markdown

+40
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,42 @@ resource "google_sql_database_instance" "postgres" {
108108
}
109109
```
110110

111+
### Private IP Instance
112+
113+
114+
```hcl
115+
resource "google_compute_network" "private_network" {
116+
name = "private_network"
117+
}
118+
119+
resource "google_compute_global_address" "private_ip_address" {
120+
name = "private_ip_address"
121+
purpose = "VPC_PEERING"
122+
address_type = "INTERNAL"
123+
prefix_length = 16
124+
network = "${google_compute_network.private_network.self_link}"
125+
}
126+
127+
resource "google_service_networking_connection" "private_vpc_connection" {
128+
network = "${google_compute_network.private_network.self_link}"
129+
service = "servicenetworking.googleapis.com"
130+
reserved_peering_ranges = ["${google_compute_global_address.private_ip_address.name}"]
131+
}
132+
133+
resource "google_sql_database_instance" "instance" {
134+
depends_on = ["google_service_networking_connection.private_vpc_connection"]
135+
name = "private_instance"
136+
region = "us-central1"
137+
settings {
138+
tier = "db-f1-micro"
139+
ip_configuration {
140+
ipv4_enabled = "false"
141+
private_network = "${google_compute_network.private_network.self_link}"
142+
}
143+
}
144+
}
145+
```
146+
111147
## Argument Reference
112148

113149
The following arguments are supported:
@@ -205,6 +241,8 @@ The optional `settings.ip_configuration` subblock supports:
205241
* `require_ssl` - (Optional) True if mysqld should default to `REQUIRE X509`
206242
for users connecting over IP.
207243

244+
* `private_network` - (Optional) The resource link for the VPC network from which the Cloud SQL instance is accessible for private IP.
245+
208246
The optional `settings.ip_configuration.authorized_networks[]` sublist supports:
209247

210248
* `expiration_time` - (Optional) The [RFC 3339](https://tools.ietf.org/html/rfc3339)
@@ -286,6 +324,8 @@ when the resource is configured with a `count`.
286324
* `ip_address.0.time_to_retire` - The time this IP address will be retired, in RFC
287325
3339 format.
288326

327+
* `ip_address.0.type` - The type of this IP address. A PRIMARY address is an address that can accept incoming connections. An OUTGOING address is the source address of connections originating from the instance, if supported. A PRIVATE address is an address for an instance which has been configured to use private networking see: [Private IP](https://cloud.google.com/sql/docs/mysql/private-ip).
328+
289329
* `self_link` - The URI of the created resource.
290330

291331
* `settings.version` - Used to make sure changes to the `settings` block are

0 commit comments

Comments
 (0)