From 8d348f42b402fec12995c061391c8645f56c721e Mon Sep 17 00:00:00 2001 From: jinyangyang222 Date: Thu, 1 Aug 2024 10:40:44 +0800 Subject: [PATCH] feat(evs): support volume transfers datasource (#5328) --- docs/data-sources/evs_volume_transfers.md | 43 +++++++ huaweicloud/provider.go | 1 + ...e_huaweicloud_evs_volume_transfers_test.go | 46 +++++++ ...source_huaweicloud_evs_volume_transfers.go | 121 ++++++++++++++++++ 4 files changed, 211 insertions(+) create mode 100644 docs/data-sources/evs_volume_transfers.md create mode 100644 huaweicloud/services/acceptance/evs/data_source_huaweicloud_evs_volume_transfers_test.go create mode 100644 huaweicloud/services/evs/data_source_huaweicloud_evs_volume_transfers.go diff --git a/docs/data-sources/evs_volume_transfers.md b/docs/data-sources/evs_volume_transfers.md new file mode 100644 index 0000000000..d0e1f7a7db --- /dev/null +++ b/docs/data-sources/evs_volume_transfers.md @@ -0,0 +1,43 @@ +--- +subcategory: "Elastic Volume Service (EVS)" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_evs_volume_transfers" +description: |- + Use this data source to get the list of EVS volume transfers within HuaweiCloud. +--- + +# huaweicloud_evs_volume_transfers + +Use this data source to get the list of EVS volume transfers within HuaweiCloud. + +## Example Usage + +```hcl +data "huaweicloud_evs_volume_transfers" "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. + +* `transfers` - The list of volume transfers. + + The [transfers](#transfers_struct) structure is documented below. + + +The `transfers` block supports: + +* `id` - The volume transfer ID. + +* `name` - The volume transfer name. + +* `volume_id` - The volume ID. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 98805f8710..1418832b4c 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -677,6 +677,7 @@ func Provider() *schema.Provider { "huaweicloud_evs_snapshots": evs.DataSourceEvsSnapshots(), "huaweicloud_evs_availability_zones": evs.DataSourceEvsAvailabilityZones(), "huaweicloud_evs_volume_types": evs.DataSourceEvsVolumeTypes(), + "huaweicloud_evs_volume_transfers": evs.DataSourceEvsVolumeTransfers(), "huaweicloud_fgs_applications": fgs.DataSourceFunctionGraphApplications(), "huaweicloud_fgs_application_templates": fgs.DataSourceFunctionGraphApplicationTemplates(), diff --git a/huaweicloud/services/acceptance/evs/data_source_huaweicloud_evs_volume_transfers_test.go b/huaweicloud/services/acceptance/evs/data_source_huaweicloud_evs_volume_transfers_test.go new file mode 100644 index 0000000000..906cd950ec --- /dev/null +++ b/huaweicloud/services/acceptance/evs/data_source_huaweicloud_evs_volume_transfers_test.go @@ -0,0 +1,46 @@ +package evs + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +func TestAccDataSourceEvsVolumeTransfers_basic(t *testing.T) { + dataSource := "data.huaweicloud_evs_volume_transfers.test" + name := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5)) + dc := acceptance.InitDataSourceCheck(dataSource) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testDataSourceDataSourceEvsVolumeTransfers_basic(name), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(dataSource, "transfers.#"), + resource.TestCheckResourceAttrSet(dataSource, "transfers.0.id"), + resource.TestCheckResourceAttrSet(dataSource, "transfers.0.name"), + resource.TestCheckResourceAttrSet(dataSource, "transfers.0.volume_id"), + ), + }, + }, + }) +} + +func testDataSourceDataSourceEvsVolumeTransfers_basic(name string) string { + return fmt.Sprintf(` +%s + +data "huaweicloud_evs_volume_transfers" "test" { + depends_on = [huaweicloud_evs_volume_transfer.test] +} +`, testAccVolumeTransfer_basic(name)) +} diff --git a/huaweicloud/services/evs/data_source_huaweicloud_evs_volume_transfers.go b/huaweicloud/services/evs/data_source_huaweicloud_evs_volume_transfers.go new file mode 100644 index 0000000000..3ad6d678d5 --- /dev/null +++ b/huaweicloud/services/evs/data_source_huaweicloud_evs_volume_transfers.go @@ -0,0 +1,121 @@ +// Generated by PMS #286 +package evs + +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 DataSourceEvsVolumeTransfers() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceEvsVolumeTransfersRead, + + 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.`, + }, + "transfers": { + Type: schema.TypeList, + Computed: true, + Description: `The list of volume transfers.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + Description: `The volume transfer ID.`, + }, + "name": { + Type: schema.TypeString, + Computed: true, + Description: `The volume transfer name.`, + }, + "volume_id": { + Type: schema.TypeString, + Computed: true, + Description: `The volume ID.`, + }, + }, + }, + }, + }, + } +} + +type VolumeTransfersDSWrapper struct { + *schemas.ResourceDataWrapper + Config *config.Config +} + +func newVolumeTransfersDSWrapper(d *schema.ResourceData, meta interface{}) *VolumeTransfersDSWrapper { + return &VolumeTransfersDSWrapper{ + ResourceDataWrapper: schemas.NewSchemaWrapper(d), + Config: meta.(*config.Config), + } +} + +func dataSourceEvsVolumeTransfersRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + wrapper := newVolumeTransfersDSWrapper(d, meta) + cinLisVolTraRst, err := wrapper.CinderListVolumeTransfers() + if err != nil { + return diag.FromErr(err) + } + + id, err := uuid.GenerateUUID() + if err != nil { + return diag.FromErr(err) + } + d.SetId(id) + + err = wrapper.cinderListVolumeTransfersToSchema(cinLisVolTraRst) + if err != nil { + return diag.FromErr(err) + } + + return nil +} + +// @API EVS GET /v2/{project_id}/os-volume-transfer +func (w *VolumeTransfersDSWrapper) CinderListVolumeTransfers() (*gjson.Result, error) { + client, err := w.NewClient(w.Config, "evs") + if err != nil { + return nil, err + } + + uri := "/v2/{project_id}/os-volume-transfer" + return httphelper.New(client). + Method("GET"). + URI(uri). + OffsetPager("transfers", "offset", "limit", 20). + Request(). + Result() +} + +func (w *VolumeTransfersDSWrapper) cinderListVolumeTransfersToSchema(body *gjson.Result) error { + d := w.ResourceData + mErr := multierror.Append(nil, + d.Set("region", w.Config.GetRegion(w.ResourceData)), + d.Set("transfers", schemas.SliceToList(body.Get("transfers"), + func(transfers gjson.Result) any { + return map[string]any{ + "id": transfers.Get("id").Value(), + "name": transfers.Get("name").Value(), + "volume_id": transfers.Get("volume_id").Value(), + } + }, + )), + ) + return mErr.ErrorOrNil() +}