Skip to content

Commit

Permalink
Database (#7054) (#13376)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Jan 3, 2023
1 parent 54daef4 commit ea5767b
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/7054.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
google_sql_database
```
37 changes: 37 additions & 0 deletions google/data_source_sql_database.go
Original file line number Diff line number Diff line change
@@ -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
}
67 changes: 67 additions & 0 deletions google/data_source_sql_database_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
33 changes: 33 additions & 0 deletions website/docs/d/sql_database.html.markdown
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit ea5767b

Please sign in to comment.