Search in sources :

Example 1 with NicSecurityGroupsTaskState

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();
}
Also used : NicSecurityGroupsTaskState(com.vmware.photon.controller.model.tasks.NicSecurityGroupsTaskService.NicSecurityGroupsTaskState)

Example 2 with NicSecurityGroupsTaskState

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);
}
Also used : NicSecurityGroupsTaskState(com.vmware.photon.controller.model.tasks.NicSecurityGroupsTaskService.NicSecurityGroupsTaskState) NicSecurityGroupsTaskState(com.vmware.photon.controller.model.tasks.NicSecurityGroupsTaskService.NicSecurityGroupsTaskState) TaskState(com.vmware.xenon.common.TaskState)

Example 3 with NicSecurityGroupsTaskState

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;
    });
}
Also used : LINK(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.LINK) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) Utils(com.vmware.xenon.common.Utils) UriPaths(com.vmware.photon.controller.model.UriPaths) Map(java.util.Map) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) URI(java.net.URI) AssertUtil(com.vmware.photon.controller.model.util.AssertUtil) FIXED_ITEM_NAME(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.FIXED_ITEM_NAME) Operation(com.vmware.xenon.common.Operation) NicSecurityGroupsRequest(com.vmware.photon.controller.model.adapterapi.NicSecurityGroupsRequest) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) Set(java.util.Set) OperationRequestType(com.vmware.photon.controller.model.adapterapi.NicSecurityGroupsRequest.OperationRequestType) Collectors(java.util.stream.Collectors) OPTIONAL(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.OPTIONAL) List(java.util.List) NicSecurityGroupsTaskState(com.vmware.photon.controller.model.tasks.NicSecurityGroupsTaskService.NicSecurityGroupsTaskState) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) TaskState(com.vmware.xenon.common.TaskState) REQUIRED(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.REQUIRED) EXPAND(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.EXPAND) TaskService(com.vmware.xenon.services.common.TaskService) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) QueryTask(com.vmware.xenon.services.common.QueryTask) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)

Example 4 with NicSecurityGroupsTaskState

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;
}
Also used : NicSecurityGroupsTaskState(com.vmware.photon.controller.model.tasks.NicSecurityGroupsTaskService.NicSecurityGroupsTaskState)

Example 5 with NicSecurityGroupsTaskState

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;
}
Also used : NicSecurityGroupsTaskState(com.vmware.photon.controller.model.tasks.NicSecurityGroupsTaskService.NicSecurityGroupsTaskState)

Aggregations

NicSecurityGroupsTaskState (com.vmware.photon.controller.model.tasks.NicSecurityGroupsTaskService.NicSecurityGroupsTaskState)6 TaskState (com.vmware.xenon.common.TaskState)3 UriPaths (com.vmware.photon.controller.model.UriPaths)1 NicSecurityGroupsRequest (com.vmware.photon.controller.model.adapterapi.NicSecurityGroupsRequest)1 OperationRequestType (com.vmware.photon.controller.model.adapterapi.NicSecurityGroupsRequest.OperationRequestType)1 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)1 SecurityGroupState (com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)1 AssertUtil (com.vmware.photon.controller.model.util.AssertUtil)1 PhotonModelUriUtils.createInventoryUri (com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri)1 DeferredResult (com.vmware.xenon.common.DeferredResult)1 Operation (com.vmware.xenon.common.Operation)1 ServiceDocument (com.vmware.xenon.common.ServiceDocument)1 EXPAND (com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.EXPAND)1 FIXED_ITEM_NAME (com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.FIXED_ITEM_NAME)1 PropertyUsageOption (com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption)1 LINK (com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.LINK)1 OPTIONAL (com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.OPTIONAL)1 REQUIRED (com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.REQUIRED)1 UriUtils (com.vmware.xenon.common.UriUtils)1 Utils (com.vmware.xenon.common.Utils)1