Skip to content

Commit

Permalink
spanner: added google_spanner_database data source (GoogleCloudPlat…
Browse files Browse the repository at this point in the history
  • Loading branch information
wyardley authored and BBBmau committed Nov 5, 2024
1 parent 7489708 commit 7eddcfd
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
"google_service_accounts": resourcemanager.DataSourceGoogleServiceAccounts(),
"google_site_verification_token": siteverification.DataSourceSiteVerificationToken(),
"google_sourcerepo_repository": sourcerepo.DataSourceGoogleSourceRepoRepository(),
"google_spanner_database": spanner.DataSourceSpannerDatabase(),
"google_spanner_instance": spanner.DataSourceSpannerInstance(),
"google_sql_ca_certs": sql.DataSourceGoogleSQLCaCerts(),
"google_sql_tiers": sql.DataSourceGoogleSQLTiers(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package spanner

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

func DataSourceSpannerDatabase() *schema.Resource {

dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceSpannerDatabase().Schema)

tpgresource.AddRequiredFieldsToSchema(dsSchema, "name")
tpgresource.AddRequiredFieldsToSchema(dsSchema, "instance")

tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")

return &schema.Resource{
Read: dataSourceSpannerDatabaseRead,
Schema: dsSchema,
}
}

func dataSourceSpannerDatabaseRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)

id, err := tpgresource.ReplaceVars(d, config, "{{instance}}/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}

d.SetId(id)

err = resourceSpannerDatabaseRead(d, meta)
if err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package spanner_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-google/google/acctest"
)

func TestAccDataSourceSpannerDatabase_basic(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckSpannerDatabaseDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceSpannerDatabaseBasic(context),
Check: resource.ComposeTestCheckFunc(
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
"data.google_spanner_database.bar",
"google_spanner_database.foo",
map[string]struct{}{
"ddl.#": {},
"ddl.0": {},
"deletion_protection": {},
},
),
),
},
},
})
}

func testAccDataSourceSpannerDatabaseBasic(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_spanner_instance" "instance" {
name = "tf-test-instance-%{random_suffix}"
display_name = "Test spanner instance"
config = "regional-us-central1"
processing_units = 200
}
resource "google_spanner_database" "foo" {
name = "tf-test-db-%{random_suffix}"
instance = google_spanner_instance.instance.name
ddl = [
"CREATE TABLE t1 (t1 INT64 NOT NULL,) PRIMARY KEY(t1)",
]
deletion_protection = false
}
data "google_spanner_database" "bar" {
name = google_spanner_database.foo.name
instance = google_spanner_instance.instance.name
}
`, context)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
subcategory: "Cloud Spanner"
description: |-
Get a spanner database from Google Cloud
---

# google_spanner_database

Get a spanner database from Google Cloud by its name and instance name.

## Example Usage

```tf
data "google_spanner_database" "foo" {
name = "foo"
instance = google_spanner_instance.instance.name
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the spanner database.

* `instance` - (Required) The name of the database's spanner instance.

- - -

* `project` - (Optional) The project in which the resource belongs. If it
is not provided, the provider project is used.

## Attributes Reference
See [google_spanner_database](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/spanner_database) resource for details of all the available attributes.

**Note** `ddl` is a field where reads are ignored, and thus will show up with a null value.

0 comments on commit 7eddcfd

Please sign in to comment.