1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.redhat.vmtruckloader.vmware.action;
23
24 import java.rmi.RemoteException;
25
26 import org.redhat.vmtruckloader.service.MachineSpecification;
27 import org.redhat.vmtruckloader.vmware.VMWareMachineSpecsUtils;
28 import org.redhat.vmtruckloader.vmware.VMWareManagedObjectUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.vmware.vim25.ConcurrentAccess;
33 import com.vmware.vim25.DuplicateName;
34 import com.vmware.vim25.FileFault;
35 import com.vmware.vim25.InsufficientResourcesFault;
36 import com.vmware.vim25.InvalidDatastore;
37 import com.vmware.vim25.InvalidName;
38 import com.vmware.vim25.InvalidState;
39 import com.vmware.vim25.OptionValue;
40 import com.vmware.vim25.RuntimeFault;
41 import com.vmware.vim25.TaskInProgress;
42 import com.vmware.vim25.VirtualMachineConfigSpec;
43 import com.vmware.vim25.VmConfigFault;
44 import com.vmware.vim25.mo.ServiceInstance;
45 import com.vmware.vim25.mo.Task;
46 import com.vmware.vim25.mo.VirtualMachine;
47
48
49
50
51
52
53
54 public class EditMachineCallback extends AbstractVMWareActionCallback<VirtualMachine> {
55
56 private static final Logger LOGGER = LoggerFactory.getLogger(EditMachineCallback.class);
57
58 private final MachineSpecification spec;
59
60 public EditMachineCallback(MachineSpecification spec) {
61 this.spec = spec;
62 }
63
64 @Override
65 public VirtualMachine doInVmware(ServiceInstance serviceInstance) {
66 return runTask(doesVMexistsAndCanItBeModified(serviceInstance, spec), VMWareMachineSpecsUtils.buildVmSpecAndSetCpusAndMemory(serviceInstance, spec));
67 }
68
69 private VirtualMachine doesVMexistsAndCanItBeModified(ServiceInstance serviceInstance, MachineSpecification machineCreateWrapper) {
70 final VirtualMachine vm = VMWareManagedObjectUtils.getVm(serviceInstance, machineCreateWrapper.getVmName());
71 if ( vm == null )
72 throw new IllegalArgumentException("No VM with name:" + machineCreateWrapper.getVmName() + " (rename operation are not supported).");
73 if ( vm.getSummary().getRuntime().getPowerState().toString() == "poweredOn" )
74 throw new IllegalArgumentException(" can't change the hardware settings for a running vm ... Stop vm first.");
75 checkIfVMisNotStillDeploying(vm.getConfig().getExtraConfig());
76 return vm;
77 }
78
79 private void checkIfVMisNotStillDeploying(OptionValue[] optionsValues) {
80 for ( OptionValue optionValue : optionsValues ) {
81 if ( optionValue.getKey().equals("guestinfo.deploy.lock") && optionValue.getValue().equals("true") ) {
82 throw new IllegalArgumentException("VM " + spec.getVmName() +
83 " is still in deployment state, please finish deployment first before changing hardware settings");
84 }
85 }
86 }
87
88 private VirtualMachine runTask(VirtualMachine vm, VirtualMachineConfigSpec vmSpec) {
89 try {
90 Task task = vm.reconfigVM_Task(vmSpec);
91 task.waitForTask();
92 } catch (InvalidName e) {
93 throw new IllegalArgumentException(e);
94 } catch (VmConfigFault e) {
95 throw new IllegalArgumentException(e);
96 } catch (DuplicateName e) {
97 throw new IllegalArgumentException(e);
98 } catch (TaskInProgress e) {
99 throw new IllegalStateException(e);
100 } catch (FileFault e) {
101 throw new IllegalStateException(e);
102 } catch (InvalidState e) {
103 throw new IllegalStateException(e);
104 } catch (ConcurrentAccess e) {
105 throw new IllegalStateException(e);
106 } catch (InvalidDatastore e) {
107 throw new IllegalStateException(e);
108 } catch (InsufficientResourcesFault e) {
109 throw new IllegalStateException(e);
110 } catch (RuntimeFault e) {
111 throw new IllegalStateException(e);
112 } catch (RemoteException e) {
113 throw new IllegalStateException(e);
114 } catch (InterruptedException e) {
115 if ( LOGGER.isWarnEnabled() )
116 LOGGER.warn("Remote Task on vCenter timeout.");
117 }
118 return vm;
119 }
120 }