1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.redhat.vmtruckloader.vmware;
24
25 import org.redhat.vmtruckloader.service.MachineSpecification;
26
27 import com.vmware.vim25.Description;
28 import com.vmware.vim25.VirtualDeviceConfigSpec;
29 import com.vmware.vim25.VirtualDeviceConfigSpecOperation;
30 import com.vmware.vim25.VirtualEthernetCard;
31 import com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo;
32 import com.vmware.vim25.VirtualMachineConfigSpec;
33 import com.vmware.vim25.VirtualVmxnet3;
34 import com.vmware.vim25.mo.ServiceInstance;
35
36
37
38
39
40
41 public class VMWareMachineSpecsUtils {
42
43 private final static long GIGABYTE = 1024L;
44
45 private final static String NETWORK_INTERFACE_NAME = "Network Adapter 1";
46 private final static String NETWORK_ADDRESS_TYPE = "generated";
47
48 public static VirtualMachineConfigSpec buildVmSpec(ServiceInstance serviceInstance, MachineSpecification spec) {
49 VirtualMachineConfigSpec vmSpec = new VirtualMachineConfigSpec();
50 vmSpec.setName(spec.getHostname());
51 vmSpec.setNumCPUs(spec.getNbCpu());
52 vmSpec.setMemoryMB(spec.getVRAM() * GIGABYTE);
53 vmSpec.setAnnotation(spec.getRole());
54
55 final String netName = spec.getVLAN();
56 VirtualDeviceConfigSpec ethernet = createNicSpec(netName,VirtualDeviceConfigSpecOperation.add);
57
58 vmSpec.setDeviceChange(new VirtualDeviceConfigSpec[] { ethernet,null });
59 return vmSpec;
60 }
61
62 private static VirtualDeviceConfigSpec createNicSpec(String netName, VirtualDeviceConfigSpecOperation operation) {
63 final String nicName = NETWORK_INTERFACE_NAME;
64 VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec();
65 nicSpec.setOperation(operation);
66
67 VirtualEthernetCard nic = new VirtualVmxnet3();
68 VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo();
69 nicBacking.setDeviceName(netName);
70
71 Description info = new Description();
72 info.setLabel(nicName);
73 info.setSummary(netName);
74 nic.setDeviceInfo(info);
75
76
77 nic.setAddressType(NETWORK_ADDRESS_TYPE);
78 nic.setBacking(nicBacking);
79 nic.setKey(0);
80
81 nicSpec.setDevice(nic);
82 return nicSpec;
83 }
84
85
86 public static VirtualMachineConfigSpec buildVmSpecAndSetCpusAndMemory(ServiceInstance serviceInstance, MachineSpecification spec) {
87 VirtualMachineConfigSpec vmSpec = new VirtualMachineConfigSpec();
88 vmSpec.setName(spec.getVmName());
89
90 if ( spec.getNbCpu() != 0 )
91 vmSpec.setNumCPUs(spec.getNbCpu());
92 if ( spec.getVRAM() != 0 )
93 vmSpec.setMemoryMB(Long.valueOf(spec.getVRAM() * 1024));
94 return vmSpec;
95 }
96
97
98 public static VirtualMachineConfigSpec updateVmSpecAndSetCpusAndMemory(ServiceInstance serviceInstance, MachineSpecification spec, VirtualMachineConfigSpec vmSpec) {
99 vmSpec.setName(spec.getVmName());
100
101 if ( spec.getNbCpu() != 0 )
102 vmSpec.setNumCPUs(spec.getNbCpu());
103 if ( spec.getVRAM() != 0 )
104 vmSpec.setMemoryMB(Long.valueOf(spec.getVRAM() * 1024));
105 return vmSpec;
106 }
107 }