Skip to content

Adding in datasource for google_alloydb_instance #9307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/12940.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
`google_alloydb_instance`
```
1 change: 1 addition & 0 deletions google-beta/provider/provider_mmv1_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
"google_active_folder": resourcemanager.DataSourceGoogleActiveFolder(),
"google_alloydb_locations": alloydb.DataSourceAlloydbLocations(),
"google_alloydb_supported_database_flags": alloydb.DataSourceAlloydbSupportedDatabaseFlags(),
"google_alloydb_instance": alloydb.DataSourceAlloydbDatabaseInstance(),
"google_artifact_registry_docker_image": artifactregistry.DataSourceArtifactRegistryDockerImage(),
"google_artifact_registry_locations": artifactregistry.DataSourceGoogleArtifactRegistryLocations(),
"google_artifact_registry_repository": artifactregistry.DataSourceArtifactRegistryRepository(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package alloydb

import (
"fmt"

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

func DataSourceAlloydbDatabaseInstance() *schema.Resource {
// Generate datasource schema from resource
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceAlloydbInstance().Schema)
// Set custom fields
dsScema_cluster_id := map[string]*schema.Schema{
"cluster_id": {
Type: schema.TypeString,
Required: true,
Description: `The ID of the alloydb cluster that the instance belongs to.'alloydb_cluster_id'`,
},
"project": {
Type: schema.TypeString,
Optional: true,
Description: `Project ID of the project.`,
},
"location": {
Type: schema.TypeString,
Optional: true,
Description: `The canonical ID for the location. For example: "us-east1".`,
},
}
tpgresource.AddRequiredFieldsToSchema(dsSchema, "instance_id")

// Set 'Required' schema elements
dsSchema_m := tpgresource.MergeSchemas(dsScema_cluster_id, dsSchema)

return &schema.Resource{
Read: dataSourceAlloydbDatabaseInstanceRead,
Schema: dsSchema_m,
}
}

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

// Get feilds for setting cluster field in resource
cluster_id := d.Get("cluster_id").(string)

location, err := tpgresource.GetLocation(d, config)
if err != nil {
return err
}
project, err := tpgresource.GetProject(d, config)
if err != nil {
return err
}
// Store the ID now
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/clusters/{{cluster_id}}/instances/{{instance_id}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

// Setting cluster field as this is set as a required field in instance resource
d.Set("cluster", fmt.Sprintf("projects/%s/locations/%s/clusters/%s", project, location, cluster_id))

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

if err := tpgresource.SetDataSourceLabels(d); 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 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package alloydb_test

import (
"testing"

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

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

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-instance-mandatory-1"),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckAlloydbInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccAlloydbDatabaseInstanceDatasourceConfig(context),
},
},
})
}

func testAccAlloydbDatabaseInstanceDatasourceConfig(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_alloydb_instance" "default" {
cluster = google_alloydb_cluster.default.name
instance_id = "tf-test-alloydb-instance%{random_suffix}"
instance_type = "PRIMARY"

machine_config {
cpu_count = 2
}
}

resource "google_alloydb_cluster" "default" {
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
location = "us-central1"
network_config {
network = data.google_compute_network.default.id
}
initial_user {
password = "tf-test-alloydb-cluster%{random_suffix}"
}
}

data "google_compute_network" "default" {
name = "%{network_name}"
}

data "google_alloydb_instance" "default" {
cluster_id = google_alloydb_cluster.default.cluster_id
instance_id = google_alloydb_instance.default.instance_id
location = "us-central1"
}
`, context)
}
43 changes: 43 additions & 0 deletions website/docs/d/alloydb_instance.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
subcategory: "AlloyDB"
description: |-
Fetches the details of available instance.
---

# google_alloydb_instance

Use this data source to get information about the available instance. For more details refer the [API docs](https://cloud.google.com/alloydb/docs/reference/rest/v1/projects.locations.clusters.instances).

## Example Usage


```hcl
data "google_alloydb_instance" "qa" {
}
```

## Argument Reference

The following arguments are supported:

* `cluster_id` -
(Required)
The ID of the alloydb cluster that the instance belongs to.
'alloydb_cluster_id'

* `instance_id` -
(Required)
The ID of the alloydb instance.
'alloydb_instance_id'

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

* `location` -
(optional)
The canonical id of the location.If it is not provided, the provider project is used. For example: us-east1.

## Attributes Reference

See [google_alloydb_instance](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/alloydb_instance) resource for details of all the available attributes.