Search in sources :

Example 11 with ProvisionSecurityGroupTaskState

use of com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState in project photon-model by vmware.

the class TestProvisionAWSSecurityGroup method waitForTaskCompletion.

public static void waitForTaskCompletion(VerificationHost host, URI provisioningTaskUri) throws Throwable {
    Date expiration = host.getTestExpiration();
    ProvisionSecurityGroupTaskState provisioningTask;
    do {
        provisioningTask = host.getServiceState(null, ProvisionSecurityGroupTaskState.class, provisioningTaskUri);
        if (provisioningTask.taskInfo.stage == TaskState.TaskStage.FAILED) {
            throw new IllegalStateException("Task failed:" + Utils.toJsonHtml(provisioningTask));
        }
        if (provisioningTask.taskInfo.stage == TaskState.TaskStage.FINISHED) {
            return;
        }
        Thread.sleep(1000);
    } while (new Date().before(expiration));
    host.log("Pending task:\n%s", Utils.toJsonHtml(provisioningTask));
    throw new TimeoutException("Some tasks never finished");
}
Also used : ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException)

Example 12 with ProvisionSecurityGroupTaskState

use of com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState in project photon-model by vmware.

the class TestProvisionAWSSecurityGroup method testProvisionAWSSecurityGroup.

@Test
public void testProvisionAWSSecurityGroup() 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 sg service
    Operation securityGroupResponse = new Operation();
    SecurityGroupState initialSecurityGroupState = buildSecurityGroupState(creds, pool);
    TestUtils.postSecurityGroup(this.host, initialSecurityGroupState, 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);
    waitForTaskCompletion(this.host, UriUtils.buildUri(this.host, ps.documentSelfLink));
    validateAWSArtifacts(securityGroupState.documentSelfLink, 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);
    waitForTaskCompletion(this.host, UriUtils.buildUri(this.host, removeTask.documentSelfLink));
    // verify security group state is gone
    try {
        getSecurityGroupState(securityGroupState.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)

Example 13 with ProvisionSecurityGroupTaskState

use of com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState in project photon-model by vmware.

the class ProvisionSecurityGroupTaskService method patchAdapter.

private void patchAdapter(ProvisionSecurityGroupTaskState taskState, String subTaskLink) {
    if (subTaskLink == null) {
        createSubTask(taskState, link -> patchAdapter(taskState, link));
        return;
    }
    taskState.securityGroupDescriptionLinks.forEach(sgLink -> sendRequest(Operation.createGet(UriUtils.buildUri(this.getHost(), sgLink)).setCompletion((o, e) -> {
        if (e != null) {
            // don't fail the task; just update the subtask, which will
            // handle the failure if necessary
            ResourceOperationResponse subTaskPatchBody = ResourceOperationResponse.fail(sgLink, e);
            updateSubTask(subTaskLink, subTaskPatchBody);
            return;
        }
        SecurityGroupState securityGroupState = o.getBody(SecurityGroupState.class);
        SecurityGroupInstanceRequest req = toReq(securityGroupState, taskState, sgLink, subTaskLink);
        sendRequest(Operation.createPatch(securityGroupState.instanceAdapterReference).setBody(req).setCompletion((oo, ee) -> {
            if (ee != null) {
                ResourceOperationResponse subTaskPatchBody = ResourceOperationResponse.fail(sgLink, ee);
                updateSubTask(subTaskLink, subTaskPatchBody);
            }
        }));
    })));
}
Also used : SecurityGroupInstanceRequest(com.vmware.photon.controller.model.adapterapi.SecurityGroupInstanceRequest) Operation(com.vmware.xenon.common.Operation) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) Set(java.util.Set) ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) Consumer(java.util.function.Consumer) OPTIONAL(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.OPTIONAL) List(java.util.List) Utils(com.vmware.xenon.common.Utils) UriPaths(com.vmware.photon.controller.model.UriPaths) Map(java.util.Map) UriUtils(com.vmware.xenon.common.UriUtils) TaskState(com.vmware.xenon.common.TaskState) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) EXPAND(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.EXPAND) InstanceRequestType(com.vmware.photon.controller.model.adapterapi.SecurityGroupInstanceRequest.InstanceRequestType) TaskService(com.vmware.xenon.services.common.TaskService) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) FIXED_ITEM_NAME(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.FIXED_ITEM_NAME) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) SecurityGroupInstanceRequest(com.vmware.photon.controller.model.adapterapi.SecurityGroupInstanceRequest) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)

Example 14 with ProvisionSecurityGroupTaskState

use of com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState in project photon-model by vmware.

the class ProvisionSecurityGroupTaskService method handleStart.

@Override
public void handleStart(Operation start) {
    if (!start.hasBody()) {
        start.fail(new IllegalArgumentException("body is required"));
        return;
    }
    ProvisionSecurityGroupTaskState state = start.getBody(ProvisionSecurityGroupTaskState.class);
    try {
        state.validate();
    } catch (Exception e) {
        start.fail(e);
        return;
    }
    state.taskInfo = new TaskState();
    state.taskInfo.stage = TaskState.TaskStage.CREATED;
    state.taskSubStage = SubStage.CREATED;
    start.complete();
    // start the task
    sendSelfPatch(TaskState.TaskStage.CREATED, null);
}
Also used : ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) TaskState(com.vmware.xenon.common.TaskState)

Example 15 with ProvisionSecurityGroupTaskState

use of com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState in project photon-model by vmware.

the class ProvisionSecurityGroupTaskService method sendSelfPatch.

private void sendSelfPatch(TaskState.TaskStage stage, Throwable e) {
    ProvisionSecurityGroupTaskState body = new ProvisionSecurityGroupTaskState();
    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 : ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) TaskState(com.vmware.xenon.common.TaskState)

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