-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
828f664
commit c1a61d5
Showing
5 changed files
with
141 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 | ||
``` |
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,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 | ||
} |
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,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) | ||
} |
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,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. |