2015-10-06 05:53:07 +02:00
|
|
|
package vsphere
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
2016-03-10 22:12:33 +01:00
|
|
|
"strconv"
|
2015-10-06 05:53:07 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var DefaultDNSSuffixes = []string{
|
|
|
|
"vsphere.local",
|
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultDNSServers = []string{
|
|
|
|
"8.8.8.8",
|
|
|
|
"8.8.4.4",
|
|
|
|
}
|
|
|
|
|
2016-07-12 10:02:41 +02:00
|
|
|
var DiskControllerTypes = []string{
|
|
|
|
"scsi",
|
|
|
|
"scsi-lsi-parallel",
|
|
|
|
"scsi-buslogic",
|
|
|
|
"scsi-paravirtual",
|
|
|
|
"scsi-lsi-sas",
|
|
|
|
"ide",
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
type networkInterface struct {
|
2015-11-24 01:53:11 +01:00
|
|
|
deviceName string
|
|
|
|
label string
|
|
|
|
ipv4Address string
|
|
|
|
ipv4PrefixLength int
|
2016-05-03 18:27:24 +02:00
|
|
|
ipv4Gateway string
|
2015-11-24 01:53:11 +01:00
|
|
|
ipv6Address string
|
|
|
|
ipv6PrefixLength int
|
2016-05-03 18:27:24 +02:00
|
|
|
ipv6Gateway string
|
2015-11-24 01:53:11 +01:00
|
|
|
adapterType string // TODO: Make "adapter_type" argument
|
2016-06-02 01:38:56 +02:00
|
|
|
macAddress string
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type hardDisk struct {
|
2016-05-23 23:37:06 +02:00
|
|
|
name string
|
2016-05-21 08:21:42 +02:00
|
|
|
size int64
|
|
|
|
iops int64
|
|
|
|
initType string
|
|
|
|
vmdkPath string
|
|
|
|
controller string
|
2016-05-23 23:37:06 +02:00
|
|
|
bootable bool
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
|
2016-03-23 21:46:33 +01:00
|
|
|
//Additional options Vsphere can use clones of windows machines
|
|
|
|
type windowsOptConfig struct {
|
|
|
|
productKey string
|
|
|
|
adminPassword string
|
|
|
|
domainUser string
|
|
|
|
domain string
|
|
|
|
domainUserPassword string
|
|
|
|
}
|
|
|
|
|
2015-11-25 15:35:37 +01:00
|
|
|
type cdrom struct {
|
|
|
|
datastore string
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2016-04-25 13:15:37 +02:00
|
|
|
type memoryAllocation struct {
|
|
|
|
reservation int64
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
type virtualMachine struct {
|
2016-03-23 21:46:33 +01:00
|
|
|
name string
|
|
|
|
folder string
|
|
|
|
datacenter string
|
|
|
|
cluster string
|
|
|
|
resourcePool string
|
|
|
|
datastore string
|
2016-05-13 16:27:45 +02:00
|
|
|
vcpu int32
|
2016-03-23 21:46:33 +01:00
|
|
|
memoryMb int64
|
2016-04-25 13:15:37 +02:00
|
|
|
memoryAllocation memoryAllocation
|
2016-03-23 21:46:33 +01:00
|
|
|
template string
|
|
|
|
networkInterfaces []networkInterface
|
|
|
|
hardDisks []hardDisk
|
2015-11-25 15:35:37 +01:00
|
|
|
cdroms []cdrom
|
2016-03-23 21:46:33 +01:00
|
|
|
domain string
|
|
|
|
timeZone string
|
|
|
|
dnsSuffixes []string
|
|
|
|
dnsServers []string
|
2016-05-23 23:37:06 +02:00
|
|
|
hasBootableVmdk bool
|
2016-04-08 14:06:22 +02:00
|
|
|
linkedClone bool
|
2016-04-21 17:53:08 +02:00
|
|
|
skipCustomization bool
|
2016-06-09 10:19:10 +02:00
|
|
|
enableDiskUUID bool
|
2016-03-23 21:46:33 +01:00
|
|
|
windowsOptionalConfig windowsOptConfig
|
|
|
|
customConfigurations map[string](types.AnyType)
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
|
2015-11-16 19:44:39 +01:00
|
|
|
func (v virtualMachine) Path() string {
|
|
|
|
return vmPath(v.folder, v.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func vmPath(folder string, name string) string {
|
|
|
|
var path string
|
|
|
|
if len(folder) > 0 {
|
|
|
|
path += folder + "/"
|
|
|
|
}
|
|
|
|
return path + name
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
func resourceVSphereVirtualMachine() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceVSphereVirtualMachineCreate,
|
|
|
|
Read: resourceVSphereVirtualMachineRead,
|
2016-05-03 17:58:33 +02:00
|
|
|
Update: resourceVSphereVirtualMachineUpdate,
|
2015-10-06 05:53:07 +02:00
|
|
|
Delete: resourceVSphereVirtualMachineDelete,
|
|
|
|
|
2016-06-07 17:11:35 +02:00
|
|
|
SchemaVersion: 1,
|
|
|
|
MigrateState: resourceVSphereVirtualMachineMigrateState,
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-11-16 19:44:39 +01:00
|
|
|
"folder": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
"vcpu": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"memory": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2016-04-25 13:15:37 +02:00
|
|
|
"memory_reservation": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 0,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
"datacenter": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"cluster": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"resource_pool": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-13 18:42:55 +02:00
|
|
|
"linked_clone": &schema.Schema{
|
2016-02-24 16:39:24 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-10-06 05:53:07 +02:00
|
|
|
"gateway": &schema.Schema{
|
2016-05-03 18:27:24 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Deprecated: "Please use network_interface.ipv4_gateway",
|
2015-10-06 05:53:07 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"domain": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Default: "vsphere.local",
|
|
|
|
},
|
|
|
|
|
|
|
|
"time_zone": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Default: "Etc/UTC",
|
|
|
|
},
|
|
|
|
|
2015-10-12 06:06:29 +02:00
|
|
|
"dns_suffixes": &schema.Schema{
|
2015-10-06 05:53:07 +02:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-10-12 06:06:29 +02:00
|
|
|
"dns_servers": &schema.Schema{
|
2015-10-06 05:53:07 +02:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-21 17:53:08 +02:00
|
|
|
"skip_customization": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
|
|
|
|
2016-06-09 10:19:10 +02:00
|
|
|
"enable_disk_uuid": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
|
|
|
|
2016-06-29 22:45:59 +02:00
|
|
|
"uuid": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2015-11-09 02:02:07 +01:00
|
|
|
"custom_configuration_parameters": &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-06-09 10:19:10 +02:00
|
|
|
|
2016-03-23 21:46:33 +01:00
|
|
|
"windows_opt_config": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"product_key": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2016-06-29 20:37:04 +02:00
|
|
|
Optional: true,
|
2016-03-23 21:46:33 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"admin_password": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"domain_user": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"domain": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"domain_user_password": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-11-09 02:02:07 +01:00
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
"network_interface": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"label": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ip_address": &schema.Schema{
|
2015-11-24 01:53:11 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
Deprecated: "Please use ipv4_address",
|
|
|
|
},
|
|
|
|
|
|
|
|
"subnet_mask": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
Deprecated: "Please use ipv4_prefix_length",
|
|
|
|
},
|
|
|
|
|
|
|
|
"ipv4_address": &schema.Schema{
|
2015-10-06 05:53:07 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2015-11-24 01:53:11 +01:00
|
|
|
"ipv4_prefix_length": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
2015-10-06 05:53:07 +02:00
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
2015-11-24 01:53:11 +01:00
|
|
|
},
|
|
|
|
|
2016-05-03 18:27:24 +02:00
|
|
|
"ipv4_gateway": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-05-12 14:54:29 +02:00
|
|
|
Computed: true,
|
2016-05-03 18:27:24 +02:00
|
|
|
},
|
|
|
|
|
2015-11-24 01:53:11 +01:00
|
|
|
"ipv6_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2016-05-03 18:27:24 +02:00
|
|
|
Optional: true,
|
2016-05-05 16:38:45 +02:00
|
|
|
Computed: true,
|
2015-11-24 01:53:11 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"ipv6_prefix_length": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
2016-05-03 18:27:24 +02:00
|
|
|
Optional: true,
|
2016-05-05 16:38:45 +02:00
|
|
|
Computed: true,
|
2016-05-03 18:27:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"ipv6_gateway": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-05-12 14:54:29 +02:00
|
|
|
Computed: true,
|
2015-10-06 05:53:07 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"adapter_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-06-02 01:38:56 +02:00
|
|
|
|
|
|
|
"mac_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-10-06 05:53:07 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"disk": &schema.Schema{
|
2016-05-23 23:37:06 +02:00
|
|
|
Type: schema.TypeSet,
|
2015-10-06 05:53:07 +02:00
|
|
|
Required: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-05-23 23:37:06 +02:00
|
|
|
"uuid": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"key": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
"template": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2016-02-25 15:50:02 +01:00
|
|
|
"type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2015-12-12 09:33:01 +01:00
|
|
|
Optional: true,
|
|
|
|
Default: "eager_zeroed",
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(string)
|
2016-08-08 21:59:32 +02:00
|
|
|
if value != "thin" && value != "eager_zeroed" && value != "lazy" {
|
2015-12-12 09:33:01 +01:00
|
|
|
errors = append(errors, fmt.Errorf(
|
2016-08-08 21:59:32 +02:00
|
|
|
"only 'thin', 'eager_zeroed', and 'lazy' are supported values for 'type'"))
|
2015-12-12 09:33:01 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
"datastore": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"size": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2016-05-23 23:37:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-10-06 05:53:07 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"iops": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2016-04-27 18:22:22 +02:00
|
|
|
|
|
|
|
"vmdk": &schema.Schema{
|
|
|
|
// TODO: Add ValidateFunc to confirm path exists
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"bootable": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2016-05-23 23:37:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"keep_on_remove": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2016-04-27 18:22:22 +02:00
|
|
|
},
|
2016-05-21 08:21:42 +02:00
|
|
|
|
|
|
|
"controller_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "scsi",
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(string)
|
2016-07-12 10:02:41 +02:00
|
|
|
found := false
|
|
|
|
for _, t := range DiskControllerTypes {
|
|
|
|
if t == value {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2016-05-21 08:21:42 +02:00
|
|
|
errors = append(errors, fmt.Errorf(
|
2016-07-12 10:02:41 +02:00
|
|
|
"Supported values for 'controller_type' are %v", strings.Join(DiskControllerTypes, ", ")))
|
2016-05-21 08:21:42 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
2015-10-06 05:53:07 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-09-20 21:04:38 +02:00
|
|
|
"detach_unknown_disks_on_delete": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
|
|
|
|
2015-11-25 15:35:37 +01:00
|
|
|
"cdrom": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"datastore": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"path": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-10-06 05:53:07 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 17:58:33 +02:00
|
|
|
func resourceVSphereVirtualMachineUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
// flag if changes have to be applied
|
|
|
|
hasChanges := false
|
|
|
|
// flag if changes have to be done when powered off
|
|
|
|
rebootRequired := false
|
|
|
|
|
|
|
|
// make config spec
|
|
|
|
configSpec := types.VirtualMachineConfigSpec{}
|
|
|
|
|
|
|
|
if d.HasChange("vcpu") {
|
2016-05-13 16:27:45 +02:00
|
|
|
configSpec.NumCPUs = int32(d.Get("vcpu").(int))
|
2016-05-03 17:58:33 +02:00
|
|
|
hasChanges = true
|
|
|
|
rebootRequired = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("memory") {
|
|
|
|
configSpec.MemoryMB = int64(d.Get("memory").(int))
|
|
|
|
hasChanges = true
|
|
|
|
rebootRequired = true
|
|
|
|
}
|
|
|
|
|
|
|
|
client := meta.(*govmomi.Client)
|
|
|
|
dc, err := getDatacenter(client, d.Get("datacenter").(string))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
finder := find.NewFinder(client.Client, true)
|
|
|
|
finder = finder.SetDatacenter(dc)
|
|
|
|
|
|
|
|
vm, err := finder.VirtualMachine(context.TODO(), vmPath(d.Get("folder").(string), d.Get("name").(string)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-23 23:37:06 +02:00
|
|
|
|
|
|
|
if d.HasChange("disk") {
|
|
|
|
hasChanges = true
|
|
|
|
oldDisks, newDisks := d.GetChange("disk")
|
|
|
|
oldDiskSet := oldDisks.(*schema.Set)
|
|
|
|
newDiskSet := newDisks.(*schema.Set)
|
|
|
|
|
|
|
|
addedDisks := newDiskSet.Difference(oldDiskSet)
|
|
|
|
removedDisks := oldDiskSet.Difference(newDiskSet)
|
|
|
|
|
|
|
|
// Removed disks
|
|
|
|
for _, diskRaw := range removedDisks.List() {
|
|
|
|
if disk, ok := diskRaw.(map[string]interface{}); ok {
|
|
|
|
devices, err := vm.Device(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[ERROR] Update Remove Disk - Could not get virtual device list: %v", err)
|
|
|
|
}
|
|
|
|
virtualDisk := devices.FindByKey(int32(disk["key"].(int)))
|
|
|
|
|
|
|
|
keep := false
|
2016-07-11 16:48:34 +02:00
|
|
|
if v, ok := disk["keep_on_remove"].(bool); ok {
|
|
|
|
keep = v
|
2016-05-23 23:37:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = vm.RemoveDevice(context.TODO(), keep, virtualDisk)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[ERROR] Update Remove Disk - Error removing disk: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Added disks
|
|
|
|
for _, diskRaw := range addedDisks.List() {
|
|
|
|
if disk, ok := diskRaw.(map[string]interface{}); ok {
|
|
|
|
|
|
|
|
var datastore *object.Datastore
|
|
|
|
if disk["datastore"] == "" {
|
|
|
|
datastore, err = finder.DefaultDatastore(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[ERROR] Update Remove Disk - Error finding datastore: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
datastore, err = finder.Datastore(context.TODO(), disk["datastore"].(string))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] Couldn't find datastore %v. %s", disk["datastore"].(string), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var size int64
|
|
|
|
if disk["size"] == 0 {
|
|
|
|
size = 0
|
|
|
|
} else {
|
|
|
|
size = int64(disk["size"].(int))
|
|
|
|
}
|
|
|
|
iops := int64(disk["iops"].(int))
|
2016-06-29 20:47:56 +02:00
|
|
|
controller_type := disk["controller_type"].(string)
|
2016-05-23 23:37:06 +02:00
|
|
|
|
|
|
|
var mo mo.VirtualMachine
|
|
|
|
vm.Properties(context.TODO(), vm.Reference(), []string{"summary", "config"}, &mo)
|
|
|
|
|
|
|
|
var diskPath string
|
|
|
|
switch {
|
|
|
|
case disk["vmdk"] != "":
|
|
|
|
diskPath = disk["vmdk"].(string)
|
|
|
|
case disk["name"] != "":
|
|
|
|
snapshotFullDir := mo.Config.Files.SnapshotDirectory
|
|
|
|
split := strings.Split(snapshotFullDir, " ")
|
|
|
|
if len(split) != 2 {
|
|
|
|
return fmt.Errorf("[ERROR] createVirtualMachine - failed to split snapshot directory: %v", snapshotFullDir)
|
|
|
|
}
|
|
|
|
vmWorkingPath := split[1]
|
|
|
|
diskPath = vmWorkingPath + disk["name"].(string)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("[ERROR] resourceVSphereVirtualMachineUpdate - Neither vmdk path nor vmdk name was given")
|
|
|
|
}
|
|
|
|
|
2016-08-08 21:59:32 +02:00
|
|
|
var initType string
|
|
|
|
if disk["type"] != "" {
|
|
|
|
initType = disk["type"].(string)
|
|
|
|
} else {
|
|
|
|
initType = "thin"
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:37:06 +02:00
|
|
|
log.Printf("[INFO] Attaching disk: %v", diskPath)
|
2016-08-08 21:59:32 +02:00
|
|
|
err = addHardDisk(vm, size, iops, initType, datastore, diskPath, controller_type)
|
2016-05-23 23:37:06 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] Add Hard Disk Failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// do nothing if there are no changes
|
|
|
|
if !hasChanges {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-03 17:58:33 +02:00
|
|
|
log.Printf("[DEBUG] virtual machine config spec: %v", configSpec)
|
|
|
|
|
|
|
|
if rebootRequired {
|
|
|
|
log.Printf("[INFO] Shutting down virtual machine: %s", d.Id())
|
|
|
|
|
|
|
|
task, err := vm.PowerOff(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = task.Wait(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] Reconfiguring virtual machine: %s", d.Id())
|
|
|
|
|
|
|
|
task, err := vm.Reconfigure(context.TODO(), configSpec)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = task.Wait(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rebootRequired {
|
|
|
|
task, err = vm.PowerOn(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = task.Wait(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceVSphereVirtualMachineRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*govmomi.Client)
|
|
|
|
|
|
|
|
vm := virtualMachine{
|
|
|
|
name: d.Get("name").(string),
|
2016-05-13 16:27:45 +02:00
|
|
|
vcpu: int32(d.Get("vcpu").(int)),
|
2015-10-06 05:53:07 +02:00
|
|
|
memoryMb: int64(d.Get("memory").(int)),
|
2016-04-25 13:15:37 +02:00
|
|
|
memoryAllocation: memoryAllocation{
|
|
|
|
reservation: int64(d.Get("memory_reservation").(int)),
|
|
|
|
},
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
|
2015-11-16 19:44:39 +01:00
|
|
|
if v, ok := d.GetOk("folder"); ok {
|
|
|
|
vm.folder = v.(string)
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
if v, ok := d.GetOk("datacenter"); ok {
|
|
|
|
vm.datacenter = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("cluster"); ok {
|
|
|
|
vm.cluster = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("resource_pool"); ok {
|
|
|
|
vm.resourcePool = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("domain"); ok {
|
|
|
|
vm.domain = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("time_zone"); ok {
|
|
|
|
vm.timeZone = v.(string)
|
|
|
|
}
|
|
|
|
|
2016-04-13 18:42:55 +02:00
|
|
|
if v, ok := d.GetOk("linked_clone"); ok {
|
2016-02-24 16:39:24 +01:00
|
|
|
vm.linkedClone = v.(bool)
|
|
|
|
}
|
|
|
|
|
2016-04-21 17:53:08 +02:00
|
|
|
if v, ok := d.GetOk("skip_customization"); ok {
|
|
|
|
vm.skipCustomization = v.(bool)
|
|
|
|
}
|
|
|
|
|
2016-06-09 10:19:10 +02:00
|
|
|
if v, ok := d.GetOk("enable_disk_uuid"); ok {
|
|
|
|
vm.enableDiskUUID = v.(bool)
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:06:29 +02:00
|
|
|
if raw, ok := d.GetOk("dns_suffixes"); ok {
|
|
|
|
for _, v := range raw.([]interface{}) {
|
|
|
|
vm.dnsSuffixes = append(vm.dnsSuffixes, v.(string))
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vm.dnsSuffixes = DefaultDNSSuffixes
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:06:29 +02:00
|
|
|
if raw, ok := d.GetOk("dns_servers"); ok {
|
|
|
|
for _, v := range raw.([]interface{}) {
|
|
|
|
vm.dnsServers = append(vm.dnsServers, v.(string))
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vm.dnsServers = DefaultDNSServers
|
|
|
|
}
|
|
|
|
|
2015-11-09 02:02:07 +01:00
|
|
|
if vL, ok := d.GetOk("custom_configuration_parameters"); ok {
|
2015-11-09 05:50:09 +01:00
|
|
|
if custom_configs, ok := vL.(map[string]interface{}); ok {
|
|
|
|
custom := make(map[string]types.AnyType)
|
2015-12-03 14:59:16 +01:00
|
|
|
for k, v := range custom_configs {
|
2015-11-09 05:50:09 +01:00
|
|
|
custom[k] = v
|
|
|
|
}
|
|
|
|
vm.customConfigurations = custom
|
|
|
|
log.Printf("[DEBUG] custom_configuration_parameters init: %v", vm.customConfigurations)
|
2015-11-09 02:02:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:06:29 +02:00
|
|
|
if vL, ok := d.GetOk("network_interface"); ok {
|
|
|
|
networks := make([]networkInterface, len(vL.([]interface{})))
|
|
|
|
for i, v := range vL.([]interface{}) {
|
|
|
|
network := v.(map[string]interface{})
|
|
|
|
networks[i].label = network["label"].(string)
|
|
|
|
if v, ok := network["ip_address"].(string); ok && v != "" {
|
2015-11-24 01:53:11 +01:00
|
|
|
networks[i].ipv4Address = v
|
2015-10-12 06:06:29 +02:00
|
|
|
}
|
2016-05-03 18:27:24 +02:00
|
|
|
if v, ok := d.GetOk("gateway"); ok {
|
|
|
|
networks[i].ipv4Gateway = v.(string)
|
|
|
|
}
|
2015-10-12 06:06:29 +02:00
|
|
|
if v, ok := network["subnet_mask"].(string); ok && v != "" {
|
2015-11-24 01:53:11 +01:00
|
|
|
ip := net.ParseIP(v).To4()
|
|
|
|
if ip != nil {
|
|
|
|
mask := net.IPv4Mask(ip[0], ip[1], ip[2], ip[3])
|
|
|
|
pl, _ := mask.Size()
|
|
|
|
networks[i].ipv4PrefixLength = pl
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("subnet_mask parameter is invalid.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := network["ipv4_address"].(string); ok && v != "" {
|
|
|
|
networks[i].ipv4Address = v
|
|
|
|
}
|
|
|
|
if v, ok := network["ipv4_prefix_length"].(int); ok && v != 0 {
|
|
|
|
networks[i].ipv4PrefixLength = v
|
2015-10-12 06:06:29 +02:00
|
|
|
}
|
2016-05-03 18:27:24 +02:00
|
|
|
if v, ok := network["ipv4_gateway"].(string); ok && v != "" {
|
|
|
|
networks[i].ipv4Gateway = v
|
|
|
|
}
|
|
|
|
if v, ok := network["ipv6_address"].(string); ok && v != "" {
|
|
|
|
networks[i].ipv6Address = v
|
|
|
|
}
|
|
|
|
if v, ok := network["ipv6_prefix_length"].(int); ok && v != 0 {
|
|
|
|
networks[i].ipv6PrefixLength = v
|
|
|
|
}
|
|
|
|
if v, ok := network["ipv6_gateway"].(string); ok && v != "" {
|
|
|
|
networks[i].ipv6Gateway = v
|
|
|
|
}
|
2016-06-02 01:38:56 +02:00
|
|
|
if v, ok := network["mac_address"].(string); ok && v != "" {
|
|
|
|
networks[i].macAddress = v
|
|
|
|
}
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
2015-10-12 06:06:29 +02:00
|
|
|
vm.networkInterfaces = networks
|
|
|
|
log.Printf("[DEBUG] network_interface init: %v", networks)
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
2015-10-12 06:06:29 +02:00
|
|
|
|
2016-03-23 21:46:33 +01:00
|
|
|
if vL, ok := d.GetOk("windows_opt_config"); ok {
|
|
|
|
var winOpt windowsOptConfig
|
|
|
|
custom_configs := (vL.([]interface{}))[0].(map[string]interface{})
|
|
|
|
if v, ok := custom_configs["admin_password"].(string); ok && v != "" {
|
|
|
|
winOpt.adminPassword = v
|
|
|
|
}
|
|
|
|
if v, ok := custom_configs["domain"].(string); ok && v != "" {
|
|
|
|
winOpt.domain = v
|
|
|
|
}
|
|
|
|
if v, ok := custom_configs["domain_user"].(string); ok && v != "" {
|
|
|
|
winOpt.domainUser = v
|
|
|
|
}
|
|
|
|
if v, ok := custom_configs["product_key"].(string); ok && v != "" {
|
|
|
|
winOpt.productKey = v
|
|
|
|
}
|
|
|
|
if v, ok := custom_configs["domain_user_password"].(string); ok && v != "" {
|
|
|
|
winOpt.domainUserPassword = v
|
|
|
|
}
|
|
|
|
vm.windowsOptionalConfig = winOpt
|
|
|
|
log.Printf("[DEBUG] windows config init: %v", winOpt)
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:06:29 +02:00
|
|
|
if vL, ok := d.GetOk("disk"); ok {
|
2016-05-23 23:37:06 +02:00
|
|
|
if diskSet, ok := vL.(*schema.Set); ok {
|
|
|
|
|
|
|
|
disks := []hardDisk{}
|
|
|
|
for _, value := range diskSet.List() {
|
|
|
|
disk := value.(map[string]interface{})
|
|
|
|
newDisk := hardDisk{}
|
|
|
|
|
2015-10-12 06:06:29 +02:00
|
|
|
if v, ok := disk["template"].(string); ok && v != "" {
|
2016-05-23 23:37:06 +02:00
|
|
|
if v, ok := disk["name"].(string); ok && v != "" {
|
|
|
|
return fmt.Errorf("Cannot specify name of a template")
|
|
|
|
}
|
2015-10-12 06:06:29 +02:00
|
|
|
vm.template = v
|
2016-07-27 01:25:56 +02:00
|
|
|
if vm.hasBootableVmdk {
|
2016-05-23 23:37:06 +02:00
|
|
|
return fmt.Errorf("[ERROR] Only one bootable disk or template may be given")
|
2015-10-12 06:06:29 +02:00
|
|
|
}
|
2016-07-27 01:25:56 +02:00
|
|
|
vm.hasBootableVmdk = true
|
2015-10-12 06:06:29 +02:00
|
|
|
}
|
2016-05-23 23:37:06 +02:00
|
|
|
|
|
|
|
if v, ok := disk["type"].(string); ok && v != "" {
|
|
|
|
newDisk.initType = v
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:06:29 +02:00
|
|
|
if v, ok := disk["datastore"].(string); ok && v != "" {
|
|
|
|
vm.datastore = v
|
|
|
|
}
|
2016-05-23 23:37:06 +02:00
|
|
|
|
2015-10-12 06:06:29 +02:00
|
|
|
if v, ok := disk["size"].(int); ok && v != 0 {
|
2016-05-23 23:37:06 +02:00
|
|
|
if v, ok := disk["template"].(string); ok && v != "" {
|
|
|
|
return fmt.Errorf("Cannot specify size of a template")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := disk["name"].(string); ok && v != "" {
|
|
|
|
newDisk.name = v
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("[ERROR] Disk name must be provided when creating a new disk")
|
|
|
|
}
|
|
|
|
|
|
|
|
newDisk.size = int64(v)
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
2015-12-12 09:33:01 +01:00
|
|
|
|
2016-05-23 23:37:06 +02:00
|
|
|
if v, ok := disk["iops"].(int); ok && v != 0 {
|
|
|
|
newDisk.iops = int64(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := disk["controller_type"].(string); ok && v != "" {
|
|
|
|
newDisk.controller = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if vVmdk, ok := disk["vmdk"].(string); ok && vVmdk != "" {
|
|
|
|
if v, ok := disk["template"].(string); ok && v != "" {
|
|
|
|
return fmt.Errorf("Cannot specify a vmdk for a template")
|
|
|
|
}
|
|
|
|
if v, ok := disk["size"].(string); ok && v != "" {
|
|
|
|
return fmt.Errorf("Cannot specify size of a vmdk")
|
|
|
|
}
|
|
|
|
if v, ok := disk["name"].(string); ok && v != "" {
|
|
|
|
return fmt.Errorf("Cannot specify name of a vmdk")
|
|
|
|
}
|
|
|
|
if vBootable, ok := disk["bootable"].(bool); ok {
|
2016-07-27 01:25:56 +02:00
|
|
|
if vBootable && vm.hasBootableVmdk {
|
|
|
|
return fmt.Errorf("[ERROR] Only one bootable disk or template may be given")
|
|
|
|
}
|
2016-05-23 23:37:06 +02:00
|
|
|
newDisk.bootable = vBootable
|
2016-07-27 01:25:56 +02:00
|
|
|
vm.hasBootableVmdk = vm.hasBootableVmdk || vBootable
|
2016-05-23 23:37:06 +02:00
|
|
|
}
|
|
|
|
newDisk.vmdkPath = vVmdk
|
|
|
|
}
|
|
|
|
// Preserves order so bootable disk is first
|
|
|
|
if newDisk.bootable == true || disk["template"] != "" {
|
|
|
|
disks = append([]hardDisk{newDisk}, disks...)
|
|
|
|
} else {
|
|
|
|
disks = append(disks, newDisk)
|
|
|
|
}
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
2016-05-23 23:37:06 +02:00
|
|
|
vm.hardDisks = disks
|
|
|
|
log.Printf("[DEBUG] disk init: %v", disks)
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 15:35:37 +01:00
|
|
|
if vL, ok := d.GetOk("cdrom"); ok {
|
|
|
|
cdroms := make([]cdrom, len(vL.([]interface{})))
|
|
|
|
for i, v := range vL.([]interface{}) {
|
|
|
|
c := v.(map[string]interface{})
|
|
|
|
if v, ok := c["datastore"].(string); ok && v != "" {
|
|
|
|
cdroms[i].datastore = v
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Datastore argument must be specified when attaching a cdrom image.")
|
|
|
|
}
|
|
|
|
if v, ok := c["path"].(string); ok && v != "" {
|
|
|
|
cdroms[i].path = v
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Path argument must be specified when attaching a cdrom image.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vm.cdroms = cdroms
|
|
|
|
log.Printf("[DEBUG] cdrom init: %v", cdroms)
|
|
|
|
}
|
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
err := vm.setupVirtualMachine(client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
|
2015-11-16 19:44:39 +01:00
|
|
|
d.SetId(vm.Path())
|
2015-10-06 05:53:07 +02:00
|
|
|
log.Printf("[INFO] Created virtual machine: %s", d.Id())
|
|
|
|
|
|
|
|
return resourceVSphereVirtualMachineRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) error {
|
2016-05-23 23:37:06 +02:00
|
|
|
log.Printf("[DEBUG] virtual machine resource data: %#v", d)
|
2015-10-06 05:53:07 +02:00
|
|
|
client := meta.(*govmomi.Client)
|
|
|
|
dc, err := getDatacenter(client, d.Get("datacenter").(string))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
finder := find.NewFinder(client.Client, true)
|
|
|
|
finder = finder.SetDatacenter(dc)
|
|
|
|
|
2015-11-16 19:44:39 +01:00
|
|
|
vm, err := finder.VirtualMachine(context.TODO(), d.Id())
|
2015-10-06 05:53:07 +02:00
|
|
|
if err != nil {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-12 10:05:02 +02:00
|
|
|
state, err := vm.PowerState(context.TODO())
|
2016-05-17 20:35:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-12 10:05:02 +02:00
|
|
|
if state == types.VirtualMachinePowerStatePoweredOn {
|
|
|
|
// wait for interfaces to appear
|
2016-08-15 18:43:54 +02:00
|
|
|
log.Printf("[DEBUG] Waiting for interfaces to appear")
|
|
|
|
|
|
|
|
_, err = vm.WaitForNetIP(context.TODO(), false)
|
2016-07-12 10:05:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-15 18:43:54 +02:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] Successfully waited for interfaces to appear")
|
2016-07-12 10:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var mvm mo.VirtualMachine
|
2015-10-06 05:53:07 +02:00
|
|
|
collector := property.DefaultCollector(client.Client)
|
2016-05-23 23:37:06 +02:00
|
|
|
if err := collector.RetrieveOne(context.TODO(), vm.Reference(), []string{"guest", "summary", "datastore", "config"}, &mvm); err != nil {
|
2015-10-12 06:06:29 +02:00
|
|
|
return err
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
|
2016-05-23 23:37:06 +02:00
|
|
|
log.Printf("[DEBUG] Datacenter - %#v", dc)
|
|
|
|
log.Printf("[DEBUG] mvm.Summary.Config - %#v", mvm.Summary.Config)
|
|
|
|
log.Printf("[DEBUG] mvm.Summary.Config - %#v", mvm.Config)
|
|
|
|
log.Printf("[DEBUG] mvm.Guest.Net - %#v", mvm.Guest.Net)
|
|
|
|
|
|
|
|
disks := make([]map[string]interface{}, 0)
|
|
|
|
templateDisk := make(map[string]interface{}, 1)
|
|
|
|
for _, device := range mvm.Config.Hardware.Device {
|
|
|
|
if vd, ok := device.(*types.VirtualDisk); ok {
|
|
|
|
|
|
|
|
virtualDevice := vd.GetVirtualDevice()
|
|
|
|
|
2016-06-01 01:03:33 +02:00
|
|
|
backingInfo := virtualDevice.Backing
|
|
|
|
var diskFullPath string
|
|
|
|
var diskUuid string
|
|
|
|
if v, ok := backingInfo.(*types.VirtualDiskFlatVer2BackingInfo); ok {
|
|
|
|
diskFullPath = v.FileName
|
|
|
|
diskUuid = v.Uuid
|
|
|
|
} else if v, ok := backingInfo.(*types.VirtualDiskSparseVer2BackingInfo); ok {
|
|
|
|
diskFullPath = v.FileName
|
|
|
|
diskUuid = v.Uuid
|
|
|
|
}
|
2016-05-23 23:37:06 +02:00
|
|
|
log.Printf("[DEBUG] resourceVSphereVirtualMachineRead - Analyzing disk: %v", diskFullPath)
|
|
|
|
|
|
|
|
// Separate datastore and path
|
|
|
|
diskFullPathSplit := strings.Split(diskFullPath, " ")
|
|
|
|
if len(diskFullPathSplit) != 2 {
|
|
|
|
return fmt.Errorf("[ERROR] Failed trying to parse disk path: %v", diskFullPath)
|
|
|
|
}
|
|
|
|
diskPath := diskFullPathSplit[1]
|
|
|
|
// Isolate filename
|
|
|
|
diskNameSplit := strings.Split(diskPath, "/")
|
|
|
|
diskName := diskNameSplit[len(diskNameSplit)-1]
|
|
|
|
// Remove possible extension
|
|
|
|
diskName = strings.Split(diskName, ".")[0]
|
|
|
|
|
|
|
|
if prevDisks, ok := d.GetOk("disk"); ok {
|
|
|
|
if prevDiskSet, ok := prevDisks.(*schema.Set); ok {
|
|
|
|
for _, v := range prevDiskSet.List() {
|
|
|
|
prevDisk := v.(map[string]interface{})
|
|
|
|
|
|
|
|
// We're guaranteed only one template disk. Passing value directly through since templates should be immutable
|
|
|
|
if prevDisk["template"] != "" {
|
|
|
|
if len(templateDisk) == 0 {
|
|
|
|
templateDisk = prevDisk
|
|
|
|
disks = append(disks, templateDisk)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is enforced that prevDisk["name"] should only be set in the case
|
|
|
|
// of creating a new disk for the user.
|
|
|
|
// size case: name was set by user, compare parsed filename from mo.filename (without path or .vmdk extension) with name
|
|
|
|
// vmdk case: compare prevDisk["vmdk"] and mo.Filename
|
|
|
|
if diskName == prevDisk["name"] || diskPath == prevDisk["vmdk"] {
|
|
|
|
|
|
|
|
prevDisk["key"] = virtualDevice.Key
|
2016-06-01 01:03:33 +02:00
|
|
|
prevDisk["uuid"] = diskUuid
|
2016-05-23 23:37:06 +02:00
|
|
|
|
|
|
|
disks = append(disks, prevDisk)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] disks: %#v", disks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = d.Set("disk", disks)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Invalid disks to set: %#v", disks)
|
|
|
|
}
|
2015-10-06 05:53:07 +02:00
|
|
|
|
|
|
|
networkInterfaces := make([]map[string]interface{}, 0)
|
|
|
|
for _, v := range mvm.Guest.Net {
|
|
|
|
if v.DeviceConfigId >= 0 {
|
2016-05-23 23:37:06 +02:00
|
|
|
log.Printf("[DEBUG] v.Network - %#v", v.Network)
|
2015-10-06 05:53:07 +02:00
|
|
|
networkInterface := make(map[string]interface{})
|
|
|
|
networkInterface["label"] = v.Network
|
2016-06-02 01:38:56 +02:00
|
|
|
networkInterface["mac_address"] = v.MacAddress
|
2015-11-24 01:53:11 +01:00
|
|
|
for _, ip := range v.IpConfig.IpAddress {
|
|
|
|
p := net.ParseIP(ip.IpAddress)
|
|
|
|
if p.To4() != nil {
|
2016-05-23 23:37:06 +02:00
|
|
|
log.Printf("[DEBUG] p.String - %#v", p.String())
|
|
|
|
log.Printf("[DEBUG] ip.PrefixLength - %#v", ip.PrefixLength)
|
2015-11-24 01:53:11 +01:00
|
|
|
networkInterface["ipv4_address"] = p.String()
|
|
|
|
networkInterface["ipv4_prefix_length"] = ip.PrefixLength
|
|
|
|
} else if p.To16() != nil {
|
2016-05-23 23:37:06 +02:00
|
|
|
log.Printf("[DEBUG] p.String - %#v", p.String())
|
|
|
|
log.Printf("[DEBUG] ip.PrefixLength - %#v", ip.PrefixLength)
|
2015-11-24 01:53:11 +01:00
|
|
|
networkInterface["ipv6_address"] = p.String()
|
|
|
|
networkInterface["ipv6_prefix_length"] = ip.PrefixLength
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] networkInterface: %#v", networkInterface)
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
2015-11-24 01:53:11 +01:00
|
|
|
log.Printf("[DEBUG] networkInterface: %#v", networkInterface)
|
2015-10-06 05:53:07 +02:00
|
|
|
networkInterfaces = append(networkInterfaces, networkInterface)
|
|
|
|
}
|
|
|
|
}
|
2016-05-11 17:21:54 +02:00
|
|
|
if mvm.Guest.IpStack != nil {
|
|
|
|
for _, v := range mvm.Guest.IpStack {
|
|
|
|
if v.IpRouteConfig != nil && v.IpRouteConfig.IpRoute != nil {
|
|
|
|
for _, route := range v.IpRouteConfig.IpRoute {
|
|
|
|
if route.Gateway.Device != "" {
|
|
|
|
gatewaySetting := ""
|
|
|
|
if route.Network == "::" {
|
|
|
|
gatewaySetting = "ipv6_gateway"
|
|
|
|
} else if route.Network == "0.0.0.0" {
|
|
|
|
gatewaySetting = "ipv4_gateway"
|
|
|
|
}
|
|
|
|
if gatewaySetting != "" {
|
|
|
|
deviceID, err := strconv.Atoi(route.Gateway.Device)
|
2017-01-20 15:11:24 +01:00
|
|
|
if len(networkInterfaces) == 1 {
|
|
|
|
deviceID = 0
|
|
|
|
}
|
2016-05-11 17:21:54 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[WARN] error at processing %s of device id %#v: %#v", gatewaySetting, route.Gateway.Device, err)
|
|
|
|
} else {
|
|
|
|
log.Printf("[DEBUG] %s of device id %d: %s", gatewaySetting, deviceID, route.Gateway.IpAddress)
|
|
|
|
networkInterfaces[deviceID][gatewaySetting] = route.Gateway.IpAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 02:32:19 +01:00
|
|
|
log.Printf("[DEBUG] networkInterfaces: %#v", networkInterfaces)
|
2015-10-12 06:06:29 +02:00
|
|
|
err = d.Set("network_interface", networkInterfaces)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Invalid network interfaces to set: %#v", networkInterfaces)
|
|
|
|
}
|
2015-10-06 05:53:07 +02:00
|
|
|
|
2016-07-12 10:05:02 +02:00
|
|
|
if len(networkInterfaces) > 0 {
|
|
|
|
if _, ok := networkInterfaces[0]["ipv4_address"]; ok {
|
|
|
|
log.Printf("[DEBUG] ip address: %v", networkInterfaces[0]["ipv4_address"].(string))
|
|
|
|
d.SetConnInfo(map[string]string{
|
|
|
|
"type": "ssh",
|
|
|
|
"host": networkInterfaces[0]["ipv4_address"].(string),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-04-22 01:07:52 +02:00
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
var rootDatastore string
|
|
|
|
for _, v := range mvm.Datastore {
|
|
|
|
var md mo.Datastore
|
|
|
|
if err := collector.RetrieveOne(context.TODO(), v, []string{"name", "parent"}, &md); err != nil {
|
2015-10-12 06:06:29 +02:00
|
|
|
return err
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
if md.Parent.Type == "StoragePod" {
|
|
|
|
var msp mo.StoragePod
|
|
|
|
if err := collector.RetrieveOne(context.TODO(), *md.Parent, []string{"name"}, &msp); err != nil {
|
2015-10-12 06:06:29 +02:00
|
|
|
return err
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
rootDatastore = msp.Name
|
|
|
|
log.Printf("[DEBUG] %#v", msp.Name)
|
|
|
|
} else {
|
|
|
|
rootDatastore = md.Name
|
|
|
|
log.Printf("[DEBUG] %#v", md.Name)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("datacenter", dc)
|
|
|
|
d.Set("memory", mvm.Summary.Config.MemorySizeMB)
|
2016-04-25 13:15:37 +02:00
|
|
|
d.Set("memory_reservation", mvm.Summary.Config.MemoryReservation)
|
2015-10-06 05:53:07 +02:00
|
|
|
d.Set("cpu", mvm.Summary.Config.NumCpu)
|
|
|
|
d.Set("datastore", rootDatastore)
|
2016-06-29 22:45:59 +02:00
|
|
|
d.Set("uuid", mvm.Summary.Config.Uuid)
|
2015-10-06 05:53:07 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceVSphereVirtualMachineDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*govmomi.Client)
|
|
|
|
dc, err := getDatacenter(client, d.Get("datacenter").(string))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
finder := find.NewFinder(client.Client, true)
|
|
|
|
finder = finder.SetDatacenter(dc)
|
|
|
|
|
2015-11-16 19:44:39 +01:00
|
|
|
vm, err := finder.VirtualMachine(context.TODO(), vmPath(d.Get("folder").(string), d.Get("name").(string)))
|
2015-10-06 05:53:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-11 16:48:34 +02:00
|
|
|
devices, err := vm.Device(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[DEBUG] resourceVSphereVirtualMachineDelete - Failed to get device list: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2015-10-06 05:53:07 +02:00
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting virtual machine: %s", d.Id())
|
2016-04-21 18:27:36 +02:00
|
|
|
state, err := vm.PowerState(context.TODO())
|
2015-10-06 05:53:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-21 18:27:36 +02:00
|
|
|
if state == types.VirtualMachinePowerStatePoweredOn {
|
|
|
|
task, err := vm.PowerOff(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = task.Wait(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
|
2016-07-11 16:48:34 +02:00
|
|
|
// Safely eject any disks the user marked as keep_on_remove
|
2016-09-20 21:04:38 +02:00
|
|
|
var diskSetList []interface{}
|
2016-07-11 16:48:34 +02:00
|
|
|
if vL, ok := d.GetOk("disk"); ok {
|
|
|
|
if diskSet, ok := vL.(*schema.Set); ok {
|
2016-09-20 21:04:38 +02:00
|
|
|
diskSetList = diskSet.List()
|
|
|
|
for _, value := range diskSetList {
|
2016-07-11 16:48:34 +02:00
|
|
|
disk := value.(map[string]interface{})
|
|
|
|
|
|
|
|
if v, ok := disk["keep_on_remove"].(bool); ok && v == true {
|
|
|
|
log.Printf("[DEBUG] not destroying %v", disk["name"])
|
|
|
|
virtualDisk := devices.FindByKey(int32(disk["key"].(int)))
|
|
|
|
err = vm.RemoveDevice(context.TODO(), true, virtualDisk)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] Update Remove Disk - Error removing disk: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 21:04:38 +02:00
|
|
|
// Safely eject any disks that are not managed by this resource
|
|
|
|
if v, ok := d.GetOk("detach_unknown_disks_on_delete"); ok && v.(bool) {
|
|
|
|
var disksToRemove object.VirtualDeviceList
|
|
|
|
for _, device := range devices {
|
|
|
|
if devices.TypeName(device) != "VirtualDisk" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
vd := device.GetVirtualDevice()
|
|
|
|
var skip bool
|
|
|
|
for _, value := range diskSetList {
|
|
|
|
disk := value.(map[string]interface{})
|
|
|
|
if int32(disk["key"].(int)) == vd.Key {
|
|
|
|
skip = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if skip {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
disksToRemove = append(disksToRemove, device)
|
|
|
|
}
|
|
|
|
if len(disksToRemove) != 0 {
|
|
|
|
err = vm.RemoveDevice(context.TODO(), true, disksToRemove...)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] Update Remove Disk - Error removing disk: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 18:27:36 +02:00
|
|
|
task, err := vm.Destroy(context.TODO())
|
2015-10-06 05:53:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = task.Wait(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addHardDisk adds a new Hard Disk to the VirtualMachine.
|
2016-05-21 08:21:42 +02:00
|
|
|
func addHardDisk(vm *object.VirtualMachine, size, iops int64, diskType string, datastore *object.Datastore, diskPath string, controller_type string) error {
|
2015-10-06 05:53:07 +02:00
|
|
|
devices, err := vm.Device(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] vm devices: %#v\n", devices)
|
|
|
|
|
2016-06-01 01:03:33 +02:00
|
|
|
var controller types.BaseVirtualController
|
2016-07-12 10:02:41 +02:00
|
|
|
switch controller_type {
|
|
|
|
case "scsi":
|
|
|
|
controller, err = devices.FindDiskController(controller_type)
|
|
|
|
case "scsi-lsi-parallel":
|
|
|
|
controller = devices.PickController(&types.VirtualLsiLogicController{})
|
|
|
|
case "scsi-buslogic":
|
|
|
|
controller = devices.PickController(&types.VirtualBusLogicController{})
|
|
|
|
case "scsi-paravirtual":
|
|
|
|
controller = devices.PickController(&types.ParaVirtualSCSIController{})
|
|
|
|
case "scsi-lsi-sas":
|
|
|
|
controller = devices.PickController(&types.VirtualLsiLogicSASController{})
|
|
|
|
case "ide":
|
|
|
|
controller, err = devices.FindDiskController(controller_type)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("[ERROR] Unsupported disk controller provided: %v", controller_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil || controller == nil {
|
2016-08-04 21:29:02 +02:00
|
|
|
// Check if max number of scsi controller are already used
|
|
|
|
diskControllers := getSCSIControllers(devices)
|
|
|
|
if len(diskControllers) >= 4 {
|
|
|
|
return fmt.Errorf("[ERROR] Maximum number of SCSI controllers created")
|
|
|
|
}
|
|
|
|
|
2016-06-01 01:03:33 +02:00
|
|
|
log.Printf("[DEBUG] Couldn't find a %v controller. Creating one..", controller_type)
|
|
|
|
|
|
|
|
var c types.BaseVirtualDevice
|
|
|
|
switch controller_type {
|
|
|
|
case "scsi":
|
|
|
|
// Create scsi controller
|
|
|
|
c, err = devices.CreateSCSIController("scsi")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[ERROR] Failed creating SCSI controller: %v", err)
|
|
|
|
}
|
2016-07-12 10:02:41 +02:00
|
|
|
case "scsi-lsi-parallel":
|
|
|
|
// Create scsi controller
|
|
|
|
c, err = devices.CreateSCSIController("lsilogic")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[ERROR] Failed creating SCSI controller: %v", err)
|
|
|
|
}
|
|
|
|
case "scsi-buslogic":
|
|
|
|
// Create scsi controller
|
|
|
|
c, err = devices.CreateSCSIController("buslogic")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[ERROR] Failed creating SCSI controller: %v", err)
|
|
|
|
}
|
|
|
|
case "scsi-paravirtual":
|
|
|
|
// Create scsi controller
|
|
|
|
c, err = devices.CreateSCSIController("pvscsi")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[ERROR] Failed creating SCSI controller: %v", err)
|
|
|
|
}
|
|
|
|
case "scsi-lsi-sas":
|
|
|
|
// Create scsi controller
|
|
|
|
c, err = devices.CreateSCSIController("lsilogic-sas")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[ERROR] Failed creating SCSI controller: %v", err)
|
|
|
|
}
|
2016-06-01 01:03:33 +02:00
|
|
|
case "ide":
|
|
|
|
// Create ide controller
|
|
|
|
c, err = devices.CreateIDEController()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[ERROR] Failed creating IDE controller: %v", err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("[ERROR] Unsupported disk controller provided: %v", controller_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
vm.AddDevice(context.TODO(), c)
|
2016-06-29 20:47:56 +02:00
|
|
|
// Update our devices list
|
|
|
|
devices, err := vm.Device(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-12 10:02:41 +02:00
|
|
|
controller = devices.PickController(c.(types.BaseVirtualController))
|
|
|
|
if controller == nil {
|
|
|
|
log.Printf("[ERROR] Could not find the new %v controller", controller_type)
|
|
|
|
return fmt.Errorf("Could not find the new %v controller", controller_type)
|
2016-06-01 01:03:33 +02:00
|
|
|
}
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
2016-06-01 01:03:33 +02:00
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
log.Printf("[DEBUG] disk controller: %#v\n", controller)
|
|
|
|
|
2016-05-23 23:37:06 +02:00
|
|
|
// TODO Check if diskPath & datastore exist
|
|
|
|
// If diskPath is not specified, pass empty string to CreateDisk()
|
|
|
|
if diskPath == "" {
|
|
|
|
return fmt.Errorf("[ERROR] addHardDisk - No path proided")
|
|
|
|
} else {
|
2016-08-16 09:58:22 +02:00
|
|
|
diskPath = datastore.Path(diskPath)
|
2016-05-23 23:37:06 +02:00
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] addHardDisk - diskPath: %v", diskPath)
|
2016-05-13 16:27:45 +02:00
|
|
|
disk := devices.CreateDisk(controller, datastore.Reference(), diskPath)
|
2016-05-23 23:37:06 +02:00
|
|
|
|
2016-08-04 21:29:02 +02:00
|
|
|
if strings.Contains(controller_type, "scsi") {
|
|
|
|
unitNumber, err := getNextUnitNumber(devices, controller)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*disk.UnitNumber = unitNumber
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
existing := devices.SelectByBackingInfo(disk.Backing)
|
|
|
|
log.Printf("[DEBUG] disk: %#v\n", disk)
|
|
|
|
|
|
|
|
if len(existing) == 0 {
|
|
|
|
disk.CapacityInKB = int64(size * 1024 * 1024)
|
|
|
|
if iops != 0 {
|
|
|
|
disk.StorageIOAllocation = &types.StorageIOAllocationInfo{
|
|
|
|
Limit: iops,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
backing := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo)
|
|
|
|
|
|
|
|
if diskType == "eager_zeroed" {
|
|
|
|
// eager zeroed thick virtual disk
|
|
|
|
backing.ThinProvisioned = types.NewBool(false)
|
|
|
|
backing.EagerlyScrub = types.NewBool(true)
|
2016-08-08 21:59:32 +02:00
|
|
|
} else if diskType == "lazy" {
|
|
|
|
// lazy zeroed thick virtual disk
|
|
|
|
backing.ThinProvisioned = types.NewBool(false)
|
|
|
|
backing.EagerlyScrub = types.NewBool(false)
|
2015-10-06 05:53:07 +02:00
|
|
|
} else if diskType == "thin" {
|
|
|
|
// thin provisioned virtual disk
|
|
|
|
backing.ThinProvisioned = types.NewBool(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] addHardDisk: %#v\n", disk)
|
2016-05-23 23:37:06 +02:00
|
|
|
log.Printf("[DEBUG] addHardDisk capacity: %#v\n", disk.CapacityInKB)
|
2015-10-06 05:53:07 +02:00
|
|
|
|
|
|
|
return vm.AddDevice(context.TODO(), disk)
|
|
|
|
} else {
|
|
|
|
log.Printf("[DEBUG] addHardDisk: Disk already present.\n")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 21:29:02 +02:00
|
|
|
func getSCSIControllers(vmDevices object.VirtualDeviceList) []*types.VirtualController {
|
|
|
|
// get virtual scsi controllers of all supported types
|
|
|
|
var scsiControllers []*types.VirtualController
|
|
|
|
for _, device := range vmDevices {
|
|
|
|
devType := vmDevices.Type(device)
|
|
|
|
switch devType {
|
|
|
|
case "scsi", "lsilogic", "buslogic", "pvscsi", "lsilogic-sas":
|
|
|
|
if c, ok := device.(types.BaseVirtualController); ok {
|
|
|
|
scsiControllers = append(scsiControllers, c.GetVirtualController())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return scsiControllers
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNextUnitNumber(devices object.VirtualDeviceList, c types.BaseVirtualController) (int32, error) {
|
|
|
|
key := c.GetVirtualController().Key
|
|
|
|
|
|
|
|
var unitNumbers [16]bool
|
|
|
|
unitNumbers[7] = true
|
|
|
|
|
|
|
|
for _, device := range devices {
|
|
|
|
d := device.GetVirtualDevice()
|
|
|
|
|
|
|
|
if d.ControllerKey == key {
|
|
|
|
if d.UnitNumber != nil {
|
|
|
|
unitNumbers[*d.UnitNumber] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, taken := range unitNumbers {
|
|
|
|
if !taken {
|
|
|
|
return int32(i), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, fmt.Errorf("[ERROR] getNextUnitNumber - controller is full")
|
|
|
|
}
|
|
|
|
|
2015-11-25 15:35:37 +01:00
|
|
|
// addCdrom adds a new virtual cdrom drive to the VirtualMachine and attaches an image (ISO) to it from a datastore path.
|
2016-08-16 09:58:22 +02:00
|
|
|
func addCdrom(client *govmomi.Client, vm *object.VirtualMachine, datacenter *object.Datacenter, datastore, path string) error {
|
2015-11-25 15:35:37 +01:00
|
|
|
devices, err := vm.Device(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] vm devices: %#v", devices)
|
|
|
|
|
2016-06-01 01:03:33 +02:00
|
|
|
var controller *types.VirtualIDEController
|
|
|
|
controller, err = devices.FindIDEController("")
|
2015-11-25 15:35:37 +01:00
|
|
|
if err != nil {
|
2016-06-01 01:03:33 +02:00
|
|
|
log.Printf("[DEBUG] Couldn't find a ide controller. Creating one..")
|
|
|
|
|
|
|
|
var c types.BaseVirtualDevice
|
|
|
|
c, err := devices.CreateIDEController()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[ERROR] Failed creating IDE controller: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := c.(*types.VirtualIDEController); ok {
|
|
|
|
controller = v
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("[ERROR] Controller type could not be asserted")
|
|
|
|
}
|
|
|
|
vm.AddDevice(context.TODO(), c)
|
2016-06-29 20:47:56 +02:00
|
|
|
// Update our devices list
|
|
|
|
devices, err := vm.Device(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-01 01:03:33 +02:00
|
|
|
controller, err = devices.FindIDEController("")
|
|
|
|
if err != nil {
|
2016-06-29 20:47:56 +02:00
|
|
|
log.Printf("[ERROR] Could not find the new disk IDE controller: %v", err)
|
|
|
|
return err
|
2016-06-01 01:03:33 +02:00
|
|
|
}
|
2015-11-25 15:35:37 +01:00
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] ide controller: %#v", controller)
|
|
|
|
|
|
|
|
c, err := devices.CreateCdrom(controller)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-16 09:58:22 +02:00
|
|
|
finder := find.NewFinder(client.Client, true)
|
|
|
|
finder = finder.SetDatacenter(datacenter)
|
|
|
|
ds, err := getDatastore(finder, datastore)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c = devices.InsertIso(c, ds.Path(path))
|
2015-11-25 15:35:37 +01:00
|
|
|
log.Printf("[DEBUG] addCdrom: %#v", c)
|
|
|
|
|
|
|
|
return vm.AddDevice(context.TODO(), c)
|
|
|
|
}
|
|
|
|
|
2015-11-20 13:01:02 +01:00
|
|
|
// buildNetworkDevice builds VirtualDeviceConfigSpec for Network Device.
|
2016-06-02 01:38:56 +02:00
|
|
|
func buildNetworkDevice(f *find.Finder, label, adapterType string, macAddress string) (*types.VirtualDeviceConfigSpec, error) {
|
2015-10-06 05:53:07 +02:00
|
|
|
network, err := f.Network(context.TODO(), "*"+label)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
backing, err := network.EthernetCardBackingInfo(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-02 01:38:56 +02:00
|
|
|
var address_type string
|
|
|
|
if macAddress == "" {
|
|
|
|
address_type = string(types.VirtualEthernetCardMacTypeGenerated)
|
|
|
|
} else {
|
|
|
|
address_type = string(types.VirtualEthernetCardMacTypeManual)
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
if adapterType == "vmxnet3" {
|
|
|
|
return &types.VirtualDeviceConfigSpec{
|
|
|
|
Operation: types.VirtualDeviceConfigSpecOperationAdd,
|
|
|
|
Device: &types.VirtualVmxnet3{
|
2016-02-12 22:57:39 +01:00
|
|
|
VirtualVmxnet: types.VirtualVmxnet{
|
|
|
|
VirtualEthernetCard: types.VirtualEthernetCard{
|
2015-10-06 05:53:07 +02:00
|
|
|
VirtualDevice: types.VirtualDevice{
|
|
|
|
Key: -1,
|
|
|
|
Backing: backing,
|
|
|
|
},
|
2016-06-02 01:38:56 +02:00
|
|
|
AddressType: address_type,
|
|
|
|
MacAddress: macAddress,
|
2015-10-06 05:53:07 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
} else if adapterType == "e1000" {
|
|
|
|
return &types.VirtualDeviceConfigSpec{
|
|
|
|
Operation: types.VirtualDeviceConfigSpecOperationAdd,
|
|
|
|
Device: &types.VirtualE1000{
|
2016-02-12 22:57:39 +01:00
|
|
|
VirtualEthernetCard: types.VirtualEthernetCard{
|
2015-10-06 05:53:07 +02:00
|
|
|
VirtualDevice: types.VirtualDevice{
|
|
|
|
Key: -1,
|
|
|
|
Backing: backing,
|
|
|
|
},
|
2016-06-02 01:38:56 +02:00
|
|
|
AddressType: address_type,
|
|
|
|
MacAddress: macAddress,
|
2015-10-06 05:53:07 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Invalid network adapter type.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 13:01:02 +01:00
|
|
|
// buildVMRelocateSpec builds VirtualMachineRelocateSpec to set a place for a new VirtualMachine.
|
2016-02-24 16:39:24 +01:00
|
|
|
func buildVMRelocateSpec(rp *object.ResourcePool, ds *object.Datastore, vm *object.VirtualMachine, linkedClone bool, initType string) (types.VirtualMachineRelocateSpec, error) {
|
2016-05-13 16:27:45 +02:00
|
|
|
var key int32
|
2016-02-24 16:39:24 +01:00
|
|
|
var moveType string
|
|
|
|
if linkedClone {
|
|
|
|
moveType = "createNewChildDiskBacking"
|
|
|
|
} else {
|
|
|
|
moveType = "moveAllDiskBackingsAndDisallowSharing"
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] relocate type: [%s]", moveType)
|
2015-10-06 05:53:07 +02:00
|
|
|
|
|
|
|
devices, err := vm.Device(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return types.VirtualMachineRelocateSpec{}, err
|
|
|
|
}
|
|
|
|
for _, d := range devices {
|
|
|
|
if devices.Type(d) == "disk" {
|
2016-05-13 16:27:45 +02:00
|
|
|
key = int32(d.GetVirtualDevice().Key)
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-12 09:33:01 +01:00
|
|
|
isThin := initType == "thin"
|
2016-08-08 21:59:32 +02:00
|
|
|
eagerScrub := initType == "eager_zeroed"
|
2015-10-06 05:53:07 +02:00
|
|
|
rpr := rp.Reference()
|
|
|
|
dsr := ds.Reference()
|
|
|
|
return types.VirtualMachineRelocateSpec{
|
2016-02-24 16:39:24 +01:00
|
|
|
Datastore: &dsr,
|
|
|
|
Pool: &rpr,
|
|
|
|
DiskMoveType: moveType,
|
2015-10-06 05:53:07 +02:00
|
|
|
Disk: []types.VirtualMachineRelocateSpecDiskLocator{
|
2016-05-23 23:37:06 +02:00
|
|
|
{
|
2015-10-06 05:53:07 +02:00
|
|
|
Datastore: dsr,
|
|
|
|
DiskBackingInfo: &types.VirtualDiskFlatVer2BackingInfo{
|
|
|
|
DiskMode: "persistent",
|
2015-12-12 09:33:01 +01:00
|
|
|
ThinProvisioned: types.NewBool(isThin),
|
2016-08-08 21:59:32 +02:00
|
|
|
EagerlyScrub: types.NewBool(eagerScrub),
|
2015-10-06 05:53:07 +02:00
|
|
|
},
|
|
|
|
DiskId: key,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDatastoreObject gets datastore object.
|
|
|
|
func getDatastoreObject(client *govmomi.Client, f *object.DatacenterFolders, name string) (types.ManagedObjectReference, error) {
|
|
|
|
s := object.NewSearchIndex(client.Client)
|
|
|
|
ref, err := s.FindChild(context.TODO(), f.DatastoreFolder, name)
|
|
|
|
if err != nil {
|
|
|
|
return types.ManagedObjectReference{}, err
|
|
|
|
}
|
|
|
|
if ref == nil {
|
|
|
|
return types.ManagedObjectReference{}, fmt.Errorf("Datastore '%s' not found.", name)
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] getDatastoreObject: reference: %#v", ref)
|
|
|
|
return ref.Reference(), nil
|
|
|
|
}
|
|
|
|
|
2015-11-20 13:01:02 +01:00
|
|
|
// buildStoragePlacementSpecCreate builds StoragePlacementSpec for create action.
|
|
|
|
func buildStoragePlacementSpecCreate(f *object.DatacenterFolders, rp *object.ResourcePool, storagePod object.StoragePod, configSpec types.VirtualMachineConfigSpec) types.StoragePlacementSpec {
|
2015-10-06 05:53:07 +02:00
|
|
|
vmfr := f.VmFolder.Reference()
|
|
|
|
rpr := rp.Reference()
|
|
|
|
spr := storagePod.Reference()
|
|
|
|
|
|
|
|
sps := types.StoragePlacementSpec{
|
|
|
|
Type: "create",
|
|
|
|
ConfigSpec: &configSpec,
|
|
|
|
PodSelectionSpec: types.StorageDrsPodSelectionSpec{
|
|
|
|
StoragePod: &spr,
|
|
|
|
},
|
|
|
|
Folder: &vmfr,
|
|
|
|
ResourcePool: &rpr,
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] findDatastore: StoragePlacementSpec: %#v\n", sps)
|
|
|
|
return sps
|
|
|
|
}
|
|
|
|
|
2015-11-20 13:01:02 +01:00
|
|
|
// buildStoragePlacementSpecClone builds StoragePlacementSpec for clone action.
|
|
|
|
func buildStoragePlacementSpecClone(c *govmomi.Client, f *object.DatacenterFolders, vm *object.VirtualMachine, rp *object.ResourcePool, storagePod object.StoragePod) types.StoragePlacementSpec {
|
2015-10-06 05:53:07 +02:00
|
|
|
vmr := vm.Reference()
|
|
|
|
vmfr := f.VmFolder.Reference()
|
|
|
|
rpr := rp.Reference()
|
|
|
|
spr := storagePod.Reference()
|
|
|
|
|
|
|
|
var o mo.VirtualMachine
|
|
|
|
err := vm.Properties(context.TODO(), vmr, []string{"datastore"}, &o)
|
|
|
|
if err != nil {
|
|
|
|
return types.StoragePlacementSpec{}
|
|
|
|
}
|
|
|
|
ds := object.NewDatastore(c.Client, o.Datastore[0])
|
|
|
|
log.Printf("[DEBUG] findDatastore: datastore: %#v\n", ds)
|
|
|
|
|
|
|
|
devices, err := vm.Device(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return types.StoragePlacementSpec{}
|
|
|
|
}
|
|
|
|
|
2016-05-13 16:27:45 +02:00
|
|
|
var key int32
|
2015-10-06 05:53:07 +02:00
|
|
|
for _, d := range devices.SelectByType((*types.VirtualDisk)(nil)) {
|
2016-05-13 16:27:45 +02:00
|
|
|
key = int32(d.GetVirtualDevice().Key)
|
2015-10-06 05:53:07 +02:00
|
|
|
log.Printf("[DEBUG] findDatastore: virtual devices: %#v\n", d.GetVirtualDevice())
|
|
|
|
}
|
|
|
|
|
|
|
|
sps := types.StoragePlacementSpec{
|
|
|
|
Type: "clone",
|
|
|
|
Vm: &vmr,
|
|
|
|
PodSelectionSpec: types.StorageDrsPodSelectionSpec{
|
|
|
|
StoragePod: &spr,
|
|
|
|
},
|
|
|
|
CloneSpec: &types.VirtualMachineCloneSpec{
|
|
|
|
Location: types.VirtualMachineRelocateSpec{
|
|
|
|
Disk: []types.VirtualMachineRelocateSpecDiskLocator{
|
2016-05-23 23:37:06 +02:00
|
|
|
{
|
2015-10-06 05:53:07 +02:00
|
|
|
Datastore: ds.Reference(),
|
|
|
|
DiskBackingInfo: &types.VirtualDiskFlatVer2BackingInfo{},
|
|
|
|
DiskId: key,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Pool: &rpr,
|
|
|
|
},
|
|
|
|
PowerOn: false,
|
|
|
|
Template: false,
|
|
|
|
},
|
|
|
|
CloneName: "dummy",
|
|
|
|
Folder: &vmfr,
|
|
|
|
}
|
|
|
|
return sps
|
|
|
|
}
|
|
|
|
|
|
|
|
// findDatastore finds Datastore object.
|
|
|
|
func findDatastore(c *govmomi.Client, sps types.StoragePlacementSpec) (*object.Datastore, error) {
|
|
|
|
var datastore *object.Datastore
|
|
|
|
log.Printf("[DEBUG] findDatastore: StoragePlacementSpec: %#v\n", sps)
|
|
|
|
|
|
|
|
srm := object.NewStorageResourceManager(c.Client)
|
|
|
|
rds, err := srm.RecommendDatastores(context.TODO(), sps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] findDatastore: recommendDatastores: %#v\n", rds)
|
|
|
|
|
|
|
|
spa := rds.Recommendations[0].Action[0].(*types.StoragePlacementAction)
|
|
|
|
datastore = object.NewDatastore(c.Client, spa.Destination)
|
|
|
|
log.Printf("[DEBUG] findDatastore: datastore: %#v", datastore)
|
|
|
|
|
|
|
|
return datastore, nil
|
|
|
|
}
|
|
|
|
|
2015-11-25 15:35:37 +01:00
|
|
|
// createCdroms is a helper function to attach virtual cdrom devices (and their attached disk images) to a virtual IDE controller.
|
2016-08-16 09:58:22 +02:00
|
|
|
func createCdroms(client *govmomi.Client, vm *object.VirtualMachine, datacenter *object.Datacenter, cdroms []cdrom) error {
|
2015-11-25 15:35:37 +01:00
|
|
|
log.Printf("[DEBUG] add cdroms: %v", cdroms)
|
|
|
|
for _, cd := range cdroms {
|
|
|
|
log.Printf("[DEBUG] add cdrom (datastore): %v", cd.datastore)
|
|
|
|
log.Printf("[DEBUG] add cdrom (cd path): %v", cd.path)
|
2016-08-16 09:58:22 +02:00
|
|
|
err := addCdrom(client, vm, datacenter, cd.datastore, cd.path)
|
2015-11-25 15:35:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
func (vm *virtualMachine) setupVirtualMachine(c *govmomi.Client) error {
|
2015-10-06 05:53:07 +02:00
|
|
|
dc, err := getDatacenter(c, vm.datacenter)
|
2015-11-16 19:44:39 +01:00
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
finder := find.NewFinder(c.Client, true)
|
|
|
|
finder = finder.SetDatacenter(dc)
|
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
var template *object.VirtualMachine
|
|
|
|
var template_mo mo.VirtualMachine
|
2016-05-23 23:37:06 +02:00
|
|
|
var vm_mo mo.VirtualMachine
|
2016-05-17 23:18:35 +02:00
|
|
|
if vm.template != "" {
|
|
|
|
template, err = finder.VirtualMachine(context.TODO(), vm.template)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] template: %#v", template)
|
|
|
|
|
|
|
|
err = template.Properties(context.TODO(), template.Reference(), []string{"parent", "config.template", "config.guestId", "resourcePool", "snapshot", "guest.toolsVersionStatus2", "config.guestFullName"}, &template_mo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
var resourcePool *object.ResourcePool
|
|
|
|
if vm.resourcePool == "" {
|
|
|
|
if vm.cluster == "" {
|
|
|
|
resourcePool, err = finder.DefaultResourcePool(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resourcePool, err = finder.ResourcePool(context.TODO(), "*"+vm.cluster+"/Resources")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resourcePool, err = finder.ResourcePool(context.TODO(), vm.resourcePool)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] resource pool: %#v", resourcePool)
|
|
|
|
|
|
|
|
dcFolders, err := dc.Folders(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-16 19:44:39 +01:00
|
|
|
log.Printf("[DEBUG] folder: %#v", vm.folder)
|
2016-05-17 23:18:35 +02:00
|
|
|
|
2015-11-16 19:44:39 +01:00
|
|
|
folder := dcFolders.VmFolder
|
|
|
|
if len(vm.folder) > 0 {
|
|
|
|
si := object.NewSearchIndex(c.Client)
|
|
|
|
folderRef, err := si.FindByInventoryPath(
|
|
|
|
context.TODO(), fmt.Sprintf("%v/vm/%v", vm.datacenter, vm.folder))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading folder %s: %s", vm.folder, err)
|
|
|
|
} else if folderRef == nil {
|
|
|
|
return fmt.Errorf("Cannot find folder %s", vm.folder)
|
|
|
|
} else {
|
|
|
|
folder = folderRef.(*object.Folder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
// make config spec
|
|
|
|
configSpec := types.VirtualMachineConfigSpec{
|
|
|
|
Name: vm.name,
|
|
|
|
NumCPUs: vm.vcpu,
|
|
|
|
NumCoresPerSocket: 1,
|
|
|
|
MemoryMB: vm.memoryMb,
|
2016-04-25 13:15:37 +02:00
|
|
|
MemoryAllocation: &types.ResourceAllocationInfo{
|
|
|
|
Reservation: vm.memoryAllocation.reservation,
|
|
|
|
},
|
2016-06-09 10:19:10 +02:00
|
|
|
Flags: &types.VirtualMachineFlagInfo{
|
|
|
|
DiskUuidEnabled: &vm.enableDiskUUID,
|
|
|
|
},
|
2016-05-17 23:18:35 +02:00
|
|
|
}
|
|
|
|
if vm.template == "" {
|
|
|
|
configSpec.GuestId = "otherLinux64Guest"
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] virtual machine config spec: %v", configSpec)
|
|
|
|
|
2015-11-09 02:02:07 +01:00
|
|
|
// make ExtraConfig
|
2015-11-09 05:50:09 +01:00
|
|
|
log.Printf("[DEBUG] virtual machine Extra Config spec start")
|
2015-11-09 02:02:07 +01:00
|
|
|
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,
|
|
|
|
}
|
2015-12-03 14:59:16 +01:00
|
|
|
log.Printf("[DEBUG] virtual machine Extra Config spec: %s,%s", k, v)
|
2015-11-09 02:02:07 +01:00
|
|
|
ov = append(ov, &o)
|
|
|
|
}
|
|
|
|
configSpec.ExtraConfig = ov
|
|
|
|
log.Printf("[DEBUG] virtual machine Extra Config spec: %v", configSpec.ExtraConfig)
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
var datastore *object.Datastore
|
|
|
|
if vm.datastore == "" {
|
|
|
|
datastore, err = finder.DefaultDatastore(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
datastore, err = finder.Datastore(context.TODO(), vm.datastore)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: datastore cluster support in govmomi finder function
|
|
|
|
d, err := getDatastoreObject(c, dcFolders, vm.datastore)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Type == "StoragePod" {
|
|
|
|
sp := object.StoragePod{
|
2016-02-17 20:39:12 +01:00
|
|
|
Folder: object.NewFolder(c.Client, d),
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
2015-11-25 15:35:37 +01:00
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
var sps types.StoragePlacementSpec
|
|
|
|
if vm.template != "" {
|
|
|
|
sps = buildStoragePlacementSpecClone(c, dcFolders, template, resourcePool, sp)
|
|
|
|
} else {
|
|
|
|
sps = buildStoragePlacementSpecCreate(dcFolders, resourcePool, sp, configSpec)
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
2015-12-12 09:33:01 +01:00
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
datastore, err = findDatastore(c, sps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
datastore = object.NewDatastore(c.Client, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
log.Printf("[DEBUG] datastore: %#v", datastore)
|
2015-10-06 05:53:07 +02:00
|
|
|
|
|
|
|
// network
|
|
|
|
networkDevices := []types.BaseVirtualDeviceConfigSpec{}
|
|
|
|
networkConfigs := []types.CustomizationAdapterMapping{}
|
|
|
|
for _, network := range vm.networkInterfaces {
|
|
|
|
// network device
|
2016-05-17 23:18:35 +02:00
|
|
|
var networkDeviceType string
|
|
|
|
if vm.template == "" {
|
|
|
|
networkDeviceType = "e1000"
|
|
|
|
} else {
|
|
|
|
networkDeviceType = "vmxnet3"
|
|
|
|
}
|
2016-06-02 01:38:56 +02:00
|
|
|
nd, err := buildNetworkDevice(finder, network.label, networkDeviceType, network.macAddress)
|
2015-10-06 05:53:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-02 01:38:56 +02:00
|
|
|
log.Printf("[DEBUG] network device: %+v", nd.Device)
|
2015-10-06 05:53:07 +02:00
|
|
|
networkDevices = append(networkDevices, nd)
|
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
if vm.template != "" {
|
|
|
|
var ipSetting types.CustomizationIPSettings
|
|
|
|
if network.ipv4Address == "" {
|
|
|
|
ipSetting.Ip = &types.CustomizationDhcpIpGenerator{}
|
|
|
|
} else {
|
|
|
|
if network.ipv4PrefixLength == 0 {
|
|
|
|
return fmt.Errorf("Error: ipv4_prefix_length argument is empty.")
|
|
|
|
}
|
|
|
|
m := net.CIDRMask(network.ipv4PrefixLength, 32)
|
|
|
|
sm := net.IPv4(m[0], m[1], m[2], m[3])
|
|
|
|
subnetMask := sm.String()
|
|
|
|
log.Printf("[DEBUG] ipv4 gateway: %v\n", network.ipv4Gateway)
|
|
|
|
log.Printf("[DEBUG] ipv4 address: %v\n", network.ipv4Address)
|
|
|
|
log.Printf("[DEBUG] ipv4 prefix length: %v\n", network.ipv4PrefixLength)
|
|
|
|
log.Printf("[DEBUG] ipv4 subnet mask: %v\n", subnetMask)
|
|
|
|
ipSetting.Gateway = []string{
|
|
|
|
network.ipv4Gateway,
|
|
|
|
}
|
|
|
|
ipSetting.Ip = &types.CustomizationFixedIp{
|
|
|
|
IpAddress: network.ipv4Address,
|
|
|
|
}
|
|
|
|
ipSetting.SubnetMask = subnetMask
|
2016-05-03 18:27:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
ipv6Spec := &types.CustomizationIPSettingsIpV6AddressSpec{}
|
|
|
|
if network.ipv6Address == "" {
|
|
|
|
ipv6Spec.Ip = []types.BaseCustomizationIpV6Generator{
|
|
|
|
&types.CustomizationDhcpIpV6Generator{},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("[DEBUG] ipv6 gateway: %v\n", network.ipv6Gateway)
|
|
|
|
log.Printf("[DEBUG] ipv6 address: %v\n", network.ipv6Address)
|
|
|
|
log.Printf("[DEBUG] ipv6 prefix length: %v\n", network.ipv6PrefixLength)
|
|
|
|
|
|
|
|
ipv6Spec.Ip = []types.BaseCustomizationIpV6Generator{
|
|
|
|
&types.CustomizationFixedIpV6{
|
|
|
|
IpAddress: network.ipv6Address,
|
|
|
|
SubnetMask: int32(network.ipv6PrefixLength),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ipv6Spec.Gateway = []string{network.ipv6Gateway}
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
2016-05-17 23:18:35 +02:00
|
|
|
ipSetting.IpV6Spec = ipv6Spec
|
2015-10-06 05:53:07 +02:00
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
// network config
|
|
|
|
config := types.CustomizationAdapterMapping{
|
|
|
|
Adapter: ipSetting,
|
2015-11-09 02:02:07 +01:00
|
|
|
}
|
2016-05-17 23:18:35 +02:00
|
|
|
networkConfigs = append(networkConfigs, config)
|
2015-11-09 02:02:07 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-02 01:38:56 +02:00
|
|
|
log.Printf("[DEBUG] network devices: %#v", networkDevices)
|
|
|
|
log.Printf("[DEBUG] network configs: %#v", networkConfigs)
|
2015-11-09 02:02:07 +01:00
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
var task *object.Task
|
|
|
|
if vm.template == "" {
|
|
|
|
var mds mo.Datastore
|
|
|
|
if err = datastore.Properties(context.TODO(), datastore.Reference(), []string{"name"}, &mds); err != nil {
|
|
|
|
return err
|
2016-04-13 21:41:58 +02:00
|
|
|
}
|
2016-05-17 23:18:35 +02:00
|
|
|
log.Printf("[DEBUG] datastore: %#v", mds.Name)
|
|
|
|
scsi, err := object.SCSIControllerTypes().CreateSCSIController("scsi")
|
2016-03-10 22:12:33 +01:00
|
|
|
if err != nil {
|
2016-05-17 23:18:35 +02:00
|
|
|
log.Printf("[ERROR] %s", err)
|
2016-03-10 22:12:33 +01:00
|
|
|
}
|
2016-03-23 21:46:33 +01:00
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
configSpec.DeviceChange = append(configSpec.DeviceChange, &types.VirtualDeviceConfigSpec{
|
|
|
|
Operation: types.VirtualDeviceConfigSpecOperationAdd,
|
|
|
|
Device: scsi,
|
|
|
|
})
|
2016-03-23 21:46:33 +01:00
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
configSpec.Files = &types.VirtualMachineFileInfo{VmPathName: fmt.Sprintf("[%s]", mds.Name)}
|
2016-03-23 21:46:33 +01:00
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
task, err = folder.CreateVM(context.TODO(), configSpec, resourcePool, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] %s", err)
|
2016-03-23 21:46:33 +01:00
|
|
|
}
|
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
err = task.Wait(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] %s", err)
|
2016-03-23 21:46:33 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 22:12:33 +01:00
|
|
|
} else {
|
2016-05-17 23:18:35 +02:00
|
|
|
|
|
|
|
relocateSpec, err := buildVMRelocateSpec(resourcePool, datastore, template, vm.linkedClone, vm.hardDisks[0].initType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-03-10 22:12:33 +01:00
|
|
|
}
|
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
log.Printf("[DEBUG] relocate spec: %v", relocateSpec)
|
2015-10-06 05:53:07 +02:00
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
// make vm clone spec
|
|
|
|
cloneSpec := types.VirtualMachineCloneSpec{
|
|
|
|
Location: relocateSpec,
|
|
|
|
Template: false,
|
|
|
|
Config: &configSpec,
|
|
|
|
PowerOn: false,
|
2016-02-24 16:39:24 +01:00
|
|
|
}
|
2016-05-17 23:18:35 +02:00
|
|
|
if vm.linkedClone {
|
|
|
|
if template_mo.Snapshot == nil {
|
|
|
|
return fmt.Errorf("`linkedClone=true`, but image VM has no snapshots")
|
|
|
|
}
|
|
|
|
cloneSpec.Snapshot = template_mo.Snapshot.CurrentSnapshot
|
2016-02-24 16:39:24 +01:00
|
|
|
}
|
2016-05-17 23:18:35 +02:00
|
|
|
log.Printf("[DEBUG] clone spec: %v", cloneSpec)
|
2015-10-06 05:53:07 +02:00
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
task, err = template.Clone(context.TODO(), folder, vm.name, cloneSpec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
|
2016-05-17 23:18:35 +02:00
|
|
|
err = task.Wait(context.TODO())
|
2015-10-06 05:53:07 +02:00
|
|
|
if err != nil {
|
2016-05-17 23:18:35 +02:00
|
|
|
log.Printf("[ERROR] %s", err)
|
2015-10-06 05:53:07 +02:00
|
|
|
}
|
|
|
|
|
2015-11-16 19:44:39 +01:00
|
|
|
newVM, err := finder.VirtualMachine(context.TODO(), vm.Path())
|
2015-10-06 05:53:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] new vm: %v", newVM)
|
|
|
|
|
2015-10-28 08:43:43 +01:00
|
|
|
devices, err := newVM.Device(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[DEBUG] Template devices can't be found")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dvc := range devices {
|
|
|
|
// Issue 3559/3560: Delete all ethernet devices to add the correct ones later
|
|
|
|
if devices.Type(dvc) == "ethernet" {
|
2016-05-13 16:27:45 +02:00
|
|
|
err := newVM.RemoveDevice(context.TODO(), false, dvc)
|
2015-10-28 08:43:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add Network devices
|
|
|
|
for _, dvc := range networkDevices {
|
|
|
|
err := newVM.AddDevice(
|
|
|
|
context.TODO(), dvc.GetVirtualDeviceConfigSpec().Device)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 15:35:37 +01:00
|
|
|
// Create the cdroms if needed.
|
2016-08-16 09:58:22 +02:00
|
|
|
if err := createCdroms(c, newVM, dc, vm.cdroms); err != nil {
|
2015-11-25 15:35:37 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:37:06 +02:00
|
|
|
newVM.Properties(context.TODO(), newVM.Reference(), []string{"summary", "config"}, &vm_mo)
|
2016-05-17 23:18:35 +02:00
|
|
|
firstDisk := 0
|
|
|
|
if vm.template != "" {
|
|
|
|
firstDisk++
|
|
|
|
}
|
|
|
|
for i := firstDisk; i < len(vm.hardDisks); i++ {
|
|
|
|
log.Printf("[DEBUG] disk index: %v", i)
|
2016-05-23 23:37:06 +02:00
|
|
|
|
|
|
|
var diskPath string
|
|
|
|
switch {
|
|
|
|
case vm.hardDisks[i].vmdkPath != "":
|
|
|
|
diskPath = vm.hardDisks[i].vmdkPath
|
|
|
|
case vm.hardDisks[i].name != "":
|
|
|
|
snapshotFullDir := vm_mo.Config.Files.SnapshotDirectory
|
|
|
|
split := strings.Split(snapshotFullDir, " ")
|
|
|
|
if len(split) != 2 {
|
|
|
|
return fmt.Errorf("[ERROR] setupVirtualMachine - failed to split snapshot directory: %v", snapshotFullDir)
|
|
|
|
}
|
|
|
|
vmWorkingPath := split[1]
|
|
|
|
diskPath = vmWorkingPath + vm.hardDisks[i].name
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("[ERROR] setupVirtualMachine - Neither vmdk path nor vmdk name was given: %#v", vm.hardDisks[i])
|
|
|
|
}
|
|
|
|
err = addHardDisk(newVM, vm.hardDisks[i].size, vm.hardDisks[i].iops, vm.hardDisks[i].initType, datastore, diskPath, vm.hardDisks[i].controller)
|
2016-05-17 23:18:35 +02:00
|
|
|
if err != nil {
|
2016-08-04 21:29:02 +02:00
|
|
|
err2 := addHardDisk(newVM, vm.hardDisks[i].size, vm.hardDisks[i].iops, vm.hardDisks[i].initType, datastore, diskPath, vm.hardDisks[i].controller)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
2016-05-17 23:18:35 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vm.skipCustomization || vm.template == "" {
|
2016-04-21 17:53:08 +02:00
|
|
|
log.Printf("[DEBUG] VM customization skipped")
|
|
|
|
} else {
|
2016-05-17 23:18:35 +02:00
|
|
|
var identity_options types.BaseCustomizationIdentitySettings
|
|
|
|
if strings.HasPrefix(template_mo.Config.GuestId, "win") {
|
|
|
|
var timeZone int
|
|
|
|
if vm.timeZone == "Etc/UTC" {
|
|
|
|
vm.timeZone = "085"
|
|
|
|
}
|
|
|
|
timeZone, err := strconv.Atoi(vm.timeZone)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error converting TimeZone: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
guiUnattended := types.CustomizationGuiUnattended{
|
|
|
|
AutoLogon: false,
|
|
|
|
AutoLogonCount: 1,
|
|
|
|
TimeZone: int32(timeZone),
|
|
|
|
}
|
|
|
|
|
|
|
|
customIdentification := types.CustomizationIdentification{}
|
|
|
|
|
|
|
|
userData := types.CustomizationUserData{
|
|
|
|
ComputerName: &types.CustomizationFixedName{
|
|
|
|
Name: strings.Split(vm.name, ".")[0],
|
|
|
|
},
|
|
|
|
ProductId: vm.windowsOptionalConfig.productKey,
|
|
|
|
FullName: "terraform",
|
|
|
|
OrgName: "terraform",
|
|
|
|
}
|
|
|
|
|
|
|
|
if vm.windowsOptionalConfig.domainUserPassword != "" && vm.windowsOptionalConfig.domainUser != "" && vm.windowsOptionalConfig.domain != "" {
|
|
|
|
customIdentification.DomainAdminPassword = &types.CustomizationPassword{
|
|
|
|
PlainText: true,
|
|
|
|
Value: vm.windowsOptionalConfig.domainUserPassword,
|
|
|
|
}
|
|
|
|
customIdentification.DomainAdmin = vm.windowsOptionalConfig.domainUser
|
|
|
|
customIdentification.JoinDomain = vm.windowsOptionalConfig.domain
|
|
|
|
}
|
|
|
|
|
|
|
|
if vm.windowsOptionalConfig.adminPassword != "" {
|
|
|
|
guiUnattended.Password = &types.CustomizationPassword{
|
|
|
|
PlainText: true,
|
|
|
|
Value: vm.windowsOptionalConfig.adminPassword,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
identity_options = &types.CustomizationSysprep{
|
|
|
|
GuiUnattended: guiUnattended,
|
|
|
|
Identification: customIdentification,
|
|
|
|
UserData: userData,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
identity_options = &types.CustomizationLinuxPrep{
|
|
|
|
HostName: &types.CustomizationFixedName{
|
|
|
|
Name: strings.Split(vm.name, ".")[0],
|
|
|
|
},
|
|
|
|
Domain: vm.domain,
|
|
|
|
TimeZone: vm.timeZone,
|
|
|
|
HwClockUTC: types.NewBool(true),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create CustomizationSpec
|
|
|
|
customSpec := types.CustomizationSpec{
|
|
|
|
Identity: identity_options,
|
|
|
|
GlobalIPSettings: types.CustomizationGlobalIPSettings{
|
|
|
|
DnsSuffixList: vm.dnsSuffixes,
|
|
|
|
DnsServerList: vm.dnsServers,
|
|
|
|
},
|
|
|
|
NicSettingMap: networkConfigs,
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] custom spec: %v", customSpec)
|
|
|
|
|
2016-04-21 17:53:08 +02:00
|
|
|
log.Printf("[DEBUG] VM customization starting")
|
|
|
|
taskb, err := newVM.Customize(context.TODO(), customSpec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = taskb.WaitForResult(context.TODO(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] VM customization finished")
|
2015-10-28 08:43:43 +01:00
|
|
|
}
|
|
|
|
|
2016-05-23 23:37:06 +02:00
|
|
|
if vm.hasBootableVmdk || vm.template != "" {
|
2017-02-01 15:31:40 +01:00
|
|
|
t, err := newVM.PowerOn(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = t.WaitForResult(context.TODO(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-12 10:05:02 +02:00
|
|
|
err = newVM.WaitForPowerState(context.TODO(), types.VirtualMachinePowerStatePoweredOn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-03 14:59:16 +01:00
|
|
|
}
|
2015-10-06 05:53:07 +02:00
|
|
|
return nil
|
|
|
|
}
|