Skip to content

Commit

Permalink
feat(GaussDBforMySQL): add gaussdb mysql engine versions data source
Browse files Browse the repository at this point in the history
  • Loading branch information
houpeng80 committed Nov 4, 2024
1 parent 6e095ad commit 3446c31
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 8 deletions.
53 changes: 53 additions & 0 deletions docs/data-sources/gaussdb_mysql_engine_versions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
subcategory: "GaussDB(for MySQL)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_gaussdb_mysql_engine_versions"
description: |-
Use this data source to get the database specifications of a specified DB engine version.
---

# huaweicloud_gaussdb_mysql_engine_versions

Use this data source to get the database specifications of a specified DB engine version.

## Example Usage

```hcl
data "huaweicloud_gaussdb_mysql_engine_versions" "test" {
database_name = "gaussdb-mysql"
}
```

## 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.

* `database_name` - (Required, String) Specifies the DB engine.
Value options: **gaussdb-mysql**.

## Attribute Reference

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

* `id` - The data source ID.

* `datastores` - Indicates the DB version list.

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

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

* `id` - Indicates the DB version ID.

* `name` - Indicates the DB version number.
Only the major version number with two digits is returned.

* `version` - Indicates the compatible open-source DB version.
A three-digit open-source version is returned.

* `kernel_version` - Indicates the DB version.
A complete four-digit version is returned.
18 changes: 10 additions & 8 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,14 +755,16 @@ func Provider() *schema.Provider {
"huaweicloud_ga_health_checks": ga.DataSourceHealthChecks(),
"huaweicloud_ga_listeners": ga.DataSourceListeners(),

"huaweicloud_gaussdb_cassandra_dedicated_resource": gaussdb.DataSourceGeminiDBDehResource(),
"huaweicloud_gaussdb_cassandra_flavors": gaussdb.DataSourceCassandraFlavors(),
"huaweicloud_gaussdb_nosql_flavors": gaussdb.DataSourceGaussDBNoSQLFlavors(),
"huaweicloud_gaussdb_redis_flavors": gaussdb.DataSourceGaussDBRedisFlavors(),
"huaweicloud_gaussdb_cassandra_instance": gaussdb.DataSourceGeminiDBInstance(),
"huaweicloud_gaussdb_cassandra_instances": gaussdb.DataSourceGeminiDBInstances(),
"huaweicloud_gaussdb_opengauss_instance": gaussdb.DataSourceOpenGaussInstance(),
"huaweicloud_gaussdb_opengauss_instances": gaussdb.DataSourceOpenGaussInstances(),
"huaweicloud_gaussdb_cassandra_dedicated_resource": gaussdb.DataSourceGeminiDBDehResource(),
"huaweicloud_gaussdb_cassandra_flavors": gaussdb.DataSourceCassandraFlavors(),
"huaweicloud_gaussdb_nosql_flavors": gaussdb.DataSourceGaussDBNoSQLFlavors(),
"huaweicloud_gaussdb_redis_flavors": gaussdb.DataSourceGaussDBRedisFlavors(),
"huaweicloud_gaussdb_cassandra_instance": gaussdb.DataSourceGeminiDBInstance(),
"huaweicloud_gaussdb_cassandra_instances": gaussdb.DataSourceGeminiDBInstances(),
"huaweicloud_gaussdb_opengauss_instance": gaussdb.DataSourceOpenGaussInstance(),
"huaweicloud_gaussdb_opengauss_instances": gaussdb.DataSourceOpenGaussInstances(),

"huaweicloud_gaussdb_mysql_engine_versions": gaussdb.DataSourceGaussdbMysqlEngineVersions(),
"huaweicloud_gaussdb_mysql_configuration": gaussdb.DataSourceGaussdbMysqlConfiguration(),
"huaweicloud_gaussdb_mysql_configurations": gaussdb.DataSourceGaussdbMysqlConfigurations(),
"huaweicloud_gaussdb_mysql_dedicated_resource": gaussdb.DataSourceGaussDBMysqlDehResource(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gaussdb

import (
"testing"

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

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

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

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

func testDataSourceGaussdbMysqlEngineVersions_basic() string {
return `
data "huaweicloud_gaussdb_mysql_engine_versions" "test" {
database_name = "gaussdb-mysql"
}
`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Generated by PMS #400
package gaussdb

import (
"context"
"strings"

"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 DataSourceGaussdbMysqlEngineVersions() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGaussdbMysqlEngineVersionsRead,

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.`,
},
"database_name": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the DB engine.`,
},
"datastores": {
Type: schema.TypeList,
Computed: true,
Description: `Indicates the DB version list.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the DB version ID.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the DB version number.`,
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the compatible open-source DB version.`,
},
"kernel_version": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the DB version.`,
},
},
},
},
},
}
}

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

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

func dataSourceGaussdbMysqlEngineVersionsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newMysqlEngineVersionsDSWrapper(d, meta)
shoGauMySqlEngVerRst, err := wrapper.ShowGaussMySqlEngineVersion()
if err != nil {
return diag.FromErr(err)
}

err = wrapper.showGaussMySqlEngineVersionToSchema(shoGauMySqlEngVerRst)
if err != nil {
return diag.FromErr(err)
}

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

// @API GAUSSDB GET /v3/{project_id}/datastores/{database_name}
func (w *MysqlEngineVersionsDSWrapper) ShowGaussMySqlEngineVersion() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "gaussdb")
if err != nil {
return nil, err
}

uri := "/v3/{project_id}/datastores/{database_name}"
uri = strings.ReplaceAll(uri, "{database_name}", w.Get("database_name").(string))
return httphelper.New(client).
Method("GET").
URI(uri).
Request().
Result()
}

func (w *MysqlEngineVersionsDSWrapper) showGaussMySqlEngineVersionToSchema(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{
"id": datastores.Get("id").Value(),
"name": datastores.Get("name").Value(),
"version": datastores.Get("version").Value(),
"kernel_version": datastores.Get("kernel_version").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type ctxType string
// @API GaussDBforMySQL PUT /v3/{project_id}/instances/{instance_id}/configurations
// @API GaussDBforMySQL PUT /v3/{project_id}/instances/{instance_id}/auto-scaling/policy
// @API GaussDBforMySQL POST /v3/{project_id}/instances/{instance_id}/backups/encryption
// @API GaussDBforMySQL POST /v3/{project_id}/instances/{instance_id}/slowlog/modify
// @API GaussDBforMySQL GET /v3/{project_id}/instances/{instance_id}
// @API GaussDBforMySQL GET /v3/{project_id}/instances/{instance_id}/proxy
// @API GaussDBforMySQL GET /v3/{project_id}/instance/{instance_id}/audit-log/switch-status
Expand All @@ -73,6 +74,7 @@ type ctxType string
// @API GaussDBforMySQL GET /v3/{project_id}/instances/{instance_id}/auto-scaling/policy
// @API GaussDBforMySQL GET /v3/{project_id}/instances/{instance_id}/backups/encryption
// @API GaussDBforMySQL GET /v3/{project_id}/instances/{instance_id}/database-version
// @API GaussDBforMySQL GET /v3/{project_id}/instances/{instance_id}/slowlog/query
// @API GaussDBforMySQL DELETE /v3/{project_id}/instances/{instance_id}
// @API EPS POST /v1.0/enterprise-projects/{enterprise_project_id}/resources-migrat
// @API BSS GET /v2/orders/customer-orders/details/{order_id}
Expand Down

0 comments on commit 3446c31

Please sign in to comment.