本文整理汇总了Golang中github.com/vmware/govmomi/object.Datastore类的典型用法代码示例。如果您正苦于以下问题:Golang Datastore类的具体用法?Golang Datastore怎么用?Golang Datastore使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Datastore类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: newVm
// newVm creates a new virtual machine.
func (vm *VirtualMachine) newVm(f *object.Folder, p *object.ResourcePool, ds *object.Datastore, h *object.HostSystem) error {
if vm.Hardware == nil {
return errors.New("Missing hardware configuration")
}
Logf("%s creating virtual machine\n", vm.ID())
spec := types.VirtualMachineConfigSpec{
Name: vm.Name,
Version: vm.Hardware.Version,
GuestId: vm.GuestID,
Annotation: vm.Annotation,
NumCPUs: vm.Hardware.Cpu,
NumCoresPerSocket: vm.Hardware.Cores,
MemoryMB: vm.Hardware.Memory,
MaxMksConnections: vm.MaxMksConnections,
Files: &types.VirtualMachineFileInfo{
VmPathName: ds.Path(vm.Name),
},
}
task, err := f.CreateVM(vm.ctx, spec, p, h)
if err != nil {
return err
}
return task.Wait(vm.ctx)
}
开发者ID:dnaeon,项目名称:gru,代码行数:29,代码来源:vsphere_vm.go
示例2: buildVMRelocateSpec
// buildVMRelocateSpec builds VirtualMachineRelocateSpec to set a place for a new VirtualMachine.
func buildVMRelocateSpec(rp *object.ResourcePool, ds *object.Datastore, vm *object.VirtualMachine) (types.VirtualMachineRelocateSpec, error) {
var key int
devices, err := vm.Device(context.TODO())
if err != nil {
return types.VirtualMachineRelocateSpec{}, err
}
for _, d := range devices {
if devices.Type(d) == "disk" {
key = d.GetVirtualDevice().Key
}
}
rpr := rp.Reference()
dsr := ds.Reference()
return types.VirtualMachineRelocateSpec{
Datastore: &dsr,
Pool: &rpr,
Disk: []types.VirtualMachineRelocateSpecDiskLocator{
types.VirtualMachineRelocateSpecDiskLocator{
Datastore: dsr,
DiskBackingInfo: &types.VirtualDiskFlatVer2BackingInfo{
DiskMode: "persistent",
ThinProvisioned: types.NewBool(false),
EagerlyScrub: types.NewBool(true),
},
DiskId: key,
},
},
}, nil
}
开发者ID:AssertSelenium,项目名称:terraform,代码行数:32,代码来源:resource_vsphere_virtual_machine.go
示例3: lsFolder
func (d *Dispatcher) lsFolder(ds *object.Datastore, dsPath string) (*types.HostDatastoreBrowserSearchResults, error) {
defer trace.End(trace.Begin(dsPath))
spec := types.HostDatastoreBrowserSearchSpec{
MatchPattern: []string{"*"},
}
b, err := ds.Browser(d.ctx)
if err != nil {
return nil, err
}
task, err := b.SearchDatastore(d.ctx, dsPath, &spec)
if err != nil {
return nil, err
}
info, err := task.WaitForResult(d.ctx, nil)
if err != nil {
return nil, err
}
res := info.Result.(types.HostDatastoreBrowserSearchResults)
return &res, nil
}
开发者ID:vmware,项目名称:vic,代码行数:25,代码来源:store_files.go
示例4: 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,
NumCPUs: int32(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
var datastore *object.Datastore
// If storage pod is specified, collect placement recommendations
if cmd.StoragePod != nil {
datastore, err = cmd.recommendDatastore(ctx, spec)
if err != nil {
return nil, err
}
} else {
datastore = cmd.Datastore
}
if !cmd.force {
vmxPath := fmt.Sprintf("%s/%s.vmx", cmd.name, cmd.name)
_, err := datastore.Stat(ctx, vmxPath)
if err == nil {
dsPath := cmd.Datastore.Path(vmxPath)
return nil, fmt.Errorf("File %s already exists", dsPath)
}
}
folder := cmd.Folder
spec.Files = &types.VirtualMachineFileInfo{
VmPathName: fmt.Sprintf("[%s]", datastore.Name()),
}
return folder.CreateVM(ctx, *spec, cmd.ResourcePool, cmd.HostSystem)
}
开发者ID:hmahmood,项目名称:govmomi,代码行数:58,代码来源:create.go
示例5: buildVMRelocateSpec
// buildVMRelocateSpec builds VirtualMachineRelocateSpec to set a place for a new VirtualMachine.
func buildVMRelocateSpec(finder *find.Finder, rp *object.ResourcePool, ds *object.Datastore, vm *object.VirtualMachine, linked bool) (types.VirtualMachineRelocateSpec, error) {
var key int
var parent *types.VirtualDiskFlatVer2BackingInfo
devices, err := vm.Device(context.TODO())
if err != nil {
return types.VirtualMachineRelocateSpec{}, err
}
for _, d := range devices {
if devices.Type(d) == "disk" {
vd := d.GetVirtualDevice()
parent = vd.Backing.(*types.VirtualDiskFlatVer2BackingInfo)
key = vd.Key
}
}
rpr := rp.Reference()
relocateSpec := types.VirtualMachineRelocateSpec{}
// Treat linked clones a bit differently.
if linked {
parentDs := strings.SplitN(parent.FileName[1:], "]", 2)
parentDsObj, err := finder.Datastore(context.TODO(), parentDs[0])
if err != nil {
return types.VirtualMachineRelocateSpec{}, err
}
parentDbObjRef := parentDsObj.Reference()
relocateSpec = types.VirtualMachineRelocateSpec{
Datastore: &parentDbObjRef,
Pool: &rpr,
DiskMoveType: "createNewChildDiskBacking",
}
} else {
dsr := ds.Reference()
relocateSpec = types.VirtualMachineRelocateSpec{
Datastore: &dsr,
Pool: &rpr,
Disk: []types.VirtualMachineRelocateSpecDiskLocator{
types.VirtualMachineRelocateSpecDiskLocator{
Datastore: dsr,
DiskId: key,
DiskBackingInfo: &types.VirtualDiskFlatVer2BackingInfo{
DiskMode: "persistent",
ThinProvisioned: types.NewBool(false),
EagerlyScrub: types.NewBool(true),
},
},
},
}
}
return relocateSpec, nil
}
开发者ID:NetworkBytes,项目名称:terraform,代码行数:56,代码来源:resource_vsphere_virtual_machine.go
示例6: addHardDisk
// addHardDisk adds a new Hard Disk to the VirtualMachine.
func addHardDisk(vm *object.VirtualMachine, size, iops int64, diskType string, datastore *object.Datastore, diskPath string, controller_type string) error {
devices, err := vm.Device(context.TODO())
if err != nil {
return err
}
log.Printf("[DEBUG] vm devices: %#v\n", devices)
controller, err := devices.FindDiskController(controller_type)
if err != nil {
return err
}
log.Printf("[DEBUG] disk controller: %#v\n", controller)
// 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 {
// TODO Check if diskPath & datastore exist
diskPath = fmt.Sprintf("[%v] %v", datastore.Name(), diskPath)
}
log.Printf("[DEBUG] addHardDisk - diskPath: %v", diskPath)
disk := devices.CreateDisk(controller, datastore.Reference(), diskPath)
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)
} else if diskType == "thin" {
// thin provisioned virtual disk
backing.ThinProvisioned = types.NewBool(true)
}
log.Printf("[DEBUG] addHardDisk: %#v\n", disk)
log.Printf("[DEBUG] addHardDisk capacity: %#v\n", disk.CapacityInKB)
return vm.AddDevice(context.TODO(), disk)
} else {
log.Printf("[DEBUG] addHardDisk: Disk already present.\n")
return nil
}
}
开发者ID:srikalyan,项目名称:terraform,代码行数:56,代码来源:resource_vsphere_virtual_machine.go
示例7: vmCleanup
func vmCleanup(dc *object.Datacenter, ds *object.Datastore, vmName string) error {
client := testAccProvider.Meta().(*govmomi.Client)
fileManager := object.NewFileManager(client.Client)
task, err := fileManager.DeleteDatastoreFile(context.TODO(), ds.Path(vmName), dc)
if err != nil {
log.Printf("[ERROR] checkForDisk - Couldn't delete vm folder '%v': %v", vmName, err)
return err
}
_, err = task.WaitForResult(context.TODO(), nil)
if err != nil {
log.Printf("[ERROR] checForDisk - Failed while deleting vm folder '%v': %v", vmName, err)
return err
}
return nil
}
开发者ID:paultyng,项目名称:terraform,代码行数:16,代码来源:resource_vsphere_virtual_machine_test.go
示例8: addHardDisk
// addHardDisk adds a new Hard Disk to the VirtualMachine.
func addHardDisk(vm *object.VirtualMachine, size, iops int64, diskType string, datastore *object.Datastore, diskPath string) error {
devices, err := vm.Device(context.TODO())
if err != nil {
return err
}
log.Printf("[DEBUG] vm devices: %#v\n", devices)
controller, err := devices.FindDiskController("scsi")
if err != nil {
return err
}
log.Printf("[DEBUG] disk controller: %#v\n", controller)
disk := devices.CreateDisk(controller, datastore.Reference(), diskPath)
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)
} else if diskType == "thin" {
// thin provisioned virtual disk
backing.ThinProvisioned = types.NewBool(true)
}
log.Printf("[DEBUG] addHardDisk: %#v\n", disk)
log.Printf("[DEBUG] addHardDisk: %#v\n", disk.CapacityInKB)
return vm.AddDevice(context.TODO(), disk)
} else {
log.Printf("[DEBUG] addHardDisk: Disk already present.\n")
return nil
}
}
开发者ID:ack,项目名称:terraform,代码行数:46,代码来源:resource_vsphere_virtual_machine.go
示例9: AddDisk
func (c *configSpec) AddDisk(ds *object.Datastore, path string) {
var devices object.VirtualDeviceList
controller, err := devices.CreateSCSIController("")
if err != nil {
panic(err)
}
devices = append(devices, controller)
disk := devices.CreateDisk(controller.(types.BaseVirtualController), ds.Reference(), ds.Path(path))
devices = append(devices, disk)
spec, err := devices.ConfigSpec(types.VirtualDeviceConfigSpecOperationAdd)
if err != nil {
panic(err)
}
c.DeviceChange = append(c.DeviceChange, spec...)
}
开发者ID:vmware,项目名称:vic,代码行数:19,代码来源:vmdk.go
示例10: createDatastoreFiles
func createDatastoreFiles(d *Dispatcher, ds *object.Datastore, t *testing.T) error {
tmpfile, err := ioutil.TempFile("", "tempDatastoreFile.vmdk")
if err != nil {
t.Errorf("Failed to create file: %s", err)
return err
}
defer os.Remove(tmpfile.Name()) // clean up
if err = ds.UploadFile(d.ctx, tmpfile.Name(), "Test/folder/data/temp.vmdk", nil); err != nil {
t.Errorf("Failed to upload file %q: %s", "Test/folder/data/temp.vmdk", err)
return err
}
if err = ds.UploadFile(d.ctx, tmpfile.Name(), "Test/folder/tempMetadata", nil); err != nil {
t.Errorf("Failed to upload file %q: %s", "Test/folder/tempMetadata", err)
return err
}
return nil
}
开发者ID:vmware,项目名称:vic,代码行数:19,代码来源:delete_test.go
示例11: AddDisk
func (c *configSpec) AddDisk(ds *object.Datastore, path string) {
controller := &types.VirtualLsiLogicController{
VirtualSCSIController: types.VirtualSCSIController{
SharedBus: types.VirtualSCSISharingNoSharing,
VirtualController: types.VirtualController{
BusNumber: 0,
VirtualDevice: types.VirtualDevice{
Key: -1,
},
},
},
}
controllerSpec := &types.VirtualDeviceConfigSpec{
Device: controller,
Operation: types.VirtualDeviceConfigSpecOperationAdd,
}
c.AddChange(controllerSpec)
disk := &types.VirtualDisk{
VirtualDevice: types.VirtualDevice{
Key: -1,
ControllerKey: -1,
UnitNumber: -1,
Backing: &types.VirtualDiskFlatVer2BackingInfo{
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
FileName: ds.Path(path),
},
DiskMode: string(types.VirtualDiskModePersistent),
ThinProvisioned: types.NewBool(true),
},
},
}
diskSpec := &types.VirtualDeviceConfigSpec{
Device: disk,
Operation: types.VirtualDeviceConfigSpecOperationAdd,
}
c.AddChange(diskSpec)
}
开发者ID:fdawg4l,项目名称:govmomi,代码行数:42,代码来源:vmdk.go
示例12: deleteDatastoreFiles
func (d *Dispatcher) deleteDatastoreFiles(ds *object.Datastore, path string, force bool) (bool, error) {
defer trace.End(trace.Begin(fmt.Sprintf("path %q, force %t", path, force)))
// refuse to delete everything on the datstore, ignore force
if path == "" {
dsn, _ := ds.ObjectName(d.ctx)
msg := fmt.Sprintf("refusing to remove datastore files for path \"\" on datastore %q", dsn)
return false, errors.New(msg)
}
var empty bool
dsPath := ds.Path(path)
res, err := d.lsFolder(ds, dsPath)
if err != nil {
if !types.IsFileNotFound(err) {
err = errors.Errorf("Failed to browse folder %q: %s", dsPath, err)
return empty, err
}
log.Debugf("Folder %q is not found", dsPath)
empty = true
return empty, nil
}
if len(res.File) > 0 && !force {
log.Debugf("Folder %q is not empty, leave it there", dsPath)
return empty, nil
}
m := object.NewFileManager(ds.Client())
if err = d.deleteFilesIteratively(m, ds, dsPath); err != nil {
return empty, err
}
return true, nil
}
开发者ID:vmware,项目名称:vic,代码行数:34,代码来源:store_files.go
示例13: buildVMRelocateSpec
// buildVMRelocateSpec builds VirtualMachineRelocateSpec to set a place for a new VirtualMachine.
func buildVMRelocateSpec(rp *object.ResourcePool, ds *object.Datastore, vm *object.VirtualMachine, linkedClone bool, initType string) (types.VirtualMachineRelocateSpec, error) {
var key int
var moveType string
if linkedClone {
moveType = "createNewChildDiskBacking"
} else {
moveType = "moveAllDiskBackingsAndDisallowSharing"
}
log.Printf("[DEBUG] relocate type: [%s]", moveType)
devices, err := vm.Device(context.TODO())
if err != nil {
return types.VirtualMachineRelocateSpec{}, err
}
for _, d := range devices {
if devices.Type(d) == "disk" {
key = d.GetVirtualDevice().Key
}
}
isThin := initType == "thin"
rpr := rp.Reference()
dsr := ds.Reference()
return types.VirtualMachineRelocateSpec{
Datastore: &dsr,
Pool: &rpr,
DiskMoveType: moveType,
Disk: []types.VirtualMachineRelocateSpecDiskLocator{
types.VirtualMachineRelocateSpecDiskLocator{
Datastore: dsr,
DiskBackingInfo: &types.VirtualDiskFlatVer2BackingInfo{
DiskMode: "persistent",
ThinProvisioned: types.NewBool(isThin),
EagerlyScrub: types.NewBool(!isThin),
},
DiskId: key,
},
},
}, nil
}
开发者ID:mafrosis,项目名称:terraform,代码行数:41,代码来源:resource_vsphere_virtual_machine.go
示例14: cloneVm
// cloneVm creates the virtual machine using a template.
func (vm *VirtualMachine) cloneVm(f *object.Folder, p *object.ResourcePool, ds *object.Datastore, h *object.HostSystem) error {
Logf("%s cloning virtual machine from %s\n", vm.ID(), vm.TemplateConfig.Use)
obj, err := vm.finder.VirtualMachine(vm.ctx, vm.TemplateConfig.Use)
if err != nil {
return err
}
folderRef := f.Reference()
datastoreRef := ds.Reference()
poolRef := p.Reference()
var hostRef *types.ManagedObjectReference
if h != nil {
ref := h.Reference()
hostRef = &ref
}
spec := types.VirtualMachineCloneSpec{
Location: types.VirtualMachineRelocateSpec{
Folder: &folderRef,
Datastore: &datastoreRef,
Pool: &poolRef,
Host: hostRef,
},
Template: vm.TemplateConfig.MarkAsTemplate,
PowerOn: vm.TemplateConfig.PowerOn,
}
task, err := obj.Clone(vm.ctx, f, vm.Name, spec)
if err != nil {
return err
}
return task.Wait(vm.ctx)
}
开发者ID:dnaeon,项目名称:gru,代码行数:37,代码来源:vsphere_vm.go
示例15: deleteUpgradeImages
func (d *Dispatcher) deleteUpgradeImages(ds *object.Datastore, settings *data.InstallerData) {
defer trace.End(trace.Begin(""))
log.Infof("Deleting upgrade images")
// do clean up aggressively, even the previous operation failed with context deadline excceeded.
d.ctx = context.Background()
m := object.NewFileManager(ds.Client())
file := ds.Path(path.Join(d.vmPathName, settings.ApplianceISO))
if err := d.deleteVMFSFiles(m, ds, file); err != nil {
log.Warnf("Image file %q is not removed for %s. Use the vSphere UI to delete content", file, err)
}
file = ds.Path(path.Join(d.vmPathName, settings.BootstrapISO))
if err := d.deleteVMFSFiles(m, ds, file); err != nil {
log.Warnf("Image file %q is not removed for %s. Use the vSphere UI to delete content", file, err)
}
}
开发者ID:vmware,项目名称:vic,代码行数:20,代码来源:upgrade.go
示例16: 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{
//.........这里部分代码省略.........
开发者ID:krishnagade18,项目名称:terraform,代码行数:101,代码来源:resource_vsphere_virtual_machine.go
示例17: isVSAN
func (d *Dispatcher) isVSAN(ds *object.Datastore) bool {
dsType, _ := ds.Type(d.ctx)
return dsType == types.HostFileSystemVolumeFileSystemTypeVsan
}
开发者ID:vmware,项目名称:vic,代码行数:5,代码来源:store_files.go
示例18: setupVirtualMachine
func (vm *virtualMachine) setupVirtualMachine(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 template *object.VirtualMachine
var template_mo mo.VirtualMachine
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
}
}
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)
}
}
// make config spec
configSpec := types.VirtualMachineConfigSpec{
Name: vm.name,
NumCPUs: vm.vcpu,
NumCoresPerSocket: 1,
MemoryMB: vm.memoryMb,
MemoryAllocation: &types.ResourceAllocationInfo{
Reservation: vm.memoryAllocation.reservation,
},
}
if vm.template == "" {
configSpec.GuestId = "otherLinux64Guest"
}
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 == "" {
//.........这里部分代码省略.........
开发者ID:ack,项目名称:terraform,代码行数:101,代码来源:resource_vsphere_virtual_machine.go
示例19: 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
}
//.........这里部分代码省略.........
开发者ID:mafrosis,项目名称:terraform,代码行数:101,代码来源:resource_vsphere_virtual_machine.go
示例20: 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)
//.........这里部分代码省略.........
开发者ID:panther-it,项目名称:terraform-provider-vsphere,代码行数:101,代码来源:virtual_machine.go
注:本文中的github.com/vmware/govmomi/object.Datastore类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论