Skip to content

Commit

Permalink
feat(GaussDB): add gaussdb opengauss parameter templates data source (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
houpeng80 authored Dec 18, 2024
1 parent fe52479 commit 7eb7279
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 6 deletions.
58 changes: 58 additions & 0 deletions docs/data-sources/gaussdb_opengauss_parameter_templates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
subcategory: "GaussDB"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_parameter_templates"
description: |-
Use this data source to get the list of GaussDB OpenGauss parameter templates.
---

# huaweicloud_gaussdb_opengauss_parameter_templates

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

## Example Usage

```hcl
data "huaweicloud_gaussdb_opengauss_parameter_templates" "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.

* `configurations` - Indicates the parameter template information

The [configurations](#configurations_struct) structure is documented below.

<a name="configurations_struct"></a>
The `configurations` block supports:

* `id` - Indicates the parameter template ID, which is the unique ID of a parameter template.

* `name` - Indicates the parameter template name.

* `description` - Indicates the parameter template description.

* `datastore_version` - Indicates the engine version.

* `datastore_name` - Indicates the engine name.

* `ha_mode` - Indicates the instance type.

* `user_defined` - Indicates whether the parameter template is a custom template.
The value can be:
+ **false**: The parameter template is a default template.
+ **true**: The parameter template is a custom template.

* `created_at` - Indicates the creation time in the **yyyy-MM-dd HH:mm:ss** format.

* `updated_at` - Indicates the update time in the **yyyy-MM-dd HH:mm:ss** format.
13 changes: 7 additions & 6 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,12 +802,13 @@ 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(),
"huaweicloud_gaussdb_opengauss_instance_nodes": gaussdb.DataSourceGaussdbOpengaussInstanceNodes(),
"huaweicloud_gaussdb_opengauss_databases": gaussdb.DataSourceOpenGaussDatabases(),
"huaweicloud_gaussdb_opengauss_datastores": gaussdb.DataSourceGaussdbOpengaussDatastores(),
"huaweicloud_gaussdb_opengauss_flavors": gaussdb.DataSourceGaussdbOpengaussFlavors(),
"huaweicloud_gaussdb_opengauss_instance": gaussdb.DataSourceOpenGaussInstance(),
"huaweicloud_gaussdb_opengauss_instances": gaussdb.DataSourceOpenGaussInstances(),
"huaweicloud_gaussdb_opengauss_instance_nodes": gaussdb.DataSourceGaussdbOpengaussInstanceNodes(),
"huaweicloud_gaussdb_opengauss_databases": gaussdb.DataSourceOpenGaussDatabases(),
"huaweicloud_gaussdb_opengauss_parameter_templates": gaussdb.DataSourceGaussdbOpengaussParameterTemplates(),

"huaweicloud_gaussdb_mysql_engine_versions": taurusdb.DataSourceGaussdbMysqlEngineVersions(),
"huaweicloud_gaussdb_mysql_configuration": taurusdb.DataSourceGaussdbMysqlConfiguration(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package gaussdb

import (
"testing"

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

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

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceGaussdbOpengaussParameterTemplates_basic(),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSource, "configurations.#"),
resource.TestCheckResourceAttrSet(dataSource, "configurations.0.id"),
resource.TestCheckResourceAttrSet(dataSource, "configurations.0.name"),
resource.TestCheckResourceAttrSet(dataSource, "configurations.0.description"),
resource.TestCheckResourceAttrSet(dataSource, "configurations.0.datastore_version"),
resource.TestCheckResourceAttrSet(dataSource, "configurations.0.datastore_name"),
resource.TestCheckResourceAttrSet(dataSource, "configurations.0.ha_mode"),
resource.TestCheckResourceAttrSet(dataSource, "configurations.0.user_defined"),
resource.TestCheckResourceAttrSet(dataSource, "configurations.0.created_at"),
resource.TestCheckResourceAttrSet(dataSource, "configurations.0.updated_at"),
),
},
},
})
}

func testDataSourceGaussdbOpengaussParameterTemplates_basic() string {
return `
data "huaweicloud_gaussdb_opengauss_parameter_templates" "test" {}
`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Generated by PMS #477
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 DataSourceGaussdbOpengaussParameterTemplates() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGaussdbOpengaussParameterTemplatesRead,

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.`,
},
"configurations": {
Type: schema.TypeList,
Computed: true,
Description: `Indicates the parameter template information`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the parameter template ID, which is the unique ID of a parameter template.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the parameter template name.`,
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the parameter template description.`,
},
"datastore_version": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the engine version.`,
},
"datastore_name": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the engine name.`,
},
"ha_mode": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the instance type.`,
},
"user_defined": {
Type: schema.TypeBool,
Computed: true,
Description: `Indicates whether the parameter template is a custom template.`,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the creation time in the **yyyy-MM-dd HH:mm:ss** format.`,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the update time in the **yyyy-MM-dd HH:mm:ss** format.`,
},
},
},
},
},
}
}

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

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

func dataSourceGaussdbOpengaussParameterTemplatesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newOpengaussParameterTemplatesDSWrapper(d, meta)
lisParGroTemRst, err := wrapper.ListParamGroupTemplates()
if err != nil {
return diag.FromErr(err)
}

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

err = wrapper.listParamGroupTemplatesToSchema(lisParGroTemRst)
if err != nil {
return diag.FromErr(err)
}

return nil
}

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

uri := "/v3.1/{project_id}/configurations"
return httphelper.New(client).
Method("GET").
URI(uri).
OffsetPager("configurations", "offset", "limit", 0).
Request().
Result()
}

func (w *OpengaussParameterTemplatesDSWrapper) listParamGroupTemplatesToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("configurations", schemas.SliceToList(body.Get("configurations"),
func(configurations gjson.Result) any {
return map[string]any{
"id": configurations.Get("id").Value(),
"name": configurations.Get("name").Value(),
"description": configurations.Get("description").Value(),
"datastore_version": configurations.Get("datastore_version").Value(),
"datastore_name": configurations.Get("datastore_name").Value(),
"ha_mode": configurations.Get("ha_mode").Value(),
"user_defined": configurations.Get("user_defined").Value(),
"created_at": configurations.Get("created").Value(),
"updated_at": configurations.Get("updated").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}

0 comments on commit 7eb7279

Please sign in to comment.