Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Datasource
alicloud_cdn_blocked_regions
;
- Loading branch information
1 parent
06c71e5
commit 0ed3875
Showing
5 changed files
with
203 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,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 | ||
} |
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,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 | ||
} |
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
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,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. |