From f6f91212b865bd481da013d2d8eed0a97c9a3a76 Mon Sep 17 00:00:00 2001 From: ewypych Date: Tue, 13 Jun 2017 21:49:12 +0200 Subject: [PATCH 1/2] standalone hostname during OS customization --- vsphere/resource_vsphere_virtual_machine.go | 24 +++++++++++++-- .../resource_vsphere_virtual_machine_test.go | 29 +++++++++++++++++++ website/docs/r/virtual_machine.html.markdown | 2 ++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/vsphere/resource_vsphere_virtual_machine.go b/vsphere/resource_vsphere_virtual_machine.go index c9a92994b..991a3ee27 100644 --- a/vsphere/resource_vsphere_virtual_machine.go +++ b/vsphere/resource_vsphere_virtual_machine.go @@ -78,6 +78,7 @@ type memoryAllocation struct { type virtualMachine struct { name string + hostname string folder string datacenter string cluster string @@ -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, @@ -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) } @@ -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", @@ -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, diff --git a/vsphere/resource_vsphere_virtual_machine_test.go b/vsphere/resource_vsphere_virtual_machine_test.go index 050ce4564..f10f05fe9 100644 --- a/vsphere/resource_vsphere_virtual_machine_test.go +++ b/vsphere/resource_vsphere_virtual_machine_test.go @@ -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 diff --git a/website/docs/r/virtual_machine.html.markdown b/website/docs/r/virtual_machine.html.markdown index 5ba3f2d6a..6cba43475 100644 --- a/website/docs/r/virtual_machine.html.markdown +++ b/website/docs/r/virtual_machine.html.markdown @@ -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 @@ -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 From 17e75ce06db4396ce2a022a3b318cd89cac223f2 Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Thu, 24 Aug 2017 11:43:12 -0700 Subject: [PATCH 2/2] r/vsphere_virtual_machine: Small doc correction for hostname Just a small grammar fix, and also re-run the sample code through terraform fmt. --- website/docs/r/virtual_machine.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/r/virtual_machine.html.markdown b/website/docs/r/virtual_machine.html.markdown index 6cba43475..2d0c654f5 100644 --- a/website/docs/r/virtual_machine.html.markdown +++ b/website/docs/r/virtual_machine.html.markdown @@ -34,7 +34,7 @@ resource "vsphere_virtual_machine" "web" { ```hcl resource "vsphere_virtual_machine" "lb" { name = "lb01" - hostname = "lbalancer01" + hostname = "lbalancer01" folder = "Loadbalancers" vcpu = 2 memory = 4096 @@ -65,7 +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 +* `hostname` - (Optional) The virtual machine hostname used during the OS customization. Defaults to the `name` attribute. * `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