public static String getExceptionMessage(Throwable e, boolean printStack) { //TODO: in vim 5.1, exceptions do not have a base exception class, MethodFault becomes a FaultInfo that we can only get // from individual exception through getFaultInfo, so we have to use reflection here to get MethodFault information. try { Class<? extends Throwable> cls = e.getClass(); Method mth = cls.getDeclaredMethod("getFaultInfo", (Class<?>)null); if (mth != null) { Object fault = mth.invoke(e, (Object[])null); if (fault instanceof MethodFault) { final StringWriter writer = new StringWriter(); writer.append("Exception: " + fault.getClass().getName() + "\n"); writer.append("message: " + ((MethodFault)fault).getFaultMessage() + "\n"); if (printStack) { writer.append("stack: "); e.printStackTrace(new PrintWriter(writer)); } return writer.toString(); } } } catch (Exception ex) { s_logger.info("[ignored]" + "failed toi get message for exception: " + e.getLocalizedMessage()); } return ExceptionUtil.toString(e, printStack); }
public ComputeState createInstanceFromSnapshot() throws Exception { String message = ""; if (this.ctx.snapshotMoRef == null) { message = String.format("No MoRef found for the specified snapshot %s", this.ctx.child.documentSelfLink); logger.error(message); this.ctx.fail(new IllegalStateException(message)); } if (this.ctx.referenceComputeMoRef == null) { if (this.ctx.snapshotMoRef == null) { message = String.format("No MoRef found for the reference compute for linkedclone creation for %s.", this.ctx.child.documentSelfLink); logger.error(message); this.ctx.fail(new IllegalStateException(message)); } } VirtualMachineRelocateSpec relocateSpec = new VirtualMachineRelocateSpec(); relocateSpec.setDiskMoveType(VirtualMachineRelocateDiskMoveOptions .CREATE_NEW_CHILD_DISK_BACKING.value()); VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec(); cloneSpec.setPowerOn(false); cloneSpec.setLocation(relocateSpec); cloneSpec.setSnapshot(this.ctx.snapshotMoRef); cloneSpec.setTemplate(false); ManagedObjectReference folder = getVmFolder(); String displayName = this.ctx.child.name; ManagedObjectReference linkedCloneTask = getVimPort() .cloneVMTask(this.ctx.referenceComputeMoRef, folder, displayName, cloneSpec); TaskInfo info = waitTaskEnd(linkedCloneTask); if (info.getState() == TaskInfoState.ERROR) { MethodFault fault = info.getError().getFault(); if (fault instanceof FileAlreadyExists) { // a .vmx file already exists, assume someone won the race to create the vm return null; } else { return VimUtils.rethrow(info.getError()); } } ManagedObjectReference clonedVM = (ManagedObjectReference) info.getResult(); if (clonedVM == null) { // vm was created by someone else return null; } // store reference to created vm for further processing this.vm = clonedVM; customizeAfterClone(); ComputeState state = new ComputeState(); state.resourcePoolLink = VimUtils .firstNonNull(this.ctx.child.resourcePoolLink, this.ctx.parent.resourcePoolLink); return state; }
private ManagedObjectReference cloneVm(ManagedObjectReference template) throws Exception { ManagedObjectReference folder = getVmFolder(); List<VirtualMachineDefinedProfileSpec> pbmSpec = getPbmProfileSpec(this.bootDisk); ManagedObjectReference datastore = getDataStoreForDisk(this.bootDisk, pbmSpec); ManagedObjectReference resourcePool = getResourcePool(); Map<String, Object> props = this.get.entityProps(template, VimPath.vm_config_hardware_device); ArrayOfVirtualDevice devices = (ArrayOfVirtualDevice) props .get(VimPath.vm_config_hardware_device); VirtualDisk vd = devices.getVirtualDevice().stream() .filter(d -> d instanceof VirtualDisk) .map(d -> (VirtualDisk) d).findFirst().orElse(null); VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec(); relocSpec.setDatastore(datastore); if (pbmSpec != null) { pbmSpec.stream().forEach(spec -> { relocSpec.getProfile().add(spec); }); } relocSpec.setFolder(folder); relocSpec.setPool(resourcePool); relocSpec.setDiskMoveType(computeDiskMoveType().value()); VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec(); cloneSpec.setLocation(relocSpec); //Set the provisioning type of the parent disk. VirtualMachineRelocateSpecDiskLocator diskProvisionTypeLocator = setProvisioningType(vd, datastore, pbmSpec); if (diskProvisionTypeLocator != null) { cloneSpec.getLocation().getDisk().add(diskProvisionTypeLocator); } cloneSpec.setPowerOn(false); cloneSpec.setTemplate(false); String displayName = this.ctx.child.name; ManagedObjectReference cloneTask = getVimPort() .cloneVMTask(template, folder, displayName, cloneSpec); TaskInfo info = waitTaskEnd(cloneTask); if (info.getState() == TaskInfoState.ERROR) { MethodFault fault = info.getError().getFault(); if (fault instanceof FileAlreadyExists) { // a .vmx file already exists, assume someone won the race to create the vm return null; } else { return VimUtils.rethrow(info.getError()); } } return (ManagedObjectReference) info.getResult(); }
/** * Creates a VM in vsphere. This method will block until the CreateVM_Task completes. The path * to the .vmx file is explicitly set and its existence is iterpreted as if the VM has been * successfully created and returns null. * * @return * @throws FinderException * @throws Exception */ private ManagedObjectReference createVm() throws Exception { ManagedObjectReference folder = getVmFolder(); List<VirtualMachineDefinedProfileSpec> pbmSpec = getPbmProfileSpec(this.bootDisk); ManagedObjectReference datastore = getDataStoreForDisk(this.bootDisk, pbmSpec); ManagedObjectReference resourcePool = getResourcePool(); ManagedObjectReference host = getHost(); // If datastore for disk is null, if storage policy is configured pick the compatible // datastore from that. if (datastore == null) { ManagedObjectReference dsFromSp = getDatastoreFromStoragePolicy(this.connection, pbmSpec); datastore = dsFromSp == null ? getDatastore() : dsFromSp; } String datastoreName = this.get.entityProp(datastore, "name"); VirtualMachineConfigSpec spec = buildVirtualMachineConfigSpec(datastoreName); String gt = CustomProperties.of(this.ctx.child).getString(CustomProperties.GUEST_ID, null); if (gt != null) { try { gt = VirtualMachineGuestOsIdentifier.valueOf(gt).value(); } catch (IllegalArgumentException e) { // silently default to generic 64 bit guest. gt = DEFAULT_GUEST_ID.value(); } spec.setGuestId(gt); } populateCloudConfig(spec, null); recordTimestamp(spec.getExtraConfig()); // set the maximum snapshot limit if specified final String snapshotLimit = CustomProperties.of(this.ctx.child).getString(CustomProperties.SNAPSHOT_MAXIMUM_LIMIT); recordSnapshotLimit(spec.getExtraConfig(), snapshotLimit); ManagedObjectReference vmTask = getVimPort().createVMTask(folder, spec, resourcePool, host); TaskInfo info = waitTaskEnd(vmTask); if (info.getState() == TaskInfoState.ERROR) { MethodFault fault = info.getError().getFault(); if (fault instanceof FileAlreadyExists) { // a .vmx file already exists, assume someone won the race to create the vm return null; } else { return VimUtils.rethrow(info.getError()); } } return (ManagedObjectReference) info.getResult(); }
public GenericVimFault(String message, MethodFault fault) { super(message); this.fault = fault; }
public MethodFault getFaultInfo() { return this.fault; }
/** * Create an instance of {@link JAXBElement }{@code <}{@link MethodFault }{@code >}} * */ @XmlElementDecl(namespace = "urn:pbm", name = "MethodFaultFault") public JAXBElement<MethodFault> createMethodFaultFault(MethodFault value) { return new JAXBElement<MethodFault>(_MethodFaultFault_QNAME, MethodFault.class, null, value); }