package alicloud

import (
	"fmt"
	"github.com/PaesslerAG/jsonpath"
	"log"
	"strings"
	"testing"
	"time"

	"github.com/aliyun/alibaba-cloud-sdk-go/services/dcdn"
	"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
	"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
	"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func init() {
	resource.AddTestSweepers(
		"alicloud_dcdn_domain",
		&resource.Sweeper{
			Name: "alicloud_dcdn_domain",
			F:    testSweepDcdnDomain,
		})
}

func testSweepDcdnDomain(region string) error {
	rawClient, err := sharedClientForRegion(region)
	if err != nil {
		return WrapError(err)
	}
	client := rawClient.(*connectivity.AliyunClient)

	action := "DescribeDcdnUserDomains"
	request := make(map[string]interface{})
	request["PageSize"] = PageSizeLarge
	request["PageNumber"] = 1

	var allDomains []string
	var response map[string]interface{}
	for {
		response, err = client.RpcPost("dcdn", "2018-01-15", action, nil, request, true)
		if err != nil {
			return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_dcdn_domains", action, AlibabaCloudSdkGoERROR)
		}
		addDebug(action, response, request)

		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 strings.HasPrefix(item["DomainName"].(string), "tf-testacc") {
				allDomains = append(allDomains, item["DomainName"].(string))
			} else {
				log.Printf("Skip %#v", item["DomainName"].(string))
			}
		}
		if len(result) < PageSizeLarge {
			break
		}
		request["PageNumber"] = request["PageNumber"].(int) + 1
	}

	action = "DeleteDcdnDomain"
	for _, domain := range allDomains {
		query := make(map[string]interface{})
		request = make(map[string]interface{})
		query["DomainName"] = domain

		wait := incrementalWait(3*time.Second, 5*time.Second)
		err = resource.Retry(1*time.Minute, func() *resource.RetryError {
			response, err = client.RpcPost("dcdn", "2018-01-15", action, query, request, false)
			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, domain, action, AlibabaCloudSdkGoERROR)
		}
	}

	return nil
}

func TestAccAliCloudDcdnDomain_basic0(t *testing.T) {
	var v dcdn.DomainDetail
	resourceId := "alicloud_dcdn_domain.default"
	ra := resourceAttrInit(resourceId, DcdnDomainMap)
	rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
		return &DcdnService{testAccProvider.Meta().(*connectivity.AliyunClient)}
	}, "DescribeDcdnDomain")
	rac := resourceAttrCheckInit(rc, ra)
	testAccCheck := rac.resourceAttrMapUpdateSet()
	rand := acctest.RandIntRange(1000000, 9999999)
	name := fmt.Sprintf("tf-testacc%s%d.alicloud-provider.cn", defaultRegionToTest, rand)
	testAccConfig := resourceTestAccConfigFunc(resourceId, name, DcdnDomainBasicdependence)
	resource.Test(t, resource.TestCase{
		PreCheck: func() {
			testAccPreCheck(t)
		},

		IDRefreshName: resourceId,
		Providers:     testAccProviders,
		CheckDestroy:  rac.checkResourceDestroy(),
		Steps: []resource.TestStep{
			{
				Config: testAccConfig(map[string]interface{}{
					"domain_name": name,
					"scope":       "overseas",
					"status":      "online",
					"sources": []map[string]interface{}{
						{
							"content":  "1.1.1.1",
							"port":     "80",
							"priority": "20",
							"type":     "ipaddr",
							"weight":   "10",
						},
					},
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"domain_name": name,
						"sources.#":   "1",
						"scope":       "overseas",
					}),
				),
			},
			{
				Config: testAccConfig(map[string]interface{}{
					"ssl_protocol": "on",
					"ssl_pub":      "${var.public_key}",
					"ssl_pri":      "${var.private_key}",
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"ssl_protocol": "on",
						"ssl_pub":      CHECKSET,
						"ssl_pri":      CHECKSET,
					}),
				),
			},
			{
				Config: testAccConfig(map[string]interface{}{
					"resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}",
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"resource_group_id": CHECKSET,
					}),
				),
			},
			{
				Config: testAccConfig(map[string]interface{}{
					"status": "offline",
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"status": "offline",
					}),
				),
			},
			{
				Config: testAccConfig(map[string]interface{}{
					"status": "online",
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"status": "online",
					}),
				),
			},
			{
				Config: testAccConfig(map[string]interface{}{
					"sources": []map[string]interface{}{
						{
							"content":  "1.1.1.1",
							"port":     "80",
							"priority": "20",
							"type":     "ipaddr",
							"weight":   "10",
						},
						{
							"content": "2.2.2.2",
							"type":    "ipaddr",
						},
					},
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"sources.#": "2",
					}),
				),
			},
			{
				Config: testAccConfig(map[string]interface{}{
					"sources": []map[string]interface{}{
						{
							"content":  "1.1.1.1",
							"port":     "80",
							"priority": "20",
							"type":     "ipaddr",
							"weight":   "10",
						},
						{
							"content":  "3.3.3.3",
							"port":     "8080",
							"priority": "30",
							"type":     "ipaddr",
							"weight":   "20",
						},
					},
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"sources.#": "2",
					}),
				),
			},
			{
				ResourceName:            resourceId,
				ImportState:             true,
				ImportStateVerify:       true,
				ImportStateVerifyIgnore: []string{"top_level_domain", "cert_type", "check_url", "ssl_pri"},
			},
		},
	})
}

