Skip to content

Commit

Permalink
standalone hostname during OS customization
Browse files Browse the repository at this point in the history
  • Loading branch information
ewypych committed Aug 24, 2017
1 parent 4079169 commit f6f9121
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
24 changes: 22 additions & 2 deletions vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type memoryAllocation struct {

type virtualMachine struct {
name string
hostname string
folder string
datacenter string
cluster string
Expand Down Expand Up @@ -133,6 +134,12 @@ func resourceVSphereVirtualMachine() *schema.Resource {
ForceNew: true,
},

"hostname": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"folder": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -688,6 +695,10 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{
},
}

if v, ok := d.GetOk("hostname"); ok {
vm.hostname = v.(string)
}

if v, ok := d.GetOk("folder"); ok {
vm.folder = v.(string)
}
Expand Down Expand Up @@ -2107,9 +2118,13 @@ func (vm *virtualMachine) setupVirtualMachine(c *govmomi.Client) error {

customIdentification := types.CustomizationIdentification{}

if len(vm.hostname) == 0 {
vm.hostname = vm.name
}

userData := types.CustomizationUserData{
ComputerName: &types.CustomizationFixedName{
Name: strings.Split(vm.name, ".")[0],
Name: strings.Split(vm.hostname, ".")[0],
},
ProductId: vm.windowsOptionalConfig.productKey,
FullName: "terraform",
Expand Down Expand Up @@ -2138,9 +2153,14 @@ func (vm *virtualMachine) setupVirtualMachine(c *govmomi.Client) error {
UserData: userData,
}
} else {

if len(vm.hostname) == 0 {
vm.hostname = vm.name
}

identity_options = &types.CustomizationLinuxPrep{
HostName: &types.CustomizationFixedName{
Name: strings.Split(vm.name, ".")[0],
Name: strings.Split(vm.hostname, ".")[0],
},
Domain: vm.domain,
TimeZone: vm.timeZone,
Expand Down
29 changes: 29 additions & 0 deletions vsphere/resource_vsphere_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,35 @@ func TestAccVSphereVirtualMachine_noPanicShutdown(t *testing.T) {
})
}

const testAccCheckVSphereVirtualMachineConfig_hostname = `
resource "vsphere_virtual_machine" "foo" {
name = "terraform-test"
hostname = "testterraform"
` + testAccTemplateBasicBodyWithEnd

func TestAccVSphereVirtualMachine_hostname(t *testing.T) {
var vm virtualMachine
basic_vars := setupTemplateBasicBodyVars()
config := basic_vars.testSprintfTemplateBody(testAccCheckVSphereVirtualMachineConfig_hostname)

log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_hostname)
log.Printf("[DEBUG] template config= %s", config)

resource.Test(t, resource.TestCase{
PreCheck: func() { testBasicPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
TestFuncData{vm: vm, label: basic_vars.label}.testCheckFuncBasic(),
),
},
},
})
}

const testAccCheckVSphereVirtualMachineConfig_debug = `
provider "vsphere" {
client_debug = true
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/virtual_machine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ resource "vsphere_virtual_machine" "web" {
```hcl
resource "vsphere_virtual_machine" "lb" {
name = "lb01"
hostname = "lbalancer01"
folder = "Loadbalancers"
vcpu = 2
memory = 4096
Expand Down Expand Up @@ -64,6 +65,7 @@ The following arguments are supported:
* `folder` - (Optional) The folder to group the VM in.
* `vcpu` - (Required) The number of virtual CPUs to allocate to the virtual machine
* `memory` - (Required) The amount of RAM (in MB) to allocate to the virtual machine
* `hostname` - (Optional) The virtual machine hostname used during the OS customization - if not specified then `name` will be a hostname
* `memory_reservation` - (Optional) The amount of RAM (in MB) to reserve physical memory resource; defaults to 0 (means not to reserve)
* `datacenter` - (Optional) The name of a Datacenter in which to launch the virtual machine
* `cluster` - (Optional) Name of a Cluster in which to launch the virtual machine
Expand Down

0 comments on commit f6f9121

Please sign in to comment.