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

Commit a8cf57d

Browse files
author
David Chung
authored
restrict hostname checks to softlayer only (#453)
Signed-off-by: David Chung <[email protected]>
1 parent 022ed70 commit a8cf57d

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

examples/instance/terraform/plugin.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,34 @@ func ensureUniqueFile(dir string) string {
332332
return n
333333
}
334334

335+
// Special processing of hostname on some platforms. Where supported, you can
336+
// add a special @hostname_prefix that will allow the setting of hostname in given format
337+
// TODO - expand this to formatting string
338+
func (p *plugin) optionalProcessHostname(properties *SpecPropertiesFormat, name string) {
339+
switch properties.Type {
340+
case "softlayer_virtual_guest": // # list the platforms here
341+
default:
342+
return
343+
}
344+
345+
// Use the given hostname value as a prefix if it is a non-empty string
346+
if hostnamePrefix, is := properties.Value["@hostname_prefix"].(string); is {
347+
hostnamePrefix = strings.Trim(hostnamePrefix, " ")
348+
// Use the default behavior if hostnamePrefix was either not a string, or an empty string
349+
if hostnamePrefix == "" {
350+
properties.Value["hostname"] = name
351+
} else {
352+
// Remove "instance-" from "instance-XXXX", then append that string to the hostnamePrefix to create the new hostname
353+
properties.Value["hostname"] = fmt.Sprintf("%s-%s", hostnamePrefix, strings.Replace(name, "instance-", "", -1))
354+
}
355+
} else {
356+
properties.Value["hostname"] = name
357+
}
358+
// Delete hostnamePrefix so it will not be written in the *.tf.json file
359+
delete(properties.Value, "@hostname_prefix")
360+
log.Debugln("Adding hostname to properties: hostname=", properties.Value["hostname"])
361+
}
362+
335363
// Provision creates a new instance based on the spec.
336364
func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
337365
// Simply writes a file and call terraform apply
@@ -359,22 +387,7 @@ func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
359387
}
360388
}
361389

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"])
390+
p.optionalProcessHostname(&properties, name)
378391

379392
switch properties.Type {
380393
case "aws_instance", "azurerm_virtual_machine", "digitalocean_droplet", "google_compute_instance":

examples/instance/terraform/plugin_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,6 @@ func run(t *testing.T, resourceType, properties string) {
178178
m := testingData.(map[string]interface{})
179179
value, _ := m["value"].(map[string]interface{})
180180

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-
193181
switch resourceType {
194182
case "softlayer_virtual_guest":
195183
require.Equal(t, conv([]interface{}{
@@ -199,6 +187,19 @@ func run(t *testing.T, resourceType, properties string) {
199187
"Name:" + string(*id),
200188
}), conv(props["tags"].([]interface{})))
201189
require.Equal(t, instanceSpec.Init, props["user_metadata"])
190+
191+
// If a hostname was specified, the expectation is that the hostname is appended with the timestamp from the ID
192+
if value["@hostname_prefix"] != nil && strings.Trim(value["@hostname_prefix"].(string), " ") != "" {
193+
newID := strings.Replace(string(*id), "instance-", "", -1)
194+
expectedHostname := "softlayer-hostname-" + newID
195+
require.Equal(t, expectedHostname, props["hostname"])
196+
} else {
197+
// If no hostname was specified, the hostname should equal the ID
198+
require.Equal(t, string(*id), props["hostname"])
199+
}
200+
// Verify the hostname prefix key/value is no longer in the props
201+
require.Nil(t, props["@hostname_prefix"])
202+
202203
case "aws_instance":
203204
require.Equal(t, map[string]interface{}{
204205
"InstancePlugin": "terraform",

0 commit comments

Comments
 (0)