From 10be93463df935e5a8f1ddfeb1e00f57309ce615 Mon Sep 17 00:00:00 2001 From: The Magician Date: Wed, 20 Feb 2019 11:06:20 -0800 Subject: [PATCH] Add convenience outputs for public/private IP in Cloud SQL (#3091) /cc @rileykarson --- google/resource_sql_database_instance.go | 27 +++++++++++++- .../r/sql_database_instance.html.markdown | 35 ++++++++++++++----- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/google/resource_sql_database_instance.go b/google/resource_sql_database_instance.go index 28ca0fad..8af4fdc0 100644 --- a/google/resource_sql_database_instance.go +++ b/google/resource_sql_database_instance.go @@ -186,7 +186,7 @@ func resourceSqlDatabaseInstance() *schema.Resource { "private_network": { Type: schema.TypeString, Optional: true, - ValidateFunc: validateRegexp(privateNetworkLinkRegex), + ValidateFunc: orEmpty(validateRegexp(privateNetworkLinkRegex)), DiffSuppressFunc: compareSelfLinkRelativePaths, }, }, @@ -291,6 +291,16 @@ func resourceSqlDatabaseInstance() *schema.Resource { Computed: true, }, + "public_ip_address": { + Type: schema.TypeString, + Computed: true, + }, + + "private_ip_address": { + Type: schema.TypeString, + Computed: true, + }, + "name": { Type: schema.TypeString, Optional: true, @@ -721,6 +731,21 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e d.Set("first_ip_address", ipAddresses[0]["ip_address"]) } + publicIpAddress := "" + privateIpAddress := "" + for _, ip := range instance.IpAddresses { + if publicIpAddress == "" && ip.Type == "PRIMARY" { + publicIpAddress = ip.IpAddress + } + + if privateIpAddress == "" && ip.Type == "PRIVATE" { + privateIpAddress = ip.IpAddress + } + } + + d.Set("public_ip_address", publicIpAddress) + d.Set("private_ip_address", privateIpAddress) + if err := d.Set("server_ca_cert", flattenServerCaCert(instance.ServerCaCert)); err != nil { log.Printf("[WARN] Failed to set SQL Database CA Certificate") } diff --git a/website/docs/r/sql_database_instance.html.markdown b/website/docs/r/sql_database_instance.html.markdown index b5249580..70cbabde 100644 --- a/website/docs/r/sql_database_instance.html.markdown +++ b/website/docs/r/sql_database_instance.html.markdown @@ -313,20 +313,40 @@ to work, cannot be updated, and supports: In addition to the arguments listed above, the following computed attributes are exported: -* `first_ip_address` - The first IPv4 address of the addresses assigned. This is -is to support accessing the [first address in the list in a terraform output](https://github.com/terraform-providers/terraform-provider-google/issues/912) -when the resource is configured with a `count`. +* `self_link` - The URI of the created resource. -* `connection_name` - The connection name of the instance to be used in connection strings. +* `connection_name` - The connection name of the instance to be used in +connection strings. For example, when connecting with [Cloud SQL Proxy](https://cloud.google.com/sql/docs/mysql/connect-admin-proxy). + +* `service_account_email_address` - The service account email address assigned to the +instance. This property is applicable only to Second Generation instances. * `ip_address.0.ip_address` - The IPv4 address assigned. * `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). +* `ip_address.0.type` - The type of this IP address. -* `self_link` - The URI of the created resource. + * 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). + +* `first_ip_address` - The first IPv4 address of any type assigned. This is to +support accessing the [first address in the list in a terraform output](https://github.com/terraform-providers/terraform-provider-google/issues/912) +when the resource is configured with a `count`. + +* `public_ip_address` - The first public (`PRIMARY`) IPv4 address assigned. This is +a workaround for an [issue fixed in Terraform 0.12](https://github.com/hashicorp/terraform/issues/17048) +but also provides a convenient way to access an IP of a specific type without +performing filtering in a Terraform config. + +* `private_ip_address` - The first private (`PRIVATE`) IPv4 address assigned. This is +a workaround for an [issue fixed in Terraform 0.12](https://github.com/hashicorp/terraform/issues/17048) +but also provides a convenient way to access an IP of a specific type without +performing filtering in a Terraform config. * `settings.version` - Used to make sure changes to the `settings` block are atomic. @@ -341,9 +361,6 @@ when the resource is configured with a `count`. * `server_ca_cert.0.sha1_fingerprint` - SHA Fingerprint of the CA Cert. -* `service_account_email_address` - The service account email address assigned to the -instance. This property is applicable only to Second Generation instances. - ## Timeouts `google_sql_database_instance` provides the following