Skip to content

Commit

Permalink
feat(evs): support volume transfers datasource (#5328)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyangyang222 authored Aug 1, 2024
1 parent 77a2058 commit 8d348f4
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 0 deletions.
43 changes: 43 additions & 0 deletions docs/data-sources/evs_volume_transfers.md
Original file line number Diff line number Diff line change
@@ -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.

<a name="transfers_struct"></a>
The `transfers` block supports:

* `id` - The volume transfer ID.

* `name` - The volume transfer name.

* `volume_id` - The volume ID.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 8d348f4

Please sign in to comment.