Skip to content

Commit ea5767b

Browse files
Database (#7054) (#13376)
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent 54daef4 commit ea5767b

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

.changelog/7054.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
google_sql_database
3+
```

google/data_source_sql_database.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
)
8+
9+
func dataSourceSqlDatabase() *schema.Resource {
10+
11+
dsSchema := datasourceSchemaFromResourceSchema(resourceSQLDatabase().Schema)
12+
addRequiredFieldsToSchema(dsSchema, "name")
13+
addRequiredFieldsToSchema(dsSchema, "instance")
14+
addOptionalFieldsToSchema(dsSchema, "project")
15+
16+
return &schema.Resource{
17+
Read: dataSourceSqlDatabaseRead,
18+
Schema: dsSchema,
19+
}
20+
}
21+
22+
func dataSourceSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error {
23+
config := meta.(*Config)
24+
project, err := getProject(d, config)
25+
if err != nil {
26+
return fmt.Errorf("Error fetching project for Database: %s", err)
27+
}
28+
d.SetId(fmt.Sprintf("projects/%s/instances/%s/databases/%s", project, d.Get("instance").(string), d.Get("name").(string)))
29+
err = resourceSQLDatabaseRead(d, meta)
30+
if err != nil {
31+
return err
32+
}
33+
if err := d.Set("deletion_policy", nil); err != nil {
34+
return fmt.Errorf("Error setting deletion_policy: %s", err)
35+
}
36+
return nil
37+
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package google
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccDataSourceSqlDatabase_basic(t *testing.T) {
10+
t.Parallel()
11+
12+
context := map[string]interface{}{
13+
"random_suffix": randString(t, 10),
14+
}
15+
16+
vcrTest(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
CheckDestroy: testAccSqlDatabaseDestroyProducer(t),
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testAccDataSourceSqlDatabase_basic(context),
23+
Check: resource.ComposeTestCheckFunc(
24+
checkDataSourceStateMatchesResourceStateWithIgnores(
25+
"data.google_sql_database.qa",
26+
"google_sql_database.db",
27+
map[string]struct{}{
28+
"deletion_policy": {},
29+
},
30+
),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
37+
func testAccDataSourceSqlDatabase_basic(context map[string]interface{}) string {
38+
return Nprintf(`
39+
resource "google_sql_database_instance" "main" {
40+
name = "tf-test-instance-%{random_suffix}"
41+
database_version = "POSTGRES_14"
42+
region = "us-central1"
43+
44+
settings {
45+
tier = "db-f1-micro"
46+
}
47+
48+
deletion_protection = false
49+
}
50+
51+
resource "google_sql_database" "db" {
52+
name = "tf-test-db-%{random_suffix}"
53+
instance = google_sql_database_instance.main.name
54+
depends_on = [
55+
google_sql_database_instance.main
56+
]
57+
}
58+
59+
data "google_sql_database" "qa" {
60+
name = google_sql_database.db.name
61+
instance = google_sql_database_instance.main.name
62+
depends_on = [
63+
google_sql_database.db
64+
]
65+
}
66+
`, context)
67+
}

google/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ func Provider() *schema.Provider {
937937
"google_spanner_instance": dataSourceSpannerInstance(),
938938
"google_sql_ca_certs": dataSourceGoogleSQLCaCerts(),
939939
"google_sql_backup_run": dataSourceSqlBackupRun(),
940+
"google_sql_database": dataSourceSqlDatabase(),
940941
"google_sql_database_instance": dataSourceSqlDatabaseInstance(),
941942
"google_service_networking_peered_dns_domain": dataSourceGoogleServiceNetworkingPeeredDNSDomain(),
942943
"google_storage_bucket": dataSourceGoogleStorageBucket(),
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
subcategory: "Cloud SQL"
3+
page_title: "Google: google_sql_database"
4+
description: |-
5+
Get a database in a Cloud SQL database instance.
6+
---
7+
8+
# google\_sql\_database
9+
10+
Use this data source to get information about a database in a Cloud SQL instance.
11+
12+
## Example Usage
13+
14+
15+
```hcl
16+
data "google_sql_database" "qa" {
17+
name = "test-sql-database"
18+
instance = google_sql_database_instance.main.name
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
The following arguments are supported:
25+
26+
* `name` - (required) The name of the database.
27+
28+
* `instance` - (required) The name of the Cloud SQL database instance in which the database belongs.
29+
30+
* `project` - (optional) The ID of the project in which the instance belongs.
31+
32+
## Attributes Reference
33+
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 commit comments

Comments
 (0)