-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a new datasource google_sql_database_instance_latest_recovery_t…
…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
1 parent
dffaac5
commit 441c40a
Showing
5 changed files
with
170 additions
and
0 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,3 @@ | ||
```release-note:new-datasource | ||
`google_sql_database_instance_latest_recovery_time` | ||
``` |
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
66 changes: 66 additions & 0 deletions
66
google-beta/services/sql/data_source_sql_database_instance_latest_recovery_time.go
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,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 | ||
} |
61 changes: 61 additions & 0 deletions
61
google-beta/services/sql/data_source_sql_database_instance_latest_recovery_time_test.go
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,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) | ||
} |
39 changes: 39 additions & 0 deletions
39
website/docs/d/sql_database_instance_latest_recovery_time.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,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. |