func TestAccAliCloudDcdnDomain_basic1(t *testing.T) {
	var v dcdn.DomainDetail
	resourceId := "alicloud_dcdn_domain.default"
	ra := resourceAttrInit(resourceId, DcdnDomainMap)
	rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
		return &DcdnService{testAccProvider.Meta().(*connectivity.AliyunClient)}
	}, "DescribeDcdnDomain")
	rac := resourceAttrCheckInit(rc, ra)
	testAccCheck := rac.resourceAttrMapUpdateSet()
	rand := acctest.RandIntRange(1000000, 9999999)
	name := fmt.Sprintf("tf-testacc%s%d.alicloud-provider.cn", defaultRegionToTest, rand)
	testAccConfig := resourceTestAccConfigFunc(resourceId, name, DcdnDomainBasicdependence)
	resource.Test(t, resource.TestCase{
		PreCheck: func() {
			testAccPreCheck(t)
		},
		IDRefreshName: resourceId,
		Providers:     testAccProviders,
		CheckDestroy:  rac.checkResourceDestroy(),
		Steps: []resource.TestStep{
			{
				Config: testAccConfig(map[string]interface{}{
					"domain_name":       name,
					"scope":             "overseas",
					"status":            "online",
					"resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}",
					"sources": []map[string]interface{}{
						{
							"content":  "1.1.1.1",
							"port":     "80",
							"priority": "20",
							"type":     "ipaddr",
							"weight":   "10",
						},
					},
					"tags": map[string]string{
						"Created": "TF",
						"For":     "Domain",
					},
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"domain_name":       name,
						"sources.#":         "1",
						"resource_group_id": CHECKSET,
						"tags.%":            "2",
						"tags.Created":      "TF",
						"tags.For":          "Domain",
					}),
				),
			},
			{
				Config: testAccConfig(map[string]interface{}{
					"tags": map[string]string{
						"Created": "TF_Update",
						"For":     "Domain_Update",
					},
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"tags.%":       "2",
						"tags.Created": "TF_Update",
						"tags.For":     "Domain_Update",
					}),
				),
			},
			{
				ResourceName:            resourceId,
				ImportState:             true,
				ImportStateVerify:       true,
				ImportStateVerifyIgnore: []string{"top_level_domain", "cert_type", "check_url", "ssl_pri"},
			},
		},
	})
}

var DcdnDomainMap = map[string]string{
	"resource_group_id": CHECKSET,
	"ssl_protocol":      "off",
	"scope":             "overseas",
	"status":            CHECKSET,
	"cname":             CHECKSET,
}

