use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class AWSComputeDiskDay2ServiceTest method performDiskOperationAndVerify.
private void performDiskOperationAndVerify(String computeStateLink, List<String> diskLinks, String requestType, DiskTaskService.DiskTaskState.SubStage expectedTerminalState) throws Throwable {
List<String> taskServiceLinks = new ArrayList<>();
diskLinks.forEach(diskLink -> {
DiskTaskService.DiskTaskState diskOpTask = new DiskTaskService.DiskTaskState();
diskOpTask.taskSubStage = DiskTaskService.DiskTaskState.SubStage.STARTED;
try {
diskOpTask = TestUtils.doPost(this.host, diskOpTask, DiskTaskService.DiskTaskState.class, UriUtils.buildUri(this.host, DiskTaskService.FACTORY_LINK));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
ResourceOperationRequest request = new ResourceOperationRequest();
request.isMockRequest = this.isMock;
request.operation = requestType;
request.payload = new HashMap<>();
request.payload.put(PhotonModelConstants.DISK_LINK, diskLink);
request.resourceReference = UriUtils.buildUri(this.host, computeStateLink);
request.taskReference = UriUtils.buildUri(this.host, diskOpTask.documentSelfLink);
Operation diskOp = Operation.createPatch(UriUtils.buildUri(this.host, AWSComputeDiskDay2Service.SELF_LINK)).setBody(request).setReferer(this.host.getReferer());
TestRequestSender sender = new TestRequestSender(this.host);
sender.sendRequest(diskOp);
this.host.log("Waiting for disk operation to complete");
taskServiceLinks.add(diskOpTask.documentSelfLink);
});
taskServiceLinks.forEach(taskServiceLink -> {
this.host.waitFor("disk Operation failed.", () -> {
DiskTaskService.DiskTaskState diskTaskState = this.host.getServiceState(null, DiskTaskService.DiskTaskState.class, UriUtils.buildUri(this.host, taskServiceLink));
// Check if the disk operation is successful or not.
if (diskTaskState.taskSubStage == DiskTaskService.DiskTaskState.SubStage.FINISHED || diskTaskState.taskSubStage == DiskTaskService.DiskTaskState.SubStage.FAILED) {
assertEquals("diskOperation Task did not terminate as expected.", expectedTerminalState, diskTaskState.taskSubStage);
return true;
} else {
return false;
}
});
});
}
use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class AWSRebootServiceTest method testReboot.
// Test the reboot operation.
@Test
public void testReboot() throws Throwable {
provisionSingleAWS();
// check that the VM has been created
ProvisioningUtils.queryComputeInstances(this.host, 2);
ComputeState compute = getCompute(this.host, this.vmState.documentSelfLink);
if (!this.isMock) {
List<Instance> instances = getAwsInstancesByIds(this.client, this.host, Collections.singletonList(compute.id));
Instance instance = instances.get(0);
ComputeState vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
assertAndSetVMSecurityGroupsToBeDeleted(instance, vm);
}
String taskLink = UUID.randomUUID().toString();
ResourceOperationRequest request = new ResourceOperationRequest();
request.isMockRequest = this.isMock;
request.operation = ResourceOperation.REBOOT.operation;
request.payload = new HashMap<>();
request.resourceReference = UriUtils.buildUri(this.host, compute.documentSelfLink);
request.taskReference = UriUtils.buildUri(this.host, taskLink);
TestContext ctx = this.host.testCreate(2);
createTaskResultListener(this.host, taskLink, (u) -> {
if (u.getAction() != Service.Action.PATCH) {
return false;
}
ResourceOperationResponse response = u.getBody(ResourceOperationResponse.class);
if (TaskState.isFailed(response.taskInfo)) {
ctx.failIteration(new IllegalStateException(response.taskInfo.failure.message));
} else if (TaskState.isFinished(response.taskInfo)) {
ctx.completeIteration();
}
return true;
});
Operation rebootOp = Operation.createPatch(UriUtils.buildUri(this.host, AWSRebootService.SELF_LINK)).setBody(request).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
if (e != null) {
ctx.failIteration(e);
return;
}
ctx.completeIteration();
});
this.host.send(rebootOp);
ctx.await();
ComputeState vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
assertEquals(ComputeService.PowerState.ON, vm.powerState);
}
use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class AWSResetService method reset.
private void reset(AmazonEC2AsyncClient client, ResourceOperationRequest pr, DefaultAdapterContext c) {
if (!c.child.powerState.equals(ComputeService.PowerState.ON)) {
logWarning(() -> String.format("Cannot perform a reset on this EC2 instance. " + "The machine should be in powered on state"));
c.taskManager.patchTaskToFailure(new IllegalStateException("Incorrect power state. Expected the machine " + "to be powered on "));
return;
}
// The stop action for reset is a force stop. So we use the withForce method to set the force parameter to TRUE
// This is similar to unplugging the machine from the power circuit.
// The OS and the applications are forcefully stopped.
StopInstancesRequest stopRequest = new StopInstancesRequest();
stopRequest.withInstanceIds(c.child.id).withForce(Boolean.TRUE);
client.stopInstancesAsync(stopRequest, new AWSAsyncHandler<StopInstancesRequest, StopInstancesResult>() {
@Override
protected void handleError(Exception e) {
c.taskManager.patchTaskToFailure(e);
}
@Override
protected void handleSuccess(StopInstancesRequest request, StopInstancesResult result) {
AWSUtils.waitForTransitionCompletion(getHost(), result.getStoppingInstances(), "stopped", client, (is, e) -> {
if (e != null) {
onError(e);
return;
}
// Instances will be started only if they're successfully stopped
startInstance(client, c);
});
}
});
}
use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class AWSResetServiceTest method testReset.
// Test the reset operation.
@Test
public void testReset() throws Throwable {
provisionSingleAWS();
// check that the VM has been created
ProvisioningUtils.queryComputeInstances(this.host, 2);
ComputeState compute = getCompute(this.host, this.vmState.documentSelfLink);
if (!this.isMock) {
List<Instance> instances = getAwsInstancesByIds(this.client, this.host, Collections.singletonList(compute.id));
Instance instance = instances.get(0);
ComputeState vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
setVMSecurityGroupsToBeDeleted(instance, vm);
}
String taskLink = UUID.randomUUID().toString();
ResourceOperationRequest request = new ResourceOperationRequest();
request.isMockRequest = this.isMock;
request.operation = ResourceOperation.RESET.operation;
request.payload = new HashMap<>();
request.resourceReference = UriUtils.buildUri(this.host, compute.documentSelfLink);
request.taskReference = UriUtils.buildUri(this.host, taskLink);
TestContext ctx = this.host.testCreate(1);
createTaskResultListener(this.host, taskLink, (u) -> {
if (u.getAction() != Service.Action.PATCH) {
return false;
}
ResourceOperationResponse response = u.getBody(ResourceOperationResponse.class);
if (TaskState.isFailed(response.taskInfo)) {
ctx.failIteration(new IllegalStateException(response.taskInfo.failure.message));
} else if (TaskState.isFinished(response.taskInfo)) {
ctx.completeIteration();
}
return true;
});
TestContext ctx2 = this.host.testCreate(1);
Operation resetOp = Operation.createPatch(UriUtils.buildUri(this.host, AWSResetService.SELF_LINK)).setBody(request).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
if (e != null) {
ctx2.failIteration(e);
return;
}
ctx2.completeIteration();
});
this.host.send(resetOp);
ctx2.await();
// in EC2 documentation
if (!this.isMock) {
// Waiting for power off
this.host.waitFor("Timed out waiting for EC2 to power off", () -> {
String state = getVMState(this.client, compute.id);
if (("stopping".equals(state) || "stopped".equals(state))) {
this.host.log(Level.INFO, "EC2 is being powered off");
return true;
} else {
return false;
}
});
// Waiting for power on
this.host.waitFor("Timed out waiting for EC2 to power on", () -> {
String state = getVMState(this.client, compute.id);
if ("running".equals(state)) {
this.host.log(Level.INFO, "EC2 is being powered on");
return true;
} else {
return false;
}
});
}
}
use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class BaseVSphereAdapterTest method getDeleteOrRevertSnapshotRequest.
private ResourceOperationRequest getDeleteOrRevertSnapshotRequest(String operationId, String operationType, String computeSelfLink, String snapshotSelfLink, String taskLink) {
Map<String, String> payload = new HashMap<>();
payload.put(VSphereConstants.VSPHERE_SNAPSHOT_REQUEST_TYPE, operationType);
payload.put(VSphereConstants.VSPHERE_SNAPSHOT_DOCUMENT_LINK, snapshotSelfLink);
ResourceOperationRequest resourceOperationRequest = new ResourceOperationRequest();
resourceOperationRequest.operation = operationId;
resourceOperationRequest.isMockRequest = isMock();
resourceOperationRequest.resourceReference = UriUtils.buildUri(this.host, computeSelfLink);
resourceOperationRequest.taskReference = UriUtils.buildUri(this.host, taskLink);
resourceOperationRequest.payload = payload;
return resourceOperationRequest;
}
Aggregations