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

Commit a390bf2

Browse files
JacobFrericksDavid Chung
authored andcommitted
Adding configurable hostnames to the SoftLayer Terraform plugin. (#433)
Signed-off-by: Jacob Frericks <[email protected]>
1 parent 210a5d1 commit a390bf2

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

examples/instance/terraform/plugin.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,23 @@ func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
359359
}
360360
}
361361

362+
// Use the given hostname value as a prefix if it is a non-empty string
363+
if hostnamePrefix, is := properties.Value["@hostname_prefix"].(string); is {
364+
hostnamePrefix = strings.Trim(hostnamePrefix, " ")
365+
// Use the default behavior if hostnamePrefix was either not a string, or an empty string
366+
if hostnamePrefix == "" {
367+
properties.Value["hostname"] = name
368+
} else {
369+
// Remove "instance-" from "instance-XXXX", then append that string to the hostnamePrefix to create the new hostname
370+
properties.Value["hostname"] = fmt.Sprintf("%s-%s", hostnamePrefix, strings.Replace(name, "instance-", "", -1))
371+
}
372+
} else {
373+
properties.Value["hostname"] = name
374+
}
375+
// Delete hostnamePrefix so it will not be written in the *.tf.json file
376+
delete(properties.Value, "@hostname_prefix")
377+
log.Debugln("Adding hostname to properties: hostname=", properties.Value["hostname"])
378+
362379
switch properties.Type {
363380
case "aws_instance", "azurerm_virtual_machine", "digitalocean_droplet", "google_compute_instance":
364381
if t, exists := properties.Value["tags"]; !exists {
@@ -370,9 +387,6 @@ func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
370387
}
371388
}
372389
case "softlayer_virtual_guest":
373-
log.Debugln("softlayer_virtual_guest detected, adding hostname to properties: hostname=", name)
374-
properties.Value["hostname"] = name
375-
376390
if _, has := properties.Value["tags"]; !has {
377391
properties.Value["tags"] = []interface{}{}
378392
}

examples/instance/terraform/plugin_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"io/ioutil"
56
"os"
67
"path/filepath"
78
"sort"
9+
"strings"
810
"testing"
911

1012
"github.com/docker/infrakit/pkg/spi/instance"
@@ -13,10 +15,12 @@ import (
1315
)
1416

1517
func TestUsage(t *testing.T) {
18+
// Test a softlayer_virtual_guest with an @hostname_prefix
1619
run(t, "softlayer_virtual_guest", `
1720
{
1821
"type": "softlayer_virtual_guest",
1922
"value": {
23+
"@hostname_prefix": "softlayer-hostname",
2024
"cores": 2,
2125
"memory": 2048,
2226
"tags": [
@@ -39,6 +43,61 @@ func TestUsage(t *testing.T) {
3943
}
4044
`)
4145

46+
// Test a softlayer_virtual_guest without an @hostname_prefix
47+
run(t, "softlayer_virtual_guest", `
48+
{
49+
"type": "softlayer_virtual_guest",
50+
"value": {
51+
"cores": 2,
52+
"memory": 2048,
53+
"tags": [
54+
"terraform_demo_swarm_mgr_sl"
55+
],
56+
"connection": {
57+
"user": "root",
58+
"private_key": "${file(\"~/.ssh/id_rsa_de\")}"
59+
},
60+
"hourly_billing": true,
61+
"local_disk": true,
62+
"network_speed": 100,
63+
"datacenter": "dal10",
64+
"os_reference_code": "UBUNTU_14_64",
65+
"domain": "softlayer.com",
66+
"ssh_key_ids": [
67+
"${data.softlayer_ssh_key.public_key.id}"
68+
]
69+
}
70+
}
71+
`)
72+
73+
// Test a softlayer_virtual_guest with an empty @hostname_prefix
74+
run(t, "softlayer_virtual_guest", `
75+
{
76+
"type": "softlayer_virtual_guest",
77+
"value": {
78+
"@hostname_prefix": " ",
79+
"cores": 2,
80+
"memory": 2048,
81+
"tags": [
82+
"terraform_demo_swarm_mgr_sl"
83+
],
84+
"connection": {
85+
"user": "root",
86+
"private_key": "${file(\"~/.ssh/id_rsa_de\")}"
87+
},
88+
"hourly_billing": true,
89+
"local_disk": true,
90+
"network_speed": 100,
91+
"datacenter": "dal10",
92+
"os_reference_code": "UBUNTU_14_64",
93+
"domain": "softlayer.com",
94+
"ssh_key_ids": [
95+
"${data.softlayer_ssh_key.public_key.id}"
96+
]
97+
}
98+
}
99+
`)
100+
42101
run(t, "aws_instance", `
43102
{
44103
"type" : "aws_instance",
@@ -113,6 +172,24 @@ func run(t *testing.T, resourceType, properties string) {
113172

114173
props := parsed.Resource[resourceType][string(*id)]
115174

175+
// Unmarshal json for easy access
176+
var testingData interface{}
177+
json.Unmarshal([]byte(properties), &testingData)
178+
m := testingData.(map[string]interface{})
179+
value, _ := m["value"].(map[string]interface{})
180+
181+
// If a hostname was specified, the expectation is that the hostname is appended with the timestamp from the ID
182+
if value["@hostname_prefix"] != nil && strings.Trim(value["@hostname_prefix"].(string), " ") != "" {
183+
newID := strings.Replace(string(*id), "instance-", "", -1)
184+
expectedHostname := "softlayer-hostname-" + newID
185+
require.Equal(t, expectedHostname, props["hostname"])
186+
} else {
187+
// If no hostname was specified, the hostname should equal the ID
188+
require.Equal(t, string(*id), props["hostname"])
189+
}
190+
// Verify the hostname prefix key/value is no longer in the props
191+
require.Nil(t, props["@hostname_prefix"])
192+
116193
switch resourceType {
117194
case "softlayer_virtual_guest":
118195
require.Equal(t, conv([]interface{}{

0 commit comments

Comments
 (0)