Skip to content

Commit

Permalink
New Data Source: alicloud_gwlb_zones.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenHanZhang committed Nov 28, 2024
1 parent 906d080 commit 3c4b46d
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 0 deletions.
160 changes: 160 additions & 0 deletions alicloud/data_source_alicloud_gwlb_zones.go
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
}
66 changes: 66 additions & 0 deletions alicloud/data_source_alicloud_gwlb_zones_test.go
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
}
1 change: 1 addition & 0 deletions alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
49 changes: 49 additions & 0 deletions website/docs/d/gwlb_zones.html.markdown
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.

0 comments on commit 3c4b46d

Please sign in to comment.