-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Data Source: alicloud_gwlb_zones.
- Loading branch information
1 parent
906d080
commit 3c4b46d
Showing
4 changed files
with
276 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,160 @@ | ||
// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! | ||
package alicloud | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/PaesslerAG/jsonpath" | ||
util "github.com/alibabacloud-go/tea-utils/service" | ||
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func dataSourceAliCloudGwlbZones() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAliCloudGwlbZoneRead, | ||
Schema: map[string]*schema.Schema{ | ||
"ids": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Computed: true, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"accept_language": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"zones": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"local_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"zone_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"output_file": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAliCloudGwlbZoneRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*connectivity.AliyunClient) | ||
|
||
var request map[string]interface{} | ||
var response map[string]interface{} | ||
var query map[string]interface{} | ||
action := "DescribeZones" | ||
conn, err := client.NewGwlbClient() | ||
if err != nil { | ||
return WrapError(err) | ||
} | ||
request = make(map[string]interface{}) | ||
query = make(map[string]interface{}) | ||
request["RegionId"] = client.RegionId | ||
if v, ok := d.GetOk("accept_language"); ok { | ||
request["AcceptLanguage"] = v | ||
} | ||
runtime := util.RuntimeOptions{} | ||
runtime.SetAutoretry(true) | ||
wait := incrementalWait(3*time.Second, 5*time.Second) | ||
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { | ||
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-04-15"), StringPointer("AK"), query, request, &runtime) | ||
|
||
if err != nil { | ||
if NeedRetry(err) { | ||
wait() | ||
return resource.RetryableError(err) | ||
} | ||
return resource.NonRetryableError(err) | ||
} | ||
addDebug(action, response, request) | ||
return nil | ||
}) | ||
if err != nil { | ||
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) | ||
} | ||
|
||
var objects []map[string]interface{} | ||
var nameRegex *regexp.Regexp | ||
if v, ok := d.GetOk("name_regex"); ok { | ||
r, err := regexp.Compile(v.(string)) | ||
if err != nil { | ||
return WrapError(err) | ||
} | ||
nameRegex = r | ||
} | ||
|
||
idsMap := make(map[string]string) | ||
if v, ok := d.GetOk("ids"); ok { | ||
for _, vv := range v.([]interface{}) { | ||
if vv == nil { | ||
continue | ||
} | ||
idsMap[vv.(string)] = vv.(string) | ||
} | ||
} | ||
|
||
resp, _ := jsonpath.Get("$.Zones[*]", response) | ||
|
||
result, _ := resp.([]interface{}) | ||
for _, v := range result { | ||
item := v.(map[string]interface{}) | ||
if nameRegex != nil && !nameRegex.MatchString(fmt.Sprint(item["LocalName"])) { | ||
continue | ||
} | ||
if len(idsMap) > 0 { | ||
if _, ok := idsMap[fmt.Sprint(item["ZoneId"])]; !ok { | ||
continue | ||
} | ||
} | ||
objects = append(objects, item) | ||
} | ||
|
||
ids := make([]string, 0) | ||
names := make([]interface{}, 0) | ||
s := make([]map[string]interface{}, 0) | ||
for _, objectRaw := range objects { | ||
mapping := map[string]interface{}{} | ||
|
||
mapping["local_name"] = objectRaw["LocalName"] | ||
mapping["zone_id"] = objectRaw["ZoneId"] | ||
mapping["id"] = objectRaw["ZoneId"] | ||
|
||
ids = append(ids, fmt.Sprint(mapping["id"])) | ||
names = append(names, objectRaw["LocalName"]) | ||
s = append(s, mapping) | ||
} | ||
|
||
d.SetId(dataResourceIdHash(ids)) | ||
if err := d.Set("ids", ids); err != nil { | ||
return WrapError(err) | ||
} | ||
|
||
if err := d.Set("zones", s); err != nil { | ||
return WrapError(err) | ||
} | ||
|
||
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" { | ||
writeToFile(output.(string), s) | ||
} | ||
return nil | ||
} |
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,66 @@ | ||
package alicloud | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
) | ||
|
||
func TestAccAlicloudGwlbZoneDataSource(t *testing.T) { | ||
checkoutSupportedRegions(t, true, connectivity.EfloSupportRegions) | ||
rand := acctest.RandIntRange(1000000, 9999999) | ||
|
||
idsConf := dataSourceTestAccConfig{ | ||
existConfig: testAccCheckAlicloudGwlbZoneSourceConfig(rand, map[string]string{ | ||
"ids": `["cn-wulanchabu-b"]`, | ||
"accept_language": `"zh-CN"`, | ||
}), | ||
fakeConfig: testAccCheckAlicloudGwlbZoneSourceConfig(rand, map[string]string{ | ||
"ids": `["cn-wulanchabu-a"]`, | ||
"accept_language": `"zh-CN"`, | ||
}), | ||
} | ||
|
||
GwlbZoneCheckInfo.dataSourceTestCheck(t, rand, idsConf) | ||
} | ||
|
||
var existGwlbZoneMapFunc = func(rand int) map[string]string { | ||
return map[string]string{ | ||
"zones.#": CHECKSET, | ||
"zones.0.id": CHECKSET, | ||
"zones.0.zone_id": CHECKSET, | ||
"zones.0.local_name": CHECKSET, | ||
} | ||
} | ||
|
||
var fakeGwlbZoneMapFunc = func(rand int) map[string]string { | ||
return map[string]string{ | ||
"zones.#": "0", | ||
} | ||
} | ||
|
||
var GwlbZoneCheckInfo = dataSourceAttr{ | ||
resourceId: "data.alicloud_gwlb_zones.default", | ||
existMapFunc: existGwlbZoneMapFunc, | ||
fakeMapFunc: fakeGwlbZoneMapFunc, | ||
} | ||
|
||
func testAccCheckAlicloudGwlbZoneSourceConfig(rand int, attrMap map[string]string) string { | ||
var pairs []string | ||
for k, v := range attrMap { | ||
pairs = append(pairs, k+" = "+v) | ||
} | ||
config := fmt.Sprintf(` | ||
variable "name" { | ||
default = "tf-testAccGwlbZone%d" | ||
} | ||
data "alicloud_gwlb_zones" "default" { | ||
%s | ||
} | ||
`, rand, strings.Join(pairs, "\n ")) | ||
return config | ||
} |
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
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,49 @@ | ||
--- | ||
subcategory: "GWLB" | ||
layout: "alicloud" | ||
page_title: "Alicloud: alicloud_gwlb_zones" | ||
sidebar_current: "docs-alicloud-datasource-gwlb-zones" | ||
description: |- | ||
Provides a list of Gwlb Zone owned by an Alibaba Cloud account. | ||
--- | ||
|
||
# alicloud_gwlb_zones | ||
|
||
This data source provides Gwlb Zone available to the user.[What is Zone](https://www.alibabacloud.com/help/en/) | ||
|
||
-> **NOTE:** Available since v1.236.0. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
provider "alicloud" { | ||
region = "cn-wulanchabu" | ||
} | ||
data "alicloud_gwlb_zones" "default" { | ||
} | ||
output "alicloud_gwlb_zone_example_id" { | ||
value = data.alicloud_gwlb_zones.default.zones.0.id | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
* `accept_language` - (ForceNew, Optional) The supported language. Valid values: | ||
- **zh-CN**: Chinese | ||
- **en-US** (default): English | ||
- **ja**: Japanese | ||
* `ids` - (Optional, ForceNew, Computed) A list of Zone IDs. | ||
* `output_file` - (Optional) File name where to save data source results (after running `terraform plan`). | ||
|
||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported in addition to the arguments listed above: | ||
* `ids` - A list of Zone IDs. | ||
* `zones` - A list of Zone Entries. Each element contains the following attributes: | ||
* `local_name` - The zone name. | ||
* `zone_id` - The zone ID. | ||
* `id` - The zone ID. |