Skip to content

Commit b19536a

Browse files
Adding in datasource for google_alloydb_cluster (#13055) (#9361)
[upstream:6f9f724c63b6c5e9a33e61bfa0bdf04b3c5e54c1] Signed-off-by: Modular Magician <[email protected]>
1 parent 7621529 commit b19536a

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

.changelog/13055.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_alloydb_cluster`
3+
```

google-beta/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
170170
"google_access_approval_project_service_account": accessapproval.DataSourceAccessApprovalProjectServiceAccount(),
171171
"google_access_context_manager_access_policy": accesscontextmanager.DataSourceAccessContextManagerAccessPolicy(),
172172
"google_active_folder": resourcemanager.DataSourceGoogleActiveFolder(),
173+
"google_alloydb_cluster": alloydb.DataSourceAlloydbDatabaseCluster(),
173174
"google_alloydb_locations": alloydb.DataSourceAlloydbLocations(),
174175
"google_alloydb_supported_database_flags": alloydb.DataSourceAlloydbSupportedDatabaseFlags(),
175176
"google_alloydb_instance": alloydb.DataSourceAlloydbDatabaseInstance(),
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package alloydb
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
10+
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
11+
)
12+
13+
func DataSourceAlloydbDatabaseCluster() *schema.Resource {
14+
// Generate datasource schema from resource
15+
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceAlloydbCluster().Schema)
16+
17+
// Set 'Required' schema elements
18+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "cluster_id")
19+
20+
// Set 'Optional' schema elements
21+
tpgresource.AddOptionalFieldsToSchema(dsSchema, "location", "project")
22+
23+
return &schema.Resource{
24+
Read: dataSourceAlloydbDatabaseClusterRead,
25+
Schema: dsSchema,
26+
}
27+
}
28+
29+
func dataSourceAlloydbDatabaseClusterRead(d *schema.ResourceData, meta interface{}) error {
30+
config := meta.(*transport_tpg.Config)
31+
32+
// Get location for setting location field in resource
33+
location, err := tpgresource.GetLocation(d, config)
34+
if err != nil {
35+
return err
36+
}
37+
38+
// Store the ID now
39+
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/clusters/{{cluster_id}}")
40+
if err != nil {
41+
return fmt.Errorf("Error constructing id: %s", err)
42+
}
43+
d.SetId(id)
44+
45+
// Setting location field as this is set as a required field in cluster resource
46+
d.Set("location", location)
47+
48+
err = resourceAlloydbClusterRead(d, meta)
49+
if err != nil {
50+
return err
51+
}
52+
53+
if err := tpgresource.SetDataSourceLabels(d); err != nil {
54+
return err
55+
}
56+
57+
if d.Id() == "" {
58+
return fmt.Errorf("%s not found", id)
59+
}
60+
return nil
61+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package alloydb_test
4+
5+
import (
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
10+
)
11+
12+
func TestAccAlloydbDatabaseClusterDatasourceConfig(t *testing.T) {
13+
t.Parallel()
14+
15+
context := map[string]interface{}{
16+
"random_suffix": acctest.RandString(t, 10),
17+
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-cluster-ds"),
18+
}
19+
20+
acctest.VcrTest(t, resource.TestCase{
21+
PreCheck: func() { acctest.AccTestPreCheck(t) },
22+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
23+
CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t),
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testAccAlloydbDatabaseClusterDatasourceConfig(context),
27+
},
28+
},
29+
})
30+
}
31+
32+
func testAccAlloydbDatabaseClusterDatasourceConfig(context map[string]interface{}) string {
33+
return acctest.Nprintf(`
34+
resource "google_alloydb_cluster" "default" {
35+
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
36+
location = "us-central1"
37+
network_config {
38+
network = data.google_compute_network.default.id
39+
}
40+
initial_user {
41+
password = "tf-test-alloydb-cluster%{random_suffix}"
42+
}
43+
}
44+
45+
data "google_compute_network" "default" {
46+
name = "%{network_name}"
47+
}
48+
49+
data "google_alloydb_cluster" "default" {
50+
cluster_id = google_alloydb_cluster.default.cluster_id
51+
location = "us-central1"
52+
}
53+
`, context)
54+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
subcategory: "AlloyDB"
3+
description: |-
4+
Fetches the details of available cluster.
5+
---
6+
7+
# google_alloydb_cluster
8+
9+
Use this data source to get information about the available cluster. For more details refer the [API docs](https://cloud.google.com/alloydb/docs/reference/rest/v1/projects.locations.clusters).
10+
11+
## Example Usage
12+
13+
14+
```hcl
15+
data "google_alloydb_cluster" "qa" {
16+
}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `cluster_id` -
24+
(Required)
25+
The ID of the alloydb cluster that the instance belongs to.
26+
'alloydb_cluster_id'
27+
28+
* `project` -
29+
(optional)
30+
The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
31+
32+
* `location` -
33+
(optional)
34+
The canonical id of the location.If it is not provided, the provider project is used. For example: us-east1.
35+
36+
## Attributes Reference
37+
38+
See [google_alloydb_cluster](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/alloydb_cluster) resource for details of all the available attributes.

0 commit comments

Comments
 (0)