func DcdnDomainBasicdependence(name string) string {
	return fmt.Sprintf(`
	
variable "name" {
	default = "%s"
}

variable "private_key" {
  default = <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aGXpOjQMn
4g6L4NNQFt5meLrVTnibw0trHbjNIV2+q+LvFRKUa0n0WfkFgkOJq8PfKS8mUVRf
a+gQABJwlfs5ShLO40h25ZnXXOQMozqFNm2cSfUCis6H/M3MhGrATNfWdOotwmYf
5+VMkDg6c4aDiDrjzh4trGfS5xNhA0tiAeRokdrNpLvtzpR9YrBGgWFl6BARlcTI
avwgrYKk1t1zfo/ahqH057xPT1jbfpAcxQf/Ic0dAgMBAAGjgdkwgdYwDgYDVR0P
AQH/BAQDAgWgMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAfBgNVHSME
GDAWgBQogSYF0TQaP8FzD7uTzxUcPwO/fzBjBggrBgEFBQcBAQRXMFUwIQYIKwYB
BQUHMAGGFWh0dHA6Ly9vY3NwLm15c3NsLmNvbTAwBggrBgEFBQcwAoYkaHR0cDov
L2NhLm15c3NsLmNvbS9teXNzbHRlc3Ryc2EuY3J0MB8GA1UdEQQYMBaCFGFsaWNs
b3VkLXByb3ZpZGVyLmNuMA0GCSqGSIb3DQEBCwUAA4IBAQABSp2RLQD+NEudQ1Z2
yhCD9ADbdrWQHPBZgtUV0EjN4gMYucz7dzWo1xjg5BhKd8naku21U2ZUa8TgnIgt
IK+GL8gLex4iXq9CiZqZsFhYnuopR0ISULtC+Oz+YfrKfzMHDK9UU3AZT8bKT4mm
T9nAWV5Fa4Ik1HlA0kykNVrNCef+zLT4W7x/YMSPIMUDHRMeGXOEPnqIOBnR0ha+
KDhZPviYhN2M4u0tVVb/2NBQLYgVLspj28dQShBlrXC51SurAwmnw5gcVlJG3r1H
b494lL9Ycx+Q3rlziqYLMYq3+8x+bNQhI1iDjWeYtVoG2qyX4Q8l5IOWQ/mKtYz1
nf8k
-----END CERTIFICATE-----
EOF
}

data "alicloud_resource_manager_resource_groups" "default" {
}
`, name)
}

// Case SetDcdnDomainSSLCertificate替换 7246
var AlicloudDcdnDomainMap7246 = map[string]string{
	"status":      CHECKSET,
	"create_time": CHECKSET,
}

