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

Added a new datasource google_sql_database_instance_latest_recovery_time #15551

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
3 changes: 3 additions & 0 deletions .changelog/8683.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
`google_sql_database_instance_latest_recovery_time`
```
1 change: 1 addition & 0 deletions google/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ func DatasourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_spanner_instance": spanner.DataSourceSpannerInstance(),
"google_sql_ca_certs": sql.DataSourceGoogleSQLCaCerts(),
"google_sql_tiers": sql.DataSourceGoogleSQLTiers(),
"google_sql_database_instance_latest_recovery_time": sql.DataSourceSqlDatabaseInstanceLatestRecoveryTime(),
"google_sql_backup_run": sql.DataSourceSqlBackupRun(),
"google_sql_databases": sql.DataSourceSqlDatabases(),
"google_sql_database": sql.DataSourceSqlDatabase(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package sql

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

func DataSourceSqlDatabaseInstanceLatestRecoveryTime() *schema.Resource {
return &schema.Resource{
Read: dataSourceSqlDatabaseInstanceLatestRecoveryTimeRead,

Schema: map[string]*schema.Schema{
"instance": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
},
"project": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
"latest_recovery_time": {
Type: schema.TypeString,
Computed: true,
Description: `Timestamp, identifies the latest recovery time of the source instance.`,
},
},
}
}

func dataSourceSqlDatabaseInstanceLatestRecoveryTimeRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}

fv, err := tpgresource.ParseProjectFieldValue("instances", d.Get("instance").(string), "project", d, config, false)
if err != nil {
return err
}
project := fv.Project
instance := fv.Name

latestRecoveryTime, err := config.NewSqlAdminClient(userAgent).Projects.Instances.GetLatestRecoveryTime(project, instance).Do()
if err != nil {
return err
}

if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error setting project: %s", err)
}

if err := d.Set("latest_recovery_time", latestRecoveryTime.LatestRecoveryTime); err != nil {
return fmt.Errorf("Error setting latest_recovery_time: %s", err)
}
d.SetId(fmt.Sprintf("projects/%s/instance/%s", project, instance))
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package sql_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-google/google/acctest"
)

func TestAccDataSourceSqlDatabaseInstanceLatestRecoveryTime_basic(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}
resourceName := "data.google_sql_database_instance_latest_recovery_time.default"

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceSqlDatabaseInstanceLatestRecoveryTime_basic(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "instance"),
resource.TestCheckResourceAttrSet(resourceName, "project"),
resource.TestCheckResourceAttrSet(resourceName, "latest_recovery_time"),
),
},
},
})
}

func testAccDataSourceSqlDatabaseInstanceLatestRecoveryTime_basic(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_sql_database_instance" "main" {
name = "tf-test-instance-%{random_suffix}"
database_version = "POSTGRES_14"
region = "us-central1"

settings {
tier = "db-g1-small"
backup_configuration {
enabled = true
point_in_time_recovery_enabled = true
start_time = "20:55"
transaction_log_retention_days = "3"
}
}

deletion_protection = false
}

data "google_sql_database_instance_latest_recovery_time" "default" {
instance = resource.google_sql_database_instance.main.name
}
`, context)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
subcategory: "Cloud SQL"
description: |-
Get Latest Recovery Time for a given instance.
---

# google\_sql\_database\_instance\_latest\_recovery\_time

Get Latest Recovery Time for a given instance. For more information see the
[official documentation](https://cloud.google.com/sql/)
and
[API](https://cloud.google.com/sql/docs/postgres/backup-recovery/pitr#get-the-latest-recovery-time).


## Example Usage

```hcl
data "google_sql_database_instance_latest_recovery_time" "default" {
instance = "sample-instance"
}

output "latest_recovery_time" {
value = data.google_sql_database_instance_latest_recovery_time.default
}
```

## Argument Reference

The following arguments are supported:

* `instance` - (Required) The name of the instance.

## Attributes Reference

The following attributes are exported:

* `instance` - The name of the instance.
* `project` - The ID of the project in which the resource belongs.
* `latest_recovery_time` - Timestamp, identifies the latest recovery time of the source instance.