Skip to content

Commit 9e50bd9

Browse files
Merge pull request #143 from zamrih/fix/re-create-missing-target
Fix re-creating a missing target on an upstream having multiple ones
2 parents f64accf + 4bb6c84 commit 9e50bd9

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

kong/resource_kong_target.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ func resourceKongTargetRead(ctx context.Context, d *schema.ResourceData, meta in
8080
return diag.FromErr(fmt.Errorf("could not find kong target: %v", err))
8181
}
8282

83-
if targets == nil {
84-
d.SetId("")
85-
} else {
83+
var recreate = true
84+
85+
if targets != nil {
8686
for _, element := range targets {
8787
if *element.ID == ids[1] {
88+
recreate = false
89+
8890
err := d.Set("target", element.Target)
8991
if err != nil {
9092
return diag.FromErr(err)
@@ -105,6 +107,10 @@ func resourceKongTargetRead(ctx context.Context, d *schema.ResourceData, meta in
105107
}
106108
}
107109

110+
if recreate {
111+
d.SetId("")
112+
}
113+
108114
return diags
109115
}
110116

kong/resource_kong_target_test.go

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
`
295330
const testUpdateTargetConfig = `
296331
resource "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
`
308350
const testDeleteTargetConfig = `
309351
resource "kong_upstream" "upstream" {

0 commit comments

Comments
 (0)