use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext in project cloudbreak by hortonworks.
the class DownscaleStackCollectResourcesHandler method accept.
@Override
public void accept(Event<DownscaleStackCollectResourcesRequest> collectResourcesRequestEvent) {
LOGGER.info("Received event: {}", collectResourcesRequestEvent);
DownscaleStackCollectResourcesRequest request = collectResourcesRequestEvent.getData();
DownscaleStackCollectResourcesResult result;
try {
CloudContext cloudContext = request.getCloudContext();
CloudConnector connector = cloudPlatformConnectors.get(cloudContext.getPlatformVariant());
AuthenticatedContext ac = connector.authentication().authenticate(cloudContext, request.getCloudCredential());
Object resourcesToScale = connector.resources().collectResourcesToRemove(ac, request.getCloudStack(), request.getCloudResources(), request.getInstances());
LOGGER.info("Collect resources successfully finished for {}", cloudContext);
result = new DownscaleStackCollectResourcesResult(request, resourcesToScale);
} catch (RuntimeException e) {
LOGGER.error("Failed to handle DownscaleStackCollectResourcesRequest.", e);
result = new DownscaleStackCollectResourcesResult(e.getMessage(), e, request);
}
request.getResult().onNext(result);
LOGGER.info("DownscaleStackCollectResourcesRequest finished");
eventBus.notify(result.selector(), new Event<>(collectResourcesRequestEvent.getHeaders(), result));
}
use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext in project cloudbreak by hortonworks.
the class MockInstanceConnector method check.
@Override
public List<CloudVmInstanceStatus> check(AuthenticatedContext authenticatedContext, List<CloudInstance> vms) {
try {
MockCredentialView mockCredentialView = mockCredentialViewFactory.createCredetialView(authenticatedContext.getCloudCredential());
LOGGER.info("collect instance statuses from mock spi, server address: " + mockCredentialView.getMockEndpoint());
CloudVmInstanceStatus[] cloudVmInstanceStatusArray = Unirest.post(mockCredentialView.getMockEndpoint() + "/spi/cloud_instance_statuses").asObject(CloudVmInstanceStatus[].class).getBody();
LOGGER.info("collected instance statuses: " + Arrays.toString(cloudVmInstanceStatusArray));
List<CloudVmInstanceStatus> cloudVmInstanceStatuses = new ArrayList<>();
for (CloudInstance instance : vms) {
Optional<CloudVmInstanceStatus> vmInstanceStatusFromApi = Arrays.stream(cloudVmInstanceStatusArray).filter(instanceStatus -> instanceStatus.getCloudInstance().getTemplate().getPrivateId().equals(instance.getTemplate().getPrivateId())).findFirst();
InstanceStatus instanceStatus = InstanceStatus.TERMINATED;
if (vmInstanceStatusFromApi.isPresent()) {
instanceStatus = vmInstanceStatusFromApi.get().getStatus();
}
CloudVmInstanceStatus cloudVmInstanceStatus = new CloudVmInstanceStatus(instance, instanceStatus);
cloudVmInstanceStatuses.add(cloudVmInstanceStatus);
}
return cloudVmInstanceStatuses;
} catch (UnirestException e) {
throw new RuntimeException("can't convert to object", e);
}
}
use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext in project cloudbreak by hortonworks.
the class StartStackHandler method accept.
@Override
public void accept(Event<StartInstancesRequest> event) {
LOGGER.info("Received event: {}", event);
StartInstancesRequest request = event.getData();
CloudContext cloudContext = request.getCloudContext();
try {
CloudConnector connector = cloudPlatformConnectors.get(cloudContext.getPlatformVariant());
AuthenticatedContext authenticatedContext = connector.authentication().authenticate(cloudContext, request.getCloudCredential());
List<CloudInstance> instances = request.getCloudInstances();
List<CloudVmInstanceStatus> instanceStatuses = connector.instances().start(authenticatedContext, request.getResources(), instances);
PollTask<InstancesStatusResult> task = statusCheckFactory.newPollInstanceStateTask(authenticatedContext, instances, Sets.newHashSet(InstanceStatus.STARTED, InstanceStatus.FAILED));
InstancesStatusResult statusResult = new InstancesStatusResult(cloudContext, instanceStatuses);
if (!task.completed(statusResult)) {
statusResult = syncPollingScheduler.schedule(task);
}
StartInstancesResult result = new StartInstancesResult(request, cloudContext, statusResult);
request.getResult().onNext(result);
eventBus.notify(result.selector(), new Event<>(event.getHeaders(), result));
} catch (Exception e) {
StartInstancesResult failure = new StartInstancesResult("Failed to start stack", e, request);
request.getResult().onNext(failure);
eventBus.notify(failure.selector(), new Event<>(event.getHeaders(), failure));
}
}
use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext 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));
}
}
use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext in project cloudbreak by hortonworks.
the class ProvisionSetupHandler method accept.
@Override
public void accept(Event<SetupRequest> event) {
LOGGER.info("Received event: {}", event);
SetupRequest request = event.getData();
CloudContext cloudContext = request.getCloudContext();
SetupResult result;
try {
CloudConnector connector = cloudPlatformConnectors.get(cloudContext.getPlatformVariant());
AuthenticatedContext auth = connector.authentication().authenticate(cloudContext, request.getCloudCredential());
CloudStack cloudStack = request.getCloudStack();
connector.setup().prerequisites(auth, cloudStack, resourceNotifier);
result = new SetupResult(request);
LOGGER.info("Provision setup finished for {}", cloudContext);
} catch (RuntimeException e) {
result = new SetupResult(e, request);
}
request.getResult().onNext(result);
eventBus.notify(result.selector(), new Event<>(event.getHeaders(), result));
}
Aggregations