-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rms): add new data_source rms regions (#4473)
- Loading branch information
1 parent
d71b067
commit ea817d7
Showing
4 changed files
with
238 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,38 @@ | ||
--- | ||
subcategory: "Config" | ||
--- | ||
|
||
# huaweicloud_rms_regions | ||
|
||
Use this data source to get the list of RMS regions. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "huaweicloud_rms_regions" "regions" {} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `region_id` - (Optional, String) Specifies the region ID. | ||
|
||
* `display_name` - (Optional, String) Specifies the region dispaly name. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The data source ID. | ||
|
||
* `regions` - The region list. | ||
|
||
The [regions](#regions_struct) structure is documented below. | ||
|
||
<a name="regions_struct"></a> | ||
The `regions` block supports: | ||
|
||
* `region_id` - The region ID. | ||
|
||
* `display_name` - The display name of the region. |
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
76 changes: 76 additions & 0 deletions
76
huaweicloud/services/acceptance/rms/data_source_huaweicloud_rms_regions_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,76 @@ | ||
package rms | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
) | ||
|
||
func TestAccDataSourceRmsRegions_basic(t *testing.T) { | ||
dataSource := "data.huaweicloud_rms_regions.test" | ||
dc := acceptance.InitDataSourceCheck(dataSource) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testDataSourceDataSourceRmsRegions_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
dc.CheckResourceExists(), | ||
resource.TestCheckOutput("is_regions_not_empty", "true"), | ||
), | ||
}, | ||
{ | ||
Config: testDataSourceDataSourceRmsRegions_regionID, | ||
Check: resource.ComposeTestCheckFunc( | ||
dc.CheckResourceExists(), | ||
resource.TestCheckOutput("is_regions_not_empty", "true"), | ||
resource.TestCheckResourceAttr(dataSource, "regions.#", "1"), | ||
resource.TestCheckResourceAttr(dataSource, "regions.0.region_id", "cn-north-4"), | ||
), | ||
}, | ||
{ | ||
Config: testDataSourceDataSourceRmsRegions_displayName, | ||
Check: resource.ComposeTestCheckFunc( | ||
dc.CheckResourceExists(), | ||
resource.TestCheckOutput("is_regions_not_empty", "true"), | ||
resource.TestCheckResourceAttr(dataSource, "regions.#", "1"), | ||
resource.TestCheckResourceAttr(dataSource, "regions.0.display_name", "CN North-Beijing4"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testDataSourceDataSourceRmsRegions_basic = ` | ||
data "huaweicloud_rms_regions" "test" {} | ||
output "is_regions_not_empty" { | ||
value = length(data.huaweicloud_rms_regions.test.regions) > 0 | ||
} | ||
` | ||
|
||
const testDataSourceDataSourceRmsRegions_regionID = ` | ||
data "huaweicloud_rms_regions" "test" { | ||
region_id = "cn-north-4" | ||
} | ||
output "is_regions_not_empty" { | ||
value = length(data.huaweicloud_rms_regions.test.regions) > 0 | ||
} | ||
` | ||
|
||
const testDataSourceDataSourceRmsRegions_displayName = ` | ||
data "huaweicloud_rms_regions" "test" { | ||
display_name = "CN North-Beijing4" | ||
} | ||
output "is_regions_not_empty" { | ||
value = length(data.huaweicloud_rms_regions.test.regions) > 0 | ||
} | ||
` |
123 changes: 123 additions & 0 deletions
123
huaweicloud/services/rms/data_source_huaweicloud_rms_regions.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,123 @@ | ||
// Generated by PMS #43 | ||
package rms | ||
|
||
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/filters" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas" | ||
) | ||
|
||
func DataSourceRmsRegions() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceRmsRegionsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: `Specifies the region ID.`, | ||
}, | ||
"display_name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: `Specifies the region dispaly name.`, | ||
}, | ||
"regions": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: `The region list.`, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"region_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The region ID.`, | ||
}, | ||
"display_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The display name of the region.`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
type RegionsDSWrapper struct { | ||
*schemas.ResourceDataWrapper | ||
Config *config.Config | ||
} | ||
|
||
func newRegionsDSWrapper(d *schema.ResourceData, meta interface{}) *RegionsDSWrapper { | ||
return &RegionsDSWrapper{ | ||
ResourceDataWrapper: schemas.NewSchemaWrapper(d), | ||
Config: meta.(*config.Config), | ||
} | ||
} | ||
|
||
func dataSourceRmsRegionsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
wrapper := newRegionsDSWrapper(d, meta) | ||
listRegionsRst, err := wrapper.ListRegions() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
id, _ := uuid.GenerateUUID() | ||
d.SetId(id) | ||
|
||
err = wrapper.listRegionsToSchema(listRegionsRst) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// @API CONFIG GET /v1/resource-manager/domains/{domain_id}/regions | ||
func (w *RegionsDSWrapper) ListRegions() (*gjson.Result, error) { | ||
client, err := w.NewClient(w.Config, "rms") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
uri := "/v1/resource-manager/domains/{domain_id}/regions" | ||
uri = strings.ReplaceAll(uri, "{domain_id}", w.Config.DomainID) | ||
return httphelper.New(client). | ||
Method("GET"). | ||
URI(uri). | ||
Filter( | ||
filters.New(). | ||
From("value"). | ||
Where("display_name", "=", w.Get("display_name")). | ||
Where("region_id", "=", w.Get("region_id")), | ||
). | ||
Request(). | ||
Result() | ||
} | ||
|
||
func (w *RegionsDSWrapper) listRegionsToSchema(body *gjson.Result) error { | ||
d := w.ResourceData | ||
mErr := multierror.Append(nil, | ||
d.Set("regions", schemas.SliceToList(body.Get("value"), | ||
func(regions gjson.Result) any { | ||
return map[string]any{ | ||
"region_id": regions.Get("region_id").Value(), | ||
"display_name": regions.Get("display_name").Value(), | ||
} | ||
}, | ||
)), | ||
) | ||
return mErr.ErrorOrNil() | ||
} |