adding capability to set custom configuration value in virtual machines

This commit is contained in:
Chris Love 2015-11-08 18:02:07 -07:00
parent 536ba76b21
commit e899a2949f
1 changed files with 69 additions and 15 deletions

View File

@ -41,21 +41,22 @@ type hardDisk struct {
} }
type virtualMachine struct { type virtualMachine struct {
name string name string
datacenter string datacenter string
cluster string cluster string
resourcePool string resourcePool string
datastore string datastore string
vcpu int vcpu int
memoryMb int64 memoryMb int64
template string template string
networkInterfaces []networkInterface networkInterfaces []networkInterface
hardDisks []hardDisk hardDisks []hardDisk
gateway string gateway string
domain string domain string
timeZone string timeZone string
dnsSuffixes []string dnsSuffixes []string
dnsServers []string dnsServers []string
customConfigurations map[string](types.AnyType)
} }
func resourceVSphereVirtualMachine() *schema.Resource { func resourceVSphereVirtualMachine() *schema.Resource {
@ -135,6 +136,12 @@ func resourceVSphereVirtualMachine() *schema.Resource {
ForceNew: true, ForceNew: true,
}, },
"custom_configuration_parameters": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
"network_interface": &schema.Schema{ "network_interface": &schema.Schema{
Type: schema.TypeList, Type: schema.TypeList,
Required: true, Required: true,
@ -261,6 +268,12 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{
vm.dnsServers = DefaultDNSServers 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 { if vL, ok := d.GetOk("network_interface"); ok {
networks := make([]networkInterface, len(vL.([]interface{}))) networks := make([]networkInterface, len(vL.([]interface{})))
for i, v := range 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("datacenter", dc)
d.Set("memory", mvm.Summary.Config.MemorySizeMB) d.Set("memory", mvm.Summary.Config.MemorySizeMB)
d.Set("cpu", mvm.Summary.Config.NumCpu) 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) d.Set("datastore", rootDatastore)
// Initialize the connection info // 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) 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 var datastore *object.Datastore
if vm.datastore == "" { if vm.datastore == "" {
datastore, err = finder.DefaultDatastore(context.TODO()) 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) 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 // create CustomizationSpec
customSpec := types.CustomizationSpec{ customSpec := types.CustomizationSpec{
Identity: &types.CustomizationLinuxPrep{ Identity: &types.CustomizationLinuxPrep{