use of com.sequenceiq.cloudbreak.cloud.event.instance.DelayedStartInstancesRequest in project cloudbreak by hortonworks.
the class DelayedStartStackHandler method accept.
@Override
public void accept(Event<DelayedStartInstancesRequest> event) {
LOGGER.debug("Received event: {}", event);
DelayedStartInstancesRequest request = event.getData();
CloudContext cloudContext = request.getCloudContext();
try {
CloudConnector<Object> connector = cloudPlatformConnectors.get(cloudContext.getPlatformVariant());
AuthenticatedContext authenticatedContext = connector.authentication().authenticate(cloudContext, request.getCloudCredential());
List<CloudInstance> instances = request.getCloudInstances();
List<CloudVmInstanceStatus> instanceStatuses = startFirstInstance(request.getResources(), connector, authenticatedContext, instances);
if (instances.size() > 1) {
for (CloudInstance instance : instances.subList(1, instances.size())) {
List<CloudVmInstanceStatus> startInstanceResult = delayedExecutorService.runWithDelay(() -> startInstance(request.getResources(), connector, authenticatedContext, instance), request.getDelayInSec(), TimeUnit.SECONDS);
instanceStatuses.addAll(startInstanceResult);
}
}
InstancesStatusResult statusResult = new InstancesStatusResult(cloudContext, instanceStatuses);
StartInstancesResult result = new StartInstancesResult(request.getResourceId(), 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.getResourceId());
request.getResult().onNext(failure);
eventBus.notify(failure.selector(), new Event<>(event.getHeaders(), failure));
}
}
use of com.sequenceiq.cloudbreak.cloud.event.instance.DelayedStartInstancesRequest in project cloudbreak by hortonworks.
the class DelayedStartStackHandlerTest method testDelayedStopMultipleInstance.
@Test
public void testDelayedStopMultipleInstance() throws ExecutionException, InterruptedException {
CloudContext cloudContext = mock(CloudContext.class);
CloudCredential cloudCredential = mock(CloudCredential.class);
CloudInstance cloudInstance1 = mock(CloudInstance.class);
CloudInstance cloudInstance2 = mock(CloudInstance.class);
CloudInstance cloudInstance3 = mock(CloudInstance.class);
List<CloudInstance> cloudInstances = List.of(cloudInstance1, cloudInstance2, cloudInstance3);
CloudConnector<Object> cloudConnector = mock(CloudConnector.class);
when(cloudPlatformConnectors.get(any())).thenReturn(cloudConnector);
Authenticator authenticator = mock(Authenticator.class);
when(cloudConnector.authentication()).thenReturn(authenticator);
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
when(authenticator.authenticate(any(), any())).thenReturn(authenticatedContext);
InstanceConnector instanceConnector = mock(InstanceConnector.class);
when(cloudConnector.instances()).thenReturn(instanceConnector);
when(delayedExecutorService.runWithDelay(any(Callable.class), eq(DELAY), eq(TimeUnit.SECONDS))).thenAnswer(inv -> inv.getArgument(0, Callable.class).call());
underTest.accept(new Event<>(new DelayedStartInstancesRequest(cloudContext, cloudCredential, List.of(), cloudInstances, DELAY)));
ArgumentCaptor<List<CloudInstance>> instanceCaptor = ArgumentCaptor.forClass(List.class);
verify(instanceConnector, times(3)).start(eq(authenticatedContext), eq(List.of()), instanceCaptor.capture());
List<List<CloudInstance>> instanceCaptorAllValues = instanceCaptor.getAllValues();
assertEquals(3, instanceCaptorAllValues.size());
assertEquals(1, instanceCaptorAllValues.get(0).size());
assertEquals(cloudInstance1, instanceCaptorAllValues.get(0).get(0));
assertEquals(1, instanceCaptorAllValues.get(1).size());
assertEquals(cloudInstance2, instanceCaptorAllValues.get(1).get(0));
assertEquals(1, instanceCaptorAllValues.get(2).size());
assertEquals(cloudInstance3, instanceCaptorAllValues.get(2).get(0));
verify(delayedExecutorService, times(2)).runWithDelay(any(Callable.class), eq(DELAY), eq(TimeUnit.SECONDS));
verify(eventBus).notify(eq(StartInstancesResult.class.getSimpleName().toUpperCase()), any(Event.class));
}
use of com.sequenceiq.cloudbreak.cloud.event.instance.DelayedStartInstancesRequest in project cloudbreak by hortonworks.
the class DelayedStartStackHandlerTest method testExceptionThrown.
@Test
public void testExceptionThrown() {
CloudContext cloudContext = mock(CloudContext.class);
CloudCredential cloudCredential = mock(CloudCredential.class);
List<CloudInstance> cloudInstances = List.of();
CloudConnector<Object> cloudConnector = mock(CloudConnector.class);
when(cloudPlatformConnectors.get(any())).thenThrow(new RuntimeException("test ex"));
underTest.accept(new Event<>(new DelayedStartInstancesRequest(cloudContext, cloudCredential, List.of(), cloudInstances, DELAY)));
verifyNoInteractions(delayedExecutorService);
verify(cloudConnector, never()).instances();
verify(eventBus).notify(eq(StartInstancesResult.class.getSimpleName().toUpperCase() + "_ERROR"), any(Event.class));
}
use of com.sequenceiq.cloudbreak.cloud.event.instance.DelayedStartInstancesRequest in project cloudbreak by hortonworks.
the class DelayedStartStackHandlerTest method testDelayedStopNoInstance.
@Test
public void testDelayedStopNoInstance() {
CloudContext cloudContext = mock(CloudContext.class);
CloudCredential cloudCredential = mock(CloudCredential.class);
List<CloudInstance> cloudInstances = List.of();
CloudConnector<Object> cloudConnector = mock(CloudConnector.class);
when(cloudPlatformConnectors.get(any())).thenReturn(cloudConnector);
Authenticator authenticator = mock(Authenticator.class);
when(cloudConnector.authentication()).thenReturn(authenticator);
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
when(authenticator.authenticate(any(), any())).thenReturn(authenticatedContext);
underTest.accept(new Event<>(new DelayedStartInstancesRequest(cloudContext, cloudCredential, List.of(), cloudInstances, DELAY)));
verifyNoInteractions(delayedExecutorService);
verify(cloudConnector, never()).instances();
verify(eventBus).notify(eq(StartInstancesResult.class.getSimpleName().toUpperCase()), any(Event.class));
}
use of com.sequenceiq.cloudbreak.cloud.event.instance.DelayedStartInstancesRequest in project cloudbreak by hortonworks.
the class DelayedStartStackHandlerTest method testDelayedStopOneInstance.
@Test
public void testDelayedStopOneInstance() {
CloudContext cloudContext = mock(CloudContext.class);
CloudCredential cloudCredential = mock(CloudCredential.class);
CloudInstance cloudInstance1 = mock(CloudInstance.class);
List<CloudInstance> cloudInstances = List.of(cloudInstance1);
CloudConnector<Object> cloudConnector = mock(CloudConnector.class);
when(cloudPlatformConnectors.get(any())).thenReturn(cloudConnector);
Authenticator authenticator = mock(Authenticator.class);
when(cloudConnector.authentication()).thenReturn(authenticator);
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
when(authenticator.authenticate(any(), any())).thenReturn(authenticatedContext);
InstanceConnector instanceConnector = mock(InstanceConnector.class);
when(cloudConnector.instances()).thenReturn(instanceConnector);
underTest.accept(new Event<>(new DelayedStartInstancesRequest(cloudContext, cloudCredential, List.of(), cloudInstances, DELAY)));
ArgumentCaptor<List<CloudInstance>> instanceCaptor = ArgumentCaptor.forClass(List.class);
verify(instanceConnector, times(1)).start(eq(authenticatedContext), eq(List.of()), instanceCaptor.capture());
List<List<CloudInstance>> instanceCaptorAllValues = instanceCaptor.getAllValues();
assertEquals(1, instanceCaptorAllValues.size());
assertEquals(1, instanceCaptorAllValues.get(0).size());
verifyNoInteractions(delayedExecutorService);
verify(eventBus).notify(eq(StartInstancesResult.class.getSimpleName().toUpperCase()), any(Event.class));
}
Aggregations