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

Commit a3b92e9

Browse files
kaufersDavid Chung
authored andcommitted
Append LogicalID to Softlayer instance hostname (#558)
Signed-off-by: Steven Kaufer <[email protected]>
1 parent f552304 commit a3b92e9

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

examples/instance/terraform/plugin.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,30 +288,36 @@ func ensureUniqueFile(dir string) string {
288288
// Special processing of hostname on some platforms. Where supported, you can
289289
// add a special @hostname_prefix that will allow the setting of hostname in given format
290290
// TODO - expand this to formatting string
291-
func (p *plugin) optionalProcessHostname(vmType TResourceType, name TResourceName, properties TResourceProperties) {
291+
func (p *plugin) optionalProcessHostname(vmType TResourceType, name TResourceName, logicalID *instance.LogicalID, properties TResourceProperties) {
292292

293293
if properties == nil {
294294
return
295295
}
296296

297297
switch vmType {
298-
case TResourceType("softlayer_virtual_guest"): // # list the platforms here
298+
case VMSoftLayer: // # List the supported platforms here
299299
default:
300300
return
301301
}
302-
302+
// Use the LogicalID (if set), else the name
303+
var hostname string
304+
if logicalID == nil {
305+
hostname = string(name)
306+
} else {
307+
hostname = string(*logicalID)
308+
}
303309
// Use the given hostname value as a prefix if it is a non-empty string
304310
if hostnamePrefix, is := properties["@hostname_prefix"].(string); is {
305311
hostnamePrefix = strings.Trim(hostnamePrefix, " ")
306312
// Use the default behavior if hostnamePrefix was either not a string, or an empty string
307313
if hostnamePrefix == "" {
308-
properties["hostname"] = string(name)
314+
properties["hostname"] = hostname
309315
} else {
310316
// Remove "instance-" from "instance-XXXX", then append that string to the hostnamePrefix to create the new hostname
311-
properties["hostname"] = fmt.Sprintf("%s-%s", hostnamePrefix, strings.Replace(string(name), "instance-", "", -1))
317+
properties["hostname"] = fmt.Sprintf("%s-%s", hostnamePrefix, strings.Replace(hostname, "instance-", "", -1))
312318
}
313319
} else {
314-
properties["hostname"] = name
320+
properties["hostname"] = hostname
315321
}
316322
// Delete hostnamePrefix so it will not be written in the *.tf.json file
317323
delete(properties, "@hostname_prefix")
@@ -381,7 +387,8 @@ func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
381387
spec.Tags["LogicalID"] = string(*spec.LogicalID)
382388
}
383389

384-
p.optionalProcessHostname(vmType, TResourceName(name), properties)
390+
// Optionally append either part of the name or the logical ID to the given hostname
391+
p.optionalProcessHostname(vmType, TResourceName(name), spec.LogicalID, properties)
385392

386393
// Merge any user-defined tags and convert to platform specific tag type
387394
switch vmType {

examples/instance/terraform/plugin_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,13 @@ func run(t *testing.T, resourceType, properties string) {
259259
}), conv(props["tags"].([]interface{})))
260260
require.Equal(t, expectedUserData2, props["user_metadata"])
261261

262-
// If a hostname was specified, the expectation is that the hostname is appended with the timestamp from the ID
262+
// If a hostname was specified, the expectation is that the hostname is appended with the logical ID
263263
if value["@hostname_prefix"] != nil && strings.Trim(value["@hostname_prefix"].(string), " ") != "" {
264-
newID := strings.Replace(string(*id2), "instance-", "", -1)
265-
expectedHostname := "softlayer-hostname-" + newID
264+
expectedHostname := "softlayer-hostname-logical:id-2"
266265
require.Equal(t, expectedHostname, props["hostname"])
267266
} else {
268-
// If no hostname was specified, the hostname should equal the ID
269-
require.Equal(t, string(*id2), props["hostname"])
267+
// If no hostname was specified, the hostname should equal the logical ID
268+
require.Equal(t, "logical:id-2", props["hostname"])
270269
}
271270
// Verify the hostname prefix key/value is no longer in the props
272271
require.Nil(t, props["@hostname_prefix"])

0 commit comments

Comments
 (0)