Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 436005a

Browse files
kaufersDavid Chung
authored andcommitted
Terraform plugin fails to describe instance by tag (#495)
Signed-off-by: Steven Kaufer <[email protected]>
1 parent ce06e23 commit 436005a

File tree

2 files changed

+102
-46
lines changed

2 files changed

+102
-46
lines changed

examples/instance/terraform/plugin.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,8 @@ func (p *plugin) DescribeInstances(tags map[string]string, properties bool) ([]i
538538
re := regexp.MustCompile("(.*)(instance-[0-9]+)")
539539
result := []instance.Description{}
540540
// now we scan for <instance_type.instance-<timestamp> as keys
541-
scan:
542541
for t, vm := range localSpecs {
543-
542+
scan:
544543
for k, v := range vm {
545544
matches := re.FindStringSubmatch(string(k))
546545
if len(matches) == 3 {

examples/instance/terraform/plugin_test.go

Lines changed: 101 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,24 @@ func run(t *testing.T, resourceType, properties string) {
167167
err = terraform.Validate(config)
168168
require.NoError(t, err)
169169

170-
instanceSpec := instance.Spec{
170+
// Instance with tags that will not be updated
171+
instanceSpec1 := instance.Spec{
172+
Properties: config,
173+
Tags: map[string]string{
174+
"label1": "value1",
175+
"label2": "value2",
176+
},
177+
Init: "",
178+
Attachments: []instance.Attachment{},
179+
}
180+
id1, err := terraform.Provision(instanceSpec1)
181+
require.NoError(t, err)
182+
tfPath := filepath.Join(dir, string(*id1)+".tf.json")
183+
_, err = ioutil.ReadFile(tfPath)
184+
require.NoError(t, err)
185+
186+
// Instance with tags that will be updated
187+
instanceSpec2 := instance.Spec{
171188
Properties: config,
172189
Tags: map[string]string{
173190
"label1": "value1",
@@ -181,11 +198,11 @@ func run(t *testing.T, resourceType, properties string) {
181198
},
182199
},
183200
}
184-
185-
id, err := terraform.Provision(instanceSpec)
201+
id2, err := terraform.Provision(instanceSpec2)
186202
require.NoError(t, err)
203+
require.NotEqual(t, id1, id2)
187204

188-
tfPath := filepath.Join(dir, string(*id)+".tf.json")
205+
tfPath = filepath.Join(dir, string(*id2)+".tf.json")
189206
buff, err := ioutil.ReadFile(tfPath)
190207
require.NoError(t, err)
191208

@@ -213,18 +230,18 @@ func run(t *testing.T, resourceType, properties string) {
213230
"terraform_demo_swarm_mgr_sl",
214231
"label1:value1",
215232
"label2:value2",
216-
"name:" + string(*id),
233+
"name:" + string(*id2),
217234
}), conv(props["tags"].([]interface{})))
218-
require.Equal(t, instanceSpec.Init, props["user_metadata"])
235+
require.Equal(t, instanceSpec2.Init, props["user_metadata"])
219236

220237
// If a hostname was specified, the expectation is that the hostname is appended with the timestamp from the ID
221238
if value["@hostname_prefix"] != nil && strings.Trim(value["@hostname_prefix"].(string), " ") != "" {
222-
newID := strings.Replace(string(*id), "instance-", "", -1)
239+
newID := strings.Replace(string(*id2), "instance-", "", -1)
223240
expectedHostname := "softlayer-hostname-" + newID
224241
require.Equal(t, expectedHostname, props["hostname"])
225242
} else {
226243
// If no hostname was specified, the hostname should equal the ID
227-
require.Equal(t, string(*id), props["hostname"])
244+
require.Equal(t, string(*id2), props["hostname"])
228245
}
229246
// Verify the hostname prefix key/value is no longer in the props
230247
require.Nil(t, props["@hostname_prefix"])
@@ -234,16 +251,67 @@ func run(t *testing.T, resourceType, properties string) {
234251
"InstancePlugin": "terraform",
235252
"label1": "value1",
236253
"label2": "value2",
237-
"Name": string(*id),
254+
"Name": string(*id2),
238255
}, props["tags"])
239-
require.Equal(t, base64.StdEncoding.EncodeToString([]byte(instanceSpec.Init)), props["user_data"])
256+
require.Equal(t, base64.StdEncoding.EncodeToString([]byte(instanceSpec2.Init)), props["user_data"])
240257
}
241258

242-
// label resources
243-
err = terraform.Label(*id, map[string]string{
259+
// Expected instances returned from Describe
260+
var inst1 instance.Description
261+
var inst2 instance.Description
262+
switch vmType {
263+
case VMSoftLayer:
264+
inst1 = instance.Description{
265+
ID: *id1,
266+
Tags: map[string]string{
267+
"terraform_demo_swarm_mgr_sl": "",
268+
"label1": "value1",
269+
"label2": "value2",
270+
"name": string(*id1),
271+
},
272+
}
273+
inst2 = instance.Description{
274+
ID: *id2,
275+
Tags: map[string]string{
276+
"terraform_demo_swarm_mgr_sl": "",
277+
"label1": "value1",
278+
"label2": "value2",
279+
"name": string(*id2),
280+
},
281+
}
282+
case VMAmazon:
283+
inst1 = instance.Description{
284+
ID: *id1,
285+
Tags: map[string]string{
286+
"InstancePlugin": "terraform",
287+
"label1": "value1",
288+
"label2": "value2",
289+
"Name": string(*id1),
290+
},
291+
}
292+
inst2 = instance.Description{
293+
ID: *id2,
294+
Tags: map[string]string{
295+
"InstancePlugin": "terraform",
296+
"label1": "value1",
297+
"label2": "value2",
298+
"Name": string(*id2),
299+
},
300+
}
301+
}
302+
303+
// Both instances match: label=value1
304+
list, err := terraform.DescribeInstances(map[string]string{"label1": "value1"}, false)
305+
require.NoError(t, err)
306+
require.Contains(t, list, inst1)
307+
require.Contains(t, list, inst2)
308+
309+
// re-label instance2
310+
err = terraform.Label(*id2, map[string]string{
244311
"label1": "changed1",
245312
"label3": "value3",
246313
})
314+
require.NoError(t, err)
247315

248316
buff, err = ioutil.ReadFile(tfPath)
249317
require.NoError(t, err)
@@ -255,58 +323,47 @@ func run(t *testing.T, resourceType, properties string) {
255323
require.NoError(t, err)
256324

257325
vmType, _, props, err = FindVM(&parsed)
326+
require.NoError(t, err)
258327
switch vmType {
259328
case VMSoftLayer:
260329
require.Equal(t, conv([]interface{}{
261330
"terraform_demo_swarm_mgr_sl",
262331
"label1:changed1",
263332
"label2:value2",
264333
"label3:value3",
265-
"name:" + string(*id),
334+
"name:" + string(*id2),
266335
}), conv(props["tags"].([]interface{})))
267336
case VMAmazon:
268337
require.Equal(t, map[string]interface{}{
269338
"InstancePlugin": "terraform",
270339
"label1": "changed1",
271340
"label2": "value2",
272341
"label3": "value3",
273-
"Name": string(*id),
342+
"Name": string(*id2),
274343
}, props["tags"])
275344
}
276345

277-
list, err := terraform.DescribeInstances(map[string]string{"label1": "changed1"}, false)
346+
// Update expected tags on inst2
347+
inst2.Tags["label1"] = "changed1"
348+
inst2.Tags["label3"] = "value3"
349+
350+
// Only a single match: label1=changed1
351+
list, err = terraform.DescribeInstances(map[string]string{"label1": "changed1"}, false)
278352
require.NoError(t, err)
353+
require.Equal(t, []instance.Description{inst2}, list)
279354

280-
switch vmType {
281-
case VMSoftLayer:
282-
require.Equal(t, []instance.Description{
283-
{
284-
ID: *id,
285-
Tags: map[string]string{
286-
"terraform_demo_swarm_mgr_sl": "",
287-
"label1": "changed1",
288-
"label2": "value2",
289-
"label3": "value3",
290-
"name": string(*id),
291-
},
292-
},
293-
}, list)
294-
case VMAmazon:
295-
require.Equal(t, []instance.Description{
296-
{
297-
ID: *id,
298-
Tags: map[string]string{
299-
"InstancePlugin": "terraform",
300-
"label1": "changed1",
301-
"label2": "value2",
302-
"label3": "value3",
303-
"Name": string(*id),
304-
},
305-
},
306-
}, list)
307-
}
355+
// Only a single match: label1=value1
356+
list, err = terraform.DescribeInstances(map[string]string{"label1": "value1"}, false)
357+
require.NoError(t, err)
358+
require.Equal(t, []instance.Description{inst1}, list)
359+
360+
// No matches: label1=foo
361+
list, err = terraform.DescribeInstances(map[string]string{"label1": "foo"}, false)
362+
require.NoError(t, err)
363+
require.Equal(t, []instance.Description{}, list)
308364

309-
err = terraform.Destroy(*id)
365+
// Destroy, then none should match
366+
err = terraform.Destroy(*id2)
310367
require.NoError(t, err)
311368

312369
list, err = terraform.DescribeInstances(map[string]string{"label1": "changed1"}, false)

0 commit comments

Comments
 (0)