Skip to content

Commit

Permalink
Added a new datasource google_sql_database_instance_latest_recovery_t…
Browse files Browse the repository at this point in the history
…ime (#8683) (#6109)

* Added sql_database_instance_latest_recovery_time data source and tests for the same.

* Added documentation for latest_recovery_time data source

* Added link to API defination

* Moved datasource test to services/sql and corrected documentations

* Update data_source_sql_database_instance_latest_recovery_time_test.go

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Aug 17, 2023
1 parent dffaac5 commit 441c40a
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 0 deletions.
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-beta/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,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-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/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-beta/google-beta/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.

0 comments on commit 441c40a

Please sign in to comment.