func AlicloudDcdnDomainBasicDependence7246(name string) string {
	return fmt.Sprintf(`
variable "name" {
    default = "%s"
}

resource "alicloud_ssl_certificates_service_certificate" "default" {
  		certificate_name = "casdcdnterr"
  		cert             = <<EOF
-----BEGIN CERTIFICATE-----
MIID2zCCAsOgAwIBAgIRAN2UzXkVHEkUn0xJezHFFoMwDQYJKoZIhvcNAQELBQAw
XjELMAkGA1UEBhMCQ04xDjAMBgNVBAoTBU15U1NMMSswKQYDVQQLEyJNeVNTTCBU
ZXN0IFJTQSAtIEZvciB0ZXN0IHVzZSBvbmx5MRIwEAYDVQQDEwlNeVNTTC5jb20w
HhcNMjQxMjIzMDU1MjU1WhcNMjkxMjIyMDU1MjU1WjAiMQswCQYDVQQGEwJDTjET
MBEGA1UEAxMKcGZ5dGxtLnh5ejCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
ggEBAMA7x0zoPGObPQYsrAoVJzXOeete55XG2YJWVdxfcivWbdIeG144BRTcrah4
pheJTjTVS4MiS5Q3iV5Zq9n/cPUFyrMWF75NUylOHGss9MNoemFeji0WPnRWij4c
s4WfvHb7W++YUyNFIqQxt0yOscrbtmu0gUsZZyF7lXWe2FO+tbrNv17jxHSduiey
86T806IfDcTDabOEzEGnbO3P2BjlhRxexKJf7Q4MNJ5+K+N5wGNZzml1hz3/vSwh
T6etbdpcFqy5UoA/c9pS4RZfL0udx4XHoORtyP/KzlvQ0yw7wBN+mIDfCj6Is6sw
X4qi+ZVZ1DvoZBCl45H78NzZVrUCAwEAAaOBzzCBzDAOBgNVHQ8BAf8EBAMCBaAw
HQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMB8GA1UdIwQYMBaAFCiBJgXR
NBo/wXMPu5PPFRw/A79/MGMGCCsGAQUFBwEBBFcwVTAhBggrBgEFBQcwAYYVaHR0
cDovL29jc3AubXlzc2wuY29tMDAGCCsGAQUFBzAChiRodHRwOi8vY2EubXlzc2wu
Y29tL215c3NsdGVzdHJzYS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6GZpmHs7IPsFiErexsd8sfUS52evDrJtClPg0M6Gpjv65gU5kq3v
+jbHEcRe98YIn7noXfLOUCDGYQVSfyxou1czYCOdx7enIE0tNqk46j8mBVc6OhMA
XkVCRYECgYEA+1OZfO3gcinnKtFMpVCmDi64DU4l0D0vwXyCt6TdBcxPgMh51tOr
XWOQTU4uf941kKDIVMdzW1agG242HUAz+73AS0H/+QjzoYf//PSsWje/xCccfVS3
sjXBKhMoVwIrWempeakGIfOi7yjCv1kkQstPJ8Qokf3/h3cgWyHdXncCgYEAw87g
NQ4pGLLUMyNfK0SJlPLp5jdghY+/a1MHxFaOcjmOv5jqN57Rvub3ADEmlMcda7W3
A5qU8qmx5gbsM0oBDl9bbmqxF95BHqJrQcaPDw7V7HOcZf6gdMNPtSn0DK9uG31s
IZQDD7kxD4PT4/FWhPRqsFZ9L/zMIWwZCxfk4zMCgYAvFLEbIyC5ojno3n6CNYJ2
A7B85ZfV07B/iYifSGYTMPvvvx577PkcLIuav7ucPo9AQa5lm1tzz918ZgADKMTU
Mu6z6nA+QbwKFYUR6O/kkq782urOW7Fx0/oUnLQg4IoodMpHvS8l6xMpxDP/Tn6p
eJaid2+2MaPNx7Yq/EQQ7wKBgQCdvDeNRd0BUn3yvBncRxf170FQ/Wc58LSpBngJ
SBj0Fz3RRqPXLo+Uk4aClxWXYFdo/zdxJcO7P8xZm1YHcyQqqdKDvlru+VHIFdsF
X6i63p6iHfftihNEPFonfKZm2aN/baf/3LYionLNJss4op+p9yNC7klmsOTYP7Zk
41i1VQKBgFTVLZpG5vE3x13bxUez3ydg9NLClrxwRHiHPBUlQx/McQsBpnWrDBmU
rJyNbR+eJR+mjbbT75w137lc2lOV/K86SnhEa1E7LS/mzI7jlxw3dDS30rRYw3e0
CiHrbj05hznS76XoNzFKtTvMgrynZKIAtmrORPss7H/RlhhhAgDm
-----END RSA PRIVATE KEY-----
EOF
}

variable "cert" {
	default = <<EOF
-----BEGIN CERTIFICATE-----
MIID2jCCAsKgAwIB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QcVYafH33H1Z5CCFB487BycgSTxZy3NH+CRr4fhP/U
BFSi6z8AiipqahQJwKCEvgDLOBzDZjVh7//UFYpjruQPeoKM75eYbeF6lNUhsFwG
btSjLqyc3QquKeRZQq4Ouuxa1mZkSxr1e/xbb03ICgl+pmnJNcd+UhsCgYEAyKVB
pZ3ulB0lfi7IxOcEjl26LPwk9WOCSY2UVpXqmvqnigZ3i0zWd/kvv1PzFX4yGXyL
7kC3hS60foJPTbR/GAsfl8t+FVTp/MXuIC/n03DWBDO0nQAt3qXDrtLl8NDL8R9o
+XNLHM4cAFqUSxFPFZ3LVI2Q7148FAVSP95GjeMCgYBftGX8S4XupvjdlrBvu1IO
yhzNFS67IoS7P0CXJfJqHBcbSKcYpte74RPG40kSnNF6m6pHPRq+fIlhY9EqUyVz
2ZaRl1ZxRaXNoIY935OKu/mPVkWd8zs+D8S5VR4pCeyYrZynxf17IldpcRvsRK1K
ZrNQOTsKo6vXf7jfcs/C7QKBgDp9umpuZNt2t7RWLR8BfZmHBzwP8TI75QJOLJ0l
LPQq9+ZLxlOsfaUR1nJ/JZDxbedyIFS/NwCzQdjTYgzz/kzjCT22C7ZqP5/5j1aA
wKMp9Kna7N8L61NJnYb8Yh3WsG1FS9PUYWQvTYho32wWyqgxjNHERykQnpDzkCug
P48jAoGARm6kA+MugcTgU1dU7O0/W2NGKKgeTKlzj72itPliBaTIg8GgIFeMSfdO
SGPpkuSYQzClpS5mOkkNHL7YTbKkPpX08/O1V7nri1Tabr9VLIxNRSqTpxCMksu2
pSFqMSjrbbQkxmcUyomKRMWXPovJbxbdL5k6Ht8zOD9U015GXTI=
-----END RSA PRIVATE KEY-----
EOF
}
resource "alicloud_ssl_certificates_service_certificate" "change" {
  		certificate_name = "casdcdnterr_update"
  		cert             = var.cert
  		key              = var.private_key
}

`, name)
}

