-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(GaussDBforMySQL): add gaussdb mysql engine versions data source
- Loading branch information
Showing
5 changed files
with
239 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_mysql_engine_versions_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
` | ||
} |
132 changes: 132 additions & 0 deletions
132
huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_mysql_engine_versions.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters