use of com.vmware.vim25.VirtualMachineImportSpec in project cloudstack by apache.
the class HypervisorHostHelper method readOVF.
public static List<Pair<String, Boolean>> readOVF(VmwareHypervisorHost host, String ovfFilePath, DatastoreMO dsMo) throws Exception {
List<Pair<String, Boolean>> ovfVolumeInfos = new ArrayList<Pair<String, Boolean>>();
List<String> files = new ArrayList<String>();
ManagedObjectReference morRp = host.getHyperHostOwnerResourcePool();
assert (morRp != null);
ManagedObjectReference morHost = host.getMor();
String importEntityName = UUID.randomUUID().toString();
OvfCreateImportSpecParams importSpecParams = new OvfCreateImportSpecParams();
importSpecParams.setHostSystem(morHost);
importSpecParams.setLocale("US");
importSpecParams.setEntityName(importEntityName);
importSpecParams.setDeploymentOption("");
String ovfDescriptor = removeOVFNetwork(HttpNfcLeaseMO.readOvfContent(ovfFilePath));
VmwareContext context = host.getContext();
OvfCreateImportSpecResult ovfImportResult = context.getService().createImportSpec(context.getServiceContent().getOvfManager(), ovfDescriptor, morRp, dsMo.getMor(), importSpecParams);
if (ovfImportResult == null) {
String msg = "createImportSpec() failed. ovfFilePath: " + ovfFilePath;
s_logger.error(msg);
throw new Exception(msg);
}
if (!ovfImportResult.getError().isEmpty()) {
for (LocalizedMethodFault fault : ovfImportResult.getError()) {
s_logger.error("createImportSpec error: " + fault.getLocalizedMessage());
}
throw new CloudException("Failed to create an import spec from " + ovfFilePath + ". Check log for details.");
}
if (!ovfImportResult.getWarning().isEmpty()) {
for (LocalizedMethodFault fault : ovfImportResult.getError()) {
s_logger.warn("createImportSpec warning: " + fault.getLocalizedMessage());
}
}
VirtualMachineImportSpec importSpec = (VirtualMachineImportSpec) ovfImportResult.getImportSpec();
if (importSpec == null) {
String msg = "createImportSpec() failed to create import specification for OVF template at " + ovfFilePath;
s_logger.error(msg);
throw new Exception(msg);
}
File ovfFile = new File(ovfFilePath);
for (OvfFileItem ovfFileItem : ovfImportResult.getFileItem()) {
String absFile = ovfFile.getParent() + File.separator + ovfFileItem.getPath();
files.add(absFile);
}
int osDiskSeqNumber = 0;
VirtualMachineConfigSpec config = importSpec.getConfigSpec();
String paramVal = getOVFParamValue(config);
if (paramVal != null && !paramVal.isEmpty()) {
try {
osDiskSeqNumber = getOsDiskFromOvfConf(config, paramVal);
} catch (Exception e) {
osDiskSeqNumber = 0;
}
}
int diskCount = 0;
int deviceCount = 0;
List<VirtualDeviceConfigSpec> deviceConfigList = config.getDeviceChange();
for (VirtualDeviceConfigSpec deviceSpec : deviceConfigList) {
Boolean osDisk = false;
VirtualDevice device = deviceSpec.getDevice();
if (device instanceof VirtualDisk) {
if ((osDiskSeqNumber == 0 && diskCount == 0) || osDiskSeqNumber == deviceCount) {
osDisk = true;
}
Pair<String, Boolean> ovfVolumeInfo = new Pair<String, Boolean>(files.get(diskCount), osDisk);
ovfVolumeInfos.add(ovfVolumeInfo);
diskCount++;
}
deviceCount++;
}
return ovfVolumeInfos;
}
Aggregations