diff --git a/build/terraform b/build/terraform index 88cb4d5cb227..edf00aee13b5 160000 --- a/build/terraform +++ b/build/terraform @@ -1 +1 @@ -Subproject commit 88cb4d5cb227d6c0af50e6e27bf90c705580ec1d +Subproject commit edf00aee13b56bde48444b17fecc10ceb2cb716d diff --git a/build/terraform-beta b/build/terraform-beta index 8c1ec018fb05..4e7b2bda2b62 160000 --- a/build/terraform-beta +++ b/build/terraform-beta @@ -1 +1 @@ -Subproject commit 8c1ec018fb05fc80e54d627360a81d0da67aef36 +Subproject commit 4e7b2bda2b62dc00c1f4c94c66f2fd70a5ac85b7 diff --git a/third_party/terraform/resources/resource_sql_database_instance.go b/third_party/terraform/resources/resource_sql_database_instance.go.erb similarity index 96% rename from third_party/terraform/resources/resource_sql_database_instance.go rename to third_party/terraform/resources/resource_sql_database_instance.go.erb index 8dcc259dea30..3d43b4b08f37 100644 --- a/third_party/terraform/resources/resource_sql_database_instance.go +++ b/third_party/terraform/resources/resource_sql_database_instance.go.erb @@ -1,3 +1,4 @@ +<% autogen_exception -%> package google import ( @@ -16,6 +17,10 @@ import ( "google.golang.org/api/sqladmin/v1beta4" ) +<% unless version.nil? || version == 'ga' -%> +const privateNetworkLinkRegex = "projects/(" + ProjectRegex + ")/global/networks/((?:[a-z](?:[-a-z0-9]*[a-z0-9])?))$" +<% end -%> + var sqlDatabaseAuthorizedNetWorkSchemaElem *schema.Resource = &schema.Resource{ Schema: map[string]*schema.Schema{ "expiration_time": &schema.Schema{ @@ -180,6 +185,14 @@ func resourceSqlDatabaseInstance() *schema.Resource { Type: schema.TypeBool, Optional: true, }, +<% unless version.nil? || version == 'ga' -%> + "private_network": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ValidateFunc: validateRegexp(privateNetworkLinkRegex), + DiffSuppressFunc: compareSelfLinkRelativePaths, + }, +<% end -%> }, }, }, @@ -265,6 +278,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, @@ -614,10 +631,15 @@ 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), +<% unless version.nil? || version == 'ga' -%> + PrivateNetwork: _ipConfiguration["private_network"].(string), +<% end -%> AuthorizedNetworks: expandAuthorizedNetworks(_ipConfiguration["authorized_networks"].(*schema.Set).List()), + ForceSendFields: []string{"Ipv4Enabled"}, } } func expandAuthorizedNetworks(configured []interface{}) []*sqladmin.AclEntry { @@ -696,7 +718,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") @@ -870,8 +891,11 @@ 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, +<% unless version.nil? || version == 'ga' -%> + "private_network": ipConfiguration.PrivateNetwork, +<% end -%> + "require_ssl": ipConfiguration.RequireSsl, } if ipConfiguration.AuthorizedNetworks != nil { @@ -950,6 +974,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, } diff --git a/third_party/terraform/tests/resource_sql_database_instance_test.go b/third_party/terraform/tests/resource_sql_database_instance_test.go.erb similarity index 92% rename from third_party/terraform/tests/resource_sql_database_instance_test.go rename to third_party/terraform/tests/resource_sql_database_instance_test.go.erb index 7dd072ff0da3..480aa23cee7b 100644 --- a/third_party/terraform/tests/resource_sql_database_instance_test.go +++ b/third_party/terraform/tests/resource_sql_database_instance_test.go.erb @@ -1,3 +1,4 @@ +<% autogen_exception -%> package google import ( @@ -594,6 +595,32 @@ func TestAccSqlDatabaseInstance_basic_with_user_labels(t *testing.T) { }) } +<% unless version.nil? || version == 'ga' -%> +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, + }, + }, + }) +} +<% end -%> + func testAccSqlDatabaseInstanceDestroy(s *terraform.State) error { for _, rs := range s.RootModule().Resources { config := testAccProvider.Meta().(*Config) @@ -714,6 +741,44 @@ resource "google_sql_database_instance" "instance-failover" { `, instanceName, failoverName) } +<% unless version.nil? || version == 'ga' -%> +func testAccSqlDatabaseInstance_withPrivateNetwork(databaseName, networkName, addressRangeName string) string { + return fmt.Sprintf(` +resource "google_compute_network" "foobar" { + 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) +} +<% end -%> + var testGoogleSqlDatabaseInstance_settings = ` resource "google_sql_database_instance" "instance" { name = "tf-lw-%d" diff --git a/third_party/terraform/website/docs/r/sql_database_instance.html.markdown b/third_party/terraform/website/docs/r/sql_database_instance.html.markdown index 7a2a2a5d9d63..661462e7e233 100644 --- a/third_party/terraform/website/docs/r/sql_database_instance.html.markdown +++ b/third_party/terraform/website/docs/r/sql_database_instance.html.markdown @@ -108,6 +108,42 @@ resource "google_sql_database_instance" "postgres" { } ``` +### Private IP Instance + + +```hcl +resource "google_compute_network" "private_network" { + name = "private_network" +} + +resource "google_compute_global_address" "private_ip_address" { + name = "private_ip_address" + purpose = "VPC_PEERING" + address_type = "INTERNAL" + prefix_length = 16 + network = "${google_compute_network.private_network.self_link}" +} + +resource "google_service_networking_connection" "private_vpc_connection" { + network = "${google_compute_network.private_network.self_link}" + service = "servicenetworking.googleapis.com" + reserved_peering_ranges = ["${google_compute_global_address.private_ip_address.name}"] +} + +resource "google_sql_database_instance" "instance" { + depends_on = ["google_service_networking_connection.private_vpc_connection"] + name = "private_instance" + region = "us-central1" + settings { + tier = "db-f1-micro" + ip_configuration { + ipv4_enabled = "false" + private_network = "${google_compute_network.private_network.self_link}" + } + } +} +``` + ## Argument Reference The following arguments are supported: @@ -205,6 +241,8 @@ The optional `settings.ip_configuration` subblock supports: * `require_ssl` - (Optional) True if mysqld should default to `REQUIRE X509` for users connecting over IP. +* `private_network` - (Optional, [Beta](https://terraform.io/docs/providers/google/provider_versions.html)) The resource link for the VPC network from which the Cloud SQL instance is accessible for private IP. + The optional `settings.ip_configuration.authorized_networks[]` sublist supports: * `expiration_time` - (Optional) The [RFC 3339](https://tools.ietf.org/html/rfc3339) @@ -286,6 +324,8 @@ when the resource is configured with a `count`. * `ip_address.0.time_to_retire` - The time this IP address will be retired, in RFC 3339 format. +* `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). + * `self_link` - The URI of the created resource. * `settings.version` - Used to make sure changes to the `settings` block are