Skip to content

Commit

Permalink
New Datasource alicloud_cdn_blocked_regions;
Browse files Browse the repository at this point in the history
  • Loading branch information
super-eggs committed Jun 20, 2022
1 parent 06c71e5 commit 0ed3875
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 0 deletions.
107 changes: 107 additions & 0 deletions alicloud/data_source_alicloud_cdn_blocked_regions.go
@@ -0,0 +1,107 @@
package alicloud

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"strconv"
"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 dataSourceAlicloudCdnBlockedRegions() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudCdnBlockedRegionsRead,
Schema: map[string]*schema.Schema{
"language": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"zh", "en", "jp"}, false),
},
"regions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"countries_and_regions": {
Type: schema.TypeString,
Computed: true,
},
"continent": {
Type: schema.TypeString,
Computed: true,
},
"countries_and_regions_name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceAlicloudCdnBlockedRegionsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)

action := "DescribeBlockedRegions"
request := make(map[string]interface{})

request["Language"] = d.Get("language")

var response map[string]interface{}
conn, err := client.NewCdnClient()
if err != nil {
return WrapError(err)
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("GET"), StringPointer("2018-05-10"), StringPointer("AK"), request, nil, &runtime)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_cdn_blocked_regions", action, AlibabaCloudSdkGoERROR)
}
resp, err := jsonpath.Get("$.InfoList.InfoItem", response)
if err != nil {
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.InfoList.InfoItem", response)
}
objects := resp.([]interface{})
s := make([]map[string]interface{}, 0)
for _, item := range objects {
object := item.(map[string]interface{})
mapping := map[string]interface{}{
"continent": fmt.Sprint(object["Continent"]),
"countries_and_regions_name": fmt.Sprint(object["CountriesAndRegionsName"]),
"countries_and_regions": fmt.Sprint(object["CountriesAndRegions"]),
}

s = append(s, mapping)
}

d.SetId(strconv.FormatInt(time.Now().Unix(), 16))
if err := d.Set("regions", s); err != nil {
return WrapError(err)
}
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}

return nil
}
53 changes: 53 additions & 0 deletions alicloud/data_source_alicloud_cdn_blocked_regions_test.go
@@ -0,0 +1,53 @@
package alicloud

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)

func TestAccAlicloudCdnBlockedRegionsDataSource(t *testing.T) {
rand := acctest.RandIntRange(100, 999)
languageConf := dataSourceTestAccConfig{
existConfig: testAccCheckAlicloudCdnBlockedRegionsDataSourceName(rand, map[string]string{
"language": `"zh"`,
}),
fakeConfig: "",
}

var existAlicloudClickHouseRegionDataSourceNameMapFunc = func(rand int) map[string]string {
return map[string]string{
"regions.#": CHECKSET,
}
}
var fakeClickHouseRegionsMapFunc = func(rand int) map[string]string {
return map[string]string{
"regions.#": "0",
}
}
var alicloudCdnBlockedRegionsCheckInfo = dataSourceAttr{
resourceId: "data.alicloud_cdn_blocked_regions.default",
existMapFunc: existAlicloudClickHouseRegionDataSourceNameMapFunc,
fakeMapFunc: fakeClickHouseRegionsMapFunc,
}

preCheck := func() {
testAccPreCheck(t)
}
alicloudCdnBlockedRegionsCheckInfo.dataSourceTestCheckWithPreCheck(t, rand, preCheck, languageConf)
}
func testAccCheckAlicloudCdnBlockedRegionsDataSourceName(rand int, attrMap map[string]string) string {
var pairs []string
for k, v := range attrMap {
pairs = append(pairs, k+" = "+v)
}

config := fmt.Sprintf(`
data "alicloud_cdn_blocked_regions" "default" {
%s
}
`, strings.Join(pairs, " \n "))
return config
}
1 change: 1 addition & 0 deletions alicloud/provider.go
Expand Up @@ -699,6 +699,7 @@ func Provider() terraform.ResourceProvider {
"alicloud_cms_namespaces": dataSourceAlicloudCmsNamespaces(),
"alicloud_cms_sls_groups": dataSourceAlicloudCmsSlsGroups(),
"alicloud_config_aggregate_deliveries": dataSourceAlicloudConfigAggregateDeliveries(),
"alicloud_cdn_blocked_regions": dataSourceAlicloudCdnBlockedRegions(),
},
ResourcesMap: map[string]*schema.Resource{
"alicloud_instance": resourceAliyunInstance(),
Expand Down
3 changes: 3 additions & 0 deletions website/alicloud.erb
Expand Up @@ -642,6 +642,9 @@
<li>
<a href="#">Data Sources</a>
<ul class="nav nav-auto-expand">
<li>
<a href="/docs/providers/alicloud/d/cdn_blocked_regions.html">alicloud_cdn_blocked_regions</a>
</li>
<li>
<a href="/docs/providers/alicloud/d/cdn_ip_info.html">alicloud_cdn_ip_info</a>
</li>
Expand Down
39 changes: 39 additions & 0 deletions website/docs/d/cdn_blocked_regions.html.markdown
@@ -0,0 +1,39 @@
---
subcategory: "CDN"
layout: "alicloud"
page_title: "Alicloud: alicloud_cdn_blocked_regions"
sidebar_current: "docs-alicloud-datasource-cdn-blocked-regions"
description: |-
Provides a list of Cdn Blocked Regions to the user.
---

# alicloud\_cdn\_blocked\_regions

This data source provides the Cdn blocked regions.

-> **NOTE:** Available in v1.173.0+.

## Example Usage

Basic Usage

```terraform
data "alicloud_cdn_blocked_regions" "example" {
language = "zh"
}
```

## Argument Reference

The following arguments are supported:

* `language` - (Required, ForceNew) The language. Valid values: `zh`, `en`, `jp`.

## Argument Reference

The following attributes are exported in addition to the arguments listed above:

* `regions` - A list of Cdn Blocked Regions. Each element contains the following attributes:
* `continent` - The region to which the country belongs.
* `countries_and_regions` - National region abbreviation.
* `countries_and_regions_name` - The name of the country and region.

0 comments on commit 0ed3875

Please sign in to comment.