From 3c4b46d2cd4daf742589a1b43e887ac1ebe55c2a Mon Sep 17 00:00:00 2001 From: chenhanzhang Date: Thu, 28 Nov 2024 21:47:20 +0800 Subject: [PATCH] New Data Source: alicloud_gwlb_zones. --- alicloud/data_source_alicloud_gwlb_zones.go | 160 ++++++++++++++++++ .../data_source_alicloud_gwlb_zones_test.go | 66 ++++++++ alicloud/provider.go | 1 + website/docs/d/gwlb_zones.html.markdown | 49 ++++++ 4 files changed, 276 insertions(+) create mode 100644 alicloud/data_source_alicloud_gwlb_zones.go create mode 100644 alicloud/data_source_alicloud_gwlb_zones_test.go create mode 100644 website/docs/d/gwlb_zones.html.markdown diff --git a/alicloud/data_source_alicloud_gwlb_zones.go b/alicloud/data_source_alicloud_gwlb_zones.go new file mode 100644 index 000000000000..62e42b4bb525 --- /dev/null +++ b/alicloud/data_source_alicloud_gwlb_zones.go @@ -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 +} diff --git a/alicloud/data_source_alicloud_gwlb_zones_test.go b/alicloud/data_source_alicloud_gwlb_zones_test.go new file mode 100644 index 000000000000..947cda44de20 --- /dev/null +++ b/alicloud/data_source_alicloud_gwlb_zones_test.go @@ -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 +} diff --git a/alicloud/provider.go b/alicloud/provider.go index 249f5a11b048..fb5f50882268 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -166,6 +166,7 @@ func Provider() terraform.ResourceProvider { }, }, DataSourcesMap: map[string]*schema.Resource{ + "alicloud_gwlb_zones": dataSourceAliCloudGwlbZones(), "alicloud_gpdb_data_backups": dataSourceAliCloudGpdbDataBackups(), "alicloud_gpdb_log_backups": dataSourceAliCloudGpdbLogbackups(), "alicloud_governance_baselines": dataSourceAliCloudGovernanceBaselines(), diff --git a/website/docs/d/gwlb_zones.html.markdown b/website/docs/d/gwlb_zones.html.markdown new file mode 100644 index 000000000000..e4194e8928a6 --- /dev/null +++ b/website/docs/d/gwlb_zones.html.markdown @@ -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.