Search in sources :

Example 1 with ProvisionSecurityGroupTaskState

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

Example 2 with ProvisionSecurityGroupTaskState

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

Example 3 with ProvisionSecurityGroupTaskState

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;
}
Also used : ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)

Example 4 with ProvisionSecurityGroupTaskState

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));
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) Operation(com.vmware.xenon.common.Operation) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 5 with ProvisionSecurityGroupTaskState

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);
    }
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) Operation(com.vmware.xenon.common.Operation) TimeoutException(java.util.concurrent.TimeoutException) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) CompletionException(java.util.concurrent.CompletionException) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Aggregations

ProvisionSecurityGroupTaskState (com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState)15 SecurityGroupState (com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)6 Operation (com.vmware.xenon.common.Operation)6 TimeoutException (java.util.concurrent.TimeoutException)5 BaseModelTest (com.vmware.photon.controller.model.helpers.BaseModelTest)4 ResourcePoolState (com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState)4 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)4 Test (org.junit.Test)4 ServiceNotFoundException (com.vmware.xenon.common.ServiceHost.ServiceNotFoundException)3 TaskState (com.vmware.xenon.common.TaskState)3 CompletionException (java.util.concurrent.CompletionException)3 Date (java.util.Date)2 UriPaths (com.vmware.photon.controller.model.UriPaths)1 ResourceOperationResponse (com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse)1 SecurityGroupInstanceRequest (com.vmware.photon.controller.model.adapterapi.SecurityGroupInstanceRequest)1 InstanceRequestType (com.vmware.photon.controller.model.adapterapi.SecurityGroupInstanceRequest.InstanceRequestType)1 PhotonModelUriUtils.createInventoryUri (com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri)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