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

Commit 51f5aa5

Browse files
linsunDavid Chung
authored andcommitted
adding SL support for terraform instance plugin (#321)
Signed-off-by: Lin Sun <linsun@us.ibm.com>
1 parent 448073d commit 51f5aa5

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

pkg/example/instance/terraform/plugin.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (p *plugin) terraformApply() error {
189189
}
190190

191191
func (p *plugin) doTerraformApply() error {
192-
log.Infoln("Applying plan")
192+
log.Infoln(time.Now().Format(time.RFC850) + " Applying plan")
193193
cmd := exec.Command("terraform", "apply")
194194
cmd.Dir = p.Dir
195195
stdout, err := cmd.StdoutPipe()
@@ -352,6 +352,7 @@ func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
352352
spec.Tags["Name"] = string(id)
353353
}
354354
}
355+
355356
switch properties.Type {
356357
case "aws_instance", "azurerm_virtual_machine", "digitalocean_droplet", "google_compute_instance":
357358
if t, exists := properties.Value["tags"]; !exists {
@@ -362,6 +363,17 @@ func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
362363
mm[tt] = vv
363364
}
364365
}
366+
case "softlayer_virtual_guest":
367+
log.Debugln("softlayer_virtual_guest detected, adding hostname to properties: hostname=", name)
368+
properties.Value["hostname"] = name
369+
var tags []interface{}
370+
371+
//softlayer uses a list of tags, instead of a map of tags
372+
for i, v := range spec.Tags {
373+
log.Debugln("softlayer_virtual_guest detected, append system tag v=", v)
374+
tags = append(tags, i+":"+v)
375+
}
376+
properties.Value["tags"] = tags
365377
}
366378

367379
// Use tag to store the logical id
@@ -375,6 +387,8 @@ func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
375387
switch properties.Type {
376388
case "aws_instance", "digitalocean_droplet":
377389
addUserData(properties.Value, "user_data", spec.Init)
390+
case "softlayer_virtual_guest":
391+
addUserData(properties.Value, "user_metadata", spec.Init)
378392
case "azurerm_virtual_machine":
379393
// os_profile.custom_data
380394
if m, has := properties.Value["os_profile"]; !has {
@@ -414,7 +428,7 @@ func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
414428
// Destroy terminates an existing instance.
415429
func (p *plugin) Destroy(instance instance.ID) error {
416430
fp := filepath.Join(p.Dir, string(instance)+".tf.json")
417-
log.Debugln("destroy", fp)
431+
log.Debugln("destroy instance", fp)
418432
err := p.fs.Remove(fp)
419433
if err != nil {
420434
return err
@@ -445,6 +459,7 @@ scan:
445459
ID: instance.ID(id),
446460
LogicalID: terraformLogicalID(v),
447461
}
462+
448463
if len(tags) == 0 {
449464
result = append(result, inst)
450465
} else {
@@ -457,12 +472,16 @@ scan:
457472
}
458473
}
459474
}
475+
log.Debugln("describe-instances result=", result)
476+
460477
return result, nil
461478
}
462479

463480
func terraformTags(v interface{}, key string) map[string]string {
481+
log.Debugln("terraformTags", v)
464482
m, ok := v.(map[string]interface{})
465483
if !ok {
484+
log.Debugln("terraformTags: return nil")
466485
return nil
467486
}
468487
tags := map[string]string{}
@@ -471,7 +490,28 @@ func terraformTags(v interface{}, key string) map[string]string {
471490
tags[k] = fmt.Sprintf("%v", v)
472491
}
473492
return tags
493+
} else if mm, ok := m[key].([]interface{}); ok {
494+
// add each tag in the list to the tags map
495+
for _, v := range mm {
496+
value := fmt.Sprintf("%v", v)
497+
if strings.Contains(value, ":") {
498+
log.Debugln("terraformTags system tags detected v=", v)
499+
vv := strings.Split(value, ":")
500+
if len(vv) == 2 {
501+
tags[vv[0]] = vv[1]
502+
} else {
503+
log.Errorln("terraformTags: ignore invalid tag detected", value)
504+
}
505+
} else {
506+
log.Warnln("terraformTags user tags ignored v=", value)
507+
}
508+
}
509+
log.Debugln("terraformTags return tags", tags)
510+
return tags
511+
} else {
512+
log.Errorln("terraformTags: invalid terraformTags tags value", m[key])
474513
}
514+
475515
for k, v := range m {
476516
if k != "tags.%" && strings.Index(k, "tags.") == 0 {
477517
n := k[len("tags."):]

0 commit comments

Comments
 (0)