Search in sources :

Example 1 with TerminateStackResult

use of com.sequenceiq.cloudbreak.cloud.event.resource.TerminateStackResult in project cloudbreak by hortonworks.

the class TerminateStackHandler method accept.

@Override
public void accept(Event<TerminateStackRequest> terminateStackRequestEvent) {
    LOGGER.info("Received event: {}", terminateStackRequestEvent);
    TerminateStackRequest request = terminateStackRequestEvent.getData();
    try {
        CloudConnector connector = cloudPlatformConnectors.get(request.getCloudContext().getPlatformVariant());
        AuthenticatedContext ac = connector.authentication().authenticate(request.getCloudContext(), request.getCloudCredential());
        List<CloudResourceStatus> resourceStatus = connector.resources().terminate(ac, request.getCloudStack(), request.getCloudResources());
        List<CloudResource> resources = ResourceLists.transform(resourceStatus);
        TerminateStackResult result;
        if (!resources.isEmpty()) {
            PollTask<ResourcesStatePollerResult> task = statusCheckFactory.newPollResourcesStateTask(ac, resources, false);
            ResourcesStatePollerResult statePollerResult = ResourcesStatePollerResults.build(request.getCloudContext(), resourceStatus);
            if (!task.completed(statePollerResult)) {
                statePollerResult = syncPollingScheduler.schedule(task);
            }
            if (!statePollerResult.getStatus().equals(ResourceStatus.DELETED)) {
                throw new CloudConnectorException("Stack could not be terminated, Resource(s) could not be deleted on the provider side.");
            } else {
                result = new TerminateStackResult(request);
            }
        } else {
            result = new TerminateStackResult(request);
        }
        CloudCredentialStatus credentialStatus = connector.credentials().delete(ac);
        if (CredentialStatus.FAILED == credentialStatus.getStatus()) {
            if (credentialStatus.getException() != null) {
                throw new CloudConnectorException(credentialStatus.getException());
            }
            throw new CloudConnectorException(credentialStatus.getStatusReason());
        }
        request.getResult().onNext(result);
        LOGGER.info("TerminateStackHandler finished");
        eventBus.notify(result.selector(), new Event<>(terminateStackRequestEvent.getHeaders(), result));
    } catch (Exception e) {
        LOGGER.error("Failed to handle TerminateStackRequest", e);
        TerminateStackResult terminateStackResult = new TerminateStackResult("Stack termination failed.", e, request);
        request.getResult().onNext(terminateStackResult);
        eventBus.notify(terminateStackResult.selector(), new Event<>(terminateStackRequestEvent.getHeaders(), terminateStackResult));
    }
}
Also used : CloudConnector(com.sequenceiq.cloudbreak.cloud.CloudConnector) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) TerminateStackResult(com.sequenceiq.cloudbreak.cloud.event.resource.TerminateStackResult) TerminateStackRequest(com.sequenceiq.cloudbreak.cloud.event.resource.TerminateStackRequest) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Event(reactor.bus.Event) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) ResourcesStatePollerResult(com.sequenceiq.cloudbreak.cloud.task.ResourcesStatePollerResult) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus)

Example 2 with TerminateStackResult

use of com.sequenceiq.cloudbreak.cloud.event.resource.TerminateStackResult in project cloudbreak by hortonworks.

the class ServiceProviderConnectorAdapter method deleteStack.

public void deleteStack(Stack stack, Credential credential) {
    LOGGER.debug("Assembling terminate stack event for stack: {}", stack);
    Location location = location(region(stack.getRegion()), availabilityZone(stack.getAvailabilityZone()));
    CloudContext cloudContext = new CloudContext(stack.getId(), stack.getName(), stack.cloudPlatform(), stack.getOwner(), stack.getPlatformVariant(), location);
    CloudCredential cloudCredential = credentialConverter.convert(stack.getCredential());
    List<CloudResource> resources = cloudResourceConverter.convert(stack.getResources());
    CloudStack cloudStack = cloudStackConverter.convert(stack);
    TerminateStackRequest<TerminateStackResult> terminateRequest = new TerminateStackRequest<>(cloudContext, cloudStack, cloudCredential, resources);
    LOGGER.info("Triggering terminate stack event: {}", terminateRequest);
    eventBus.notify(terminateRequest.selector(), eventFactory.createEvent(terminateRequest));
    try {
        TerminateStackResult res = terminateRequest.await();
        LOGGER.info("Terminate stack result: {}", res);
        if (res.getStatus().equals(EventStatus.FAILED)) {
            if (res.getErrorDetails() != null) {
                LOGGER.error("Failed to terminate the stack", res.getErrorDetails());
                throw new OperationException(res.getErrorDetails());
            }
            throw new OperationException(format("Failed to terminate the stack: %s due to %s", cloudContext, res.getStatusReason()));
        }
    } catch (InterruptedException e) {
        LOGGER.error("Error while terminating the stack", e);
        throw new OperationException(e);
    }
}
Also used : TerminateStackResult(com.sequenceiq.cloudbreak.cloud.event.resource.TerminateStackResult) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) TerminateStackRequest(com.sequenceiq.cloudbreak.cloud.event.resource.TerminateStackRequest) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) OperationException(com.sequenceiq.cloudbreak.service.stack.connector.OperationException) Location(com.sequenceiq.cloudbreak.cloud.model.Location)

Example 3 with TerminateStackResult

use of com.sequenceiq.cloudbreak.cloud.event.resource.TerminateStackResult in project cloudbreak by hortonworks.

the class TerminateStackComponentTest method testTerminateStack.

@Test
public void testTerminateStack() {
    TerminateStackResult result = sendCloudRequest();
    assertEquals(EventStatus.OK, result.getStatus());
    assertNull(result.getErrorDetails());
}
Also used : TerminateStackResult(com.sequenceiq.cloudbreak.cloud.event.resource.TerminateStackResult) Test(org.junit.Test)

Aggregations

TerminateStackResult (com.sequenceiq.cloudbreak.cloud.event.resource.TerminateStackResult)3 TerminateStackRequest (com.sequenceiq.cloudbreak.cloud.event.resource.TerminateStackRequest)2 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)2 CloudConnector (com.sequenceiq.cloudbreak.cloud.CloudConnector)1 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)1 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)1 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)1 CloudCredentialStatus (com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus)1 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)1 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)1 Location (com.sequenceiq.cloudbreak.cloud.model.Location)1 ResourcesStatePollerResult (com.sequenceiq.cloudbreak.cloud.task.ResourcesStatePollerResult)1 OperationException (com.sequenceiq.cloudbreak.service.stack.connector.OperationException)1 Test (org.junit.Test)1 Event (reactor.bus.Event)1