use of com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState in project photon-model by vmware.
the class ProvisionSecurityGroupTaskService method handlePatch.
@Override
public void handlePatch(Operation patch) {
if (!patch.hasBody()) {
patch.fail(new IllegalArgumentException("body is required"));
return;
}
ProvisionSecurityGroupTaskState currState = getState(patch);
ProvisionSecurityGroupTaskState patchState = patch.getBody(ProvisionSecurityGroupTaskState.class);
if (TaskState.isFailed(patchState.taskInfo)) {
currState.taskInfo = patchState.taskInfo;
}
switch(patchState.taskInfo.stage) {
case CREATED:
currState.taskSubStage = nextStage(currState);
handleSubStages(currState);
logInfo(() -> String.format("Security Group %s on %s started", currState.requestType.toString(), currState.securityGroupDescriptionLinks));
break;
case STARTED:
currState.taskInfo.stage = TaskState.TaskStage.STARTED;
break;
case FINISHED:
SubStage nextStage = nextStage(currState);
if (nextStage == SubStage.FINISHED) {
currState.taskInfo.stage = TaskState.TaskStage.FINISHED;
logInfo(() -> "Task is complete");
ServiceTaskCallback.sendResponse(currState.serviceTaskCallback, this, currState);
} else {
sendSelfPatch(TaskState.TaskStage.CREATED, null);
}
break;
case FAILED:
logWarning(() -> String.format("Task failed with %s", Utils.toJsonHtml(currState.taskInfo.failure)));
ServiceTaskCallback.sendResponse(currState.serviceTaskCallback, this, currState);
break;
case CANCELLED:
break;
default:
break;
}
patch.complete();
}
use of com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState in project photon-model by vmware.
the class ProvisionSecurityGroupTaskServiceTest method postAndWaitForService.
private static ProvisionSecurityGroupTaskState postAndWaitForService(BaseModelTest test, ProvisionSecurityGroupTaskState startState) throws Throwable {
ProvisionSecurityGroupTaskState returnState = test.postServiceSynchronously(ProvisionSecurityGroupTaskService.FACTORY_LINK, startState, ProvisionSecurityGroupTaskState.class);
ProvisionSecurityGroupTaskState completeState = test.waitForServiceState(ProvisionSecurityGroupTaskState.class, returnState.documentSelfLink, state -> TaskState.TaskStage.FINISHED.ordinal() <= state.taskInfo.stage.ordinal());
return completeState;
}
use of com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState in project photon-model by vmware.
the class ProvisionSecurityGroupTaskServiceTest method buildValidStartState.
private static ProvisionSecurityGroupTaskState buildValidStartState(BaseModelTest test, SecurityGroupInstanceRequest.InstanceRequestType requestType, boolean success) throws Throwable {
ProvisionSecurityGroupTaskState startState = new ProvisionSecurityGroupTaskState();
SecurityGroupState securityGroupState1 = createSecurityGroupState(test, success);
SecurityGroupState returnState1 = test.postServiceSynchronously(SecurityGroupService.FACTORY_LINK, securityGroupState1, SecurityGroupState.class);
SecurityGroupState securityGroupState2 = createSecurityGroupState(test, success);
SecurityGroupState returnState2 = test.postServiceSynchronously(SecurityGroupService.FACTORY_LINK, securityGroupState2, SecurityGroupState.class);
startState.requestType = requestType;
startState.securityGroupDescriptionLinks = Stream.of(returnState1.documentSelfLink, returnState2.documentSelfLink).collect(Collectors.toSet());
startState.isMockRequest = true;
return startState;
}
use of com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState in project photon-model by vmware.
the class TestProvisionAWSSecurityGroup method testInvalidAuthAWSSecurityGroup.
@Test
public void testInvalidAuthAWSSecurityGroup() throws Throwable {
// create credentials
Operation authResponse = new Operation();
TestUtils.postCredentials(this.host, authResponse, this.privateKey, "invalid");
AuthCredentialsServiceState creds = authResponse.getBody(AuthCredentialsServiceState.class);
// create resource pool
Operation poolResponse = new Operation();
TestUtils.postResourcePool(this.host, poolResponse);
ResourcePoolState pool = poolResponse.getBody(ResourcePoolState.class);
// create sq service
Operation securityGroupResponse = new Operation();
SecurityGroupState securityGroupInitialState = buildSecurityGroupState(creds, pool);
TestUtils.postSecurityGroup(this.host, securityGroupInitialState, securityGroupResponse);
SecurityGroupState securityGroupState = securityGroupResponse.getBody(SecurityGroupState.class);
// set up security group task state
ProvisionSecurityGroupTaskState task = new ProvisionSecurityGroupTaskState();
task.requestType = SecurityGroupInstanceRequest.InstanceRequestType.CREATE;
task.securityGroupDescriptionLinks = Stream.of(securityGroupState.documentSelfLink).collect(Collectors.toSet());
task.customProperties = new HashMap<>();
task.customProperties.put(NETWORK_STATE_ID_PROP_NAME, this.vpcId);
Operation provision = new Operation();
provisionSecurityGroup(task, provision);
ProvisionSecurityGroupTaskState ps = provision.getBody(ProvisionSecurityGroupTaskState.class);
waitForTaskFailure(this.host, UriUtils.buildUri(this.host, ps.documentSelfLink));
}
use of com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState in project photon-model by vmware.
the class TestProvisionAWSSecurityGroup method testProvisionAWSSecurityGroupPartialFailure.
@Test
public void testProvisionAWSSecurityGroupPartialFailure() throws Throwable {
// create credentials
Operation authResponse = new Operation();
TestUtils.postCredentials(this.host, authResponse, this.privateKey, this.privateKeyId);
AuthCredentialsServiceState creds = authResponse.getBody(AuthCredentialsServiceState.class);
// create resource pool
Operation poolResponse = new Operation();
TestUtils.postResourcePool(this.host, poolResponse);
ResourcePoolState pool = poolResponse.getBody(ResourcePoolState.class);
// create two security groups
Operation securityGroupResponse = new Operation();
SecurityGroupState initialSecurityGroupState = buildSecurityGroupState(creds, pool);
TestUtils.postSecurityGroup(this.host, initialSecurityGroupState, securityGroupResponse);
SecurityGroupState securityGroupState1 = securityGroupResponse.getBody(SecurityGroupState.class);
initialSecurityGroupState = buildSecurityGroupState(creds, pool);
TestUtils.postSecurityGroup(this.host, initialSecurityGroupState, securityGroupResponse);
SecurityGroupState securityGroupState2 = securityGroupResponse.getBody(SecurityGroupState.class);
// delete the second security group to simulate failure
TestUtils.deleteSecurityGroup(this.host, securityGroupState2.documentSelfLink);
// verify the second security group is gone
try {
getSecurityGroupState(securityGroupState2.documentSelfLink);
} catch (Exception ex) {
assertTrue(ex instanceof ServiceNotFoundException);
}
// set up security group task state
ProvisionSecurityGroupTaskState task = new ProvisionSecurityGroupTaskState();
task.requestType = SecurityGroupInstanceRequest.InstanceRequestType.CREATE;
task.securityGroupDescriptionLinks = Stream.of(securityGroupState1.documentSelfLink, securityGroupState2.documentSourceLink).collect(Collectors.toSet());
task.customProperties = new HashMap<>();
task.customProperties.put(NETWORK_STATE_ID_PROP_NAME, this.vpcId);
Operation provision = new Operation();
provisionSecurityGroup(task, provision);
ProvisionSecurityGroupTaskState ps = provision.getBody(ProvisionSecurityGroupTaskState.class);
waitForTaskFailure(this.host, UriUtils.buildUri(this.host, ps.documentSelfLink));
validateAWSArtifacts(securityGroupState1.documentSelfLink, creds);
// validate that the second security group was not created
assertNull(getAWSSecurityGroup(securityGroupState2.name, creds));
// reuse previous task, but switch to a delete
task.requestType = SecurityGroupInstanceRequest.InstanceRequestType.DELETE;
Operation remove = new Operation();
provisionSecurityGroup(task, remove);
ProvisionSecurityGroupTaskState removeTask = remove.getBody(ProvisionSecurityGroupTaskState.class);
waitForTaskFailure(this.host, UriUtils.buildUri(this.host, removeTask.documentSelfLink));
// verify security group state is gone
try {
getSecurityGroupState(securityGroupState1.documentSelfLink);
} catch (Exception ex) {
assertTrue(ex instanceof ServiceNotFoundException);
}
}
Aggregations