use of com.vmware.vim25.mo.Task in project coprhd-controller by CoprHD.
the class VcenterApiClient method addHost.
public String addHost(String datacenterName, String clusterNameOrMoRef, String hostname, String username, String password) throws VcenterSystemException, VcenterObjectNotFoundException, VcenterObjectConnectionException {
try {
_log.info("Request to add host " + hostname + " to datacenter " + datacenterName + " cluster " + clusterNameOrMoRef);
ClusterComputeResource clusterComputeResource = (ClusterComputeResource) createManagedEntityMap(datacenterName, clusterNameOrMoRef, null, false).get("ClusterComputeResource");
HostSystem hostSystem = findByHostname(clusterComputeResource, hostname);
if (hostSystem == null) {
_log.info("Host " + hostname + " does not exist and will be added");
if (username == null || username.trim().equals("") || password == null || password.trim().equals("")) {
_log.error("Username and/or password missing - Both required to add host to cluster");
throw new VcenterSystemException("Username and/or password missing - Both required to add host to cluster");
}
HostConnectSpec hostConnectSpec = new HostConnectSpec();
hostConnectSpec.setHostName(hostname);
hostConnectSpec.setUserName(username);
hostConnectSpec.setPassword(password);
// ie
hostConnectSpec.setSslThumbprint(getHostCertificate(hostname, username, password));
// 1D:0C:63:FC:58:58:1C:66:F0:5B:C4:0B:F3:84:0E:27:E9:59:83:F7
_log.info("Attempt to add host " + hostname + " to " + datacenterName + "/" + clusterComputeResource.getName());
Integer hostOperationTimeout = Integer.parseInt(propertyInfo.getProperty("vcenter_host_operation_timeout"));
Integer hostOperationTimeoutMillis = hostOperationTimeout * 1000;
Integer retryCount = 1;
Long startTimeMillis = System.currentTimeMillis();
Long cutoffTimeMillis = startTimeMillis + hostOperationTimeoutMillis;
VcenterTaskMonitor taskMonitor = new VcenterTaskMonitor(hostOperationTimeout);
Task addHostTask = clusterComputeResource.addHost_Task(hostConnectSpec, true, null, null);
// call blocks
VcenterTaskMonitor.TaskStatus taskStatus = taskMonitor.monitor(addHostTask);
while ((System.currentTimeMillis() < cutoffTimeMillis) && taskStatus == VcenterTaskMonitor.TaskStatus.ERROR) {
_log.info("Add host " + hostname + " retry error " + taskMonitor.errorDescription + " count " + retryCount);
// Retry is time based and if each retry executes very quickly (ie milliseconds) then we should
Thread.sleep(60000);
// throttle and only retry every 60 seconds
addHostTask = clusterComputeResource.addHost_Task(hostConnectSpec, true, null, null);
// call blocks
taskStatus = taskMonitor.monitor(addHostTask);
retryCount++;
}
if (taskStatus == VcenterTaskMonitor.TaskStatus.SUCCESS) {
_log.info("Add host " + hostname + " task succeeded - Attempt to find host in cluster");
hostSystem = findByHostname(clusterComputeResource, hostname);
} else if (taskStatus == VcenterTaskMonitor.TaskStatus.ERROR) {
String errorMessage = "Add host " + hostname + " task failed - " + taskMonitor.errorDescription;
if (taskMonitor.errorDescription.contains("already exists.")) {
errorMessage += " - Ensure adding host to correct cluster or remove host from its existing cluster";
}
_log.error(errorMessage);
throw new VcenterSystemException(errorMessage);
} else if (taskStatus == VcenterTaskMonitor.TaskStatus.TIMED_OUT) {
_log.error("Add host " + hostname + " task timed out at " + taskMonitor.progressPercent);
throw new VcenterSystemException("Add host " + hostname + " task timed out at " + taskMonitor.progressPercent);
} else {
// Should not execute - Just here in case someone ever added a new state so we catch it
_log.error("Unknown task status encountered tracking add host " + taskStatus);
throw new VcenterSystemException("Unknown task status encountered tracking add host " + taskStatus);
}
trackHostTasks(hostSystem, hostOperationTimeout);
// Only take host out of maintenance mode if it's being added. We don't want to exit maintenance mode on other hosts since
// customer may have intentionally put it on that.
exitMaintenanceModeHost(hostSystem);
}
// Some nice conveniences to reconnect and exit maintenance mode to ready the host for action
reconnectHost(hostSystem);
// Collect some details
StringBuffer hostDetails = new StringBuffer();
hostDetails.append("Host ").append(datacenterName).append("/").append(clusterComputeResource.getName()).append("/").append(hostname).append(" ");
String os = hostSystem.getPropertyByPath("config.product.version").toString();
hostDetails.append("OS ").append(os).append(" ");
String key = hostSystem.getMOR().getVal();
hostDetails.append("key ").append(key).append(" ");
String connectionState = hostSystem.getRuntime().getConnectionState().toString();
hostDetails.append("Connection State ").append(connectionState).append(" ");
String powerState = hostSystem.getRuntime().getPowerState().toString();
hostDetails.append("Power State ").append(powerState).append(" ");
boolean maintenanceMode = hostSystem.getRuntime().isInMaintenanceMode();
hostDetails.append("Maintenance Mode ").append(maintenanceMode).append(" ");
_log.info(hostDetails.toString());
return hostSystem.getMOR().getVal();
} catch (VcenterSystemException | VcenterObjectNotFoundException | VcenterObjectConnectionException e) {
throw e;
} catch (Exception e) {
_log.error("Exception adding host: " + e);
throw new VcenterSystemException(e.getLocalizedMessage());
}
}
use of com.vmware.vim25.mo.Task in project photon-model by vmware.
the class TestVsphereComputeDay0AttachDisk method testComputeAttachDisk.
@Test
public void testComputeAttachDisk() throws Throwable {
DiskService.DiskState diskState = null;
try {
prepareEnvironment();
// Step 1: Create Disk
diskState = createDiskWithDatastore("AdditionalDisk1", DiskService.DiskType.HDD, ADDITIONAL_DISK_SIZE, buildCustomProperties());
// start provision task to do the actual disk creation
String documentSelfLink = performDiskRequest(diskState, ProvisionDiskTaskService.ProvisionDiskTaskState.SubStage.CREATING_DISK);
this.host.waitForFinishedTask(ProvisionDiskTaskService.ProvisionDiskTaskState.class, documentSelfLink);
// Step 2: Create VM
if (isMock()) {
createNetwork(networkId);
}
ComputeDescriptionService.ComputeDescription vmDescription = createVmDescription();
this.vm = createVmState(vmDescription, true, null, true, diskState.documentSelfLink);
// kick off a provision task to do the actual VM creation
ProvisionComputeTaskService.ProvisionComputeTaskState provisionTask = createProvisionTask(this.vm);
awaitTaskEnd(provisionTask);
this.vm = getComputeState(this.vm);
// put fake moref in the vm
if (isMock()) {
ManagedObjectReference moref = new ManagedObjectReference();
moref.setValue("vm-0");
moref.setType(VimNames.TYPE_VM);
CustomProperties.of(this.vm).put(MOREF, moref);
this.vm = doPost(this.host, this.vm, ComputeService.ComputeState.class, UriUtils.buildUri(this.host, ComputeService.FACTORY_LINK));
}
} finally {
if (!isMock()) {
cleanUpVm(this.vm, null);
}
}
}
use of com.vmware.vim25.mo.Task in project photon-model by vmware.
the class TestVSphereComputeDiskDay2Service method testComputePersistentDisk.
@Test
public /**
* 1. Create a VM
* 2. Create a HDD persistent disk
* 3. Attach HDD disk created in Step 2 to VM.
* 4. Delete VM.
* 5. Verify HDD disk still remains
* 6. Delete HDD disk
*/
void testComputePersistentDisk() throws Throwable {
DiskService.DiskState diskState = null;
try {
// Step 1: Create VM
prepareEnvironment();
if (isMock()) {
createNetwork(networkId);
}
snapshotFactoryState("clone-refresh", NetworkService.class);
ComputeDescriptionService.ComputeDescription vmDescription = createVmDescription();
this.vm = createVmState(vmDescription, true, null, false);
// kick off a provision task to do the actual VM creation
ProvisionComputeTaskService.ProvisionComputeTaskState provisionTask = createProvisionTask(this.vm);
awaitTaskEnd(provisionTask);
this.vm = getComputeState(this.vm);
// put fake moref in the vm
if (isMock()) {
ManagedObjectReference moref = new ManagedObjectReference();
moref.setValue("vm-0");
moref.setType(VimNames.TYPE_VM);
CustomProperties.of(this.vm).put(MOREF, moref);
this.vm = doPost(this.host, this.vm, ComputeState.class, UriUtils.buildUri(this.host, ComputeService.FACTORY_LINK));
return;
}
// Step 2: Create Disk
diskState = createDiskWithDatastore("AdditionalDisk1", DiskService.DiskType.HDD, ADDITIONAL_DISK_SIZE, buildCustomProperties(), true);
// start provision task to do the actual disk creation
String documentSelfLink = performDiskRequest(diskState, ProvisionDiskTaskService.ProvisionDiskTaskState.SubStage.CREATING_DISK);
this.host.waitForFinishedTask(ProvisionDiskTaskService.ProvisionDiskTaskState.class, documentSelfLink);
// Step 3: Attach Disk created in step 2
ResourceOperationRequest request = createResourceOperationRequest(diskState, createComputeDiskTaskService(), ResourceOperation.ATTACH_DISK);
sendRequest(request, DiskService.DiskType.HDD, computeAttachWaitHandler());
this.vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vm.documentSelfLink));
// Step 4: Delete VM
cleanUpVm(this.vm, null);
// Get the latest state of the detached disk
diskState = this.host.getServiceState(null, DiskService.DiskState.class, UriUtils.buildUri(this.host, diskState.documentSelfLink));
assertEquals(DiskService.DiskStatus.AVAILABLE, diskState.status);
} finally {
if (!isMock()) {
// Step 8: Delete disk
if (diskState != null) {
String documentSelfLink = performDiskRequest(diskState, ProvisionDiskTaskService.ProvisionDiskTaskState.SubStage.DELETING_DISK);
this.host.waitForFinishedTask(ProvisionDiskTaskService.ProvisionDiskTaskState.class, documentSelfLink);
}
}
}
}
use of com.vmware.vim25.mo.Task in project photon-model by vmware.
the class TestVSphereComputeDiskDay2Service method testCDRomInsertOnCompute.
@Test
public void testCDRomInsertOnCompute() throws Throwable {
try {
// Step 1: Create VM
prepareEnvironment();
if (isMock()) {
createNetwork(networkId);
}
snapshotFactoryState("clone-refresh", NetworkService.class);
ComputeDescriptionService.ComputeDescription vmDescription = createVmDescription();
this.vm = createVmState(vmDescription, true, null, false);
// kick off a provision task to do the actual VM creation
ProvisionComputeTaskService.ProvisionComputeTaskState provisionTask = createProvisionTask(this.vm);
awaitTaskEnd(provisionTask);
this.vm = getComputeState(this.vm);
// put fake moref in the vm
if (isMock()) {
ManagedObjectReference moref = new ManagedObjectReference();
moref.setValue("vm-0");
moref.setType(VimNames.TYPE_VM);
CustomProperties.of(this.vm).put(MOREF, moref);
this.vm = doPost(this.host, this.vm, ComputeState.class, UriUtils.buildUri(this.host, ComputeService.FACTORY_LINK));
return;
}
this.vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vm.documentSelfLink));
assertNotNull(CustomProperties.of(this.vm).getString(COMPUTE_HOST_LINK_PROP_NAME, null));
assertEquals(3, this.vm.diskLinks.size());
this.vm.diskLinks.stream().forEach(link -> {
DiskService.DiskState diskState = this.host.getServiceState(null, DiskService.DiskState.class, UriUtils.buildUri(this.host, link));
if (diskState.type == DiskService.DiskType.CDROM) {
this.CDDiskState = diskState;
}
});
// Step 1: Attach a CD-ROM disk
// prepare a disk with CD-ROM type
DiskService.DiskState cdDiskState = createCDromWithIso("cdrom-1", DiskService.DiskType.CDROM, 0, null, 1024, null, true, false);
ResourceOperationRequest request = createResourceOperationRequest(cdDiskState, createComputeDiskTaskService(), ResourceOperation.ATTACH_DISK);
sendRequest(request, DiskService.DiskType.CDROM, insertCDRomHandler());
this.vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vm.documentSelfLink));
assertNotNull(this.vm.diskLinks);
assertEquals(3, this.vm.diskLinks.size());
} finally {
if (!isMock()) {
cleanUpVm(this.vm, null);
}
}
}
use of com.vmware.vim25.mo.Task in project photon-model by vmware.
the class TestVSphereCloneTaskWithStorage method verifyBootDiskCustomizationWithStoragePolicy.
@Test
public void verifyBootDiskCustomizationWithStoragePolicy() throws Throwable {
ComputeService.ComputeState vm = null;
ComputeService.ComputeState clonedVm = null;
try {
this.auth = createAuth();
this.resourcePool = createResourcePool();
if (isMock()) {
createNetwork(networkId);
}
this.computeHostDescription = createComputeDescription();
this.computeHost = createComputeHost(this.computeHostDescription);
doRefresh();
snapshotFactoryState("clone-refresh", NetworkService.class);
ComputeDescriptionService.ComputeDescription vmDescription = createVmDescription();
DiskService.DiskState bootDisk = createDiskWithStoragePolicy("boot", DiskService.DiskType.HDD, 1, getDiskUri(), HDD_DISK_SIZE, buildCustomProperties());
vm = createVmState(vmDescription, true, bootDisk.documentSelfLink);
// kick off a provision task to do the actual VM creation
ProvisionComputeTaskService.ProvisionComputeTaskState provisionTask = createProvisionTask(vm);
awaitTaskEnd(provisionTask);
vm = getComputeState(vm);
// put fake moref in the vm
if (isMock()) {
ManagedObjectReference moref = new ManagedObjectReference();
moref.setValue("vm-0");
moref.setType(VimNames.TYPE_VM);
CustomProperties.of(vm).put(MOREF, moref);
vm = doPost(this.host, vm, ComputeService.ComputeState.class, UriUtils.buildUri(this.host, ComputeService.FACTORY_LINK));
}
// create state & desc of the clone
ComputeDescriptionService.ComputeDescription cloneDescription = createCloneDescription(vm.documentSelfLink);
clonedVm = createCloneVmState(cloneDescription, true, false);
provisionTask = createProvisionTask(clonedVm);
awaitTaskEnd(provisionTask);
clonedVm = getComputeState(clonedVm);
if (!isMock()) {
// Verify that the disk is resized
BasicConnection connection = createConnection();
GetMoRef get = new GetMoRef(connection);
verifyDiskSize(vm, get, HDD_DISK_SIZE);
verifyDiskSize(clonedVm, get, CLONE_HDD_DISK_SIZE);
verifyDiskProperties(vm, get);
}
} finally {
if (!isMock()) {
cleanUpVm(vm, clonedVm);
}
}
}
Aggregations