From 725a735c0d2c398bc7432a3adfbeadd76df55c4a Mon Sep 17 00:00:00 2001 From: Chris Love Date: Sun, 8 Nov 2015 18:02:07 -0700 Subject: [PATCH 01/10] 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/10] 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/10] 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/10] 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/10] 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/10] 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/10] 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/10] 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/10] 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/10] 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