From 4a9061eac8c41eb317492587dca9dc9f91db0dbf Mon Sep 17 00:00:00 2001 From: houpeng80 <114376095+houpeng80@users.noreply.github.com> Date: Tue, 5 Nov 2024 10:32:46 +0800 Subject: [PATCH] feat(GaussDBforMySQL): add gaussdb mysql engine versions data source (#5806) --- .../gaussdb_mysql_engine_versions.md | 53 +++++++ huaweicloud/provider.go | 18 +-- ...loud_gaussdb_mysql_engine_versions_test.go | 42 ++++++ ...aweicloud_gaussdb_mysql_engine_versions.go | 132 ++++++++++++++++++ ...urce_huaweicloud_gaussdb_mysql_instance.go | 2 + 5 files changed, 239 insertions(+), 8 deletions(-) create mode 100644 docs/data-sources/gaussdb_mysql_engine_versions.md create mode 100644 huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_mysql_engine_versions_test.go create mode 100644 huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_mysql_engine_versions.go diff --git a/docs/data-sources/gaussdb_mysql_engine_versions.md b/docs/data-sources/gaussdb_mysql_engine_versions.md new file mode 100644 index 0000000000..a0675b251e --- /dev/null +++ b/docs/data-sources/gaussdb_mysql_engine_versions.md @@ -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. +--- + +# huaweicloud_gaussdb_mysql_engine_versions + +Use this data source to get the database specifications of a specified DB engine. + +## 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. + + +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. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 3557c7de6e..a5e29c3f74 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -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(), diff --git a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_mysql_engine_versions_test.go b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_mysql_engine_versions_test.go new file mode 100644 index 0000000000..05c5f66c42 --- /dev/null +++ b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_mysql_engine_versions_test.go @@ -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" +} +` +} diff --git a/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_mysql_engine_versions.go b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_mysql_engine_versions.go new file mode 100644 index 0000000000..32c46cf024 --- /dev/null +++ b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_mysql_engine_versions.go @@ -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() +} diff --git a/huaweicloud/services/gaussdb/resource_huaweicloud_gaussdb_mysql_instance.go b/huaweicloud/services/gaussdb/resource_huaweicloud_gaussdb_mysql_instance.go index ac41078c04..e314bab5e2 100644 --- a/huaweicloud/services/gaussdb/resource_huaweicloud_gaussdb_mysql_instance.go +++ b/huaweicloud/services/gaussdb/resource_huaweicloud_gaussdb_mysql_instance.go @@ -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 @@ -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}