@@ -26,6 +26,11 @@ func TestAccKongTarget(t *testing.T) {
2626 resource .TestCheckResourceAttr ("kong_target.target" , "tags.#" , "2" ),
2727 resource .TestCheckResourceAttr ("kong_target.target" , "tags.0" , "a" ),
2828 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" ),
2934 ),
3035 },
3136 {
@@ -36,6 +41,11 @@ func TestAccKongTarget(t *testing.T) {
3641 resource .TestCheckResourceAttr ("kong_target.target" , "weight" , "200" ),
3742 resource .TestCheckResourceAttr ("kong_target.target" , "tags.#" , "1" ),
3843 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" ),
3949 ),
4050 },
4151 },
@@ -54,12 +64,16 @@ func TestAccKongTargetDelete(t *testing.T) {
5464 testAccCheckKongTargetExists ("kong_target.target" ),
5565 resource .TestCheckResourceAttr ("kong_target.target" , "target" , "mytarget:4000" ),
5666 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" ),
5770 ),
5871 },
5972 {
6073 Config : testDeleteTargetConfig ,
6174 Check : resource .ComposeTestCheckFunc (
6275 testAccCheckKongTargetDoesNotExist ("kong_target.target" , "kong_upstream.upstream" ),
76+ testAccCheckKongTargetDoesNotExist ("kong_target.fallback_target" , "kong_upstream.upstream" ),
6377 ),
6478 },
6579 },
@@ -78,6 +92,9 @@ func TestAccKongTargetCreateAndRefreshFromNonExistentUpstream(t *testing.T) {
7892 testAccCheckKongTargetExists ("kong_target.target" ),
7993 resource .TestCheckResourceAttr ("kong_target.target" , "target" , "mytarget:4000" ),
8094 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" ),
8198 deleteUpstream ("kong_upstream.upstream" ),
8299 ),
83100 ExpectNonEmptyPlan : true ,
@@ -133,7 +150,7 @@ func testAccCheckKongTargetDestroy(state *terraform.State) error {
133150
134151 targets := getResourcesByType ("kong_target" , state )
135152
136- if len (targets ) > 1 {
153+ if len (targets ) > 2 {
137154 return fmt .Errorf ("expecting max 1 target resource found %v" , len (targets ))
138155 }
139156
@@ -179,11 +196,16 @@ func testAccCheckKongTargetExists(resourceKey string) resource.TestCheckFunc {
179196 return fmt .Errorf ("target with id %v not found" , rs .Primary .ID )
180197 }
181198
199+ var targetFound = false
200+
182201 for _ , element := range api {
183202 if * element .ID == ids [1 ] {
203+ targetFound = true
184204 break
185205 }
206+ }
186207
208+ if ! targetFound {
187209 return fmt .Errorf ("target with id %v not found" , rs .Primary .ID )
188210 }
189211
@@ -212,8 +234,10 @@ func testAccCheckKongTargetDoesNotExist(targetResourceKey string, upstreamResour
212234 client := testAccProvider .Meta ().(* config ).adminClient .Targets
213235 targets , _ , err := client .List (context .Background (), kong .String (rs .Primary .ID ), nil )
214236
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 ))
217241 }
218242
219243 if err != nil {
@@ -249,16 +273,20 @@ func testAccDeleteExistingKongTarget(resourceKey string) resource.TestCheckFunc
249273 return fmt .Errorf ("target with id %v not found" , rs .Primary .ID )
250274 }
251275
252- targetID := ids [1 ]
276+ var targetFound = false
277+
253278 for _ , element := range api {
254279 if * element .ID == ids [1 ] {
280+ targetFound = true
255281 break
256282 }
283+ }
257284
285+ if ! targetFound {
258286 return fmt .Errorf ("target with id %v not found" , rs .Primary .ID )
259287 }
260288
261- return client .Delete (context .Background (), upstreamID , kong .String (targetID ))
289+ return client .Delete (context .Background (), upstreamID , kong .String (ids [ 1 ] ))
262290 }
263291}
264292
@@ -291,6 +319,13 @@ resource "kong_target" "target" {
291319 upstream_id = "${kong_upstream.upstream.id}"
292320 tags = ["a", "b"]
293321}
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+ }
294329`
295330const testUpdateTargetConfig = `
296331resource "kong_upstream" "upstream" {
@@ -304,6 +339,13 @@ resource "kong_target" "target" {
304339 upstream_id = "${kong_upstream.upstream.id}"
305340 tags = ["a"]
306341}
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+ }
307349`
308350const testDeleteTargetConfig = `
309351resource "kong_upstream" "upstream" {
0 commit comments