-
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(evs): support volume transfers datasource (#5328)
- Loading branch information
1 parent
77a2058
commit 8d348f4
Showing
4 changed files
with
211 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,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. |
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
46 changes: 46 additions & 0 deletions
46
huaweicloud/services/acceptance/evs/data_source_huaweicloud_evs_volume_transfers_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,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)) | ||
} |
121 changes: 121 additions & 0 deletions
121
huaweicloud/services/evs/data_source_huaweicloud_evs_volume_transfers.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,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() | ||
} |