diff --git a/docs/data-sources/gaussdb_opengauss_restorable_instances.md b/docs/data-sources/gaussdb_opengauss_restorable_instances.md new file mode 100644 index 0000000000..14b5b94bbc --- /dev/null +++ b/docs/data-sources/gaussdb_opengauss_restorable_instances.md @@ -0,0 +1,70 @@ +--- +subcategory: "GaussDB" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_restorable_instances" +description: |- + Use this data source to get the instances that can be used for backups and restorations. +--- + +# huaweicloud_gaussdb_opengauss_restorable_instances + +Use this data source to get the instances that can be used for backups and restorations. + +## Example Usage + +```hcl +variable "source_instance_id" {} +variable "backup_id" {} + +data "huaweicloud_gaussdb_opengauss_restorable_instances" "test" { + source_instance_id = var.source_instance_id + backup_id = var.backup_id +} +``` + +## 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. + +* `source_instance_id` - (Required, String) Specifies the ID of the GaussDB OpenGauss instance to be restored. + +* `backup_id` - (Optional, String) Specifies the instance backup ID. + +* `restore_time` - (Optional, String) Specifies the time point of data restoration in the UNIX timestamp format. + If the `backup_id` is left blank, this parameter is used to query the instance topology information and filter + the queried instances. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The data source ID. + +* `instances` - Indicates the instances that can be used for backups and restorations. + + The [instances](#instances_struct) structure is documented below. + + +The `instances` block supports: + +* `instance_id` - Indicates the instance ID. + +* `instance_name` - Indicates the instance name. + +* `instance_mode` - Indicates the instance model. + + **enterprise**: enterprise edition + + **standard**: standard edition + + **basic**: basic edition + +* `volume_type` - Indicates the storage type. + +* `data_volume_size` - Indicates the storage space, in GB + +* `version` - Indicates the instance version + +* `mode` - Indicates the deployment model. + + **Ha**: primary/standby deployment + + **Independent**: independent deployment diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 8eec42ada8..1e9ba5f227 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -805,15 +805,16 @@ 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_parameter_templates": gaussdb.DataSourceGaussdbOpengaussParameterTemplates(), - "huaweicloud_gaussdb_opengauss_restore_time_ranges": gaussdb.DataSourceGaussdbOpengaussRestoreTimeRanges(), - "huaweicloud_gaussdb_opengauss_backups": gaussdb.DataSourceGaussdbOpengaussBackups(), + "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_opengauss_restore_time_ranges": gaussdb.DataSourceGaussdbOpengaussRestoreTimeRanges(), + "huaweicloud_gaussdb_opengauss_restorable_instances": gaussdb.DataSourceGaussdbOpengaussRestorableInstances(), + "huaweicloud_gaussdb_opengauss_backups": gaussdb.DataSourceGaussdbOpengaussBackups(), "huaweicloud_gaussdb_mysql_engine_versions": taurusdb.DataSourceGaussdbMysqlEngineVersions(), "huaweicloud_gaussdb_mysql_configuration": taurusdb.DataSourceGaussdbMysqlConfiguration(), diff --git a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_instance_test.go b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_instance_test.go index 634f9f04a1..76e53f373d 100644 --- a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_instance_test.go +++ b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_instance_test.go @@ -16,7 +16,11 @@ func TestAccOpenGaussInstanceDataSource_basic(t *testing.T) { dataSourceName := "data.huaweicloud_gaussdb_opengauss_instance.test" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.TestAccPreCheck(t) }, + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckEpsID(t) + acceptance.TestAccPreCheckHighCostAllow(t) + }, ProviderFactories: acceptance.TestAccProviderFactories, Steps: []resource.TestStep{ { @@ -37,7 +41,11 @@ func TestAccOpenGaussInstanceDataSource_haModeCentralized(t *testing.T) { dataSourceName := "data.huaweicloud_gaussdb_opengauss_instance.test" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.TestAccPreCheck(t) }, + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckEpsID(t) + acceptance.TestAccPreCheckHighCostAllow(t) + }, ProviderFactories: acceptance.TestAccProviderFactories, Steps: []resource.TestStep{ { diff --git a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_instances_test.go b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_instances_test.go index 120530871b..0efbd37e0b 100644 --- a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_instances_test.go +++ b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_instances_test.go @@ -16,7 +16,11 @@ func TestAccOpenGaussInstancesDataSource_basic(t *testing.T) { dataSourceName := "data.huaweicloud_gaussdb_opengauss_instances.test" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.TestAccPreCheck(t) }, + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckEpsID(t) + acceptance.TestAccPreCheckHighCostAllow(t) + }, ProviderFactories: acceptance.TestAccProviderFactories, Steps: []resource.TestStep{ { @@ -38,7 +42,11 @@ func TestAccOpenGaussInstancesDataSource_haModeCentralized(t *testing.T) { dataSourceName := "data.huaweicloud_gaussdb_opengauss_instances.test" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.TestAccPreCheck(t) }, + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckEpsID(t) + acceptance.TestAccPreCheckHighCostAllow(t) + }, ProviderFactories: acceptance.TestAccProviderFactories, Steps: []resource.TestStep{ { diff --git a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_restorable_instances_test.go b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_restorable_instances_test.go new file mode 100644 index 0000000000..44288936a5 --- /dev/null +++ b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_restorable_instances_test.go @@ -0,0 +1,140 @@ +package gaussdb + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance/common" +) + +func TestAccDataSourceGaussdbOpengaussRestorableInstances_basic(t *testing.T) { + dataSource := "data.huaweicloud_gaussdb_opengauss_restorable_instances.test" + rName := acceptance.RandomAccResourceName() + dc := acceptance.InitDataSourceCheck(dataSource) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckEpsID(t) + acceptance.TestAccPreCheckHighCostAllow(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testDataSourceGaussdbOpengaussRestorableInstances_basic(rName), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(dataSource, "instances.#"), + resource.TestCheckResourceAttrSet(dataSource, "instances.0.instance_id"), + resource.TestCheckResourceAttrSet(dataSource, "instances.0.instance_name"), + resource.TestCheckResourceAttrSet(dataSource, "instances.0.instance_mode"), + resource.TestCheckResourceAttrSet(dataSource, "instances.0.volume_type"), + resource.TestCheckResourceAttrSet(dataSource, "instances.0.data_volume_size"), + resource.TestCheckResourceAttrSet(dataSource, "instances.0.version"), + resource.TestCheckResourceAttrSet(dataSource, "instances.0.mode"), + resource.TestCheckOutput("restore_time_filter_is_useful", "true"), + ), + }, + }, + }) +} + +func testDataSourceGaussdbOpengaussRestorableInstances_base(name string) string { + return fmt.Sprintf(` +%[1]s + +data "huaweicloud_availability_zones" "test" {} + +data "huaweicloud_gaussdb_opengauss_flavors" "test" { + version = "8.201" + ha_mode = "centralization_standard" +} + +resource "huaweicloud_networking_secgroup_rule" "in_v4_tcp_opengauss" { + security_group_id = huaweicloud_networking_secgroup.test.id + ethertype = "IPv4" + direction = "ingress" + protocol = "tcp" + remote_ip_prefix = "0.0.0.0/0" +} + +resource "huaweicloud_networking_secgroup_rule" "in_v4_tcp_opengauss_egress" { + security_group_id = huaweicloud_networking_secgroup.test.id + ethertype = "IPv4" + direction = "egress" + protocol = "tcp" + remote_ip_prefix = "0.0.0.0/0" +} + +resource "huaweicloud_gaussdb_opengauss_instance" "test" { + depends_on = [ + huaweicloud_networking_secgroup_rule.in_v4_tcp_opengauss, + huaweicloud_networking_secgroup_rule.in_v4_tcp_opengauss_egress + ] + + count = 2 + + vpc_id = huaweicloud_vpc.test.id + subnet_id = huaweicloud_vpc_subnet.test.id + security_group_id = huaweicloud_networking_secgroup.test.id + + flavor = data.huaweicloud_gaussdb_opengauss_flavors.test.flavors[0].spec_code + name = "%[2]s_${count.index}" + password = "Huangwei!120521" + replica_num = 3 + availability_zone = join(",", [data.huaweicloud_availability_zones.test.names[0], + data.huaweicloud_availability_zones.test.names[1], + data.huaweicloud_availability_zones.test.names[2]]) + + enterprise_project_id = "%[3]s" + + ha { + mode = "centralization_standard" + replication_mode = "sync" + consistency = "eventual" + instance_mode = "basic" + } + + volume { + type = "ULTRAHIGH" + size = 40 + } +} + +resource "huaweicloud_gaussdb_opengauss_backup" "test" { + instance_id = huaweicloud_gaussdb_opengauss_instance.test[0].id + name = "%[2]s_backup" +} + +data "huaweicloud_gaussdb_opengauss_restore_time_ranges" "test" { + instance_id = huaweicloud_gaussdb_opengauss_instance.test[0].id + date = split("T", huaweicloud_gaussdb_opengauss_backup.test.end_time)[0] +} +`, common.TestBaseNetwork(name), name, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST) +} + +func testDataSourceGaussdbOpengaussRestorableInstances_basic(name string) string { + return fmt.Sprintf(` +%s + +data "huaweicloud_gaussdb_opengauss_restorable_instances" "test" { + source_instance_id = huaweicloud_gaussdb_opengauss_instance.test[0].id + backup_id = huaweicloud_gaussdb_opengauss_backup.test.id +} + +locals { + restore_time = data.huaweicloud_gaussdb_opengauss_restore_time_ranges.test.restore_time[0].end_time +} +data "huaweicloud_gaussdb_opengauss_restorable_instances" "restore_time_filter" { + source_instance_id = huaweicloud_gaussdb_opengauss_instance.test[0].id + backup_id = huaweicloud_gaussdb_opengauss_backup.test.id + restore_time = data.huaweicloud_gaussdb_opengauss_restore_time_ranges.test.restore_time[0].end_time +} +output "restore_time_filter_is_useful" { + value = length(data.huaweicloud_gaussdb_opengauss_restorable_instances.restore_time_filter.instances) > 0 +} +`, testDataSourceGaussdbOpengaussRestorableInstances_base(name)) +} diff --git a/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_opengauss_backup_stop_test.go b/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_opengauss_backup_stop_test.go index 5aa4265591..01bd9de409 100644 --- a/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_opengauss_backup_stop_test.go +++ b/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_opengauss_backup_stop_test.go @@ -22,7 +22,11 @@ func TestAccGaussDBOpenGaussBackupStop_basic(t *testing.T) { ) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.TestAccPreCheck(t) }, + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckEpsID(t) + acceptance.TestAccPreCheckHighCostAllow(t) + }, ProviderFactories: acceptance.TestAccProviderFactories, CheckDestroy: nil, Steps: []resource.TestStep{ diff --git a/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_opengauss_backup_test.go b/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_opengauss_backup_test.go index c6abb8f15b..bc04efa6b7 100644 --- a/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_opengauss_backup_test.go +++ b/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_opengauss_backup_test.go @@ -64,7 +64,11 @@ func TestAccGaussDBOpenGaussBackup_basic(t *testing.T) { ) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.TestAccPreCheck(t) }, + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckEpsID(t) + acceptance.TestAccPreCheckHighCostAllow(t) + }, ProviderFactories: acceptance.TestAccProviderFactories, CheckDestroy: rc.CheckResourceDestroy(), Steps: []resource.TestStep{ diff --git a/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_opengauss_instance_restart_test.go b/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_opengauss_instance_restart_test.go index 96e9a20f51..5db8c681cd 100644 --- a/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_opengauss_instance_restart_test.go +++ b/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_opengauss_instance_restart_test.go @@ -22,7 +22,11 @@ func TestAccOpenGaussInstanceRestart_basic(t *testing.T) { ) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.TestAccPreCheck(t) }, + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckEpsID(t) + acceptance.TestAccPreCheckHighCostAllow(t) + }, ProviderFactories: acceptance.TestAccProviderFactories, CheckDestroy: nil, Steps: []resource.TestStep{ diff --git a/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_restorable_instances.go b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_restorable_instances.go new file mode 100644 index 0000000000..f10d5b30be --- /dev/null +++ b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_restorable_instances.go @@ -0,0 +1,168 @@ +// Generated by PMS #496 +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" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" +) + +func DataSourceGaussdbOpengaussRestorableInstances() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceGaussdbOpengaussRestorableInstancesRead, + + 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.`, + }, + "source_instance_id": { + Type: schema.TypeString, + Required: true, + Description: `Specifies the ID of the GaussDB OpenGauss instance to be restored.`, + }, + "backup_id": { + Type: schema.TypeString, + Optional: true, + Description: `Specifies the instance backup ID.`, + }, + "restore_time": { + Type: schema.TypeString, + Optional: true, + Description: `Specifies the time point of data restoration in the UNIX timestamp format.`, + }, + "instances": { + Type: schema.TypeList, + Computed: true, + Description: `Indicates the instances that can be used for backups and restorations.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "instance_id": { + Type: schema.TypeString, + Computed: true, + Description: `Indicates the instance ID.`, + }, + "instance_name": { + Type: schema.TypeString, + Computed: true, + Description: `Indicates the instance name.`, + }, + "instance_mode": { + Type: schema.TypeString, + Computed: true, + Description: `Indicates the instance model.`, + }, + "volume_type": { + Type: schema.TypeString, + Computed: true, + Description: `Indicates the storage type.`, + }, + "data_volume_size": { + Type: schema.TypeFloat, + Computed: true, + Description: `Indicates the storage space, in GB`, + }, + "version": { + Type: schema.TypeString, + Computed: true, + Description: `Indicates the instance version`, + }, + "mode": { + Type: schema.TypeString, + Computed: true, + Description: `Indicates the deployment model.`, + }, + }, + }, + }, + }, + } +} + +type OpengaussRestorableInstancesDSWrapper struct { + *schemas.ResourceDataWrapper + Config *config.Config +} + +func newOpengaussRestorableInstancesDSWrapper(d *schema.ResourceData, meta interface{}) *OpengaussRestorableInstancesDSWrapper { + return &OpengaussRestorableInstancesDSWrapper{ + ResourceDataWrapper: schemas.NewSchemaWrapper(d), + Config: meta.(*config.Config), + } +} + +func dataSourceGaussdbOpengaussRestorableInstancesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + wrapper := newOpengaussRestorableInstancesDSWrapper(d, meta) + lisResInsRst, err := wrapper.ListRestorableInstances() + if err != nil { + return diag.FromErr(err) + } + + id, err := uuid.GenerateUUID() + if err != nil { + return diag.FromErr(err) + } + d.SetId(id) + + err = wrapper.listRestorableInstancesToSchema(lisResInsRst) + if err != nil { + return diag.FromErr(err) + } + + return nil +} + +// @API GaussDB GET /v3/{project_id}/restorable-instances +func (w *OpengaussRestorableInstancesDSWrapper) ListRestorableInstances() (*gjson.Result, error) { + client, err := w.NewClient(w.Config, "opengauss") + if err != nil { + return nil, err + } + + uri := "/v3/{project_id}/restorable-instances" + params := map[string]any{ + "source_instance_id": w.Get("source_instance_id"), + "backup_id": w.Get("backup_id"), + "restore_time": w.Get("restore_time"), + } + params = utils.RemoveNil(params) + return httphelper.New(client). + Method("GET"). + URI(uri). + Query(params). + OffsetPager("instances", "offset", "limit", 0). + Request(). + Result() +} + +func (w *OpengaussRestorableInstancesDSWrapper) listRestorableInstancesToSchema(body *gjson.Result) error { + d := w.ResourceData + mErr := multierror.Append(nil, + d.Set("region", w.Config.GetRegion(w.ResourceData)), + d.Set("instances", schemas.SliceToList(body.Get("instances"), + func(instances gjson.Result) any { + return map[string]any{ + "instance_id": instances.Get("instance_id").Value(), + "instance_name": instances.Get("instance_name").Value(), + "instance_mode": instances.Get("instance_mode").Value(), + "volume_type": instances.Get("volume_type").Value(), + "data_volume_size": instances.Get("data_volume_size").Value(), + "version": instances.Get("version").Value(), + "mode": instances.Get("mode").Value(), + } + }, + )), + ) + return mErr.ErrorOrNil() +}