public class VSphere extends Object
Modifier and Type | Method and Description |
---|---|
void |
cloneOrDeployVm(String cloneName,
String sourceName,
boolean linkedClone,
String resourcePoolName,
String cluster,
String datastoreName,
String folderName,
boolean useCurrentSnapshot,
String namedSnapshot,
boolean powerOn,
Map<String,String> extraConfigParameters,
String customizationSpec,
PrintStream jLogger)
Creates a new VM by cloning an existing VM or Template.
|
void |
cloneVm(String cloneName,
String sourceName,
boolean linkedClone,
String resourcePoolName,
String cluster,
String datastoreName,
String folderName,
boolean powerOn,
String customizationSpec,
PrintStream jLogger)
Clones a new VM from an existing (named) VM.
|
static VSphere |
connect(String server,
boolean ignoreCert,
String user,
String pw)
Deprecated.
Use
connect(VSphereConnectionConfig) instead. |
static VSphere |
connect(VSphereConnectionConfig connectionDetails)
Initiates Connection to vSphere Server
|
int |
countVms() |
int |
countVmsByPrefix(String prefix) |
void |
deleteSnapshot(String vmName,
String snapName,
boolean consolidate,
boolean failOnNoExist) |
void |
deployVm(String cloneName,
String sourceName,
boolean linkedClone,
String resourcePoolName,
String cluster,
String datastoreName,
String folderName,
boolean powerOn,
String customizationSpec,
PrintStream jLogger)
Deploys a new VM from an existing (named) Template.
|
void |
destroyVm(String name,
boolean failOnNoExist)
Destroys the VM in vSphere
|
void |
disconnect()
Disconnect from vSphere server.
|
Boolean |
folderExists(String folderPath) |
com.vmware.vim25.CustomizationSpecItem |
getCustomizationSpecByName(String customizationSpecName) |
com.vmware.vim25.mo.ManagedEntity[] |
getDatastores() |
com.vmware.vim25.mo.DistributedVirtualPortgroup |
getDistributedVirtualPortGroupByName(com.vmware.vim25.mo.VirtualMachine virtualMachine,
String name)
Find Distributed Virtual Port Group name in the same Datacenter as the VM
|
com.vmware.vim25.mo.DistributedVirtualSwitch |
getDistributedVirtualSwitchByPortGroup(com.vmware.vim25.mo.DistributedVirtualPortgroup distributedVirtualPortgroup)
Find Distributed Virtual Switch from the provided Distributed Virtual Portgroup
|
com.vmware.vim25.mo.Folder |
getFolder(String folderPath) |
String |
getIp(com.vmware.vim25.mo.VirtualMachine vm,
int timeout)
Asks vSphere for the IP address used by a VM.
|
com.vmware.vim25.mo.Network |
getNetworkPortGroupByName(com.vmware.vim25.mo.VirtualMachine virtualMachine,
String name)
Find Distributed Virtual Port Group name in the same Datacenter as the VM
|
com.vmware.vim25.mo.VirtualMachineSnapshot |
getSnapshotInTree(com.vmware.vim25.mo.VirtualMachine vm,
String snapName) |
com.vmware.vim25.mo.VirtualMachine |
getVmByName(String vmName) |
void |
markAsTemplate(String vmName,
String snapName,
boolean force) |
void |
markAsVm(String name,
String resourcePool,
String cluster) |
void |
powerOffVm(com.vmware.vim25.mo.VirtualMachine vm,
boolean evenIfSuspended,
boolean shutdownGracefully)
Deprecated.
This method has been superseded by
powerOffVm(VirtualMachine, boolean, int) , which allows setting an arbitrary grace period. |
void |
powerOffVm(com.vmware.vim25.mo.VirtualMachine vm,
boolean evenIfSuspended,
int gracefulShutdownSeconds)
Power off the given virtual machine, optionally waiting a while for its operating system to shut down.
|
void |
reconfigureVm(String name,
com.vmware.vim25.VirtualMachineConfigSpec spec) |
void |
renameVm(String oldName,
String newName)
Renames the VM vSphere
|
void |
renameVmSnapshot(String vmName,
String oldName,
String newName,
String newDescription)
Renames a VM Snapshot
|
void |
revertToSnapshot(String vmName,
String snapName) |
void |
setExtraConfigParameters(String vmName,
Map<String,String> parameters)
Passes data to a VM's "extra config" object.
|
void |
startVm(String name,
int timeoutInSeconds) |
void |
suspendVm(com.vmware.vim25.mo.VirtualMachine vm) |
void |
takeSnapshot(String vmName,
String snapshot,
String description,
boolean snapMemory) |
boolean |
vmToolIsEnabled(com.vmware.vim25.mo.VirtualMachine vm) |
public static VSphere connect(@NonNull VSphereConnectionConfig connectionDetails) throws VSphereException
connectionDetails
- Contains all the details we need to connect.VSphereException
- If an error occurred.@Deprecated public static VSphere connect(@NonNull String server, boolean ignoreCert, @NonNull String user, @CheckForNull String pw) throws VSphereException
connect(VSphereConnectionConfig)
instead.server
- Server URLignoreCert
- If true then we disable certificate verification, allowing the use of untrusted certificates but risk man-in-the-middle attacks.user
- Username.pw
- Password.VSphereException
- If an error occurred.public void disconnect()
Note: This logs any Exception
it encounters - it does not pass
them to get to the calling method.
public void deployVm(String cloneName, String sourceName, boolean linkedClone, String resourcePoolName, String cluster, String datastoreName, String folderName, boolean powerOn, String customizationSpec, PrintStream jLogger) throws VSphereException
cloneName
- - name of VM to be createdsourceName
- - name of VM or template to be clonedlinkedClone
- - true if you want to re-use disk backingsresourcePoolName
- - resource pool to usecluster
- - ComputeClusterResource to usedatastoreName
- - Datastore to usefolderName
- - Folder name or path to usepowerOn
- - If true the VM will be powered on.customizationSpec
- - Customization spec to use for this VMjLogger
- - Where to log to.VSphereException
- If an error occurred.public void cloneVm(String cloneName, String sourceName, boolean linkedClone, String resourcePoolName, String cluster, String datastoreName, String folderName, boolean powerOn, String customizationSpec, PrintStream jLogger) throws VSphereException
cloneName
- - name of VM to be createdsourceName
- - name of VM or template to be clonedlinkedClone
- - true if you want to re-use disk backingsresourcePoolName
- - resource pool to usecluster
- - ComputeClusterResource to usedatastoreName
- - Datastore to usefolderName
- - Folder name or path to usepowerOn
- - If true the VM will be powered on.customizationSpec
- - Customization spec to use for this VMjLogger
- - Where to log to.VSphereException
- If an error occurred.public void cloneOrDeployVm(String cloneName, String sourceName, boolean linkedClone, String resourcePoolName, String cluster, String datastoreName, String folderName, boolean useCurrentSnapshot, String namedSnapshot, boolean powerOn, Map<String,String> extraConfigParameters, String customizationSpec, PrintStream jLogger) throws VSphereException
cloneName
- The name for the new VM.sourceName
- The name of the VM or Template that is to be cloned.linkedClone
- If true then the clone will be defined as a delta from the
original, rather than a "full fat" copy. If this is true then
you will need to use a snapshot.resourcePoolName
- (Optional) The name of the resource pool to use, or null.cluster
- (Optional) The name of the cluster, or null.datastoreName
- (Optional) The name of the data store, or null.folderName
- (Optional) The name or path of the VSphere folder, or nulluseCurrentSnapshot
- If true then the clone will be created from the source VM's
"current" snapshot. This means that the VM must have
at least one snapshot.namedSnapshot
- If set then the clone will be created from the source VM's
snapshot of this name. If this is set then
useCurrentSnapshot
must not be set.powerOn
- If true then the new VM will be switched on after it has been
created.extraConfigParameters
- (Optional) parameters to set in the VM's "extra config"
object. This data can then be read back at a later stage.In
the case of parameters whose name starts "guestinfo.", the
parameter can be read by the VMware Tools on the client OS.
e.g. a variable named "guestinfo.Foo" with value "Bar" could
be read on the guest using the command-line
vmtoolsd --cmd "info-get guestinfo.Foo".customizationSpec
- (Optional) Customization spec to use for this VM, or nulljLogger
- Where to log to.VSphereException
- if anything goes wrong.public void reconfigureVm(String name, com.vmware.vim25.VirtualMachineConfigSpec spec) throws VSphereException
VSphereException
public void startVm(String name, int timeoutInSeconds) throws VSphereException
name
- - Name of VM to starttimeoutInSeconds
- How long to wait for the VM to be running.VSphereException
- If an error occurred.public com.vmware.vim25.mo.VirtualMachineSnapshot getSnapshotInTree(com.vmware.vim25.mo.VirtualMachine vm, String snapName)
public void revertToSnapshot(String vmName, String snapName) throws VSphereException
VSphereException
public void deleteSnapshot(String vmName, String snapName, boolean consolidate, boolean failOnNoExist) throws VSphereException
VSphereException
public void takeSnapshot(String vmName, String snapshot, String description, boolean snapMemory) throws VSphereException
VSphereException
public void markAsTemplate(String vmName, String snapName, boolean force) throws VSphereException
VSphereException
public void markAsVm(String name, String resourcePool, String cluster) throws VSphereException
VSphereException
public String getIp(com.vmware.vim25.mo.VirtualMachine vm, int timeout) throws VSphereException
vm
- VirtualMachine name whose IP is to be returned.timeout
- How long to wait (in seconds) for the IP address to known to vSphere.VSphereException
- If an error occurred.public com.vmware.vim25.mo.VirtualMachine getVmByName(String vmName) throws VSphereException
vmName
- - name of VM object to retrieveVSphereException
- If an error occurred.public int countVms() throws VSphereException
VSphereException
public int countVmsByPrefix(String prefix) throws VSphereException
VSphereException
public Boolean folderExists(String folderPath) throws VSphereException
VSphereException
public com.vmware.vim25.mo.Folder getFolder(String folderPath) throws VSphereException
VSphereException
public com.vmware.vim25.CustomizationSpecItem getCustomizationSpecByName(String customizationSpecName) throws VSphereException
VSphereException
public com.vmware.vim25.mo.ManagedEntity[] getDatastores() throws VSphereException
VSphereException
- If an error occurred.public void destroyVm(String name, boolean failOnNoExist) throws VSphereException
name
- - VM object to destroyfailOnNoExist
- If true and the VM does not exist then a VSphereNotFoundException
will be thrown.VSphereException
- If an error occurred.public void renameVmSnapshot(String vmName, String oldName, String newName, String newDescription) throws VSphereException
vmName
- the name of the VM whose snapshot is being renamed.oldName
- the current name of the VM's snapshot.newName
- the new name of the VM's snapshot.newDescription
- the new description of the VM's snapshot.VSphereException
- If an error occurred.public void renameVm(String oldName, String newName) throws VSphereException
oldName
- the current name of the vmnewName
- the new name of the vmVSphereException
- If an error occurred.public boolean vmToolIsEnabled(com.vmware.vim25.mo.VirtualMachine vm)
@Deprecated public void powerOffVm(com.vmware.vim25.mo.VirtualMachine vm, boolean evenIfSuspended, boolean shutdownGracefully) throws VSphereException
powerOffVm(VirtualMachine, boolean, int)
, which allows setting an arbitrary grace period.vm
- The virtual machine to power off.evenIfSuspended
- If false, a suspended VM is left as it was. If true, a suspended VM gets fully powered off.shutdownGracefully
- If false, the VM is powered off immediately. If true (and VMware tools is installed), the guest operating system is given a grace period of 180 seconds to shut down.VSphereException
public void powerOffVm(com.vmware.vim25.mo.VirtualMachine vm, boolean evenIfSuspended, int gracefulShutdownSeconds) throws VSphereException
vm
- The virtual machine to power off.evenIfSuspended
- If false, a suspended VM is left as it was. If true, a suspended VM gets fully powered off.gracefulShutdownSeconds
- The number of seconds to wait for the guest operating system to shut down. If the passed value is zero or less (or if VMware tools is not installed on the VM), the VM is powered off immediately.VSphereException
public void suspendVm(com.vmware.vim25.mo.VirtualMachine vm) throws VSphereException
VSphereException
public com.vmware.vim25.mo.Network getNetworkPortGroupByName(com.vmware.vim25.mo.VirtualMachine virtualMachine, String name) throws VSphereException
virtualMachine
- - VM objectname
- - the name of the Port GroupVSphereException
- If an error occurred.public com.vmware.vim25.mo.DistributedVirtualPortgroup getDistributedVirtualPortGroupByName(com.vmware.vim25.mo.VirtualMachine virtualMachine, String name) throws VSphereException
virtualMachine
- - VM objectname
- - the name of the Port GroupVSphereException
- If an error occurred.public com.vmware.vim25.mo.DistributedVirtualSwitch getDistributedVirtualSwitchByPortGroup(com.vmware.vim25.mo.DistributedVirtualPortgroup distributedVirtualPortgroup) throws VSphereException
distributedVirtualPortgroup
- - DistributedVirtualPortgroup object for the provided vDS PortGroupVSphereException
- If an error occurred.public void setExtraConfigParameters(String vmName, Map<String,String> parameters) throws VSphereException
e.g. a variable named "guestinfo.Foo" with value "Bar" could be read on the guest using the command-line vmtoolsd --cmd "info-get guestinfo.Foo".
vmName
- The name of the VM.parameters
- A Map
of variable name to variable value.VSphereException
- If an error occurred.Copyright © 2016–2022. All rights reserved.