View Javadoc

1   /*
2    * JBoss, Home of Professional Open Source.
3    * Copyright 2013, Red Hat Middleware LLC, and individual contributors
4    * as indicated by the @author tags. See the copyright.txt file in the
5    * distribution for a full listing of individual contributors.
6    *
7    * This is free software; you can redistribute it and/or modify it
8    * under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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   * <p>Callback in charge of creating a new Machine</p>
50   *
51   * @author Romain Pelisse - <romain@redhat.com>
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 }