// Case SetDcdnDomainSSLCertificate替换 7246  raw
func TestAccAliCloudDcdnDomain_basic7246_raw(t *testing.T) {
	var v map[string]interface{}
	resourceId := "alicloud_dcdn_domain.default"
	ra := resourceAttrInit(resourceId, AlicloudDcdnDomainMap7246)
	rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
		return &DcdnServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)}
	}, "DescribeDcdnDomain")
	rac := resourceAttrCheckInit(rc, ra)
	testAccCheck := rac.resourceAttrMapUpdateSet()
	name := "test02.pfytlm.xyz"
	testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudDcdnDomainBasicDependence7246)
	resource.Test(t, resource.TestCase{
		PreCheck: func() {
			testAccPreCheck(t)
			testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"})
		},
		IDRefreshName: resourceId,
		Providers:     testAccProviders,
		CheckDestroy:  rac.checkResourceDestroy(),
		Steps: []resource.TestStep{
			{
				Config: testAccConfig(map[string]interface{}{
					"ssl_protocol": "off",
					"domain_name":  name,
					"sources": []map[string]interface{}{
						{
							"type":     "ipaddr",
							"content":  "1.1.1.1",
							"priority": "20",
							"port":     "80",
							"weight":   "20",
						},
					},
					"scope":     "domestic",
					"check_url": "http://test02.pfytlm.xyz/test.html",
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"ssl_protocol": "off",
						"domain_name":  name,
						"sources.#":    "1",
						"scope":        "domestic",
						"check_url":    "http://test02.pfytlm.xyz/test.html",
					}),
				),
			},
			{
				Config: testAccConfig(map[string]interface{}{
					"ssl_protocol": "on",
					"cert_region":  "cn-hangzhou",
					"cert_type":    "cas",
					"cert_id":      "${alicloud_ssl_certificates_service_certificate.default.id}",
					"cert_name":    "casdcdnterr",
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"ssl_protocol": "on",
						"cert_region":  "cn-hangzhou",
						"cert_type":    "cas",
						"cert_id":      CHECKSET,
						"cert_name":    "casdcdnterr",
					}),
				),
			},
			{
				Config: testAccConfig(map[string]interface{}{
					"cert_id":   "${alicloud_ssl_certificates_service_certificate.change.id}",
					"cert_name": "casdcdnterr_update",
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"cert_id":   CHECKSET,
						"cert_name": "casdcdnterr_update",
					}),
				),
			},
			{
				ResourceName:            resourceId,
				ImportState:             true,
				ImportStateVerify:       true,
				ImportStateVerifyIgnore: []string{"check_url", "env", "ssl_pri", "top_level_domain"},
			},
		},
	})
}

