From be8b92821e4520df114bbd44577fee16479e026c Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Tue, 3 Jan 2023 22:23:25 +0000 Subject: [PATCH] Database (#7054) Signed-off-by: Modular Magician --- .changelog/7054.txt | 3 + google/data_source_sql_database.go | 37 +++++++++++++ google/data_source_sql_database_test.go | 67 +++++++++++++++++++++++ google/provider.go | 1 + website/docs/d/sql_database.html.markdown | 33 +++++++++++ 5 files changed, 141 insertions(+) create mode 100644 .changelog/7054.txt create mode 100644 google/data_source_sql_database.go create mode 100644 google/data_source_sql_database_test.go create mode 100644 website/docs/d/sql_database.html.markdown diff --git a/.changelog/7054.txt b/.changelog/7054.txt new file mode 100644 index 00000000000..34af1f9b845 --- /dev/null +++ b/.changelog/7054.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +google_sql_database +``` diff --git a/google/data_source_sql_database.go b/google/data_source_sql_database.go new file mode 100644 index 00000000000..9d1d111d5fe --- /dev/null +++ b/google/data_source_sql_database.go @@ -0,0 +1,37 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceSqlDatabase() *schema.Resource { + + dsSchema := datasourceSchemaFromResourceSchema(resourceSQLDatabase().Schema) + addRequiredFieldsToSchema(dsSchema, "name") + addRequiredFieldsToSchema(dsSchema, "instance") + addOptionalFieldsToSchema(dsSchema, "project") + + return &schema.Resource{ + Read: dataSourceSqlDatabaseRead, + Schema: dsSchema, + } +} + +func dataSourceSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Database: %s", err) + } + d.SetId(fmt.Sprintf("projects/%s/instances/%s/databases/%s", project, d.Get("instance").(string), d.Get("name").(string))) + err = resourceSQLDatabaseRead(d, meta) + if err != nil { + return err + } + if err := d.Set("deletion_policy", nil); err != nil { + return fmt.Errorf("Error setting deletion_policy: %s", err) + } + return nil +} diff --git a/google/data_source_sql_database_test.go b/google/data_source_sql_database_test.go new file mode 100644 index 00000000000..867f294644d --- /dev/null +++ b/google/data_source_sql_database_test.go @@ -0,0 +1,67 @@ +package google + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccDataSourceSqlDatabase_basic(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccSqlDatabaseDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccDataSourceSqlDatabase_basic(context), + Check: resource.ComposeTestCheckFunc( + checkDataSourceStateMatchesResourceStateWithIgnores( + "data.google_sql_database.qa", + "google_sql_database.db", + map[string]struct{}{ + "deletion_policy": {}, + }, + ), + ), + }, + }, + }) +} + +func testAccDataSourceSqlDatabase_basic(context map[string]interface{}) string { + return Nprintf(` +resource "google_sql_database_instance" "main" { + name = "tf-test-instance-%{random_suffix}" + database_version = "POSTGRES_14" + region = "us-central1" + + settings { + tier = "db-f1-micro" + } + + deletion_protection = false +} + +resource "google_sql_database" "db" { + name = "tf-test-db-%{random_suffix}" + instance = google_sql_database_instance.main.name + depends_on = [ + google_sql_database_instance.main + ] +} + +data "google_sql_database" "qa" { + name = google_sql_database.db.name + instance = google_sql_database_instance.main.name + depends_on = [ + google_sql_database.db + ] +} +`, context) +} diff --git a/google/provider.go b/google/provider.go index c13a0f5d7c5..e6a07a71fb9 100644 --- a/google/provider.go +++ b/google/provider.go @@ -937,6 +937,7 @@ func Provider() *schema.Provider { "google_spanner_instance": dataSourceSpannerInstance(), "google_sql_ca_certs": dataSourceGoogleSQLCaCerts(), "google_sql_backup_run": dataSourceSqlBackupRun(), + "google_sql_database": dataSourceSqlDatabase(), "google_sql_database_instance": dataSourceSqlDatabaseInstance(), "google_service_networking_peered_dns_domain": dataSourceGoogleServiceNetworkingPeeredDNSDomain(), "google_storage_bucket": dataSourceGoogleStorageBucket(), diff --git a/website/docs/d/sql_database.html.markdown b/website/docs/d/sql_database.html.markdown new file mode 100644 index 00000000000..8f0a8996513 --- /dev/null +++ b/website/docs/d/sql_database.html.markdown @@ -0,0 +1,33 @@ +--- +subcategory: "Cloud SQL" +page_title: "Google: google_sql_database" +description: |- + Get a database in a Cloud SQL database instance. +--- + +# google\_sql\_database + +Use this data source to get information about a database in a Cloud SQL instance. + +## Example Usage + + +```hcl +data "google_sql_database" "qa" { + name = "test-sql-database" + instance = google_sql_database_instance.main.name +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (required) The name of the database. + +* `instance` - (required) The name of the Cloud SQL database instance in which the database belongs. + +* `project` - (optional) The ID of the project in which the instance belongs. + +## Attributes Reference +See [google_sql_database](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database) resource for details of all the available attributes.