-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Terraform Data Source to retrieve SQL instance CA certs (#5306)
Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Chris Sng <[email protected]>
- Loading branch information
1 parent
90324a0
commit e2d493d
Showing
5 changed files
with
253 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func dataSourceGoogleSQLCaCerts() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGoogleSQLCaCertsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"instance": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
DiffSuppressFunc: compareSelfLinkOrResourceName, | ||
}, | ||
"project": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"active_version": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"certs": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"cert": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"common_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"create_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"expiration_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"sha1_fingerprint": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGoogleSQLCaCertsRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
fv, err := parseProjectFieldValue("instances", d.Get("instance").(string), "project", d, config, false) | ||
if err != nil { | ||
return err | ||
} | ||
project := fv.Project | ||
instance := fv.Name | ||
|
||
log.Printf("[DEBUG] Fetching CA certs from instance %s", instance) | ||
|
||
response, err := config.clientSqlAdmin.Instances.ListServerCas(project, instance).Do() | ||
if err != nil { | ||
return fmt.Errorf("error retrieving CA certs: %s", err) | ||
} | ||
|
||
log.Printf("[DEBUG] Fetched CA certs from instance %s", instance) | ||
|
||
d.Set("project", project) | ||
d.Set("certs", flattenServerCaCerts(response.Certs)) | ||
d.Set("active_version", response.ActiveVersion) | ||
d.SetId(fmt.Sprintf("projects/%s/instance/%s", project, instance)) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func TestAccDataSourceGoogleSQLCaCerts_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
instanceName := fmt.Sprintf("data-ssl-ca-cert-test-%s", acctest.RandString(10)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckComputeInstanceDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceGoogleSQLCaCertsConfig(instanceName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccDataSourceGoogleSQLCaCertsCheck("data.google_sql_ca_certs.ca_certs", "google_sql_database_instance.foo"), | ||
testAccDataSourceGoogleSQLCaCertsCheck("data.google_sql_ca_certs.ca_certs_self_link", "google_sql_database_instance.foo"), | ||
resource.TestCheckResourceAttr("data.google_sql_ca_certs.ca_certs", "certs.#", "1"), | ||
resource.TestCheckResourceAttr("data.google_sql_ca_certs.ca_certs_self_link", "certs.#", "1"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceGoogleSQLCaCertsCheck(datasourceName string, resourceName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
ds, ok := s.RootModule().Resources[datasourceName] | ||
if !ok { | ||
return fmt.Errorf("root module has no resource called %s", datasourceName) | ||
} | ||
|
||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("can't find %s in state", resourceName) | ||
} | ||
|
||
datasourceAttributes := ds.Primary.Attributes | ||
resourceAttributes := rs.Primary.Attributes | ||
|
||
instanceToDatasourceAttrsMapping := map[string]string{ | ||
"server_ca_cert.0.cert": "certs.0.cert", | ||
"server_ca_cert.0.common_name": "certs.0.common_name", | ||
"server_ca_cert.0.create_time": "certs.0.create_time", | ||
"server_ca_cert.0.expiration_time": "certs.0.expiration_time", | ||
"server_ca_cert.0.sha1_fingerprint": "certs.0.sha1_fingerprint", | ||
} | ||
|
||
for resourceAttr, datasourceAttr := range instanceToDatasourceAttrsMapping { | ||
if resourceAttributes[resourceAttr] != datasourceAttributes[datasourceAttr] { | ||
return fmt.Errorf( | ||
"%s is %s; want %s", | ||
datasourceAttr, | ||
datasourceAttributes[datasourceAttr], | ||
resourceAttributes[resourceAttr], | ||
) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccDataSourceGoogleSQLCaCertsConfig(instanceName string) string { | ||
return fmt.Sprintf(` | ||
resource "google_sql_database_instance" "foo" { | ||
name = "%s" | ||
region = "us-central1" | ||
settings { | ||
tier = "db-f1-micro" | ||
crash_safe_replication = false | ||
} | ||
} | ||
data "google_sql_ca_certs" "ca_certs" { | ||
instance = google_sql_database_instance.foo.name | ||
} | ||
data "google_sql_ca_certs" "ca_certs_self_link" { | ||
instance = google_sql_database_instance.foo.self_link | ||
} | ||
`, instanceName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
website/docs/d/datasource_google_sql_ca_certs.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
subcategory: "Cloud SQL" | ||
layout: "google" | ||
page_title: "Google: google_sql_ca_certs" | ||
sidebar_current: "docs-google-datasource-sql-ca-certs" | ||
description: |- | ||
Get all of the trusted Certificate Authorities (CAs) for the specified SQL database instance. | ||
--- | ||
|
||
# google\_sql\_ca\_certs | ||
|
||
Get all of the trusted Certificate Authorities (CAs) for the specified SQL database instance. For more information see the | ||
[official documentation](https://cloud.google.com/sql/) | ||
and | ||
[API](https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1beta4/instances/listServerCas). | ||
|
||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "google_sql_ca_certs" "ca_certs" { | ||
instance = "primary-database-server" | ||
} | ||
locals { | ||
furthest_expiration_time = reverse(sort([for k, v in data.google_sql_ca_certs.ca_certs.certs : v.expiration_time]))[0] | ||
latest_ca_cert = [for v in data.google_sql_ca_certs.ca_certs.certs : v.cert if v.expiration_time == local.furthest_expiration_time] | ||
} | ||
output "db_latest_ca_cert" { | ||
description = "Latest CA cert used by the primary database server" | ||
value = local.latest_ca_cert | ||
sensitive = true | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `instance` - (Required) The name or self link of the instance. | ||
|
||
--- | ||
|
||
* `project` - (Optional) The ID of the project in which the resource belongs. If `project` is not provided, the provider project is used. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `active_version` - SHA1 fingerprint of the currently active CA certificate. | ||
|
||
* `certs` - A list of server CA certificates for the instance. Each contains: | ||
* `cert` - The CA certificate used to connect to the SQL instance via SSL. | ||
* `common_name` - The CN valid for the CA cert. | ||
* `create_time` - Creation time of the CA cert. | ||
* `expiration_time` - Expiration time of the CA cert. | ||
* `sha1_fingerprint` - SHA1 fingerprint of the CA cert. |