func TestAccAliCloudDcdnDomain_basic7246_change(t *testing.T) {
	var v map[string]interface{}
	resourceId := "alicloud_dcdn_domain.default"
	ra := resourceAttrInit(resourceId, AlicloudDcdnDomainMap7246)
	rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
		return &DcdnServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)}
	}, "DescribeDcdnDomain")
	rac := resourceAttrCheckInit(rc, ra)
	testAccCheck := rac.resourceAttrMapUpdateSet()
	name := "test02.pfytlm.xyz"
	rand := acctest.RandIntRange(1000000, 9999999)
	certName := fmt.Sprintf("tf-testacc%s%d.alicloud-provider.cn", defaultRegionToTest, rand)
	testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudDcdnDomainBasicDependence7246)
	resource.Test(t, resource.TestCase{
		PreCheck: func() {
			testAccPreCheck(t)
			testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"})
		},
		IDRefreshName: resourceId,
		Providers:     testAccProviders,
		CheckDestroy:  rac.checkResourceDestroy(),
		Steps: []resource.TestStep{
			{
				Config: testAccConfig(map[string]interface{}{
					"domain_name":  name,
					"scope":        "domestic",
					"ssl_protocol": "on",
					"sources": []map[string]interface{}{
						{
							"type":     "ipaddr",
							"content":  "1.1.1.1",
							"priority": "20",
							"port":     "80",
							"weight":   "30",
						},
					},
					"cert_type":     "upload",
					"cert_name":     certName,
					"ssl_pub":       "${var.cert}",
					"ssl_pri":       "${var.private_key}",
					"scene":         "apiscene",
					"function_type": "routine",
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"ssl_protocol":  "on",
						"cert_type":     "upload",
						"cert_name":     CHECKSET,
						"ssl_pub":       CHECKSET,
						"ssl_pri":       CHECKSET,
						"scene":         "apiscene",
						"function_type": "routine",
					}),
				),
			},
			{
				Config: testAccConfig(map[string]interface{}{
					"ssl_protocol": "off",
					"domain_name":  name,
					"sources": []map[string]interface{}{
						{
							"type":     "ipaddr",
							"content":  "1.1.1.1",
							"priority": "20",
							"port":     "80",
							"weight":   "20",
						},
					},
					"scope":       "domestic",
					"check_url":   "http://test02.pfytlm.xyz/test.html",
					"cert_region": "",
					"cert_id":     "",
					"cert_name":   "",
					"cert_type":   "cas",
					"ssl_pub":     "",
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"ssl_protocol": "off",
						"domain_name":  name,
						"sources.#":    "1",
						"scope":        "domestic",
						"check_url":    "http://test02.pfytlm.xyz/test.html",
						"cert_region":  "",
						"cert_id":      "",
						"cert_name":    "",
						"cert_type":    "cas",
						"ssl_pub":      "",
					}),
				),
			},
			{
				Config: testAccConfig(map[string]interface{}{
					"ssl_protocol": "on",
					"cert_region":  "cn-hangzhou",
					"cert_type":    "cas",
					"cert_id":      "${alicloud_ssl_certificates_service_certificate.default.id}",
					"cert_name":    "casdcdnterr",
					"env":          "no-staging",
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"ssl_protocol": "on",
						"cert_region":  "cn-hangzhou",
						"cert_type":    "cas",
						"cert_id":      CHECKSET,
						"cert_name":    "casdcdnterr",
						"ssl_pub":      CHECKSET,
					}),
				),
			},
			{
				Config: testAccConfig(map[string]interface{}{
					"cert_id":   "${alicloud_ssl_certificates_service_certificate.change.id}",
					"cert_name": certName + "_update",
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"cert_id":   CHECKSET,
						"cert_name": CHECKSET,
					}),
				),
			},
			{
				Config: testAccConfig(map[string]interface{}{
					"scope": "global",
				}),
				Check: resource.ComposeTestCheckFunc(
					testAccCheck(map[string]string{
						"scope": "global",
					}),
				),
			},
			{
				ResourceName:            resourceId,
				ImportState:             true,
				ImportStateVerify:       true,
				ImportStateVerifyIgnore: []string{"check_url", "env", "ssl_pri", "top_level_domain"},
			},
		},
	})
}

// Test Dcdn Domain. <<< Resource test cases, automatically generated.
