-
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(GaussDB): add gaussdb opengauss backup files data source
- Loading branch information
Showing
4 changed files
with
313 additions
and
0 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" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_backup_files" | ||
description: |- | ||
Use this data source to get the list of GaussDB OpenGauss backup files. | ||
--- | ||
|
||
# huaweicloud_gaussdb_opengauss_backup_files | ||
|
||
Use this data source to get the list of GaussDB OpenGauss backup files. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "backup_id" {} | ||
data "huaweicloud_gaussdb_opengauss_backup_files" "test" { | ||
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. | ||
|
||
* `backup_id` - (Required, String) Specifies the ID of the backup. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The data source ID. | ||
|
||
* `files` - Indicates the list of backup files. | ||
|
||
The [files](#files_struct) structure is documented below. | ||
|
||
* `bucket` - Indicates the bucket name. | ||
|
||
<a name="files_struct"></a> | ||
The `files` block supports: | ||
|
||
* `name` - Indicates the file name. | ||
|
||
* `size` - Indicates the file size. | ||
|
||
* `download_link` - Indicates the download link. | ||
|
||
* `link_expired_time` - Indicates the link expired time in the **yyyy-mm-ddThh:mm:ssZ** format. |
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
116 changes: 116 additions & 0 deletions
116
...ervices/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_backup_files_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,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 TestAccDataSourceGaussdbOpengaussBackupFiles_basic(t *testing.T) { | ||
dataSource := "data.huaweicloud_gaussdb_opengauss_backup_files.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: testDataSourceGaussdbOpengaussBackupFiles_basic(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
dc.CheckResourceExists(), | ||
resource.TestCheckResourceAttrSet(dataSource, "files.#"), | ||
resource.TestCheckResourceAttrSet(dataSource, "files.0.name"), | ||
resource.TestCheckResourceAttrSet(dataSource, "files.0.size"), | ||
resource.TestCheckResourceAttrSet(dataSource, "files.0.download_link"), | ||
resource.TestCheckResourceAttrSet(dataSource, "files.0.link_expired_time"), | ||
resource.TestCheckResourceAttrSet(dataSource, "bucket"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testDataSourceGaussdbOpengaussBackupFiles_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" | ||
enterprise_project_id = "%[3]s" | ||
availability_zone = join(",", [data.huaweicloud_availability_zones.test.names[0], | ||
data.huaweicloud_availability_zones.test.names[1], | ||
data.huaweicloud_availability_zones.test.names[2]]) | ||
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 testDataSourceGaussdbOpengaussBackupFiles_basic(name string) string { | ||
return fmt.Sprintf(` | ||
%s | ||
data "huaweicloud_gaussdb_opengauss_backup_files" "test" { | ||
backup_id = huaweicloud_gaussdb_opengauss_backup.test.id | ||
} | ||
`, testDataSourceGaussdbOpengaussBackupFiles_base(name)) | ||
} |
143 changes: 143 additions & 0 deletions
143
huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_backup_files.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,143 @@ | ||
// Generated by PMS #518 | ||
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 DataSourceGaussdbOpengaussBackupFiles() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceGaussdbOpengaussBackupFilesRead, | ||
|
||
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.`, | ||
}, | ||
"backup_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: `Specifies the ID of the backup.`, | ||
}, | ||
"files": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: `Indicates the list of backup files.`, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the file name.`, | ||
}, | ||
"size": { | ||
Type: schema.TypeFloat, | ||
Computed: true, | ||
Description: `Indicates the file size.`, | ||
}, | ||
"download_link": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the download link.`, | ||
}, | ||
"link_expired_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the link expired time in the **yyyy-mm-ddThh:mm:ssZ** format.`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"bucket": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the bucket name.`, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
type OpengaussBackupFilesDSWrapper struct { | ||
*schemas.ResourceDataWrapper | ||
Config *config.Config | ||
} | ||
|
||
func newOpengaussBackupFilesDSWrapper(d *schema.ResourceData, meta interface{}) *OpengaussBackupFilesDSWrapper { | ||
return &OpengaussBackupFilesDSWrapper{ | ||
ResourceDataWrapper: schemas.NewSchemaWrapper(d), | ||
Config: meta.(*config.Config), | ||
} | ||
} | ||
|
||
func dataSourceGaussdbOpengaussBackupFilesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
wrapper := newOpengaussBackupFilesDSWrapper(d, meta) | ||
downloadBackupRst, err := wrapper.DownloadBackup() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
id, err := uuid.GenerateUUID() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId(id) | ||
|
||
err = wrapper.downloadBackupToSchema(downloadBackupRst) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// @API GaussDB GET /v3/{project_id}/backup-files | ||
func (w *OpengaussBackupFilesDSWrapper) DownloadBackup() (*gjson.Result, error) { | ||
client, err := w.NewClient(w.Config, "opengauss") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
uri := "/v3/{project_id}/backup-files" | ||
params := map[string]any{ | ||
"backup_id": w.Get("backup_id"), | ||
} | ||
params = utils.RemoveNil(params) | ||
return httphelper.New(client). | ||
Method("GET"). | ||
URI(uri). | ||
Query(params). | ||
Request(). | ||
Result() | ||
} | ||
|
||
func (w *OpengaussBackupFilesDSWrapper) downloadBackupToSchema(body *gjson.Result) error { | ||
d := w.ResourceData | ||
mErr := multierror.Append(nil, | ||
d.Set("region", w.Config.GetRegion(w.ResourceData)), | ||
d.Set("files", schemas.SliceToList(body.Get("files"), | ||
func(files gjson.Result) any { | ||
return map[string]any{ | ||
"name": files.Get("name").Value(), | ||
"size": files.Get("size").Value(), | ||
"download_link": files.Get("download_link").Value(), | ||
"link_expired_time": files.Get("link_expired_time").Value(), | ||
} | ||
}, | ||
)), | ||
d.Set("bucket", body.Get("bucket").Value()), | ||
) | ||
return mErr.ErrorOrNil() | ||
} |