From 725a735c0d2c398bc7432a3adfbeadd76df55c4a Mon Sep 17 00:00:00 2001 From: Chris Love Date: Sun, 8 Nov 2015 18:02:07 -0700 Subject: [PATCH 01/25] adding capability to set custom configuration value in virtual machines --- .../resource_vsphere_virtual_machine.go | 84 +++++++++++++++---- 1 file changed, 69 insertions(+), 15 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index ac15cd97f..07d84367a 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -41,21 +41,22 @@ type hardDisk struct { } type virtualMachine struct { - name string - datacenter string - cluster string - resourcePool string - datastore string - vcpu int - memoryMb int64 - template string - networkInterfaces []networkInterface - hardDisks []hardDisk - gateway string - domain string - timeZone string - dnsSuffixes []string - dnsServers []string + name string + datacenter string + cluster string + resourcePool string + datastore string + vcpu int + memoryMb int64 + template string + networkInterfaces []networkInterface + hardDisks []hardDisk + gateway string + domain string + timeZone string + dnsSuffixes []string + dnsServers []string + customConfigurations map[string](types.AnyType) } func resourceVSphereVirtualMachine() *schema.Resource { @@ -135,6 +136,12 @@ func resourceVSphereVirtualMachine() *schema.Resource { ForceNew: true, }, + "custom_configuration_parameters": &schema.Schema{ + Type: schema.TypeMap, + Optional: true, + ForceNew: true, + }, + "network_interface": &schema.Schema{ Type: schema.TypeList, Required: true, @@ -261,6 +268,12 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{ vm.dnsServers = DefaultDNSServers } + if vL, ok := d.GetOk("custom_configuration_parameters"); ok { + if custom_configs, ok := vL.(map[string]types.AnyType); ok { + vm.customConfigurations = custom_configs + } + } + if vL, ok := d.GetOk("network_interface"); ok { networks := make([]networkInterface, len(vL.([]interface{}))) for i, v := range vL.([]interface{}) { @@ -418,6 +431,15 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("datacenter", dc) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) + + if mvm.Config && len(mvm.Config.ExtraConfig) > 0 { + custom_configs := make(map[string]string) + for _, v := range mvm.Config.ExtraConfig { + value := v.GetOptionValue() + custom_configs[value.Key] = value.Value + } + d.Set("custom_configuration_parameters", custom_configs) + } d.Set("datastore", rootDatastore) // Initialize the connection info @@ -802,6 +824,22 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error { } log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) + // make ExtraConfig + if len(vm.customConfigurations) > 0 { + var ov []types.BaseOptionValue + for k, v := range vm.customConfigurations { + key := k + value := v + o := types.OptionValue{ + Key: key, + Value: &value, + } + ov = append(ov, &o) + } + configSpec.ExtraConfig = ov + log.Printf("[DEBUG] virtual machine Extra Config spec: %v", configSpec.ExtraConfig) + } + var datastore *object.Datastore if vm.datastore == "" { datastore, err = finder.DefaultDatastore(context.TODO()) @@ -1003,6 +1041,22 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error { } log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) + // make ExtraConfig + if len(vm.customConfigurations) > 0 { + var ov []types.BaseOptionValue + for k, v := range vm.customConfigurations { + key := k + value := v + o := types.OptionValue{ + Key: key, + Value: &value, + } + ov = append(ov, &o) + } + configSpec.ExtraConfig = ov + log.Printf("[DEBUG] virtual machine Extra Config spec: %v", configSpec.ExtraConfig) + } + // create CustomizationSpec customSpec := types.CustomizationSpec{ Identity: &types.CustomizationLinuxPrep{ From b5ca1466433c6a3eed350f6d6f9e163ddcb7b032 Mon Sep 17 00:00:00 2001 From: Chris Love Date: Sun, 8 Nov 2015 18:21:17 -0700 Subject: [PATCH 02/25] fixing if and AnyTypes --- builtin/providers/vsphere/resource_vsphere_virtual_machine.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index 07d84367a..cf636e79d 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -432,8 +432,8 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) - if mvm.Config && len(mvm.Config.ExtraConfig) > 0 { - custom_configs := make(map[string]string) + if len(mvm.Config.ExtraConfig) > 0 { + custom_configs := make(map[string]types.AnyType) for _, v := range mvm.Config.ExtraConfig { value := v.GetOptionValue() custom_configs[value.Key] = value.Value From 01adcb18f3416be7e7fc02a9bec750375915968b Mon Sep 17 00:00:00 2001 From: Chris Love Date: Sun, 8 Nov 2015 18:49:49 -0700 Subject: [PATCH 03/25] adding new functional test --- .../resource_vsphere_virtual_machine_test.go | 83 ++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go index 66d6ea44f..2cae45fe4 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go @@ -127,6 +127,67 @@ func TestAccVSphereVirtualMachine_dhcp(t *testing.T) { }) } +func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { + var vm virtualMachine + var locationOpt string + var datastoreOpt string + + if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { + locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) + } + if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { + locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) + } + if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { + locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) + } + if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { + datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) + } + template := os.Getenv("VSPHERE_TEMPLATE") + label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: fmt.Sprintf( + testAccCheckVSphereVirtualMachineConfig_custom_configs, + locationOpt, + label, + datastoreOpt, + template, + ), + Check: resource.ComposeTestCheckFunc( + testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "name", "terraform-test"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "vcpu", "2"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "memory", "4096"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "disk.#", "1"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "disk.0.template", template), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "network_interface.#", "1"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "custom_configuration_parameters.foo", "bar"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "custom_configuration_parameters.car", "ferrai"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "custom_configuration_parameters.num", "42"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.bar", "network_interface.0.label", label), + ), + }, + }, + }) +} + func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error { client := testAccProvider.Meta().(*govmomi.Client) finder := find.NewFinder(client.Client, true) @@ -212,7 +273,6 @@ resource "vsphere_virtual_machine" "foo" { } } ` - const testAccCheckVSphereVirtualMachineConfig_dhcp = ` resource "vsphere_virtual_machine" "bar" { name = "terraform-test" @@ -228,3 +288,24 @@ resource "vsphere_virtual_machine" "bar" { } } ` + +const testAccCheckVSphereVirtualMachineConfig_custom_configs = ` +resource "vsphere_virtual_machine" "car" { + name = "terraform-test-custom" +%s + vcpu = 2 + memory = 4096 + network_interface { + label = "%s" + } + custom_configuration_parameters { + foo = "bar", + car = "ferrai", + num = 42 + } + disk { +%s + template = "%s" + } +} +` From a5050fe471f9256555b732f6017744d50ddfbb5f Mon Sep 17 00:00:00 2001 From: Chris Love Date: Mon, 9 Nov 2015 04:50:09 +0000 Subject: [PATCH 04/25] working on read and more testing --- .../resource_vsphere_virtual_machine.go | 23 ++++++++++++++++--- .../resource_vsphere_virtual_machine_test.go | 12 +++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index cf636e79d..338c95301 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -269,8 +269,13 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{ } if vL, ok := d.GetOk("custom_configuration_parameters"); ok { - if custom_configs, ok := vL.(map[string]types.AnyType); ok { - vm.customConfigurations = custom_configs + if custom_configs, ok := vL.(map[string]interface{}); ok { + custom := make(map[string]types.AnyType) + for k,v := range custom_configs { + custom[k] = v + } + vm.customConfigurations = custom + log.Printf("[DEBUG] custom_configuration_parameters init: %v", vm.customConfigurations) } } @@ -432,14 +437,21 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) - if len(mvm.Config.ExtraConfig) > 0 { + log.Printf("[DEBUG] ===============================") + //log.Printf("[DEBUG] Get extra config ===============================") + //log.Printf("[DEBUG] Get extra config %v", mvm.Config) + //log.Printf("[DEBUG] Get extra config %v", mvm.Config.ExtraConfig) + if mvm.Config != nil && mvm.Config.ExtraConfig != nil && len(mvm.Config.ExtraConfig) > 0 { + log.Printf("[DEBUG] reading custom configs") custom_configs := make(map[string]types.AnyType) for _, v := range mvm.Config.ExtraConfig { value := v.GetOptionValue() custom_configs[value.Key] = value.Value + log.Printf("[DEBUG] reading custom configs %s,%s",value.Key, value.Value) } d.Set("custom_configuration_parameters", custom_configs) } + log.Printf("[DEBUG] ===============================") d.Set("datastore", rootDatastore) // Initialize the connection info @@ -825,6 +837,7 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error { log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) // make ExtraConfig + log.Printf("[DEBUG] virtual machine Extra Config spec start") if len(vm.customConfigurations) > 0 { var ov []types.BaseOptionValue for k, v := range vm.customConfigurations { @@ -834,6 +847,7 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error { Key: key, Value: &value, } + log.Printf("[DEBUG] virtual machine Extra Config spec: %s,%s", k,v) ov = append(ov, &o) } configSpec.ExtraConfig = ov @@ -1041,6 +1055,8 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error { } log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) + log.Printf("[DEBUG] starting extra custom config spec: %v", vm.customConfigurations) + // make ExtraConfig if len(vm.customConfigurations) > 0 { var ov []types.BaseOptionValue @@ -1149,5 +1165,6 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error { return err } } + log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) return nil } diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go index 2cae45fe4..804e1ae07 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go @@ -161,9 +161,9 @@ func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { template, ), Check: resource.ComposeTestCheckFunc( - testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm), + testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.car", &vm), resource.TestCheckResourceAttr( - "vsphere_virtual_machine.car", "name", "terraform-test"), + "vsphere_virtual_machine.car", "name", "terraform-test-custom"), resource.TestCheckResourceAttr( "vsphere_virtual_machine.car", "vcpu", "2"), resource.TestCheckResourceAttr( @@ -181,7 +181,7 @@ func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { resource.TestCheckResourceAttr( "vsphere_virtual_machine.car", "custom_configuration_parameters.num", "42"), resource.TestCheckResourceAttr( - "vsphere_virtual_machine.bar", "network_interface.0.label", label), + "vsphere_virtual_machine.car", "network_interface.0.label", label), ), }, }, @@ -299,9 +299,9 @@ resource "vsphere_virtual_machine" "car" { label = "%s" } custom_configuration_parameters { - foo = "bar", - car = "ferrai", - num = 42 + "foo" = "bar" + "car" = "ferrai" + "num" = 42 } disk { %s From 3f37884721c4d7e27f6524fc1e6cdbc7781414c0 Mon Sep 17 00:00:00 2001 From: Chris Love Date: Sun, 8 Nov 2015 18:02:07 -0700 Subject: [PATCH 05/25] adding capability to set custom configuration value in virtual machines --- .../resource_vsphere_virtual_machine.go | 84 +++++++++++++++---- 1 file changed, 69 insertions(+), 15 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index ac15cd97f..07d84367a 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -41,21 +41,22 @@ type hardDisk struct { } type virtualMachine struct { - name string - datacenter string - cluster string - resourcePool string - datastore string - vcpu int - memoryMb int64 - template string - networkInterfaces []networkInterface - hardDisks []hardDisk - gateway string - domain string - timeZone string - dnsSuffixes []string - dnsServers []string + name string + datacenter string + cluster string + resourcePool string + datastore string + vcpu int + memoryMb int64 + template string + networkInterfaces []networkInterface + hardDisks []hardDisk + gateway string + domain string + timeZone string + dnsSuffixes []string + dnsServers []string + customConfigurations map[string](types.AnyType) } func resourceVSphereVirtualMachine() *schema.Resource { @@ -135,6 +136,12 @@ func resourceVSphereVirtualMachine() *schema.Resource { ForceNew: true, }, + "custom_configuration_parameters": &schema.Schema{ + Type: schema.TypeMap, + Optional: true, + ForceNew: true, + }, + "network_interface": &schema.Schema{ Type: schema.TypeList, Required: true, @@ -261,6 +268,12 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{ vm.dnsServers = DefaultDNSServers } + if vL, ok := d.GetOk("custom_configuration_parameters"); ok { + if custom_configs, ok := vL.(map[string]types.AnyType); ok { + vm.customConfigurations = custom_configs + } + } + if vL, ok := d.GetOk("network_interface"); ok { networks := make([]networkInterface, len(vL.([]interface{}))) for i, v := range vL.([]interface{}) { @@ -418,6 +431,15 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("datacenter", dc) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) + + if mvm.Config && len(mvm.Config.ExtraConfig) > 0 { + custom_configs := make(map[string]string) + for _, v := range mvm.Config.ExtraConfig { + value := v.GetOptionValue() + custom_configs[value.Key] = value.Value + } + d.Set("custom_configuration_parameters", custom_configs) + } d.Set("datastore", rootDatastore) // Initialize the connection info @@ -802,6 +824,22 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error { } log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) + // make ExtraConfig + if len(vm.customConfigurations) > 0 { + var ov []types.BaseOptionValue + for k, v := range vm.customConfigurations { + key := k + value := v + o := types.OptionValue{ + Key: key, + Value: &value, + } + ov = append(ov, &o) + } + configSpec.ExtraConfig = ov + log.Printf("[DEBUG] virtual machine Extra Config spec: %v", configSpec.ExtraConfig) + } + var datastore *object.Datastore if vm.datastore == "" { datastore, err = finder.DefaultDatastore(context.TODO()) @@ -1003,6 +1041,22 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error { } log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) + // make ExtraConfig + if len(vm.customConfigurations) > 0 { + var ov []types.BaseOptionValue + for k, v := range vm.customConfigurations { + key := k + value := v + o := types.OptionValue{ + Key: key, + Value: &value, + } + ov = append(ov, &o) + } + configSpec.ExtraConfig = ov + log.Printf("[DEBUG] virtual machine Extra Config spec: %v", configSpec.ExtraConfig) + } + // create CustomizationSpec customSpec := types.CustomizationSpec{ Identity: &types.CustomizationLinuxPrep{ From 6e19c3f0e0047e4f40a43c7eba34b5914aa1495b Mon Sep 17 00:00:00 2001 From: Chris Love Date: Sun, 8 Nov 2015 18:21:17 -0700 Subject: [PATCH 06/25] fixing if and AnyTypes --- builtin/providers/vsphere/resource_vsphere_virtual_machine.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index 07d84367a..cf636e79d 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -432,8 +432,8 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) - if mvm.Config && len(mvm.Config.ExtraConfig) > 0 { - custom_configs := make(map[string]string) + if len(mvm.Config.ExtraConfig) > 0 { + custom_configs := make(map[string]types.AnyType) for _, v := range mvm.Config.ExtraConfig { value := v.GetOptionValue() custom_configs[value.Key] = value.Value From 106b1264485b1bafc0e8f6672ca81491e91791b7 Mon Sep 17 00:00:00 2001 From: Chris Love Date: Sun, 8 Nov 2015 18:49:49 -0700 Subject: [PATCH 07/25] adding new functional test --- .../resource_vsphere_virtual_machine_test.go | 83 ++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go index 66d6ea44f..2cae45fe4 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go @@ -127,6 +127,67 @@ func TestAccVSphereVirtualMachine_dhcp(t *testing.T) { }) } +func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { + var vm virtualMachine + var locationOpt string + var datastoreOpt string + + if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { + locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) + } + if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { + locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) + } + if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { + locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) + } + if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { + datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) + } + template := os.Getenv("VSPHERE_TEMPLATE") + label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: fmt.Sprintf( + testAccCheckVSphereVirtualMachineConfig_custom_configs, + locationOpt, + label, + datastoreOpt, + template, + ), + Check: resource.ComposeTestCheckFunc( + testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "name", "terraform-test"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "vcpu", "2"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "memory", "4096"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "disk.#", "1"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "disk.0.template", template), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "network_interface.#", "1"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "custom_configuration_parameters.foo", "bar"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "custom_configuration_parameters.car", "ferrai"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "custom_configuration_parameters.num", "42"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.bar", "network_interface.0.label", label), + ), + }, + }, + }) +} + func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error { client := testAccProvider.Meta().(*govmomi.Client) finder := find.NewFinder(client.Client, true) @@ -212,7 +273,6 @@ resource "vsphere_virtual_machine" "foo" { } } ` - const testAccCheckVSphereVirtualMachineConfig_dhcp = ` resource "vsphere_virtual_machine" "bar" { name = "terraform-test" @@ -228,3 +288,24 @@ resource "vsphere_virtual_machine" "bar" { } } ` + +const testAccCheckVSphereVirtualMachineConfig_custom_configs = ` +resource "vsphere_virtual_machine" "car" { + name = "terraform-test-custom" +%s + vcpu = 2 + memory = 4096 + network_interface { + label = "%s" + } + custom_configuration_parameters { + foo = "bar", + car = "ferrai", + num = 42 + } + disk { +%s + template = "%s" + } +} +` From 0bf8ffd0430b067bd924d7eed0dcd72ae3b655c4 Mon Sep 17 00:00:00 2001 From: Chris Love Date: Mon, 9 Nov 2015 04:50:09 +0000 Subject: [PATCH 08/25] working on read and more testing --- .../resource_vsphere_virtual_machine.go | 23 ++++++++++++++++--- .../resource_vsphere_virtual_machine_test.go | 12 +++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index cf636e79d..338c95301 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -269,8 +269,13 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{ } if vL, ok := d.GetOk("custom_configuration_parameters"); ok { - if custom_configs, ok := vL.(map[string]types.AnyType); ok { - vm.customConfigurations = custom_configs + if custom_configs, ok := vL.(map[string]interface{}); ok { + custom := make(map[string]types.AnyType) + for k,v := range custom_configs { + custom[k] = v + } + vm.customConfigurations = custom + log.Printf("[DEBUG] custom_configuration_parameters init: %v", vm.customConfigurations) } } @@ -432,14 +437,21 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) - if len(mvm.Config.ExtraConfig) > 0 { + log.Printf("[DEBUG] ===============================") + //log.Printf("[DEBUG] Get extra config ===============================") + //log.Printf("[DEBUG] Get extra config %v", mvm.Config) + //log.Printf("[DEBUG] Get extra config %v", mvm.Config.ExtraConfig) + if mvm.Config != nil && mvm.Config.ExtraConfig != nil && len(mvm.Config.ExtraConfig) > 0 { + log.Printf("[DEBUG] reading custom configs") custom_configs := make(map[string]types.AnyType) for _, v := range mvm.Config.ExtraConfig { value := v.GetOptionValue() custom_configs[value.Key] = value.Value + log.Printf("[DEBUG] reading custom configs %s,%s",value.Key, value.Value) } d.Set("custom_configuration_parameters", custom_configs) } + log.Printf("[DEBUG] ===============================") d.Set("datastore", rootDatastore) // Initialize the connection info @@ -825,6 +837,7 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error { log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) // make ExtraConfig + log.Printf("[DEBUG] virtual machine Extra Config spec start") if len(vm.customConfigurations) > 0 { var ov []types.BaseOptionValue for k, v := range vm.customConfigurations { @@ -834,6 +847,7 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error { Key: key, Value: &value, } + log.Printf("[DEBUG] virtual machine Extra Config spec: %s,%s", k,v) ov = append(ov, &o) } configSpec.ExtraConfig = ov @@ -1041,6 +1055,8 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error { } log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) + log.Printf("[DEBUG] starting extra custom config spec: %v", vm.customConfigurations) + // make ExtraConfig if len(vm.customConfigurations) > 0 { var ov []types.BaseOptionValue @@ -1149,5 +1165,6 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error { return err } } + log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) return nil } diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go index 2cae45fe4..804e1ae07 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go @@ -161,9 +161,9 @@ func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { template, ), Check: resource.ComposeTestCheckFunc( - testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm), + testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.car", &vm), resource.TestCheckResourceAttr( - "vsphere_virtual_machine.car", "name", "terraform-test"), + "vsphere_virtual_machine.car", "name", "terraform-test-custom"), resource.TestCheckResourceAttr( "vsphere_virtual_machine.car", "vcpu", "2"), resource.TestCheckResourceAttr( @@ -181,7 +181,7 @@ func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { resource.TestCheckResourceAttr( "vsphere_virtual_machine.car", "custom_configuration_parameters.num", "42"), resource.TestCheckResourceAttr( - "vsphere_virtual_machine.bar", "network_interface.0.label", label), + "vsphere_virtual_machine.car", "network_interface.0.label", label), ), }, }, @@ -299,9 +299,9 @@ resource "vsphere_virtual_machine" "car" { label = "%s" } custom_configuration_parameters { - foo = "bar", - car = "ferrai", - num = 42 + "foo" = "bar" + "car" = "ferrai" + "num" = 42 } disk { %s From 54b103b9c7cf2ba5ecd19dfd5701ec7026d41fc3 Mon Sep 17 00:00:00 2001 From: Chris Love Date: Wed, 11 Nov 2015 22:42:36 +0000 Subject: [PATCH 09/25] testing finished --- .../resource_vsphere_virtual_machine.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index 338c95301..274a2278d 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -381,7 +381,7 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) var mvm mo.VirtualMachine collector := property.DefaultCollector(client.Client) - if err := collector.RetrieveOne(context.TODO(), vm.Reference(), []string{"guest", "summary", "datastore"}, &mvm); err != nil { + if err := collector.RetrieveOne(context.TODO(), vm.Reference(), []string{"guest", "summary", "datastore","config.extraConfig"}, &mvm); err != nil { return err } @@ -437,21 +437,18 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) - log.Printf("[DEBUG] ===============================") - //log.Printf("[DEBUG] Get extra config ===============================") - //log.Printf("[DEBUG] Get extra config %v", mvm.Config) - //log.Printf("[DEBUG] Get extra config %v", mvm.Config.ExtraConfig) if mvm.Config != nil && mvm.Config.ExtraConfig != nil && len(mvm.Config.ExtraConfig) > 0 { - log.Printf("[DEBUG] reading custom configs") - custom_configs := make(map[string]types.AnyType) + //TODO: can only set specific custom value, not everything + //Would need the config here + //custom_configs := make(map[string]types.AnyType) for _, v := range mvm.Config.ExtraConfig { value := v.GetOptionValue() - custom_configs[value.Key] = value.Value - log.Printf("[DEBUG] reading custom configs %s,%s",value.Key, value.Value) + //custom_configs[value.Key] = value.Value + log.Printf("[DEBUG] custom configs %s,%s",value.Key, value.Value) } - d.Set("custom_configuration_parameters", custom_configs) + //d.Set("custom_configuration_parameters", custom_configs) } - log.Printf("[DEBUG] ===============================") + d.Set("datastore", rootDatastore) // Initialize the connection info From 09ce6b4744476b040f89a2421c88cc7d631f16b1 Mon Sep 17 00:00:00 2001 From: Chris Love Date: Wed, 11 Nov 2015 22:50:18 +0000 Subject: [PATCH 10/25] updating documentation --- .../docs/providers/vsphere/r/virtual_machine.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown b/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown index d008357ec..4062a212b 100644 --- a/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown +++ b/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown @@ -48,6 +48,7 @@ The following arguments are supported: * `network_interface` - (Required) Configures virtual network interfaces; see [Network Interfaces](#network-interfaces) below for details. * `disk` - (Required) Configures virtual disks; see [Disks](#disks) below for details * `boot_delay` - (Optional) Time in seconds to wait for machine network to be ready. +* `custom_configuration_parameters` - (Optional) Map of values that is set as virtual machine custom configurations. ## Network Interfaces From e899a2949f92bdb1ae3fae5df1b8de68265993ef Mon Sep 17 00:00:00 2001 From: Chris Love Date: Sun, 8 Nov 2015 18:02:07 -0700 Subject: [PATCH 11/25] adding capability to set custom configuration value in virtual machines --- .../resource_vsphere_virtual_machine.go | 84 +++++++++++++++---- 1 file changed, 69 insertions(+), 15 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index ac15cd97f..07d84367a 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -41,21 +41,22 @@ type hardDisk struct { } type virtualMachine struct { - name string - datacenter string - cluster string - resourcePool string - datastore string - vcpu int - memoryMb int64 - template string - networkInterfaces []networkInterface - hardDisks []hardDisk - gateway string - domain string - timeZone string - dnsSuffixes []string - dnsServers []string + name string + datacenter string + cluster string + resourcePool string + datastore string + vcpu int + memoryMb int64 + template string + networkInterfaces []networkInterface + hardDisks []hardDisk + gateway string + domain string + timeZone string + dnsSuffixes []string + dnsServers []string + customConfigurations map[string](types.AnyType) } func resourceVSphereVirtualMachine() *schema.Resource { @@ -135,6 +136,12 @@ func resourceVSphereVirtualMachine() *schema.Resource { ForceNew: true, }, + "custom_configuration_parameters": &schema.Schema{ + Type: schema.TypeMap, + Optional: true, + ForceNew: true, + }, + "network_interface": &schema.Schema{ Type: schema.TypeList, Required: true, @@ -261,6 +268,12 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{ vm.dnsServers = DefaultDNSServers } + if vL, ok := d.GetOk("custom_configuration_parameters"); ok { + if custom_configs, ok := vL.(map[string]types.AnyType); ok { + vm.customConfigurations = custom_configs + } + } + if vL, ok := d.GetOk("network_interface"); ok { networks := make([]networkInterface, len(vL.([]interface{}))) for i, v := range vL.([]interface{}) { @@ -418,6 +431,15 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("datacenter", dc) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) + + if mvm.Config && len(mvm.Config.ExtraConfig) > 0 { + custom_configs := make(map[string]string) + for _, v := range mvm.Config.ExtraConfig { + value := v.GetOptionValue() + custom_configs[value.Key] = value.Value + } + d.Set("custom_configuration_parameters", custom_configs) + } d.Set("datastore", rootDatastore) // Initialize the connection info @@ -802,6 +824,22 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error { } log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) + // make ExtraConfig + if len(vm.customConfigurations) > 0 { + var ov []types.BaseOptionValue + for k, v := range vm.customConfigurations { + key := k + value := v + o := types.OptionValue{ + Key: key, + Value: &value, + } + ov = append(ov, &o) + } + configSpec.ExtraConfig = ov + log.Printf("[DEBUG] virtual machine Extra Config spec: %v", configSpec.ExtraConfig) + } + var datastore *object.Datastore if vm.datastore == "" { datastore, err = finder.DefaultDatastore(context.TODO()) @@ -1003,6 +1041,22 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error { } log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) + // make ExtraConfig + if len(vm.customConfigurations) > 0 { + var ov []types.BaseOptionValue + for k, v := range vm.customConfigurations { + key := k + value := v + o := types.OptionValue{ + Key: key, + Value: &value, + } + ov = append(ov, &o) + } + configSpec.ExtraConfig = ov + log.Printf("[DEBUG] virtual machine Extra Config spec: %v", configSpec.ExtraConfig) + } + // create CustomizationSpec customSpec := types.CustomizationSpec{ Identity: &types.CustomizationLinuxPrep{ From bc36ba7f3c9aac7d56e306a8e4cfc9d9a14652b4 Mon Sep 17 00:00:00 2001 From: Chris Love Date: Sun, 8 Nov 2015 18:21:17 -0700 Subject: [PATCH 12/25] fixing if and AnyTypes --- builtin/providers/vsphere/resource_vsphere_virtual_machine.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index 07d84367a..cf636e79d 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -432,8 +432,8 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) - if mvm.Config && len(mvm.Config.ExtraConfig) > 0 { - custom_configs := make(map[string]string) + if len(mvm.Config.ExtraConfig) > 0 { + custom_configs := make(map[string]types.AnyType) for _, v := range mvm.Config.ExtraConfig { value := v.GetOptionValue() custom_configs[value.Key] = value.Value From 728b2bed636630d233e8f79f290fda2c01227655 Mon Sep 17 00:00:00 2001 From: Chris Love Date: Sun, 8 Nov 2015 18:49:49 -0700 Subject: [PATCH 13/25] adding new functional test --- .../resource_vsphere_virtual_machine_test.go | 83 ++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go index 66d6ea44f..2cae45fe4 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go @@ -127,6 +127,67 @@ func TestAccVSphereVirtualMachine_dhcp(t *testing.T) { }) } +func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { + var vm virtualMachine + var locationOpt string + var datastoreOpt string + + if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { + locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) + } + if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { + locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) + } + if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { + locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) + } + if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { + datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) + } + template := os.Getenv("VSPHERE_TEMPLATE") + label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: fmt.Sprintf( + testAccCheckVSphereVirtualMachineConfig_custom_configs, + locationOpt, + label, + datastoreOpt, + template, + ), + Check: resource.ComposeTestCheckFunc( + testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "name", "terraform-test"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "vcpu", "2"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "memory", "4096"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "disk.#", "1"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "disk.0.template", template), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "network_interface.#", "1"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "custom_configuration_parameters.foo", "bar"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "custom_configuration_parameters.car", "ferrai"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.car", "custom_configuration_parameters.num", "42"), + resource.TestCheckResourceAttr( + "vsphere_virtual_machine.bar", "network_interface.0.label", label), + ), + }, + }, + }) +} + func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error { client := testAccProvider.Meta().(*govmomi.Client) finder := find.NewFinder(client.Client, true) @@ -212,7 +273,6 @@ resource "vsphere_virtual_machine" "foo" { } } ` - const testAccCheckVSphereVirtualMachineConfig_dhcp = ` resource "vsphere_virtual_machine" "bar" { name = "terraform-test" @@ -228,3 +288,24 @@ resource "vsphere_virtual_machine" "bar" { } } ` + +const testAccCheckVSphereVirtualMachineConfig_custom_configs = ` +resource "vsphere_virtual_machine" "car" { + name = "terraform-test-custom" +%s + vcpu = 2 + memory = 4096 + network_interface { + label = "%s" + } + custom_configuration_parameters { + foo = "bar", + car = "ferrai", + num = 42 + } + disk { +%s + template = "%s" + } +} +` From 0f46b3a6c569bd85bdcfb9f02ec151a4c8ecf72f Mon Sep 17 00:00:00 2001 From: Chris Love Date: Mon, 9 Nov 2015 04:50:09 +0000 Subject: [PATCH 14/25] working on read and more testing --- .../resource_vsphere_virtual_machine.go | 23 ++++++++++++++++--- .../resource_vsphere_virtual_machine_test.go | 12 +++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index cf636e79d..338c95301 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -269,8 +269,13 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{ } if vL, ok := d.GetOk("custom_configuration_parameters"); ok { - if custom_configs, ok := vL.(map[string]types.AnyType); ok { - vm.customConfigurations = custom_configs + if custom_configs, ok := vL.(map[string]interface{}); ok { + custom := make(map[string]types.AnyType) + for k,v := range custom_configs { + custom[k] = v + } + vm.customConfigurations = custom + log.Printf("[DEBUG] custom_configuration_parameters init: %v", vm.customConfigurations) } } @@ -432,14 +437,21 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) - if len(mvm.Config.ExtraConfig) > 0 { + log.Printf("[DEBUG] ===============================") + //log.Printf("[DEBUG] Get extra config ===============================") + //log.Printf("[DEBUG] Get extra config %v", mvm.Config) + //log.Printf("[DEBUG] Get extra config %v", mvm.Config.ExtraConfig) + if mvm.Config != nil && mvm.Config.ExtraConfig != nil && len(mvm.Config.ExtraConfig) > 0 { + log.Printf("[DEBUG] reading custom configs") custom_configs := make(map[string]types.AnyType) for _, v := range mvm.Config.ExtraConfig { value := v.GetOptionValue() custom_configs[value.Key] = value.Value + log.Printf("[DEBUG] reading custom configs %s,%s",value.Key, value.Value) } d.Set("custom_configuration_parameters", custom_configs) } + log.Printf("[DEBUG] ===============================") d.Set("datastore", rootDatastore) // Initialize the connection info @@ -825,6 +837,7 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error { log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) // make ExtraConfig + log.Printf("[DEBUG] virtual machine Extra Config spec start") if len(vm.customConfigurations) > 0 { var ov []types.BaseOptionValue for k, v := range vm.customConfigurations { @@ -834,6 +847,7 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error { Key: key, Value: &value, } + log.Printf("[DEBUG] virtual machine Extra Config spec: %s,%s", k,v) ov = append(ov, &o) } configSpec.ExtraConfig = ov @@ -1041,6 +1055,8 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error { } log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) + log.Printf("[DEBUG] starting extra custom config spec: %v", vm.customConfigurations) + // make ExtraConfig if len(vm.customConfigurations) > 0 { var ov []types.BaseOptionValue @@ -1149,5 +1165,6 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error { return err } } + log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) return nil } diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go index 2cae45fe4..804e1ae07 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go @@ -161,9 +161,9 @@ func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { template, ), Check: resource.ComposeTestCheckFunc( - testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm), + testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.car", &vm), resource.TestCheckResourceAttr( - "vsphere_virtual_machine.car", "name", "terraform-test"), + "vsphere_virtual_machine.car", "name", "terraform-test-custom"), resource.TestCheckResourceAttr( "vsphere_virtual_machine.car", "vcpu", "2"), resource.TestCheckResourceAttr( @@ -181,7 +181,7 @@ func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { resource.TestCheckResourceAttr( "vsphere_virtual_machine.car", "custom_configuration_parameters.num", "42"), resource.TestCheckResourceAttr( - "vsphere_virtual_machine.bar", "network_interface.0.label", label), + "vsphere_virtual_machine.car", "network_interface.0.label", label), ), }, }, @@ -299,9 +299,9 @@ resource "vsphere_virtual_machine" "car" { label = "%s" } custom_configuration_parameters { - foo = "bar", - car = "ferrai", - num = 42 + "foo" = "bar" + "car" = "ferrai" + "num" = 42 } disk { %s From b47d1cda7c8da6cf8ad0cb69148550dcb17db37e Mon Sep 17 00:00:00 2001 From: Chris Love Date: Sun, 8 Nov 2015 18:02:07 -0700 Subject: [PATCH 15/25] adding capability to set custom configuration value in virtual machines --- .../resource_vsphere_virtual_machine.go | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index 338c95301..dbaa3f884 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -269,13 +269,8 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{ } if vL, ok := d.GetOk("custom_configuration_parameters"); ok { - if custom_configs, ok := vL.(map[string]interface{}); ok { - custom := make(map[string]types.AnyType) - for k,v := range custom_configs { - custom[k] = v - } - vm.customConfigurations = custom - log.Printf("[DEBUG] custom_configuration_parameters init: %v", vm.customConfigurations) + if custom_configs, ok := vL.(map[string]types.AnyType); ok { + vm.customConfigurations = custom_configs } } @@ -437,21 +432,14 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) - log.Printf("[DEBUG] ===============================") - //log.Printf("[DEBUG] Get extra config ===============================") - //log.Printf("[DEBUG] Get extra config %v", mvm.Config) - //log.Printf("[DEBUG] Get extra config %v", mvm.Config.ExtraConfig) - if mvm.Config != nil && mvm.Config.ExtraConfig != nil && len(mvm.Config.ExtraConfig) > 0 { - log.Printf("[DEBUG] reading custom configs") - custom_configs := make(map[string]types.AnyType) + if mvm.Config && len(mvm.Config.ExtraConfig) > 0 { + custom_configs := make(map[string]string) for _, v := range mvm.Config.ExtraConfig { value := v.GetOptionValue() custom_configs[value.Key] = value.Value - log.Printf("[DEBUG] reading custom configs %s,%s",value.Key, value.Value) } d.Set("custom_configuration_parameters", custom_configs) } - log.Printf("[DEBUG] ===============================") d.Set("datastore", rootDatastore) // Initialize the connection info @@ -837,6 +825,21 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error { log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) // make ExtraConfig + if len(vm.customConfigurations) > 0 { + var ov []types.BaseOptionValue + for k, v := range vm.customConfigurations { + key := k + value := v + o := types.OptionValue{ + Key: key, + Value: &value, + } + ov = append(ov, &o) + } + configSpec.ExtraConfig = ov + log.Printf("[DEBUG] virtual machine Extra Config spec: %v", configSpec.ExtraConfig) + } + log.Printf("[DEBUG] virtual machine Extra Config spec start") if len(vm.customConfigurations) > 0 { var ov []types.BaseOptionValue From cae7fd8e4ad004df1714d1d0bb93d36917926d2c Mon Sep 17 00:00:00 2001 From: Chris Love Date: Sun, 8 Nov 2015 18:21:17 -0700 Subject: [PATCH 16/25] fixing if and AnyTypes --- builtin/providers/vsphere/resource_vsphere_virtual_machine.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index dbaa3f884..468d3cc06 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -432,8 +432,8 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) - if mvm.Config && len(mvm.Config.ExtraConfig) > 0 { - custom_configs := make(map[string]string) + if len(mvm.Config.ExtraConfig) > 0 { + custom_configs := make(map[string]types.AnyType) for _, v := range mvm.Config.ExtraConfig { value := v.GetOptionValue() custom_configs[value.Key] = value.Value From 8c47441a8bcc8ca73a3362fb4d4a316323dd8c5f Mon Sep 17 00:00:00 2001 From: Chris Love Date: Sun, 8 Nov 2015 18:49:49 -0700 Subject: [PATCH 17/25] adding new functional test --- .../resource_vsphere_virtual_machine_test.go | 64 ++----------------- 1 file changed, 6 insertions(+), 58 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go index 804e1ae07..a1bd468fe 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go @@ -127,43 +127,9 @@ func TestAccVSphereVirtualMachine_dhcp(t *testing.T) { }) } -func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { - var vm virtualMachine - var locationOpt string - var datastoreOpt string - - if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { - locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) - } - if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { - locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) - } - if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { - locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) - } - if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { - datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) - } - template := os.Getenv("VSPHERE_TEMPLATE") - label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP") - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, - Steps: []resource.TestStep{ - resource.TestStep{ - Config: fmt.Sprintf( - testAccCheckVSphereVirtualMachineConfig_custom_configs, - locationOpt, - label, - datastoreOpt, - template, - ), - Check: resource.ComposeTestCheckFunc( - testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.car", &vm), + testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm), resource.TestCheckResourceAttr( - "vsphere_virtual_machine.car", "name", "terraform-test-custom"), + "vsphere_virtual_machine.car", "name", "terraform-test"), resource.TestCheckResourceAttr( "vsphere_virtual_machine.car", "vcpu", "2"), resource.TestCheckResourceAttr( @@ -181,7 +147,7 @@ func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { resource.TestCheckResourceAttr( "vsphere_virtual_machine.car", "custom_configuration_parameters.num", "42"), resource.TestCheckResourceAttr( - "vsphere_virtual_machine.car", "network_interface.0.label", label), + "vsphere_virtual_machine.bar", "network_interface.0.label", label), ), }, }, @@ -288,24 +254,6 @@ resource "vsphere_virtual_machine" "bar" { } } ` - -const testAccCheckVSphereVirtualMachineConfig_custom_configs = ` -resource "vsphere_virtual_machine" "car" { - name = "terraform-test-custom" -%s - vcpu = 2 - memory = 4096 - network_interface { - label = "%s" - } - custom_configuration_parameters { - "foo" = "bar" - "car" = "ferrai" - "num" = 42 - } - disk { -%s - template = "%s" - } -} -` + foo = "bar", + car = "ferrai", + num = 42 From 6615285d63bccf3ced0947bc38a4b3567082cac6 Mon Sep 17 00:00:00 2001 From: Chris Love Date: Mon, 9 Nov 2015 04:50:09 +0000 Subject: [PATCH 18/25] working on read and more testing --- .../resource_vsphere_virtual_machine.go | 20 ++++++++++++++++--- .../resource_vsphere_virtual_machine_test.go | 19 ++++++++++++------ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index 468d3cc06..ea17dc09b 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -269,8 +269,13 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{ } if vL, ok := d.GetOk("custom_configuration_parameters"); ok { - if custom_configs, ok := vL.(map[string]types.AnyType); ok { - vm.customConfigurations = custom_configs + if custom_configs, ok := vL.(map[string]interface{}); ok { + custom := make(map[string]types.AnyType) + for k,v := range custom_configs { + custom[k] = v + } + vm.customConfigurations = custom + log.Printf("[DEBUG] custom_configuration_parameters init: %v", vm.customConfigurations) } } @@ -432,14 +437,21 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) - if len(mvm.Config.ExtraConfig) > 0 { + log.Printf("[DEBUG] ===============================") + //log.Printf("[DEBUG] Get extra config ===============================") + //log.Printf("[DEBUG] Get extra config %v", mvm.Config) + //log.Printf("[DEBUG] Get extra config %v", mvm.Config.ExtraConfig) + if mvm.Config != nil && mvm.Config.ExtraConfig != nil && len(mvm.Config.ExtraConfig) > 0 { + log.Printf("[DEBUG] reading custom configs") custom_configs := make(map[string]types.AnyType) for _, v := range mvm.Config.ExtraConfig { value := v.GetOptionValue() custom_configs[value.Key] = value.Value + log.Printf("[DEBUG] reading custom configs %s,%s",value.Key, value.Value) } d.Set("custom_configuration_parameters", custom_configs) } + log.Printf("[DEBUG] ===============================") d.Set("datastore", rootDatastore) // Initialize the connection info @@ -825,6 +837,7 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error { log.Printf("[DEBUG] virtual machine config spec: %v", configSpec) // make ExtraConfig + log.Printf("[DEBUG] virtual machine Extra Config spec start") if len(vm.customConfigurations) > 0 { var ov []types.BaseOptionValue for k, v := range vm.customConfigurations { @@ -834,6 +847,7 @@ func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error { Key: key, Value: &value, } + log.Printf("[DEBUG] virtual machine Extra Config spec: %s,%s", k,v) ov = append(ov, &o) } configSpec.ExtraConfig = ov diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go index a1bd468fe..9b0b29cae 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go @@ -127,9 +127,9 @@ func TestAccVSphereVirtualMachine_dhcp(t *testing.T) { }) } - testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm), + testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.car", &vm), resource.TestCheckResourceAttr( - "vsphere_virtual_machine.car", "name", "terraform-test"), + "vsphere_virtual_machine.car", "name", "terraform-test-custom"), resource.TestCheckResourceAttr( "vsphere_virtual_machine.car", "vcpu", "2"), resource.TestCheckResourceAttr( @@ -147,7 +147,7 @@ func TestAccVSphereVirtualMachine_dhcp(t *testing.T) { resource.TestCheckResourceAttr( "vsphere_virtual_machine.car", "custom_configuration_parameters.num", "42"), resource.TestCheckResourceAttr( - "vsphere_virtual_machine.bar", "network_interface.0.label", label), + "vsphere_virtual_machine.car", "network_interface.0.label", label), ), }, }, @@ -254,6 +254,13 @@ resource "vsphere_virtual_machine" "bar" { } } ` - foo = "bar", - car = "ferrai", - num = 42 + "foo" = "bar" + "car" = "ferrai" + "num" = 42 + } + disk { +%s + template = "%s" + } +} +` From ce6f0ae5e4eaef94ee1b3449d5f7614a1c21633d Mon Sep 17 00:00:00 2001 From: Chris Love Date: Wed, 11 Nov 2015 22:42:36 +0000 Subject: [PATCH 19/25] testing finished --- .../resource_vsphere_virtual_machine.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index ea17dc09b..b11021e63 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -381,7 +381,7 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) var mvm mo.VirtualMachine collector := property.DefaultCollector(client.Client) - if err := collector.RetrieveOne(context.TODO(), vm.Reference(), []string{"guest", "summary", "datastore"}, &mvm); err != nil { + if err := collector.RetrieveOne(context.TODO(), vm.Reference(), []string{"guest", "summary", "datastore","config.extraConfig"}, &mvm); err != nil { return err } @@ -437,21 +437,18 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) - log.Printf("[DEBUG] ===============================") - //log.Printf("[DEBUG] Get extra config ===============================") - //log.Printf("[DEBUG] Get extra config %v", mvm.Config) - //log.Printf("[DEBUG] Get extra config %v", mvm.Config.ExtraConfig) if mvm.Config != nil && mvm.Config.ExtraConfig != nil && len(mvm.Config.ExtraConfig) > 0 { - log.Printf("[DEBUG] reading custom configs") - custom_configs := make(map[string]types.AnyType) + //TODO: can only set specific custom value, not everything + //Would need the config here + //custom_configs := make(map[string]types.AnyType) for _, v := range mvm.Config.ExtraConfig { value := v.GetOptionValue() - custom_configs[value.Key] = value.Value - log.Printf("[DEBUG] reading custom configs %s,%s",value.Key, value.Value) + //custom_configs[value.Key] = value.Value + log.Printf("[DEBUG] custom configs %s,%s",value.Key, value.Value) } - d.Set("custom_configuration_parameters", custom_configs) + //d.Set("custom_configuration_parameters", custom_configs) } - log.Printf("[DEBUG] ===============================") + d.Set("datastore", rootDatastore) // Initialize the connection info From 6d13b9296b759cd34e89fa67dae661d86fbd4f9a Mon Sep 17 00:00:00 2001 From: Chris Love Date: Wed, 11 Nov 2015 22:50:18 +0000 Subject: [PATCH 20/25] updating documentation --- .../docs/providers/vsphere/r/virtual_machine.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown b/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown index d008357ec..4062a212b 100644 --- a/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown +++ b/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown @@ -48,6 +48,7 @@ The following arguments are supported: * `network_interface` - (Required) Configures virtual network interfaces; see [Network Interfaces](#network-interfaces) below for details. * `disk` - (Required) Configures virtual disks; see [Disks](#disks) below for details * `boot_delay` - (Optional) Time in seconds to wait for machine network to be ready. +* `custom_configuration_parameters` - (Optional) Map of values that is set as virtual machine custom configurations. ## Network Interfaces From 45fe850331e3ef47fc1358e309119ec1f5386c2d Mon Sep 17 00:00:00 2001 From: Chris Love Date: Tue, 17 Nov 2015 21:19:20 +0000 Subject: [PATCH 21/25] trying to remove changes --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff6d0ab1d..a34eadb58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,7 +51,6 @@ IMPROVEMENTS: * provider/digitalocean: Make user_data force a new droplet [GH-3740] * provider/vsphere: Do not add network interfaces by default [GH-3652] * provider/openstack: Configure Fixed IPs through ports [GH-3772] - * provider/openstack: Specify a port ID on a Router Interface [GH-3903] BUG FIXES: From a5690b751007d06633c4105a0d26015e8adc72f2 Mon Sep 17 00:00:00 2001 From: Chris Love Date: Tue, 17 Nov 2015 22:00:46 +0000 Subject: [PATCH 22/25] removing debug print statements --- .../vsphere/resource_vsphere_virtual_machine.go | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index 274a2278d..f4fd79c90 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -381,7 +381,7 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) var mvm mo.VirtualMachine collector := property.DefaultCollector(client.Client) - if err := collector.RetrieveOne(context.TODO(), vm.Reference(), []string{"guest", "summary", "datastore","config.extraConfig"}, &mvm); err != nil { + if err := collector.RetrieveOne(context.TODO(), vm.Reference(), []string{"guest", "summary", "datastore"}, &mvm); err != nil { return err } @@ -436,19 +436,6 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) d.Set("datacenter", dc) d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("cpu", mvm.Summary.Config.NumCpu) - - if mvm.Config != nil && mvm.Config.ExtraConfig != nil && len(mvm.Config.ExtraConfig) > 0 { - //TODO: can only set specific custom value, not everything - //Would need the config here - //custom_configs := make(map[string]types.AnyType) - for _, v := range mvm.Config.ExtraConfig { - value := v.GetOptionValue() - //custom_configs[value.Key] = value.Value - log.Printf("[DEBUG] custom configs %s,%s",value.Key, value.Value) - } - //d.Set("custom_configuration_parameters", custom_configs) - } - d.Set("datastore", rootDatastore) // Initialize the connection info From a35b65e5d2c319296261695b19ff92985e525059 Mon Sep 17 00:00:00 2001 From: Chris Love Date: Tue, 24 Nov 2015 21:35:40 -0700 Subject: [PATCH 23/25] working on better test --- .../resource_vsphere_virtual_machine_test.go | 90 ++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go index 804e1ae07..4051a2985 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go @@ -10,6 +10,9 @@ import ( "github.com/vmware/govmomi" "github.com/vmware/govmomi/find" "github.com/vmware/govmomi/object" + "github.com/vmware/govmomi/property" + "github.com/vmware/govmomi/vim25/mo" + "github.com/vmware/govmomi/vim25/types" "golang.org/x/net/context" ) @@ -216,8 +219,90 @@ func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error { return nil } +func testAccCheckVSphereVirtualMachineExistsHasExtraConfig(n string, vm *virtualMachine) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + client := testAccProvider.Meta().(*govmomi.Client) + finder := find.NewFinder(client.Client, true) + + dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"]) + if err != nil { + return fmt.Errorf("error %s", err) + } + + dcFolders, err := dc.Folders(context.TODO()) + if err != nil { + return fmt.Errorf("error %s", err) + } + + _, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"]) + + instance, err := finder.VirtualMachine(context.TODO(), rs.Primary.Attributes["name"]) + if err != nil { + return fmt.Errorf("error %s", err) + } + + var mvm mo.VirtualMachine + + collector := property.DefaultCollector(client.Client) + + if err := collector.RetrieveOne(context.TODO(), instance.Reference(), []string{"config.extraConfig"}, &mvm); err != nil { + return fmt.Errorf("error %s", err) + } + + var configMap = make(map[string]types.AnyType) + if mvm.Config != nil && mvm.Config.ExtraConfig != nil && len(mvm.Config.ExtraConfig) > 0 { + for _, v := range mvm.Config.ExtraConfig { + value := v.GetOptionValue() + configMap[value.Key] = value.Value + } + } else { + return fmt.Errorf("error no ExtraConfig") + } + + if configMap["foo"] == nil { + return fmt.Errorf("error no ExtraConfig for 'foo'") + } + + if configMap["foo"] != "bar" { + return fmt.Errorf("error ExtraConfig 'foo' != bar") + } + + if configMap["car"] == nil { + return fmt.Errorf("error no ExtraConfig for 'car'") + } + + if configMap["car"] != "ferrari" { + return fmt.Errorf("error ExtraConfig 'car' != ferrari") + } + + if configMap["car"] == nil { + return fmt.Errorf("error no ExtraConfig for 'car'") + } + + if configMap["car"] != "ferrari" { + return fmt.Errorf("error ExtraConfig 'car' != ferrari") + } + *vm = virtualMachine{ + name: rs.Primary.ID, + } + + return nil + } +} func testAccCheckVSphereVirtualMachineExists(n string, vm *virtualMachine) resource.TestCheckFunc { return func(s *terraform.State) error { + // todo how do I return this?? + //test1 := testAccCheckVSphereVirtualMachineExists(n, vm) + rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) @@ -247,6 +332,7 @@ func testAccCheckVSphereVirtualMachineExists(n string, vm *virtualMachine) resou } return nil + } } @@ -300,8 +386,8 @@ resource "vsphere_virtual_machine" "car" { } custom_configuration_parameters { "foo" = "bar" - "car" = "ferrai" - "num" = 42 + "car" = "ferrai" + "num" = 42 } disk { %s From 1d1de992af1e3b678c98ad638fba42d0f7196d31 Mon Sep 17 00:00:00 2001 From: Chris Love Date: Wed, 25 Nov 2015 05:41:01 +0000 Subject: [PATCH 24/25] adding better acceptance test to check custom config --- .../resource_vsphere_virtual_machine_test.go | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go index 4051a2985..f905a2fa6 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go @@ -164,7 +164,7 @@ func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { template, ), Check: resource.ComposeTestCheckFunc( - testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.car", &vm), + testAccCheckVSphereVirtualMachineExistsHasCustomConfig("vsphere_virtual_machine.car", &vm), resource.TestCheckResourceAttr( "vsphere_virtual_machine.car", "name", "terraform-test-custom"), resource.TestCheckResourceAttr( @@ -180,7 +180,7 @@ func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { resource.TestCheckResourceAttr( "vsphere_virtual_machine.car", "custom_configuration_parameters.foo", "bar"), resource.TestCheckResourceAttr( - "vsphere_virtual_machine.car", "custom_configuration_parameters.car", "ferrai"), + "vsphere_virtual_machine.car", "custom_configuration_parameters.car", "ferrari"), resource.TestCheckResourceAttr( "vsphere_virtual_machine.car", "custom_configuration_parameters.num", "42"), resource.TestCheckResourceAttr( @@ -219,8 +219,10 @@ func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error { return nil } -func testAccCheckVSphereVirtualMachineExistsHasExtraConfig(n string, vm *virtualMachine) resource.TestCheckFunc { +func testAccCheckVSphereVirtualMachineExistsHasCustomConfig(n string, vm *virtualMachine) resource.TestCheckFunc { return func(s *terraform.State) error { + + rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) @@ -243,8 +245,13 @@ func testAccCheckVSphereVirtualMachineExistsHasExtraConfig(n string, vm *virtual return fmt.Errorf("error %s", err) } - _, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"]) + _, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"]) + if err != nil { + return fmt.Errorf("error %s", err) + } + + finder = finder.SetDatacenter(dc) instance, err := finder.VirtualMachine(context.TODO(), rs.Primary.Attributes["name"]) if err != nil { return fmt.Errorf("error %s", err) @@ -284,12 +291,13 @@ func testAccCheckVSphereVirtualMachineExistsHasExtraConfig(n string, vm *virtual return fmt.Errorf("error ExtraConfig 'car' != ferrari") } - if configMap["car"] == nil { - return fmt.Errorf("error no ExtraConfig for 'car'") + if configMap["num"] == nil { + return fmt.Errorf("error no ExtraConfig for 'num'") } - if configMap["car"] != "ferrari" { - return fmt.Errorf("error ExtraConfig 'car' != ferrari") + // todo this should be an int, getting back a string + if configMap["num"] != "42" { + return fmt.Errorf("error ExtraConfig 'num' != 42") } *vm = virtualMachine{ name: rs.Primary.ID, @@ -386,8 +394,8 @@ resource "vsphere_virtual_machine" "car" { } custom_configuration_parameters { "foo" = "bar" - "car" = "ferrai" - "num" = 42 + "car" = "ferrari" + "num" = 42 } disk { %s From de2c76a61c9c4ba8ec8e1f766c02556c85afdb4d Mon Sep 17 00:00:00 2001 From: chrislovecnm Date: Wed, 25 Nov 2015 00:48:44 -0700 Subject: [PATCH 25/25] polish --- .../vsphere/resource_vsphere_virtual_machine_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go index f905a2fa6..130523a47 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go @@ -308,8 +308,6 @@ func testAccCheckVSphereVirtualMachineExistsHasCustomConfig(n string, vm *virtua } func testAccCheckVSphereVirtualMachineExists(n string, vm *virtualMachine) resource.TestCheckFunc { return func(s *terraform.State) error { - // todo how do I return this?? - //test1 := testAccCheckVSphereVirtualMachineExists(n, vm) rs, ok := s.RootModule().Resources[n] if !ok { @@ -393,7 +391,7 @@ resource "vsphere_virtual_machine" "car" { label = "%s" } custom_configuration_parameters { - "foo" = "bar" + "foo" = "bar" "car" = "ferrari" "num" = 42 }