diff --git a/docs/data-sources/gaussdb_opengauss_restore_time_ranges.md b/docs/data-sources/gaussdb_opengauss_restore_time_ranges.md new file mode 100644 index 0000000000..29fcdc4b96 --- /dev/null +++ b/docs/data-sources/gaussdb_opengauss_restore_time_ranges.md @@ -0,0 +1,52 @@ +--- +subcategory: "GaussDB" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_restore_time_ranges" +description: |- + Use this data source to get the list of GaussDB OpenGauss restore time ranges. +--- + +# huaweicloud_gaussdb_opengauss_restore_time_ranges + +Use this data source to get the list of GaussDB OpenGauss restore time ranges. + +## Example Usage + +```hcl +variable "instance_id" {} + +data "huaweicloud_gaussdb_opengauss_restore_time_ranges" "test" { + instance_id = var.instance_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. + +* `instance_id` - (Required, String) Specifies the ID of the GaussDB OpenGauss instance. + +* `date` - (Required, String) Specifies the date to be queried. + The value is in the **yyyy-mm-dd** format, and the time zone is UTC. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The data source ID. + +* `restore_time` - Indicates the list of restoration time ranges. + + The [restore_time](#restore_time_struct) structure is documented below. + + +The `restore_time` block supports: + +* `start_time` - Indicates the start time of the restoration time range in the UNIX timestamp format. + The unit is millisecond and the time zone is UTC. + +* `end_time` - Indicates the end time of the restoration time range in the UNIX timestamp format. + The unit is millisecond and the time zone is UTC. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 1acdab362c..97e9b7b2e9 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -809,6 +809,7 @@ func Provider() *schema.Provider { "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_mysql_engine_versions": taurusdb.DataSourceGaussdbMysqlEngineVersions(), "huaweicloud_gaussdb_mysql_configuration": taurusdb.DataSourceGaussdbMysqlConfiguration(), diff --git a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_instance_nodes_test.go b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_instance_nodes_test.go index 21327897a2..5bf242f6a5 100644 --- a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_instance_nodes_test.go +++ b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_instance_nodes_test.go @@ -66,11 +66,6 @@ resource "huaweicloud_networking_secgroup_rule" "in_v4_tcp_opengauss_egress" { remote_ip_prefix = "0.0.0.0/0" } -resource "huaweicloud_enterprise_project" "test" { - name = "%[2]s" - description = "terraform test update" -} - resource "huaweicloud_gaussdb_opengauss_instance" "test" { depends_on = [ huaweicloud_networking_secgroup_rule.in_v4_tcp_opengauss, diff --git a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_restore_time_ranges_test.go b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_restore_time_ranges_test.go new file mode 100644 index 0000000000..7b18ace1a4 --- /dev/null +++ b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_restore_time_ranges_test.go @@ -0,0 +1,116 @@ +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 TestAccDataSourceGaussdbOpengaussRestoreTimeRanges_basic(t *testing.T) { + dataSource := "data.huaweicloud_gaussdb_opengauss_restore_time_ranges.test" + rName := acceptance.RandomAccResourceName() + dc := acceptance.InitDataSourceCheck(dataSource) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testDataSourceGaussdbOpengaussRestoreTimeRanges_basic(rName), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(dataSource, "restore_time.#"), + resource.TestCheckResourceAttrSet(dataSource, "restore_time.0.start_time"), + resource.TestCheckResourceAttrSet(dataSource, "restore_time.0.end_time"), + ), + }, + }, + }) +} + +func testDataSourceGaussdbOpengaussRestoreTimeRanges_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 + ] + + 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" + 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.id + name = "%[2]s" + description = "test description" +} +`, common.TestBaseNetwork(name), name, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST) +} + +func testDataSourceGaussdbOpengaussRestoreTimeRanges_basic(name string) string { + return fmt.Sprintf(` +%[1]s + +data "huaweicloud_gaussdb_opengauss_restore_time_ranges" "test" { + depends_on = [huaweicloud_gaussdb_opengauss_backup.test] + + instance_id = huaweicloud_gaussdb_opengauss_instance.test.id + date = split("T", huaweicloud_gaussdb_opengauss_backup.test.end_time)[0] +} +`, testDataSourceGaussdbOpengaussRestoreTimeRanges_base(name)) +} diff --git a/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_restore_time_ranges.go b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_restore_time_ranges.go new file mode 100644 index 0000000000..51560c37f8 --- /dev/null +++ b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_restore_time_ranges.go @@ -0,0 +1,132 @@ +// Generated by PMS #478 +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" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" +) + +func DataSourceGaussdbOpengaussRestoreTimeRanges() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceGaussdbOpengaussRestoreTimeRangesRead, + + 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.`, + }, + "instance_id": { + Type: schema.TypeString, + Required: true, + Description: `Specifies the ID of the GaussDB OpenGauss instance.`, + }, + "date": { + Type: schema.TypeString, + Required: true, + Description: `Specifies the date to be queried.`, + }, + "restore_time": { + Type: schema.TypeList, + Computed: true, + Description: `Indicates the list of restoration time ranges.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "start_time": { + Type: schema.TypeInt, + Computed: true, + Description: `Indicates the start time of the restoration time range in the UNIX timestamp format.`, + }, + "end_time": { + Type: schema.TypeInt, + Computed: true, + Description: `Indicates the end time of the restoration time range in the UNIX timestamp format.`, + }, + }, + }, + }, + }, + } +} + +type OpengaussRestoreTimeRangesDSWrapper struct { + *schemas.ResourceDataWrapper + Config *config.Config +} + +func newOpengaussRestoreTimeRangesDSWrapper(d *schema.ResourceData, meta interface{}) *OpengaussRestoreTimeRangesDSWrapper { + return &OpengaussRestoreTimeRangesDSWrapper{ + ResourceDataWrapper: schemas.NewSchemaWrapper(d), + Config: meta.(*config.Config), + } +} + +func dataSourceGaussdbOpengaussRestoreTimeRangesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + wrapper := newOpengaussRestoreTimeRangesDSWrapper(d, meta) + listRestoreTimesRst, err := wrapper.ListRestoreTimes() + if err != nil { + return diag.FromErr(err) + } + + id, err := uuid.GenerateUUID() + if err != nil { + return diag.FromErr(err) + } + d.SetId(id) + + err = wrapper.listRestoreTimesToSchema(listRestoreTimesRst) + if err != nil { + return diag.FromErr(err) + } + + return nil +} + +// @API GaussDB GET /v3/{project_id}/instances/{instance_id}/restore-time +func (w *OpengaussRestoreTimeRangesDSWrapper) ListRestoreTimes() (*gjson.Result, error) { + client, err := w.NewClient(w.Config, "opengauss") + if err != nil { + return nil, err + } + + uri := "/v3/{project_id}/instances/{instance_id}/restore-time" + uri = strings.ReplaceAll(uri, "{instance_id}", w.Get("instance_id").(string)) + params := map[string]any{ + "date": w.Get("date"), + } + params = utils.RemoveNil(params) + return httphelper.New(client). + Method("GET"). + URI(uri). + Query(params). + Request(). + Result() +} + +func (w *OpengaussRestoreTimeRangesDSWrapper) listRestoreTimesToSchema(body *gjson.Result) error { + d := w.ResourceData + mErr := multierror.Append(nil, + d.Set("region", w.Config.GetRegion(w.ResourceData)), + d.Set("restore_time", schemas.SliceToList(body.Get("restore_time"), + func(restoreTime gjson.Result) any { + return map[string]any{ + "start_time": restoreTime.Get("start_time").Value(), + "end_time": restoreTime.Get("end_time").Value(), + } + }, + )), + ) + return mErr.ErrorOrNil() +}