use of com.vmware.photon.controller.model.tasks.NicSecurityGroupsTaskService.NicSecurityGroupsTaskState in project photon-model by vmware.
the class NicSecurityGroupsTaskService method handlePatch.
@Override
public void handlePatch(Operation patch) {
if (!patch.hasBody()) {
patch.fail(new IllegalArgumentException("body is required"));
return;
}
NicSecurityGroupsTaskState currState = getState(patch);
NicSecurityGroupsTaskState patchState = patch.getBody(NicSecurityGroupsTaskState.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("%s security groups for %s started", currState.requestType.toString(), currState.networkInterfaceLink));
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.NicSecurityGroupsTaskService.NicSecurityGroupsTaskState in project photon-model by vmware.
the class NicSecurityGroupsTaskService method sendSelfPatch.
private void sendSelfPatch(TaskState.TaskStage stage, Throwable e) {
NicSecurityGroupsTaskState body = new NicSecurityGroupsTaskState();
body.taskInfo = new TaskState();
if (e == null) {
body.taskInfo.stage = stage;
} else {
body.taskInfo.stage = TaskState.TaskStage.FAILED;
body.taskInfo.failure = Utils.toServiceErrorResponse(e);
logWarning(() -> String.format("Patching to failed: %s", Utils.toString(e)));
}
sendSelfPatch(body);
}
use of com.vmware.photon.controller.model.tasks.NicSecurityGroupsTaskService.NicSecurityGroupsTaskState in project photon-model by vmware.
the class NicSecurityGroupsTaskService method validateSecurityGroupsEndpoint.
/**
* Validate that all security groups belong to the same endpoint
*/
private DeferredResult<Void> validateSecurityGroupsEndpoint(NicSecurityGroupsTaskState state) {
QueryTask.Query query = QueryTask.Query.Builder.create().addKindFieldClause(SecurityGroupState.class).addInClause(ServiceDocument.FIELD_NAME_SELF_LINK, state.securityGroupLinks).build();
QueryTask queryTask = QueryTask.Builder.createDirectTask().setQuery(query).addSelectTerm(SecurityGroupState.FIELD_NAME_ENDPOINT_LINK).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_SELECTED_FIELDS).build();
return QueryUtils.startInventoryQueryTask(this, queryTask).thenApply(qrt -> {
AssertUtil.assertTrue(qrt != null && qrt.results.documentCount > 0, String.format("Could not find security groups with links %s", state.securityGroupLinks));
Set<String> endpointLinks = qrt.results.documents.values().stream().map(o -> Utils.fromJson(o, SecurityGroupState.class).endpointLink).collect(Collectors.toSet());
// we only support security groups from the same endpoint for the same request
if (endpointLinks.size() != 1) {
throw new IllegalArgumentException("All security groups must belong to the same endpoint.");
}
if (endpointLinks.iterator().next() == null) {
throw new IllegalArgumentException("All security groups must have endpoint link set.");
}
return null;
});
}
use of com.vmware.photon.controller.model.tasks.NicSecurityGroupsTaskService.NicSecurityGroupsTaskState in project photon-model by vmware.
the class NicSecurityGroupsTaskServiceTest method postAndWaitForService.
private static NicSecurityGroupsTaskState postAndWaitForService(BaseModelTest test, NicSecurityGroupsTaskState startState) throws Throwable {
NicSecurityGroupsTaskState returnState = test.postServiceSynchronously(NicSecurityGroupsTaskService.FACTORY_LINK, startState, NicSecurityGroupsTaskState.class);
NicSecurityGroupsTaskState completeState = test.waitForServiceState(NicSecurityGroupsTaskState.class, returnState.documentSelfLink, state -> TaskState.TaskStage.FINISHED.ordinal() <= state.taskInfo.stage.ordinal());
return completeState;
}
use of com.vmware.photon.controller.model.tasks.NicSecurityGroupsTaskService.NicSecurityGroupsTaskState in project photon-model by vmware.
the class NicSecurityGroupsTaskServiceTest method buildStartState.
private static NicSecurityGroupsTaskState buildStartState(BaseModelTest test, NicSecurityGroupsRequest.OperationRequestType requestType, InstanceAdapterTestTypes instanceAdapterType) throws Throwable {
NicSecurityGroupsTaskState state = new NicSecurityGroupsTaskState();
state.requestType = requestType;
state.networkInterfaceLink = createNetworkInterface(test).documentSelfLink;
state.securityGroupLinks = Arrays.asList(createSecurityGroup(test, UUID.randomUUID().toString()).documentSelfLink);
switch(instanceAdapterType) {
case SUCCESS:
state.adapterReference = UriUtils.buildUri(test.getHost(), MockAdapter.MockNetworkInterfaceSecurityGroupsSuccessAdapter.SELF_LINK);
break;
case FAILURE:
state.adapterReference = UriUtils.buildUri(test.getHost(), MockAdapter.MockNetworkInterfaceSecurityGroupsFailureAdapter.SELF_LINK);
break;
default:
state.adapterReference = null;
}
return state;
}
Aggregations