-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource
alicloud_dcdn_ipa_domain
;
New Datasource `alicloud_dcdn_ipa_domains`
- Loading branch information
1 parent
303db26
commit 2f6cff9
Showing
10 changed files
with
1,808 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
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,266 @@ | ||
package alicloud | ||
|
||
import ( | ||
"fmt" | ||
"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" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
) | ||
|
||
func dataSourceAlicloudDcdnIpaDomains() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAlicloudDcdnIpaDomainsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"ids": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
ForceNew: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Computed: true, | ||
}, | ||
"domain_name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"names": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Computed: true, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringInSlice([]string{"online", "offline", "configuring", "configure_failed", "checking", "check_failed"}, false), | ||
}, | ||
"output_file": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"domains": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"cert_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"cname": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"create_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"domain_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"resource_group_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"scope": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"sources": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"content": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"port": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"priority": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"weight": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"ssl_protocol": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"ssl_pub": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"enable_details": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAlicloudDcdnIpaDomainsRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*connectivity.AliyunClient) | ||
|
||
action := "DescribeDcdnIpaUserDomains" | ||
request := make(map[string]interface{}) | ||
if v, ok := d.GetOk("domain_name"); ok { | ||
request["DomainName"] = v | ||
} | ||
if v, ok := d.GetOk("status"); ok { | ||
request["DomainStatus"] = v | ||
} | ||
request["PageSize"] = PageSizeLarge | ||
request["PageNumber"] = 1 | ||
var objects []map[string]interface{} | ||
|
||
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) | ||
} | ||
} | ||
var response map[string]interface{} | ||
conn, err := client.NewDcdnClient() | ||
if err != nil { | ||
return WrapError(err) | ||
} | ||
for { | ||
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("POST"), StringPointer("2018-01-15"), StringPointer("AK"), nil, request, &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_dcdn_ipa_domains", action, AlibabaCloudSdkGoERROR) | ||
} | ||
resp, err := jsonpath.Get("$.Domains.PageData", response) | ||
if err != nil { | ||
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.Domains.PageData", response) | ||
} | ||
result, _ := resp.([]interface{}) | ||
for _, v := range result { | ||
item := v.(map[string]interface{}) | ||
if len(idsMap) > 0 { | ||
if _, ok := idsMap[fmt.Sprint(item["DomainName"])]; !ok { | ||
continue | ||
} | ||
} | ||
objects = append(objects, item) | ||
} | ||
if len(result) < PageSizeLarge { | ||
break | ||
} | ||
request["PageNumber"] = request["PageNumber"].(int) + 1 | ||
} | ||
ids := make([]string, 0) | ||
names := make([]string, 0) | ||
s := make([]map[string]interface{}, 0) | ||
for _, object := range objects { | ||
mapping := map[string]interface{}{ | ||
"cname": object["Cname"], | ||
"description": object["Description"], | ||
"id": fmt.Sprint(object["DomainName"]), | ||
"domain_name": fmt.Sprint(object["DomainName"]), | ||
"resource_group_id": object["ResourceGroupId"], | ||
"ssl_protocol": object["SSLProtocol"], | ||
"status": object["DomainStatus"], | ||
} | ||
if v, ok := object["Sources"].(map[string]interface{})["Source"].([]interface{}); ok { | ||
source := make([]map[string]interface{}, 0) | ||
for _, val := range v { | ||
item := val.(map[string]interface{}) | ||
source = append(source, map[string]interface{}{ | ||
"content": item["Content"], | ||
"port": formatInt(item["Port"]), | ||
"priority": item["Priority"], | ||
"type": item["Type"], | ||
"weight": formatInt(item["Weight"]), | ||
}) | ||
} | ||
mapping["sources"] = source | ||
} | ||
|
||
if detailedEnabled := d.Get("enable_details"); !detailedEnabled.(bool) { | ||
ids = append(ids, fmt.Sprint(mapping["id"])) | ||
names = append(names, object["DomainName"].(string)) | ||
s = append(s, mapping) | ||
continue | ||
} | ||
id := fmt.Sprint(object["DomainName"]) | ||
dcdnService := DcdnService{client} | ||
getResp, err := dcdnService.DescribeDcdnIpaDomain(id) | ||
if err != nil { | ||
return WrapError(err) | ||
} | ||
mapping["cert_name"] = getResp["CertName"] | ||
mapping["create_time"] = getResp["GmtCreated"] | ||
mapping["scope"] = getResp["Scope"] | ||
mapping["ssl_pub"] = getResp["SSLPub"] | ||
ids = append(ids, fmt.Sprint(object["DomainName"])) | ||
names = append(names, object["DomainName"].(string)) | ||
s = append(s, mapping) | ||
} | ||
|
||
d.SetId(dataResourceIdHash(ids)) | ||
if err := d.Set("ids", ids); err != nil { | ||
return WrapError(err) | ||
} | ||
if err := d.Set("names", names); err != nil { | ||
return WrapError(err) | ||
} | ||
if err := d.Set("domains", 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,127 @@ | ||
package alicloud | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
) | ||
|
||
func TestAccAlicloudDcdnIpaDomainsDataSource(t *testing.T) { | ||
rand := acctest.RandInt() | ||
checkoutSupportedRegions(t, true, connectivity.DCDNSupportRegions) | ||
idsConf := dataSourceTestAccConfig{ | ||
existConfig: testAccCheckAlicloudDcdnIpaDomainsDataSourceName(rand, map[string]string{ | ||
"ids": `["${alicloud_dcdn_ipa_domain.default.id}"]`, | ||
}), | ||
fakeConfig: testAccCheckAlicloudDcdnIpaDomainsDataSourceName(rand, map[string]string{ | ||
"ids": `["${alicloud_dcdn_ipa_domain.default.id}_fake"]`, | ||
}), | ||
} | ||
statusConf := dataSourceTestAccConfig{ | ||
existConfig: testAccCheckAlicloudDcdnIpaDomainsDataSourceName(rand, map[string]string{ | ||
"ids": `["${alicloud_dcdn_ipa_domain.default.id}"]`, | ||
"status": `"online"`, | ||
}), | ||
fakeConfig: testAccCheckAlicloudDcdnIpaDomainsDataSourceName(rand, map[string]string{ | ||
"ids": `["${alicloud_dcdn_ipa_domain.default.id}"]`, | ||
"status": `"offline"`, | ||
}), | ||
} | ||
domainNameConf := dataSourceTestAccConfig{ | ||
existConfig: testAccCheckAlicloudDcdnIpaDomainsDataSourceName(rand, map[string]string{ | ||
"ids": `["${alicloud_dcdn_ipa_domain.default.id}"]`, | ||
"domain_name": `"${alicloud_dcdn_ipa_domain.default.domain_name}"`, | ||
}), | ||
fakeConfig: testAccCheckAlicloudDcdnIpaDomainsDataSourceName(rand, map[string]string{ | ||
"ids": `["${alicloud_dcdn_ipa_domain.default.id}"]`, | ||
"domain_name": `"${alicloud_dcdn_ipa_domain.default.domain_name}-fake"`, | ||
}), | ||
} | ||
allConf := dataSourceTestAccConfig{ | ||
existConfig: testAccCheckAlicloudDcdnIpaDomainsDataSourceName(rand, map[string]string{ | ||
"ids": `["${alicloud_dcdn_ipa_domain.default.id}"]`, | ||
"status": `"online"`, | ||
"domain_name": `"${alicloud_dcdn_ipa_domain.default.domain_name}"`, | ||
}), | ||
fakeConfig: testAccCheckAlicloudDcdnIpaDomainsDataSourceName(rand, map[string]string{ | ||
"ids": `["${alicloud_dcdn_ipa_domain.default.id}"]`, | ||
"status": `"offline"`, | ||
"domain_name": `"${alicloud_dcdn_ipa_domain.default.domain_name}-fake"`, | ||
}), | ||
} | ||
var existAlicloudDcdnIpaDomainsDataSourceNameMapFunc = func(rand int) map[string]string { | ||
return map[string]string{ | ||
"ids.#": "1", | ||
"names.#": "1", | ||
"domains.#": "1", | ||
"domains.0.domain_name": fmt.Sprintf("tf-testacccn-%d.xiaozhu.com", rand), | ||
"domains.0.cert_name": "", | ||
"domains.0.cname": CHECKSET, | ||
"domains.0.create_time": CHECKSET, | ||
"domains.0.description": "", | ||
"domains.0.id": CHECKSET, | ||
"domains.0.resource_group_id": CHECKSET, | ||
"domains.0.scope": "domestic", | ||
"domains.0.sources.#": "1", | ||
"domains.0.sources.0.content": "www.xiaozhu.com", | ||
"domains.0.sources.0.port": "8898", | ||
"domains.0.sources.0.priority": "20", | ||
"domains.0.sources.0.type": "domain", | ||
"domains.0.sources.0.weight": "10", | ||
"domains.0.ssl_protocol": CHECKSET, | ||
"domains.0.ssl_pub": "", | ||
"domains.0.status": "online", | ||
} | ||
} | ||
var fakeAlicloudDcdnIpaDomainsDataSourceNameMapFunc = func(rand int) map[string]string { | ||
return map[string]string{ | ||
"ids.#": "0", | ||
"names.#": "0", | ||
} | ||
} | ||
var alicloudDcdnIpaDomainsCheckInfo = dataSourceAttr{ | ||
resourceId: "data.alicloud_dcdn_ipa_domains.default", | ||
existMapFunc: existAlicloudDcdnIpaDomainsDataSourceNameMapFunc, | ||
fakeMapFunc: fakeAlicloudDcdnIpaDomainsDataSourceNameMapFunc, | ||
} | ||
|
||
preCheck := func() { | ||
testAccPreCheck(t) | ||
testAccPreCheckEnterpriseAccountEnabled(t) | ||
} | ||
alicloudDcdnIpaDomainsCheckInfo.dataSourceTestCheckWithPreCheck(t, rand, preCheck, idsConf, statusConf, domainNameConf, allConf) | ||
} | ||
func testAccCheckAlicloudDcdnIpaDomainsDataSourceName(rand int, attrMap map[string]string) string { | ||
var pairs []string | ||
for k, v := range attrMap { | ||
pairs = append(pairs, k+" = "+v) | ||
} | ||
|
||
config := fmt.Sprintf(` | ||
variable "domain_name" { | ||
default = "tf-testacccn-%d.xiaozhu.com" | ||
} | ||
resource "alicloud_dcdn_ipa_domain" "default" { | ||
domain_name = "${var.domain_name}" | ||
scope = "domestic" | ||
sources { | ||
content = "www.xiaozhu.com" | ||
port = 8898 | ||
priority = "20" | ||
type = "domain" | ||
weight = 10 | ||
} | ||
} | ||
data "alicloud_dcdn_ipa_domains" "default" { | ||
enable_details = true | ||
%s | ||
} | ||
`, rand, strings.Join(pairs, " \n ")) | ||
return config | ||
} |
Oops, something went wrong.