Skip to content
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

feat(GaussDB): add gaussdb opengauss datastores data source #6031

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
44 changes: 44 additions & 0 deletions docs/data-sources/gaussdb_opengauss_datastores.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
subcategory: "GaussDB"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_datastores"
description: |-
Use this data source to get the list of GaussDB OpenGauss engines.
---

# huaweicloud_gaussdb_opengauss_datastores

Use this data source to get the list of GaussDB OpenGauss engines.

## Example Usage

```hcl
data "huaweicloud_gaussdb_opengauss_datastores" "test" {}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String) Specifies the region in which to query the resource.
If omitted, the provider-level region will be used.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The data source ID.

* `datastores` - Specifies the DB engines.

The [datastores](#datastores_struct) structure is documented below.

<a name="datastores_struct"></a>
The `datastores` block supports:

* `supported_versions` - Specifies the engine versions supported by the deployment model.

* `instance_mode` - Specifies the deployment model.
The value can be:
+ **ha**: primary/standby
+ **independent**: independent
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,7 @@ func Provider() *schema.Provider {
"huaweicloud_gaussdb_redis_flavors": geminidb.DataSourceGaussDBRedisFlavors(),
"huaweicloud_gaussdb_influx_instances": geminidb.DataSourceGaussDBInfluxInstances(),

"huaweicloud_gaussdb_opengauss_datastores": gaussdb.DataSourceGaussdbOpengaussDatastores(),
"huaweicloud_gaussdb_opengauss_flavors": gaussdb.DataSourceGaussdbOpengaussFlavors(),
"huaweicloud_gaussdb_opengauss_instance": gaussdb.DataSourceOpenGaussInstance(),
"huaweicloud_gaussdb_opengauss_instances": gaussdb.DataSourceOpenGaussInstances(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gaussdb

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccDataSourceGaussdbOpengaussDatastores_basic(t *testing.T) {
dataSource := "data.huaweicloud_gaussdb_opengauss_datastores.test"
dc := acceptance.InitDataSourceCheck(dataSource)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceGaussdbOpengaussDatastores_basic(),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSource, "datastores.#"),
resource.TestCheckResourceAttrSet(dataSource, "datastores.0.supported_versions.#"),
resource.TestCheckResourceAttrSet(dataSource, "datastores.0.supported_versions.0"),
resource.TestCheckResourceAttrSet(dataSource, "datastores.0.instance_mode"),
),
},
},
})
}

func testDataSourceGaussdbOpengaussDatastores_basic() string {
return `
data "huaweicloud_gaussdb_opengauss_datastores" "test" {}
`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Generated by PMS #473
package gaussdb

import (
"context"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tidwall/gjson"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
)

func DataSourceGaussdbOpengaussDatastores() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGaussdbOpengaussDatastoresRead,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`,
},
"datastores": {
Type: schema.TypeList,
Computed: true,
Description: `Specifies the DB engines.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"supported_versions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `Specifies the engine versions supported by the deployment model.`,
},
"instance_mode": {
Type: schema.TypeString,
Computed: true,
Description: `Specifies the deployment model.`,
},
},
},
},
},
}
}

type OpengaussDatastoresDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}

func newOpengaussDatastoresDSWrapper(d *schema.ResourceData, meta interface{}) *OpengaussDatastoresDSWrapper {
return &OpengaussDatastoresDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}

func dataSourceGaussdbOpengaussDatastoresRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newOpengaussDatastoresDSWrapper(d, meta)
lisGauDbDatRst, err := wrapper.ListGaussDbDatastores()
if err != nil {
return diag.FromErr(err)
}

id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)

err = wrapper.listGaussDbDatastoresToSchema(lisGauDbDatRst)
if err != nil {
return diag.FromErr(err)
}

return nil
}

// @API GaussDB GET /v3/{project_id}/datastores
func (w *OpengaussDatastoresDSWrapper) ListGaussDbDatastores() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "opengauss")
if err != nil {
return nil, err
}

uri := "/v3/{project_id}/datastores"
return httphelper.New(client).
Method("GET").
URI(uri).
Request().
Result()
}

func (w *OpengaussDatastoresDSWrapper) listGaussDbDatastoresToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("datastores", schemas.SliceToList(body.Get("datastores"),
func(datastores gjson.Result) any {
return map[string]any{
"supported_versions": schemas.SliceToStrList(datastores.Get("supported_versions")),
"instance_mode": datastores.Get("instance_mode").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}
Loading