Skip to content

Commit f340e38

Browse files
committed
resource/alicloud_esa_origin_pool: Changed the field origin_pool_id, origins.origin_id from TypeInt to TypeString; Improved alicloud_esa_origin_pool testcase
1 parent a68c036 commit f340e38

3 files changed

Lines changed: 164 additions & 74 deletions

File tree

alicloud/resource_alicloud_esa_origin_pool.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ func resourceAliCloudEsaOriginPool() *schema.Resource {
3333
"enabled": {
3434
Type: schema.TypeBool,
3535
Optional: true,
36+
Computed: true,
3637
},
3738
"origin_pool_id": {
38-
Type: schema.TypeInt,
39+
Type: schema.TypeString,
3940
Computed: true,
4041
},
4142
"origin_pool_name": {
@@ -69,7 +70,7 @@ func resourceAliCloudEsaOriginPool() *schema.Resource {
6970
Optional: true,
7071
},
7172
"origin_id": {
72-
Type: schema.TypeInt,
73+
Type: schema.TypeString,
7374
Computed: true,
7475
},
7576
"auth_conf": {
@@ -141,7 +142,9 @@ func resourceAliCloudEsaOriginPoolCreate(d *schema.ResourceData, meta interface{
141142
dataLoopTmp := dataLoop.(map[string]interface{})
142143
dataLoopMap := make(map[string]interface{})
143144
dataLoopMap["Address"] = dataLoopTmp["address"]
144-
dataLoopMap["Header"] = parseHeader(dataLoopTmp["header"].(string))
145+
if header, ok := dataLoopTmp["header"]; ok && fmt.Sprint(header) != "" {
146+
dataLoopMap["Header"] = parseHeader(header.(string))
147+
}
145148
dataLoopMap["Type"] = dataLoopTmp["type"]
146149
localData1 := make(map[string]interface{})
147150
version1, _ := jsonpath.Get("$[0].version", dataLoopTmp["auth_conf"])
@@ -166,7 +169,9 @@ func resourceAliCloudEsaOriginPoolCreate(d *schema.ResourceData, meta interface{
166169
}
167170
dataLoopMap["AuthConf"] = localData1
168171
dataLoopMap["Weight"] = dataLoopTmp["weight"]
169-
dataLoopMap["Enabled"] = dataLoopTmp["enabled"]
172+
if enabled, ok := dataLoopTmp["enabled"]; ok {
173+
dataLoopMap["Enabled"] = enabled
174+
}
170175
dataLoopMap["Name"] = dataLoopTmp["name"]
171176
originsMapsArray = append(originsMapsArray, dataLoopMap)
172177
}
@@ -220,9 +225,13 @@ func resourceAliCloudEsaOriginPoolRead(d *schema.ResourceData, meta interface{})
220225

221226
d.Set("enabled", objectRaw["Enabled"])
222227
d.Set("origin_pool_name", objectRaw["Name"])
223-
d.Set("origin_pool_id", objectRaw["Id"])
228+
229+
if v, ok := objectRaw["Id"]; ok {
230+
d.Set("origin_pool_id", fmt.Sprint(v))
231+
}
232+
224233
if v, ok := objectRaw["SiteId"]; ok {
225-
d.Set("site_id", v)
234+
d.Set("site_id", fmt.Sprint(v))
226235
}
227236

228237
secretKeyMap := make(map[string]interface{})
@@ -246,9 +255,11 @@ func resourceAliCloudEsaOriginPoolRead(d *schema.ResourceData, meta interface{})
246255
originsChildRaw := originsChildRaw.(map[string]interface{})
247256
originsMap["address"] = originsChildRaw["Address"]
248257
originsMap["enabled"] = originsChildRaw["Enabled"]
249-
originsMap["header"] = convertObjectToJsonString(originsChildRaw["Header"])
258+
if header, ok := originsChildRaw["Header"]; ok {
259+
originsMap["header"] = convertObjectToJsonString(header)
260+
}
250261
originsMap["name"] = originsChildRaw["Name"]
251-
originsMap["origin_id"] = originsChildRaw["Id"]
262+
originsMap["origin_id"] = fmt.Sprint(originsChildRaw["Id"])
252263
originsMap["type"] = originsChildRaw["Type"]
253264
originsMap["weight"] = originsChildRaw["Weight"]
254265

@@ -305,7 +316,9 @@ func resourceAliCloudEsaOriginPoolUpdate(d *schema.ResourceData, meta interface{
305316
dataLoopTmp := dataLoop.(map[string]interface{})
306317
dataLoopMap := make(map[string]interface{})
307318
dataLoopMap["Address"] = dataLoopTmp["address"]
308-
dataLoopMap["Header"] = parseHeader(dataLoopTmp["header"].(string))
319+
if header, ok := dataLoopTmp["header"]; ok && fmt.Sprint(header) != "" {
320+
dataLoopMap["Header"] = parseHeader(header.(string))
321+
}
309322
dataLoopMap["Type"] = dataLoopTmp["type"]
310323
if !IsNil(dataLoopTmp["auth_conf"]) {
311324
localData1 := make(map[string]interface{})
@@ -332,7 +345,9 @@ func resourceAliCloudEsaOriginPoolUpdate(d *schema.ResourceData, meta interface{
332345
dataLoopMap["AuthConf"] = localData1
333346
}
334347
dataLoopMap["Weight"] = dataLoopTmp["weight"]
335-
dataLoopMap["Enabled"] = dataLoopTmp["enabled"]
348+
if enabled, ok := dataLoopTmp["enabled"]; ok {
349+
dataLoopMap["Enabled"] = enabled
350+
}
336351
dataLoopMap["Name"] = dataLoopTmp["name"]
337352
originsMapsArray = append(originsMapsArray, dataLoopMap)
338353
}

alicloud/resource_alicloud_esa_origin_pool_test.go

Lines changed: 130 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,19 @@ import (
1010
)
1111

1212
// Test ESA OriginPool. >>> Resource test cases, automatically generated.
13-
// Case originpool_test
14-
func TestAccAliCloudESAOriginPooloriginpool_test(t *testing.T) {
13+
// Case 0
14+
func TestAccAliCloudESAOriginPool_basic0(t *testing.T) {
1515
var v map[string]interface{}
1616
resourceId := "alicloud_esa_origin_pool.default"
17-
ra := resourceAttrInit(resourceId, AliCloudESAOriginPooloriginpool_testMap)
17+
ra := resourceAttrInit(resourceId, AliCloudESAOriginPoolMap0)
1818
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
1919
return &EsaServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)}
2020
}, "DescribeEsaOriginPool")
2121
rac := resourceAttrCheckInit(rc, ra)
2222
testAccCheck := rac.resourceAttrMapUpdateSet()
2323
rand := acctest.RandIntRange(10000, 99999)
2424
name := fmt.Sprintf("bcd%d.com", rand)
25-
26-
testAccConfig := resourceTestAccConfigFunc(resourceId, name, AliCloudESAOriginPooloriginpool_testBasicDependence)
25+
testAccConfig := resourceTestAccConfigFunc(resourceId, name, AliCloudESAOriginPoolBasicDependence0)
2726
resource.Test(t, resource.TestCase{
2827
PreCheck: func() {
2928
testAccPreCheck(t)
@@ -33,44 +32,91 @@ func TestAccAliCloudESAOriginPooloriginpool_test(t *testing.T) {
3332
Providers: testAccProviders,
3433
CheckDestroy: rac.checkResourceDestroy(),
3534
Steps: []resource.TestStep{
35+
{
36+
Config: testAccConfig(map[string]interface{}{
37+
"site_id": "${data.alicloud_esa_sites.default.sites.0.id}",
38+
"origin_pool_name": name,
39+
"origins": []map[string]interface{}{
40+
{
41+
"type": "S3",
42+
"address": "test.s3.com",
43+
"auth_conf": []map[string]interface{}{
44+
{
45+
"secret_key": "bd8tjba5lXxxxxiRXFIBvoCIfJIL2WJ",
46+
"version": "v2",
47+
"region": "us-east-1",
48+
"auth_type": "private",
49+
"access_key": "LTAI5tGLgmPe1wFwpX8645BF",
50+
},
51+
},
52+
"weight": "50",
53+
"name": name,
54+
},
55+
},
56+
}),
57+
Check: resource.ComposeTestCheckFunc(
58+
testAccCheck(map[string]string{
59+
"site_id": CHECKSET,
60+
"origin_pool_name": name,
61+
"origins.#": "1",
62+
}),
63+
),
64+
},
65+
{
66+
Config: testAccConfig(map[string]interface{}{
67+
"enabled": "false",
68+
}),
69+
Check: resource.ComposeTestCheckFunc(
70+
testAccCheck(map[string]string{
71+
"enabled": "false",
72+
}),
73+
),
74+
},
75+
{
76+
Config: testAccConfig(map[string]interface{}{
77+
"enabled": "true",
78+
}),
79+
Check: resource.ComposeTestCheckFunc(
80+
testAccCheck(map[string]string{
81+
"enabled": "true",
82+
}),
83+
),
84+
},
3685
{
3786
Config: testAccConfig(map[string]interface{}{
3887
"origins": []map[string]interface{}{
39-
4088
{
4189
"type": "OSS",
42-
"address": "zhouwei123.oss-cn-beijing.aliyuncs.com",
43-
"header": "{\\\"Host\\\":[\\\"zhouwei123.oss-cn-beijing.aliyuncs.com\\\"]}",
44-
"enabled": "true",
90+
"address": "zhouwei456.oss-cn-beijing.aliyuncs.com",
91+
"header": "{\\\"Host\\\":[\\\"zhouwei456.oss-cn-beijing.aliyuncs.com\\\"]}",
92+
"enabled": "false",
4593
"auth_conf": []map[string]interface{}{
4694
{
4795
"secret_key": "bd8tjba5lXxxxxiRXFIBvoCIfJIL2WJ",
4896
"auth_type": "private_cross_account",
4997
"access_key": "LTAI5tGLgmPe1wFwpX8645BF",
5098
},
5199
},
52-
"weight": "50",
53-
"name": "origin1",
100+
"weight": "60",
101+
"name": "origin11",
54102
},
55-
56103
{
57104
"type": "S3",
58-
"address": "test.s3.com",
59-
"header": "{\\\"Host\\\":[\\\"example1.com\\\"]}",
60-
"enabled": "true",
105+
"address": "test11.s3.com",
106+
"header": "{\\\"Host\\\":[\\\"example11.com\\\"]}",
107+
"enabled": "false",
61108
"auth_conf": []map[string]interface{}{
62109
{
63110
"secret_key": "bd8tjba5lXxxxxiRXFIBvoCIfJIL2WJ",
64-
"version": "v2",
65-
"region": "us-east-1",
111+
"version": "v4",
112+
"region": "us-east-11",
66113
"auth_type": "private",
67114
"access_key": "LTAI5tGLgmPe1wFwpX8645BF",
68115
},
69116
},
70-
"weight": "50",
71-
"name": "origin2",
117+
"weight": "70",
118+
"name": "origin22",
72119
},
73-
74120
{
75121
"type": "S3",
76122
"address": "test1111.s3.com",
@@ -89,26 +135,50 @@ func TestAccAliCloudESAOriginPooloriginpool_test(t *testing.T) {
89135
"name": "origin3",
90136
},
91137
},
92-
"site_id": "${alicloud_esa_site.resource_Site_OriginPool_test.id}",
93-
"origin_pool_name": "testoriginpool",
94-
"enabled": "true",
95138
}),
96139
Check: resource.ComposeTestCheckFunc(
97-
testAccCheck(map[string]string{}),
140+
testAccCheck(map[string]string{
141+
"origins.#": "3",
142+
}),
98143
),
99144
},
100145
{
101-
Config: testAccConfig(map[string]interface{}{
102-
"enabled": "false",
103-
}),
104-
Check: resource.ComposeTestCheckFunc(
105-
testAccCheck(map[string]string{}),
106-
),
146+
ResourceName: resourceId,
147+
ImportState: true,
148+
ImportStateVerify: true,
149+
ImportStateVerifyIgnore: []string{""},
107150
},
151+
},
152+
})
153+
}
154+
155+
func TestAccAliCloudESAOriginPool_basic0_twin(t *testing.T) {
156+
var v map[string]interface{}
157+
resourceId := "alicloud_esa_origin_pool.default"
158+
ra := resourceAttrInit(resourceId, AliCloudESAOriginPoolMap0)
159+
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
160+
return &EsaServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)}
161+
}, "DescribeEsaOriginPool")
162+
rac := resourceAttrCheckInit(rc, ra)
163+
testAccCheck := rac.resourceAttrMapUpdateSet()
164+
rand := acctest.RandIntRange(10000, 99999)
165+
name := fmt.Sprintf("bcd%d.com", rand)
166+
testAccConfig := resourceTestAccConfigFunc(resourceId, name, AliCloudESAOriginPoolBasicDependence0)
167+
resource.Test(t, resource.TestCase{
168+
PreCheck: func() {
169+
testAccPreCheck(t)
170+
testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"})
171+
},
172+
IDRefreshName: resourceId,
173+
Providers: testAccProviders,
174+
CheckDestroy: rac.checkResourceDestroy(),
175+
Steps: []resource.TestStep{
108176
{
109177
Config: testAccConfig(map[string]interface{}{
178+
"site_id": "${data.alicloud_esa_sites.default.sites.0.id}",
179+
"origin_pool_name": name,
180+
"enabled": "true",
110181
"origins": []map[string]interface{}{
111-
112182
{
113183
"type": "OSS",
114184
"address": "zhouwei456.oss-cn-beijing.aliyuncs.com",
@@ -124,16 +194,6 @@ func TestAccAliCloudESAOriginPooloriginpool_test(t *testing.T) {
124194
"weight": "60",
125195
"name": "origin11",
126196
},
127-
},
128-
}),
129-
Check: resource.ComposeTestCheckFunc(
130-
testAccCheck(map[string]string{}),
131-
),
132-
},
133-
{
134-
Config: testAccConfig(map[string]interface{}{
135-
"origins": []map[string]interface{}{
136-
137197
{
138198
"type": "S3",
139199
"address": "test11.s3.com",
@@ -151,50 +211,58 @@ func TestAccAliCloudESAOriginPooloriginpool_test(t *testing.T) {
151211
"weight": "70",
152212
"name": "origin22",
153213
},
214+
{
215+
"type": "S3",
216+
"address": "test1111.s3.com",
217+
"header": "{\\\"Host\\\":[\\\"example1111.com\\\"]}",
218+
"enabled": "true",
219+
"auth_conf": []map[string]interface{}{
220+
{
221+
"secret_key": "bd8tjba5lXxxxxiRXFIBvoCIfJIL2WJ",
222+
"version": "v2",
223+
"region": "us-east-1",
224+
"auth_type": "private",
225+
"access_key": "LTAI5tGLgmPe1wFwpX8645BF",
226+
},
227+
},
228+
"weight": "30",
229+
"name": "origin3",
230+
},
154231
},
155232
}),
156233
Check: resource.ComposeTestCheckFunc(
157-
testAccCheck(map[string]string{}),
158-
),
159-
},
160-
{
161-
Config: testAccConfig(map[string]interface{}{}),
162-
Check: resource.ComposeTestCheckFunc(
163-
testAccCheck(map[string]string{}),
234+
testAccCheck(map[string]string{
235+
"site_id": CHECKSET,
236+
"origin_pool_name": name,
237+
"enabled": "true",
238+
"origins.#": "3",
239+
}),
164240
),
165241
},
166242
{
167243
ResourceName: resourceId,
168244
ImportState: true,
169245
ImportStateVerify: true,
170-
ImportStateVerifyIgnore: []string{"origins"},
246+
ImportStateVerifyIgnore: []string{""},
171247
},
172248
},
173249
})
174250
}
175251

176-
var AliCloudESAOriginPooloriginpool_testMap = map[string]string{
177-
"id": CHECKSET,
252+
var AliCloudESAOriginPoolMap0 = map[string]string{
253+
"enabled": CHECKSET,
254+
"origin_pool_id": CHECKSET,
178255
}
179256

180-
func AliCloudESAOriginPooloriginpool_testBasicDependence(name string) string {
257+
func AliCloudESAOriginPoolBasicDependence0(name string) string {
181258
return fmt.Sprintf(`
182259
variable "name" {
183-
default = "%s"
260+
default = "%s"
184261
}
185262
186-
187263
data "alicloud_esa_sites" "default" {
188264
plan_subscribe_type = "enterpriseplan"
189265
}
190-
191-
resource "alicloud_esa_site" "resource_Site_OriginPool_test" {
192-
site_name = var.name
193-
instance_id = data.alicloud_esa_sites.default.sites.0.instance_id
194-
coverage = "overseas"
195-
access_type = "NS"
196-
}
197-
198266
`, name)
199267
}
200268

0 commit comments

Comments
 (0)