-
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.
Browse files
Browse the repository at this point in the history
* Add SQL Backup Run Datasource * Rename dataSourceSqlDatabaseInstanceBackupRun -> dataSourceSqlBackupRun * bump bootstrap timeout * Skip VCR Signed-off-by: Modular Magician <magic-modules@google.com>
- Loading branch information
1 parent
49662ee
commit cc87c28
Showing
7 changed files
with
318 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_backup_run` | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
sqladmin "google.golang.org/api/sqladmin/v1beta4" | ||
) | ||
|
||
func dataSourceSqlBackupRun() *schema.Resource { | ||
|
||
return &schema.Resource{ | ||
Read: dataSourceSqlBackupRunRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"backup_id": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Computed: true, | ||
Description: `The identifier for this backup run. Unique only for a specific Cloud SQL instance. If left empty and multiple backups exist for the instance, most_recent must be set to true.`, | ||
}, | ||
"instance": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: `Name of the database instance.`, | ||
}, | ||
"location": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Location of the backups.`, | ||
}, | ||
"start_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The time the backup operation actually started in UTC timezone in RFC 3339 format, for example 2012-11-15T16:19:00.094Z.`, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The status of this run.`, | ||
}, | ||
"most_recent": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Description: `Toggles use of the most recent backup run if multiple backups exist for a Cloud SQL instance.`, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceSqlBackupRunRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
userAgent, err := generateUserAgentString(d, config.userAgent) | ||
if err != nil { | ||
return err | ||
} | ||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
instance := d.Get("instance").(string) | ||
|
||
var backup *sqladmin.BackupRun | ||
if backupId, ok := d.GetOk("backup_id"); ok { | ||
backup, err = config.NewSqlAdminClient(userAgent).BackupRuns.Get(project, instance, int64(backupId.(int))).Do() | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
res, err := config.NewSqlAdminClient(userAgent).BackupRuns.List(project, instance).Do() | ||
if err != nil { | ||
return err | ||
} | ||
backupsList := res.Items | ||
if len(backupsList) == 0 { | ||
return fmt.Errorf("No backups found for SQL Database Instance %s", instance) | ||
} else if len(backupsList) > 1 { | ||
mostRecent := d.Get("most_recent").(bool) | ||
if !mostRecent { | ||
return fmt.Errorf("Multiple SQL backup runs listed for Instance %s. Consider setting most_recent or specifying a backup_id", instance) | ||
} | ||
} | ||
backup = backupsList[0] | ||
} | ||
|
||
if err := d.Set("backup_id", backup.Id); err != nil { | ||
return fmt.Errorf("Error setting backup_id: %s", err) | ||
} | ||
if err := d.Set("location", backup.Location); err != nil { | ||
return fmt.Errorf("Error setting location: %s", err) | ||
} | ||
if err := d.Set("start_time", backup.StartTime); err != nil { | ||
return fmt.Errorf("Error setting start_time: %s", err) | ||
} | ||
if err := d.Set("status", backup.Status); err != nil { | ||
return fmt.Errorf("Error setting status: %s", err) | ||
} | ||
|
||
id, err := replaceVars(d, config, "projects/{{project}}/instances/{{instance}}/backupRuns/{{backup_id}}") | ||
if err != nil { | ||
return fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
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,85 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceSqlBackupRun_basic(t *testing.T) { | ||
// Sqladmin client | ||
skipIfVcr(t) | ||
t.Parallel() | ||
|
||
instance := BootstrapSharedSQLInstanceBackupRun(t) | ||
|
||
vcrTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceSqlBackupRun_basic(instance), | ||
Check: resource.TestMatchResourceAttr("data.google_sql_backup_run.backup", "status", regexp.MustCompile("SUCCESSFUL")), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceSqlBackupRun_notFound(t *testing.T) { | ||
// Sqladmin client | ||
skipIfVcr(t) | ||
t.Parallel() | ||
|
||
context := map[string]interface{}{ | ||
"random_suffix": randString(t, 10), | ||
} | ||
|
||
vcrTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceSqlBackupRun_notFound(context), | ||
ExpectError: regexp.MustCompile("No backups found for SQL Database Instance"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceSqlBackupRun_basic(instance string) string { | ||
return fmt.Sprintf(` | ||
data "google_sql_backup_run" "backup" { | ||
instance = "%s" | ||
most_recent = true | ||
} | ||
`, instance) | ||
} | ||
|
||
func testAccDataSourceSqlBackupRun_notFound(context map[string]interface{}) string { | ||
return Nprintf(` | ||
resource "google_sql_database_instance" "instance" { | ||
name = "tf-test-instance-%{random_suffix}" | ||
database_version = "POSTGRES_11" | ||
region = "us-central1" | ||
settings { | ||
tier = "db-f1-micro" | ||
backup_configuration { | ||
enabled = "false" | ||
} | ||
} | ||
deletion_protection = false | ||
} | ||
data "google_sql_backup_run" "backup" { | ||
instance = google_sql_database_instance.instance.name | ||
most_recent = true | ||
depends_on = [google_sql_database_instance.instance] | ||
} | ||
`, context) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
subcategory: "Cloud SQL" | ||
layout: "google" | ||
page_title: "Google: google_sql_backup_run" | ||
sidebar_current: "docs-google-datasource-sql-backup-run" | ||
description: |- | ||
Get a SQL backup run in Google Cloud SQL. | ||
--- | ||
|
||
# google\_sql\_backup\_run | ||
|
||
Use this data source to get information about a Cloud SQL instance backup run. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "google_sql_backup_run" "backup" { | ||
instance = google_sql_database_instance.master.name | ||
most_recent = true | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `instance` - (required) The name of the instance the backup is taken from. | ||
|
||
* `backup_id` - (optional) The identifier for this backup run. Unique only for a specific Cloud SQL instance. | ||
If left empty and multiple backups exist for the instance, `most_recent` must be set to `true`. | ||
|
||
* `most_recent` - (optional) Toggles use of the most recent backup run if multiple backups exist for a | ||
Cloud SQL instance. | ||
|
||
## Attributes Reference | ||
|
||
In addition to the arguments listed above, the following attributes are exported: | ||
|
||
* `location` - Location of the backups. | ||
|
||
* `start_time` - The time the backup operation actually started in UTC timezone in RFC 3339 format, for | ||
example 2012-11-15T16:19:00.094Z. | ||
|
||
* `status` - The status of this run. Refer to [API reference](https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1beta4/backupRuns#SqlBackupRunStatus) for possible status values. |
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