forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spanner: added
google_spanner_database
data source (GoogleCloudPlat…
- Loading branch information
Showing
4 changed files
with
148 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
46 changes: 46 additions & 0 deletions
46
mmv1/third_party/terraform/services/spanner/data_source_spanner_database.go
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,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 | ||
} |
65 changes: 65 additions & 0 deletions
65
mmv1/third_party/terraform/services/spanner/data_source_spanner_database_test.go
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,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) | ||
} |
36 changes: 36 additions & 0 deletions
36
mmv1/third_party/terraform/website/docs/d/spanner_database.html.markdown
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,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. |