本文整理汇总了Golang中github.com/vmware/govmomi/vim25/types.VirtualMachineConfigSpec类的典型用法代码示例。如果您正苦于以下问题:Golang VirtualMachineConfigSpec类的具体用法?Golang VirtualMachineConfigSpec怎么用?Golang VirtualMachineConfigSpec使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了VirtualMachineConfigSpec类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Attach
func (m *Manager) Attach(op trace.Operation, disk *types.VirtualDisk) error {
deviceList := object.VirtualDeviceList{}
deviceList = append(deviceList, disk)
changeSpec, err := deviceList.ConfigSpec(types.VirtualDeviceConfigSpecOperationAdd)
if err != nil {
return err
}
machineSpec := types.VirtualMachineConfigSpec{}
machineSpec.DeviceChange = append(machineSpec.DeviceChange, changeSpec...)
m.reconfig.Lock()
_, err = m.vm.WaitForResult(op, func(ctx context.Context) (tasks.Task, error) {
t, er := m.vm.Reconfigure(ctx, machineSpec)
op.Debugf("Attach reconfigure task=%s", t.Reference())
return t, er
})
m.reconfig.Unlock()
if err != nil {
op.Errorf("vmdk storage driver failed to attach disk: %s", errors.ErrorStack(err))
return errors.Trace(err)
}
return nil
}
示例2: configureDevice
func (v VirtualMachine) configureDevice(ctx context.Context, op types.VirtualDeviceConfigSpecOperation, fop types.VirtualDeviceConfigSpecFileOperation, devices ...types.BaseVirtualDevice) error {
spec := types.VirtualMachineConfigSpec{}
for _, device := range devices {
config := &types.VirtualDeviceConfigSpec{
Device: device,
Operation: op,
}
if disk, ok := device.(*types.VirtualDisk); ok {
config.FileOperation = fop
// Special case to attach an existing disk
if op == types.VirtualDeviceConfigSpecOperationAdd && disk.CapacityInKB == 0 {
childDisk := false
if b, ok := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo); ok {
childDisk = b.Parent != nil
}
if !childDisk {
config.FileOperation = "" // existing disk
}
}
}
spec.DeviceChange = append(spec.DeviceChange, config)
}
task, err := v.Reconfigure(ctx, spec)
if err != nil {
return err
}
return task.Wait(ctx)
}
示例3: Detach
func (m *Manager) Detach(op trace.Operation, d *VirtualDisk) error {
defer trace.End(trace.Begin(d.DevicePath))
op.Infof("Detaching disk %s", d.DevicePath)
d.lock()
defer d.unlock()
if !d.Attached() {
op.Infof("Disk %s is already detached", d.DevicePath)
return nil
}
if err := d.canBeDetached(); err != nil {
return errors.Trace(err)
}
spec := types.VirtualMachineConfigSpec{}
disk, err := findDisk(op, m.vm, d.DatastoreURI)
if err != nil {
return errors.Trace(err)
}
config := []types.BaseVirtualDeviceConfigSpec{
&types.VirtualDeviceConfigSpec{
Device: disk,
Operation: types.VirtualDeviceConfigSpecOperationRemove,
},
}
spec.DeviceChange = config
m.reconfig.Lock()
_, err = m.vm.WaitForResult(op, func(ctx context.Context) (tasks.Task, error) {
t, er := m.vm.Reconfigure(ctx, spec)
op.Debugf("Detach reconfigure task=%s", t.Reference())
return t, er
})
m.reconfig.Unlock()
if err != nil {
op.Errorf(err.Error())
log.Warnf("detach for %s failed with %s", d.DevicePath, errors.ErrorStack(err))
return errors.Trace(err)
}
func() {
select {
case <-m.maxAttached:
default:
}
}()
return d.setDetached()
}
示例4: SetBootOptions
// SetBootOptions reconfigures the VirtualMachine with the given options.
func (v VirtualMachine) SetBootOptions(ctx context.Context, options *types.VirtualMachineBootOptions) error {
spec := types.VirtualMachineConfigSpec{}
spec.BootOptions = options
task, err := v.Reconfigure(ctx, spec)
if err != nil {
return err
}
return task.Wait(ctx)
}
示例5: Detach
func (m *Manager) Detach(ctx context.Context, d *VirtualDisk) error {
defer trace.End(trace.Begin(d.DevicePath))
log.Infof("Detaching disk %s", d.DevicePath)
d.lock()
defer d.unlock()
if !d.Attached() {
log.Infof("Disk %s is already detached", d.DevicePath)
return nil
}
if err := d.canBeDetached(); err != nil {
return errors.Trace(err)
}
spec := types.VirtualMachineConfigSpec{}
disk, err := findDisk(ctx, m.vm, d.DatastoreURI)
if err != nil {
return errors.Trace(err)
}
config := []types.BaseVirtualDeviceConfigSpec{
&types.VirtualDeviceConfigSpec{
Device: disk,
Operation: types.VirtualDeviceConfigSpecOperationRemove,
},
}
spec.DeviceChange = config
err = tasks.Wait(ctx, func(ctx context.Context) (tasks.Waiter, error) {
return m.vm.Reconfigure(ctx, spec)
})
if err != nil {
log.Warnf("detach for %s failed with %s", d.DevicePath, errors.ErrorStack(err))
return errors.Trace(err)
}
func() {
select {
case <-m.maxAttached:
default:
}
}()
return d.setDetached()
}
示例6: createVM
func (cmd *create) createVM(ctx context.Context) (*object.Task, error) {
var devices object.VirtualDeviceList
var err error
spec := types.VirtualMachineConfigSpec{
Name: cmd.name,
GuestId: cmd.guestID,
Files: &types.VirtualMachineFileInfo{VmPathName: fmt.Sprintf("[%s]", cmd.Datastore.Name())},
NumCPUs: cmd.cpus,
MemoryMB: int64(cmd.memory),
}
devices, err = cmd.addStorage(nil)
if err != nil {
return nil, err
}
devices, err = cmd.addNetwork(devices)
if err != nil {
return nil, err
}
deviceChange, err := devices.ConfigSpec(types.VirtualDeviceConfigSpecOperationAdd)
if err != nil {
return nil, err
}
spec.DeviceChange = deviceChange
if !cmd.force {
vmxPath := fmt.Sprintf("%s/%s.vmx", cmd.name, cmd.name)
_, err := cmd.Datastore.Stat(ctx, vmxPath)
if err == nil {
dsPath := cmd.Datastore.Path(vmxPath)
return nil, fmt.Errorf("File %s already exists", dsPath)
}
}
folders, err := cmd.Datacenter.Folders(ctx)
if err != nil {
return nil, err
}
return folders.VmFolder.CreateVM(ctx, spec, cmd.ResourcePool, cmd.HostSystem)
}
示例7: createVM
func (cmd *create) createVM(name string) (*object.Task, error) {
spec := types.VirtualMachineConfigSpec{
Name: name,
GuestId: cmd.guestID,
Files: &types.VirtualMachineFileInfo{VmPathName: fmt.Sprintf("[%s]", cmd.Datastore.Name())},
NumCPUs: cmd.cpus,
MemoryMB: int64(cmd.memory),
}
if !cmd.force {
vmxPath := fmt.Sprintf("%s/%s.vmx", name, name)
_, err := cmd.Datastore.Stat(context.TODO(), vmxPath)
if err == nil {
dsPath := cmd.Datastore.Path(vmxPath)
return nil, fmt.Errorf("File %s already exists", dsPath)
}
}
if cmd.controller != "ide" {
scsi, err := object.SCSIControllerTypes().CreateSCSIController(cmd.controller)
if err != nil {
return nil, err
}
spec.DeviceChange = append(spec.DeviceChange, &types.VirtualDeviceConfigSpec{
Operation: types.VirtualDeviceConfigSpecOperationAdd,
Device: scsi,
})
}
folders, err := cmd.Datacenter.Folders(context.TODO())
if err != nil {
return nil, err
}
return folders.VmFolder.CreateVM(context.TODO(), spec, cmd.ResourcePool, cmd.HostSystem)
}
示例8: createVirtualMachine
// createVirtualMchine creates a new VirtualMachine.
func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error {
dc, err := getDatacenter(c, vm.datacenter)
if err != nil {
return err
}
finder := find.NewFinder(c.Client, true)
finder = finder.SetDatacenter(dc)
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
}
// network
networkDevices := []types.BaseVirtualDeviceConfigSpec{}
for _, network := range vm.networkInterfaces {
// network device
nd, err := createNetworkDevice(finder, network.label, "e1000")
if err != nil {
return err
}
networkDevices = append(networkDevices, nd)
}
// make config spec
configSpec := types.VirtualMachineConfigSpec{
GuestId: "otherLinux64Guest",
Name: vm.name,
NumCPUs: vm.vcpu,
NumCoresPerSocket: 1,
MemoryMB: vm.memoryMb,
DeviceChange: networkDevices,
}
log.Printf("[DEBUG] virtual machine config spec: %v", configSpec)
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{
object.NewFolder(c.Client, d),
}
sps := createStoragePlacementSpecCreate(dcFolders, resourcePool, sp, configSpec)
datastore, err = findDatastore(c, sps)
if err != nil {
return err
}
} else {
datastore = object.NewDatastore(c.Client, d)
}
}
}
log.Printf("[DEBUG] datastore: %#v", datastore)
var mds mo.Datastore
if err = datastore.Properties(context.TODO(), datastore.Reference(), []string{"name"}, &mds); err != nil {
return err
}
log.Printf("[DEBUG] datastore: %#v", mds.Name)
scsi, err := object.SCSIControllerTypes().CreateSCSIController("scsi")
if err != nil {
log.Printf("[ERROR] %s", err)
}
configSpec.DeviceChange = append(configSpec.DeviceChange, &types.VirtualDeviceConfigSpec{
//.........这里部分代码省略.........
示例9: deployVirtualMachine
//.........这里部分代码省略.........
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] gateway: %v", vm.gateway)
log.Printf("[DEBUG] ipv4 address: %v", network.ipv4Address)
log.Printf("[DEBUG] ipv4 prefix length: %v", network.ipv4PrefixLength)
log.Printf("[DEBUG] ipv4 subnet mask: %v", subnetMask)
ipSetting = types.CustomizationIPSettings{
Gateway: []string{
vm.gateway,
},
Ip: &types.CustomizationFixedIp{
IpAddress: network.ipv4Address,
},
SubnetMask: subnetMask,
}
}
// network config
config := types.CustomizationAdapterMapping{
Adapter: ipSetting,
}
networkConfigs = append(networkConfigs, config)
}
log.Printf("[DEBUG] network configs: %v", networkConfigs[0].Adapter)
// make config spec
configSpec := types.VirtualMachineConfigSpec{
NumCPUs: vm.vcpu,
NumCoresPerSocket: 1,
MemoryMB: vm.memoryMb,
}
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
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 template_mo mo.VirtualMachine
err = template.Properties(context.TODO(), template.Reference(), []string{"parent", "config.template", "config.guestId", "resourcePool", "snapshot", "guest.toolsVersionStatus2", "config.guestFullName"}, &template_mo)
var identity_options types.BaseCustomizationIdentitySettings
if strings.HasPrefix(template_mo.Config.GuestId, "win") {
var timeZone int
if vm.timeZone == "Etc/UTC" {
示例10: Create
// Create has the following implementation:
// 1. check whether the docker directory contains the boot2docker ISO
// 2. generate an SSH keypair and bundle it in a tar.
// 3. create a virtual machine with the boot2docker ISO mounted;
// 4. reconfigure the virtual machine network and disk size;
func (d *Driver) Create() error {
b2dutils := mcnutils.NewB2dUtils(d.StorePath)
if err := b2dutils.CopyIsoToMachineDir(d.Boot2DockerURL, d.MachineName); err != nil {
return err
}
log.Infof("Generating SSH Keypair...")
if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return err
}
// Create context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, err := d.vsphereLogin(ctx)
if err != nil {
return err
}
defer c.Logout(ctx)
// Create a new finder
f := find.NewFinder(c.Client, true)
dc, err := f.DatacenterOrDefault(ctx, d.Datacenter)
if err != nil {
return err
}
f.SetDatacenter(dc)
dss, err := f.DatastoreOrDefault(ctx, d.Datastore)
if err != nil {
return err
}
net, err := f.NetworkOrDefault(ctx, d.Network)
if err != nil {
return err
}
hs, err := f.HostSystemOrDefault(ctx, d.HostSystem)
if err != nil {
return err
}
var rp *object.ResourcePool
if d.Pool != "" {
// Find specified Resource Pool
rp, err = f.ResourcePool(ctx, d.Pool)
if err != nil {
return err
}
} else {
// Pick default Resource Pool for Host System
rp, err = hs.ResourcePool(ctx)
if err != nil {
return err
}
}
spec := types.VirtualMachineConfigSpec{
Name: d.MachineName,
GuestId: "otherLinux64Guest",
Files: &types.VirtualMachineFileInfo{VmPathName: fmt.Sprintf("[%s]", dss.Name())},
NumCPUs: int32(d.CPU),
MemoryMB: int64(d.Memory),
}
scsi, err := object.SCSIControllerTypes().CreateSCSIController("pvscsi")
if err != nil {
return err
}
spec.DeviceChange = append(spec.DeviceChange, &types.VirtualDeviceConfigSpec{
Operation: types.VirtualDeviceConfigSpecOperationAdd,
Device: scsi,
})
log.Infof("Creating VM...")
folders, err := dc.Folders(ctx)
task, err := folders.VmFolder.CreateVM(ctx, spec, rp, hs)
if err != nil {
return err
}
info, err := task.WaitForResult(ctx, nil)
if err != nil {
return err
}
log.Infof("Uploading Boot2docker ISO ...")
dsurl, err := dss.URL(ctx, dc, fmt.Sprintf("%s/%s", d.MachineName, isoFilename))
if err != nil {
return err
//.........这里部分代码省略.........
示例11: TestCreateVm
func TestCreateVm(t *testing.T) {
ctx := context.Background()
for _, model := range []*Model{ESX(), VPX()} {
defer model.Remove()
err := model.Create()
if err != nil {
t.Fatal(err)
}
s := model.Service.NewServer()
defer s.Close()
c, err := govmomi.NewClient(ctx, s.URL, true)
if err != nil {
t.Fatal(err)
}
spec := types.VirtualMachineConfigSpec{
// Note: real ESX allows the VM to be created without a GuestId,
// but will power on will fail.
GuestId: string(types.VirtualMachineGuestOsIdentifierOtherGuest),
}
steps := []func(){
func() {
spec.Name = "test"
},
func() {
spec.Files = &types.VirtualMachineFileInfo{
VmPathName: fmt.Sprintf("[LocalDS_0] %s/%s.vmx", spec.Name, spec.Name),
}
},
}
finder := find.NewFinder(c.Client, false)
dc, err := finder.DefaultDatacenter(ctx)
if err != nil {
t.Fatal(err)
}
finder.SetDatacenter(dc)
folders, err := dc.Folders(ctx)
if err != nil {
t.Fatal(err)
}
hosts, err := finder.HostSystemList(ctx, "*/*")
if err != nil {
t.Fatal(err)
}
nhosts := len(hosts)
host := hosts[rand.Intn(nhosts)]
pool, err := host.ResourcePool(ctx)
if err != nil {
t.Fatal(err)
}
if nhosts == 1 {
// test the default path against the ESX model
host = nil
}
vmFolder := folders.VmFolder
// expecting CreateVM to fail until all steps are taken
for _, step := range steps {
task, cerr := vmFolder.CreateVM(ctx, spec, pool, host)
if cerr != nil {
t.Fatal(err)
}
_, cerr = task.WaitForResult(ctx, nil)
if cerr == nil {
t.Error("expected error")
}
step()
}
task, err := vmFolder.CreateVM(ctx, spec, pool, host)
if err != nil {
t.Fatal(err)
}
info, err := task.WaitForResult(ctx, nil)
if err != nil {
t.Fatal(err)
}
vm := object.NewVirtualMachine(c.Client, info.Result.(types.ManagedObjectReference))
name, err := vm.ObjectName(ctx)
if err != nil {
t.Fatal(err)
}
if name != spec.Name {
//.........这里部分代码省略.........
示例12: createVirtualMachine
// createVirtualMachine creates a new VirtualMachine.
func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error {
dc, err := getDatacenter(c, vm.datacenter)
if err != nil {
return err
}
finder := find.NewFinder(c.Client, true)
finder = finder.SetDatacenter(dc)
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
}
log.Printf("[DEBUG] folder: %#v", vm.folder)
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)
}
}
// network
networkDevices := []types.BaseVirtualDeviceConfigSpec{}
for _, network := range vm.networkInterfaces {
// network device
nd, err := buildNetworkDevice(finder, network.label, "e1000")
if err != nil {
return err
}
networkDevices = append(networkDevices, nd)
}
// make config spec
configSpec := types.VirtualMachineConfigSpec{
GuestId: "otherLinux64Guest",
Name: vm.name,
NumCPUs: vm.vcpu,
NumCoresPerSocket: 1,
MemoryMB: vm.memoryMb,
MemoryAllocation: &types.ResourceAllocationInfo{
Reservation: vm.memoryAllocation.reservation,
},
DeviceChange: networkDevices,
}
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 {
key := k
value := v
o := types.OptionValue{
Key: key,
Value: &value,
}
log.Printf("[DEBUG] virtual machine Extra Config spec: %s,%s", k, v)
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())
if err != nil {
return err
}
//.........这里部分代码省略.........
示例13: createVirtualMachine
// createVirtualMchine creates a new VirtualMachine.
func (vm *virtualMachine) createVirtualMachine(c *govmomi.Client) error {
var dc *object.Datacenter
var err error
finder := find.NewFinder(c.Client, true)
if vm.datacenter != "" {
dc, err = finder.Datacenter(context.TODO(), vm.datacenter)
if err != nil {
return err
}
} else {
dc, err = finder.DefaultDatacenter(context.TODO())
if err != nil {
return err
}
}
finder = finder.SetDatacenter(dc)
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
}
// network
networkDevices := []types.BaseVirtualDeviceConfigSpec{}
for _, network := range vm.networkInterfaces {
// network device
nd, err := createNetworkDevice(finder, network.label, "e1000")
if err != nil {
return err
}
networkDevices = append(networkDevices, nd)
}
// make config spec
configSpec := types.VirtualMachineConfigSpec{
GuestId: "otherLinux64Guest",
Name: vm.name,
NumCPUs: vm.vcpu,
NumCoresPerSocket: 1,
MemoryMB: vm.memoryMb,
DeviceChange: networkDevices,
}
log.Printf("[DEBUG] virtual machine config spec: %v", configSpec)
var datastore *object.Datastore
if vm.datastore == "" {
datastore, err = finder.DefaultDatastore(context.TODO())
if err != nil {
return err
}
} else {
s := object.NewSearchIndex(c.Client)
ref, err := s.FindChild(context.TODO(), dcFolders.DatastoreFolder, vm.datastore)
if err != nil {
return err
}
log.Printf("[DEBUG] findDatastore: reference: %#v", ref)
mor := ref.Reference()
if mor.Type == "StoragePod" {
storagePod := object.NewFolder(c.Client, mor)
vmfr := dcFolders.VmFolder.Reference()
rpr := resourcePool.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)
//.........这里部分代码省略.........
示例14: deployVirtualMachine
//.........这里部分代码省略.........
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] gateway: %v", vm.gateway)
log.Printf("[DEBUG] ipv4 address: %v", network.ipv4Address)
log.Printf("[DEBUG] ipv4 prefix length: %v", network.ipv4PrefixLength)
log.Printf("[DEBUG] ipv4 subnet mask: %v", subnetMask)
ipSetting = types.CustomizationIPSettings{
Gateway: []string{
vm.gateway,
},
Ip: &types.CustomizationFixedIp{
IpAddress: network.ipv4Address,
},
SubnetMask: subnetMask,
}
}
// network config
config := types.CustomizationAdapterMapping{
Adapter: ipSetting,
}
networkConfigs = append(networkConfigs, config)
}
log.Printf("[DEBUG] network configs: %v", networkConfigs[0].Adapter)
// make config spec
configSpec := types.VirtualMachineConfigSpec{
NumCPUs: vm.vcpu,
NumCoresPerSocket: 1,
MemoryMB: vm.memoryMb,
}
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
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{
HostName: &types.CustomizationFixedName{
Name: strings.Split(vm.name, ".")[0],
},
Domain: vm.domain,
TimeZone: vm.timeZone,
示例15: resourceVSphereVirtualMachineUpdate
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") {
configSpec.NumCPUs = int32(d.Get("vcpu").(int))
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
}
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
if v, ok := d.GetOk("keep_on_remove"); ok {
keep = v.(bool)
}
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))
controller_type := disk["controller"].(string)
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, " ")
//.........这里部分代码省略.........