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

Append LogicalID to Softlayer instance hostname #558

Merged
merged 1 commit into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions examples/instance/terraform/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,30 +288,36 @@ func ensureUniqueFile(dir string) string {
// Special processing of hostname on some platforms. Where supported, you can
// add a special @hostname_prefix that will allow the setting of hostname in given format
// TODO - expand this to formatting string
func (p *plugin) optionalProcessHostname(vmType TResourceType, name TResourceName, properties TResourceProperties) {
func (p *plugin) optionalProcessHostname(vmType TResourceType, name TResourceName, logicalID *instance.LogicalID, properties TResourceProperties) {

if properties == nil {
return
}

switch vmType {
case TResourceType("softlayer_virtual_guest"): // # list the platforms here
case VMSoftLayer: // # List the supported platforms here
default:
return
}

// Use the LogicalID (if set), else the name
var hostname string
if logicalID == nil {
hostname = string(name)
} else {
hostname = string(*logicalID)
}
// Use the given hostname value as a prefix if it is a non-empty string
if hostnamePrefix, is := properties["@hostname_prefix"].(string); is {
hostnamePrefix = strings.Trim(hostnamePrefix, " ")
// Use the default behavior if hostnamePrefix was either not a string, or an empty string
if hostnamePrefix == "" {
properties["hostname"] = string(name)
properties["hostname"] = hostname
} else {
// Remove "instance-" from "instance-XXXX", then append that string to the hostnamePrefix to create the new hostname
properties["hostname"] = fmt.Sprintf("%s-%s", hostnamePrefix, strings.Replace(string(name), "instance-", "", -1))
properties["hostname"] = fmt.Sprintf("%s-%s", hostnamePrefix, strings.Replace(hostname, "instance-", "", -1))
}
} else {
properties["hostname"] = name
properties["hostname"] = hostname
}
// Delete hostnamePrefix so it will not be written in the *.tf.json file
delete(properties, "@hostname_prefix")
Expand Down Expand Up @@ -381,7 +387,8 @@ func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
spec.Tags["LogicalID"] = string(*spec.LogicalID)
}

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

// Merge any user-defined tags and convert to platform specific tag type
switch vmType {
Expand Down
9 changes: 4 additions & 5 deletions examples/instance/terraform/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,13 @@ func run(t *testing.T, resourceType, properties string) {
}), conv(props["tags"].([]interface{})))
require.Equal(t, expectedUserData2, props["user_metadata"])

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