Skip to content

Commit f7ce05f

Browse files
danawillownat-henderson
authored andcommitted
Merge pull request hashicorp#145 from craigatgoogle/pr-46
Support cloud sql private ip (incorporating previous PR feedback)
1 parent d5da736 commit f7ce05f

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,
@@ -612,10 +624,13 @@ func expandIpConfiguration(configured []interface{}) *sqladmin.IpConfiguration {
612624
}
613625

614626
_ipConfiguration := configured[0].(map[string]interface{})
627+
615628
return &sqladmin.IpConfiguration{
616629
Ipv4Enabled: _ipConfiguration["ipv4_enabled"].(bool),
617630
RequireSsl: _ipConfiguration["require_ssl"].(bool),
631+
PrivateNetwork: _ipConfiguration["private_network"].(string),
618632
AuthorizedNetworks: expandAuthorizedNetworks(_ipConfiguration["authorized_networks"].(*schema.Set).List()),
633+
ForceSendFields: []string{"Ipv4Enabled"},
619634
}
620635
}
621636
func expandAuthorizedNetworks(configured []interface{}) []*sqladmin.AclEntry {
@@ -694,7 +709,6 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
694709
if err := d.Set("replica_configuration", flattenReplicaConfiguration(instance.ReplicaConfiguration, d)); err != nil {
695710
log.Printf("[WARN] Failed to set SQL Database Instance Replica Configuration")
696711
}
697-
698712
ipAddresses := flattenIpAddresses(instance.IpAddresses)
699713
if err := d.Set("ip_address", ipAddresses); err != nil {
700714
log.Printf("[WARN] Failed to set SQL Database Instance IP Addresses")
@@ -1124,8 +1138,9 @@ func flattenDatabaseFlags(databaseFlags []*sqladmin.DatabaseFlags) []map[string]
11241138

11251139
func flattenIpConfiguration(ipConfiguration *sqladmin.IpConfiguration) interface{} {
11261140
data := map[string]interface{}{
1127-
"ipv4_enabled": ipConfiguration.Ipv4Enabled,
1128-
"require_ssl": ipConfiguration.RequireSsl,
1141+
"ipv4_enabled": ipConfiguration.Ipv4Enabled,
1142+
"private_network": ipConfiguration.PrivateNetwork,
1143+
"require_ssl": ipConfiguration.RequireSsl,
11291144
}
11301145

11311146
if ipConfiguration.AuthorizedNetworks != nil {
@@ -1204,6 +1219,7 @@ func flattenIpAddresses(ipAddresses []*sqladmin.IpMapping) []map[string]interfac
12041219
for _, ip := range ipAddresses {
12051220
data := map[string]interface{}{
12061221
"ip_address": ip.IpAddress,
1222+
"type": ip.Type,
12071223
"time_to_retire": ip.TimeToRetire,
12081224
}
12091225

google-beta/resource_sql_database_instance_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,30 @@ func testAccCheckGoogleSqlDatabaseInstanceExists(n string,
853853
}
854854
}
855855

856+
func TestAccSqlDatabaseInstance_withPrivateNetwork(t *testing.T) {
857+
t.Parallel()
858+
859+
databaseName := "tf-test-" + acctest.RandString(10)
860+
networkName := "tf-test-" + acctest.RandString(10)
861+
addressName := "tf-test-" + acctest.RandString(10)
862+
863+
resource.Test(t, resource.TestCase{
864+
PreCheck: func() { testAccPreCheck(t) },
865+
Providers: testAccProviders,
866+
CheckDestroy: testAccSqlDatabaseInstanceDestroy,
867+
Steps: []resource.TestStep{
868+
resource.TestStep{
869+
Config: testAccSqlDatabaseInstance_withPrivateNetwork(databaseName, networkName, addressName),
870+
},
871+
resource.TestStep{
872+
ResourceName: "google_sql_database_instance.instance",
873+
ImportState: true,
874+
ImportStateVerify: true,
875+
},
876+
},
877+
})
878+
}
879+
856880
func testAccSqlDatabaseInstanceDestroy(s *terraform.State) error {
857881
for _, rs := range s.RootModule().Resources {
858882
config := testAccProvider.Meta().(*Config)
@@ -974,6 +998,42 @@ resource "google_sql_database_instance" "instance-failover" {
974998
`, instanceName, failoverName)
975999
}
9761000

1001+
func testAccSqlDatabaseInstance_withPrivateNetwork(databaseName, networkName, addressRangeName string) string {
1002+
return fmt.Sprintf(`
1003+
resource "google_compute_network" "foobar" {
1004+
name = "%s"
1005+
auto_create_subnetworks = false
1006+
}
1007+
1008+
resource "google_compute_global_address" "foobar" {
1009+
name = "%s"
1010+
purpose = "VPC_PEERING"
1011+
address_type = "INTERNAL"
1012+
prefix_length = 16
1013+
network = "${google_compute_network.foobar.self_link}"
1014+
}
1015+
1016+
resource "google_service_networking_connection" "foobar" {
1017+
network = "${google_compute_network.foobar.self_link}"
1018+
service = "servicenetworking.googleapis.com"
1019+
reserved_peering_ranges = ["${google_compute_global_address.foobar.name}"]
1020+
}
1021+
1022+
resource "google_sql_database_instance" "instance" {
1023+
depends_on = ["google_service_networking_connection.foobar"]
1024+
name = "%s"
1025+
region = "us-central1"
1026+
settings {
1027+
tier = "db-f1-micro"
1028+
ip_configuration {
1029+
ipv4_enabled = "false"
1030+
private_network = "${google_compute_network.foobar.self_link}"
1031+
}
1032+
}
1033+
}
1034+
`, networkName, addressRangeName, databaseName)
1035+
}
1036+
9771037
var testGoogleSqlDatabaseInstance_settings = `
9781038
resource "google_sql_database_instance" "instance" {
9791039
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)