diff --git a/docs/data-sources/gaussdb_opengauss_datastores.md b/docs/data-sources/gaussdb_opengauss_datastores.md new file mode 100644 index 0000000000..d11fcd710c --- /dev/null +++ b/docs/data-sources/gaussdb_opengauss_datastores.md @@ -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. + + +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 diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index d1fb29c78f..6240988941 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -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(), diff --git a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_datastores_test.go b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_datastores_test.go new file mode 100644 index 0000000000..e03eb7bb50 --- /dev/null +++ b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_datastores_test.go @@ -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" {} +` +} diff --git a/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_datastores.go b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_datastores.go new file mode 100644 index 0000000000..b39ed58e6f --- /dev/null +++ b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_datastores.go @@ -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() +}