• vSphere SDK for Java


    示例代码:

    import com.vmware.vim25.*;
    import com.vmware.vim25.mo.*;
    import org.springframework.stereotype.Service;
    
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.rmi.RemoteException;
    import java.util.concurrent.atomic.AtomicInteger;
    
    @Service
    public class VirtualMachineService {
    
        private ServiceInstance vCenterServiceInstance;
    
        private InventoryNavigator inventoryNavigator;
    
        public VirtualMachineService() {
            vCenterServiceInstance = this.vcInit();
            inventoryNavigator = new InventoryNavigator(vCenterServiceInstance.getRootFolder());
        }
    
        public ServiceInstance vcInit(){
            try {
                return new ServiceInstance(new URL("https://10.110.164.133/sdk"), "administrator@vsphere.local", "VMware1!", true);
            } catch (MalformedURLException | RemoteException e) {
                e.printStackTrace();
            }
            return null;
        }private VirtualMachine getVirtualMachineByName(String virtualMachineName) throws RemoteException {
            return  (VirtualMachine) inventoryNavigator.searchManagedEntity("VirtualMachine", virtualMachineName);
        }public static VirtualDeviceConfigSpec createVirtualDiskSpec(int diskSizeGb, Integer unitNumber, Integer controllerKey, String fileName ) {
    
            // assemble diskBackingInfo
            VirtualDiskFlatVer2BackingInfo diskBackingInfo = new VirtualDiskFlatVer2BackingInfo();
            diskBackingInfo.setThinProvisioned(true);
            diskBackingInfo.setDiskMode("persistent");
            diskBackingInfo.setFileName(fileName);
    
            // assemble virtualDisk
            VirtualDisk virtualDisk = new VirtualDisk();
            virtualDisk.setBacking(diskBackingInfo);
            virtualDisk.setUnitNumber(unitNumber);
            virtualDisk.setCapacityInKB(diskSizeGb * 1024 * 1024);
            virtualDisk.setControllerKey(controllerKey);
            virtualDisk.setKey(-1);
    
            // assemble virtualDeviceConfigSpec
            VirtualDeviceConfigSpec diskSpec = new VirtualDeviceConfigSpec();
            diskSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.create);
            diskSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
            diskSpec.setDevice(virtualDisk);
            return diskSpec;
        }
    
        // 获取虚拟机磁盘管理的ControllerKey
        private int getControllerKeyByVirtualMachine(VirtualMachine vm) {
            AtomicInteger controllerKey = new AtomicInteger();
            if (vm != null) {
                VirtualDevice[] devices = vm.getConfig().getHardware().getDevice();
                if (devices != null && devices.length > 0) {
                    for (VirtualDevice device : devices) {
                        if (device instanceof VirtualDisk) {
                            controllerKey.set(device.getControllerKey());
                        }
                    }
                }
            }
            return controllerKey.get();
        }
    
        // 获取虚拟机已生成UnitNumber
        private int getUnitNumberByVirtualMachine(VirtualMachine vm) {
            AtomicInteger unitNumber = new AtomicInteger();
            if (vm != null) {
                VirtualDevice[] devices = vm.getConfig().getHardware().getDevice();
                if (devices != null && devices.length > 0) {
                    for (VirtualDevice device : devices) {
                        if (device instanceof VirtualDisk) {
                            unitNumber.set(device.getUnitNumber());
                        }
                    }
                }
            }
            unitNumber.set(unitNumber.get() + 1);
            return unitNumber.get();
        }
    
        private String getFreeDataStoreName(VirtualMachine vm, int size) throws Exception {
            Datastore[] dataStores = vm.getDatastores();
            for(Datastore datastore: dataStores)
            {
                DatastoreSummary ds = datastore.getSummary();
                if(ds.getFreeSpace() > size)
                {
                    return ds.getName();
                }
            }
            return null;
        }
    
        public static void main(String[] args) throws Exception {
            VirtualMachineService virtualMachineService = new VirtualMachineService();
    
            VirtualMachine virtualMachine = virtualMachineService.getVirtualMachineByName("T-001");
    
            Integer controllerKey = virtualMachineService.getControllerKeyByVirtualMachine(virtualMachine);
            Integer unitNumber = virtualMachineService.getUnitNumberByVirtualMachine(virtualMachine);
            int diskSize = 2;
            String diskName = "test_disk";
    
            String dsName = virtualMachineService.getFreeDataStoreName(virtualMachine, diskSize);
            String fileName = "[" + dsName + "] " + virtualMachine.getName() + "/" + diskName + ".vmdk";
            VirtualDeviceConfigSpec virtualDeviceConfigSpec = createVirtualDiskSpec(diskSize, unitNumber, controllerKey, fileName);
    
            VirtualMachineConfigSpec virtualMachineConfigSpec = new VirtualMachineConfigSpec();
            VirtualDeviceConfigSpec[] diskSpecArray = {virtualDeviceConfigSpec};
            virtualMachineConfigSpec.setDeviceChange(diskSpecArray);
            Task task = virtualMachine.reconfigVM_Task(virtualMachineConfigSpec);
            task.waitForTask();
        }
    }
  • 相关阅读:
    【linux基础】usleep和sleep的区别
    【算法基础】opencv函数approxPolyDP和Ramer-Douglas-Peucker Algorithm
    【算法基础】散点轮廓算法-Alpha Shapes
    【图像处理算法基础】图像分割经典算法-泛洪算法FloodFill
    .net core 部署在Linux系统上运行的环境搭建
    Linux部署Net Core网站,三种自定义绑定端口号的方法(UseUrls,UseKestrel,手动指定)
    .NetCore部署Linux环境搭建
    .Net Core 项目发布到Linux
    .Net Core 项目发布到Linux
    Linux桌面操作系统排行榜
  • 原文地址:https://www.cnblogs.com/vincenshen/p/10803254.html
Copyright © 2020-2023  润新知