@@ -26,6 +26,11 @@ func TestAccKongTarget(t *testing.T) {
26
26
resource .TestCheckResourceAttr ("kong_target.target" , "tags.#" , "2" ),
27
27
resource .TestCheckResourceAttr ("kong_target.target" , "tags.0" , "a" ),
28
28
resource .TestCheckResourceAttr ("kong_target.target" , "tags.1" , "b" ),
29
+ testAccCheckKongTargetExists ("kong_target.fallback_target" ),
30
+ resource .TestCheckResourceAttr ("kong_target.fallback_target" , "target" , "myfallbacktarget:4000" ),
31
+ resource .TestCheckResourceAttr ("kong_target.fallback_target" , "weight" , "50" ),
32
+ resource .TestCheckResourceAttr ("kong_target.fallback_target" , "tags.#" , "1" ),
33
+ resource .TestCheckResourceAttr ("kong_target.fallback_target" , "tags.0" , "c" ),
29
34
),
30
35
},
31
36
{
@@ -36,6 +41,11 @@ func TestAccKongTarget(t *testing.T) {
36
41
resource .TestCheckResourceAttr ("kong_target.target" , "weight" , "200" ),
37
42
resource .TestCheckResourceAttr ("kong_target.target" , "tags.#" , "1" ),
38
43
resource .TestCheckResourceAttr ("kong_target.target" , "tags.0" , "a" ),
44
+ testAccCheckKongTargetExists ("kong_target.fallback_target" ),
45
+ resource .TestCheckResourceAttr ("kong_target.fallback_target" , "target" , "myfallbacktarget:4000" ),
46
+ resource .TestCheckResourceAttr ("kong_target.fallback_target" , "weight" , "150" ),
47
+ resource .TestCheckResourceAttr ("kong_target.fallback_target" , "tags.#" , "1" ),
48
+ resource .TestCheckResourceAttr ("kong_target.fallback_target" , "tags.0" , "d" ),
39
49
),
40
50
},
41
51
},
@@ -54,12 +64,16 @@ func TestAccKongTargetDelete(t *testing.T) {
54
64
testAccCheckKongTargetExists ("kong_target.target" ),
55
65
resource .TestCheckResourceAttr ("kong_target.target" , "target" , "mytarget:4000" ),
56
66
resource .TestCheckResourceAttr ("kong_target.target" , "weight" , "100" ),
67
+ testAccCheckKongTargetExists ("kong_target.fallback_target" ),
68
+ resource .TestCheckResourceAttr ("kong_target.fallback_target" , "target" , "myfallbacktarget:4000" ),
69
+ resource .TestCheckResourceAttr ("kong_target.fallback_target" , "weight" , "50" ),
57
70
),
58
71
},
59
72
{
60
73
Config : testDeleteTargetConfig ,
61
74
Check : resource .ComposeTestCheckFunc (
62
75
testAccCheckKongTargetDoesNotExist ("kong_target.target" , "kong_upstream.upstream" ),
76
+ testAccCheckKongTargetDoesNotExist ("kong_target.fallback_target" , "kong_upstream.upstream" ),
63
77
),
64
78
},
65
79
},
@@ -78,6 +92,9 @@ func TestAccKongTargetCreateAndRefreshFromNonExistentUpstream(t *testing.T) {
78
92
testAccCheckKongTargetExists ("kong_target.target" ),
79
93
resource .TestCheckResourceAttr ("kong_target.target" , "target" , "mytarget:4000" ),
80
94
resource .TestCheckResourceAttr ("kong_target.target" , "weight" , "100" ),
95
+ testAccCheckKongTargetExists ("kong_target.fallback_target" ),
96
+ resource .TestCheckResourceAttr ("kong_target.fallback_target" , "target" , "myfallbacktarget:4000" ),
97
+ resource .TestCheckResourceAttr ("kong_target.fallback_target" , "weight" , "50" ),
81
98
deleteUpstream ("kong_upstream.upstream" ),
82
99
),
83
100
ExpectNonEmptyPlan : true ,
@@ -133,7 +150,7 @@ func testAccCheckKongTargetDestroy(state *terraform.State) error {
133
150
134
151
targets := getResourcesByType ("kong_target" , state )
135
152
136
- if len (targets ) > 1 {
153
+ if len (targets ) > 2 {
137
154
return fmt .Errorf ("expecting max 1 target resource found %v" , len (targets ))
138
155
}
139
156
@@ -179,11 +196,16 @@ func testAccCheckKongTargetExists(resourceKey string) resource.TestCheckFunc {
179
196
return fmt .Errorf ("target with id %v not found" , rs .Primary .ID )
180
197
}
181
198
199
+ var targetFound = false
200
+
182
201
for _ , element := range api {
183
202
if * element .ID == ids [1 ] {
203
+ targetFound = true
184
204
break
185
205
}
206
+ }
186
207
208
+ if ! targetFound {
187
209
return fmt .Errorf ("target with id %v not found" , rs .Primary .ID )
188
210
}
189
211
@@ -212,8 +234,10 @@ func testAccCheckKongTargetDoesNotExist(targetResourceKey string, upstreamResour
212
234
client := testAccProvider .Meta ().(* config ).adminClient .Targets
213
235
targets , _ , err := client .List (context .Background (), kong .String (rs .Primary .ID ), nil )
214
236
215
- if len (targets ) > 0 {
216
- return fmt .Errorf ("expecting zero target resources found %v" , len (targets ))
237
+ resourceTargets := getResourcesByType ("kong_target" , s )
238
+
239
+ if len (targets ) > len (resourceTargets ) {
240
+ return fmt .Errorf ("expecting %v target resources found %v" , len (resourceTargets ), len (targets ))
217
241
}
218
242
219
243
if err != nil {
@@ -249,16 +273,20 @@ func testAccDeleteExistingKongTarget(resourceKey string) resource.TestCheckFunc
249
273
return fmt .Errorf ("target with id %v not found" , rs .Primary .ID )
250
274
}
251
275
252
- targetID := ids [1 ]
276
+ var targetFound = false
277
+
253
278
for _ , element := range api {
254
279
if * element .ID == ids [1 ] {
280
+ targetFound = true
255
281
break
256
282
}
283
+ }
257
284
285
+ if ! targetFound {
258
286
return fmt .Errorf ("target with id %v not found" , rs .Primary .ID )
259
287
}
260
288
261
- return client .Delete (context .Background (), upstreamID , kong .String (targetID ))
289
+ return client .Delete (context .Background (), upstreamID , kong .String (ids [ 1 ] ))
262
290
}
263
291
}
264
292
@@ -291,6 +319,13 @@ resource "kong_target" "target" {
291
319
upstream_id = "${kong_upstream.upstream.id}"
292
320
tags = ["a", "b"]
293
321
}
322
+
323
+ resource "kong_target" "fallback_target" {
324
+ target = "myfallbacktarget:4000"
325
+ weight = 50
326
+ upstream_id = "${kong_upstream.upstream.id}"
327
+ tags = ["c"]
328
+ }
294
329
`
295
330
const testUpdateTargetConfig = `
296
331
resource "kong_upstream" "upstream" {
@@ -304,6 +339,13 @@ resource "kong_target" "target" {
304
339
upstream_id = "${kong_upstream.upstream.id}"
305
340
tags = ["a"]
306
341
}
342
+
343
+ resource "kong_target" "fallback_target" {
344
+ target = "myfallbacktarget:4000"
345
+ weight = 150
346
+ upstream_id = "${kong_upstream.upstream.id}"
347
+ tags = ["d"]
348
+ }
307
349
`
308
350
const testDeleteTargetConfig = `
309
351
resource "kong_upstream" "upstream" {
0 commit comments