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

Add convenience outputs for public/private IP in Cloud SQL #3091

Merged
merged 1 commit into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func resourceSqlDatabaseInstance() *schema.Resource {
"private_network": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateRegexp(privateNetworkLinkRegex),
ValidateFunc: orEmpty(validateRegexp(privateNetworkLinkRegex)),
DiffSuppressFunc: compareSelfLinkRelativePaths,
},
},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
}
Expand Down
35 changes: 26 additions & 9 deletions website/docs/r/sql_database_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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. For example, when connecting with [Cloud SQL Proxy](https://cloud.google.com/sql/docs/mysql/connect-admin-proxy).

* `connection_name` - The connection name of the instance to be used in connection strings.
* `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.
Expand All @@ -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
Expand Down