Search in sources :

Example 1 with DelayedStartInstancesRequest

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));
    }
}
Also used : CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) InstancesStatusResult(com.sequenceiq.cloudbreak.cloud.event.instance.InstancesStatusResult) DelayedStartInstancesRequest(com.sequenceiq.cloudbreak.cloud.event.instance.DelayedStartInstancesRequest) StartInstancesResult(com.sequenceiq.cloudbreak.cloud.event.instance.StartInstancesResult) Event(reactor.bus.Event)

Example 2 with DelayedStartInstancesRequest

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));
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) InstanceConnector(com.sequenceiq.cloudbreak.cloud.InstanceConnector) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DelayedStartInstancesRequest(com.sequenceiq.cloudbreak.cloud.event.instance.DelayedStartInstancesRequest) Callable(java.util.concurrent.Callable) StartInstancesResult(com.sequenceiq.cloudbreak.cloud.event.instance.StartInstancesResult) Event(reactor.bus.Event) List(java.util.List) Authenticator(com.sequenceiq.cloudbreak.cloud.Authenticator) Test(org.junit.jupiter.api.Test)

Example 3 with DelayedStartInstancesRequest

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));
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Event(reactor.bus.Event) DelayedStartInstancesRequest(com.sequenceiq.cloudbreak.cloud.event.instance.DelayedStartInstancesRequest) Test(org.junit.jupiter.api.Test)

Example 4 with DelayedStartInstancesRequest

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));
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) StartInstancesResult(com.sequenceiq.cloudbreak.cloud.event.instance.StartInstancesResult) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Event(reactor.bus.Event) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DelayedStartInstancesRequest(com.sequenceiq.cloudbreak.cloud.event.instance.DelayedStartInstancesRequest) Authenticator(com.sequenceiq.cloudbreak.cloud.Authenticator) Test(org.junit.jupiter.api.Test)

Example 5 with DelayedStartInstancesRequest

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));
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) InstanceConnector(com.sequenceiq.cloudbreak.cloud.InstanceConnector) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DelayedStartInstancesRequest(com.sequenceiq.cloudbreak.cloud.event.instance.DelayedStartInstancesRequest) StartInstancesResult(com.sequenceiq.cloudbreak.cloud.event.instance.StartInstancesResult) Event(reactor.bus.Event) List(java.util.List) Authenticator(com.sequenceiq.cloudbreak.cloud.Authenticator) Test(org.junit.jupiter.api.Test)

Aggregations

DelayedStartInstancesRequest (com.sequenceiq.cloudbreak.cloud.event.instance.DelayedStartInstancesRequest)6 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)6 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)5 StartInstancesResult (com.sequenceiq.cloudbreak.cloud.event.instance.StartInstancesResult)5 Event (reactor.bus.Event)5 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)4 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)4 Test (org.junit.jupiter.api.Test)4 Authenticator (com.sequenceiq.cloudbreak.cloud.Authenticator)3 List (java.util.List)3 InstanceConnector (com.sequenceiq.cloudbreak.cloud.InstanceConnector)2 EntitlementService (com.sequenceiq.cloudbreak.auth.altus.EntitlementService)1 CollectMetadataRequest (com.sequenceiq.cloudbreak.cloud.event.instance.CollectMetadataRequest)1 CollectMetadataResult (com.sequenceiq.cloudbreak.cloud.event.instance.CollectMetadataResult)1 InstancesStatusResult (com.sequenceiq.cloudbreak.cloud.event.instance.InstancesStatusResult)1 StartInstancesRequest (com.sequenceiq.cloudbreak.cloud.event.instance.StartInstancesRequest)1 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)1 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)1 Selectable (com.sequenceiq.cloudbreak.common.event.Selectable)1 InstanceMetaDataToCloudInstanceConverter (com.sequenceiq.freeipa.converter.cloud.InstanceMetaDataToCloudInstanceConverter)1