Search in sources :

Example 1 with ImageChangeEvent

use of com.sequenceiq.freeipa.flow.stack.image.change.event.ImageChangeEvent in project cloudbreak by hortonworks.

the class ImageChangeService method changeImage.

public FlowIdentifier changeImage(String accountId, ImageChangeRequest request) {
    Stack stack = stackService.getByEnvironmentCrnAndAccountId(request.getEnvironmentCrn(), accountId);
    MDCBuilder.buildMdcContext(stack);
    ImageChangeEvent imageChangeEvent = new ImageChangeEvent(IMAGE_CHANGE_EVENT.event(), stack.getId(), request.getImageSettings());
    LOGGER.info("Triggering image change flow with event: {}", imageChangeEvent);
    return flowManager.notify(imageChangeEvent.selector(), imageChangeEvent);
}
Also used : ImageChangeEvent(com.sequenceiq.freeipa.flow.stack.image.change.event.ImageChangeEvent) Stack(com.sequenceiq.freeipa.entity.Stack)

Example 2 with ImageChangeEvent

use of com.sequenceiq.freeipa.flow.stack.image.change.event.ImageChangeEvent in project cloudbreak by hortonworks.

the class ImageChangeAction method doExecute.

@Override
protected void doExecute(StackContext context, ImageChangeEvent payload, Map<Object, Object> variables) throws Exception {
    setOperationId(variables, payload.getOperationId());
    ImageEntity originalImageEntity = storeOriginalImageRevision(payload, variables);
    ImageEntity newImageEntity = imageService.changeImage(context.getStack(), payload.getRequest());
    variables.put(ImageChangeActions.IMAGE_CHANGED_IN_DB, Boolean.TRUE);
    if (originalImageEntity.equals(newImageEntity)) {
        LOGGER.info("New and original image is the same, no change is required");
        sendEvent(context, new ImageChangeEvent(IMAGE_CHANGE_NOT_REQUIRED_EVENT.event(), payload.getResourceId(), payload.getRequest()));
    } else {
        sendEvent(context, new ImageChangeEvent(IMAGE_CHANGED_IN_DB_EVENT.event(), payload.getResourceId(), payload.getRequest()));
    }
}
Also used : ImageEntity(com.sequenceiq.freeipa.entity.ImageEntity) ImageChangeEvent(com.sequenceiq.freeipa.flow.stack.image.change.event.ImageChangeEvent)

Example 3 with ImageChangeEvent

use of com.sequenceiq.freeipa.flow.stack.image.change.event.ImageChangeEvent in project cloudbreak by hortonworks.

the class ImageChangeActions method prepareImage.

@Bean(name = "PREPARE_IMAGE_STATE")
public AbstractImageChangeAction<?> prepareImage() {
    return new AbstractImageChangeAction<>(ImageChangeEvent.class) {

        @Inject
        private ImageConverter imageConverter;

        @Inject
        private ImageService imageService;

        @Override
        protected void doExecute(StackContext context, ImageChangeEvent payload, Map<Object, Object> variables) throws Exception {
            CloudStack cloudStack = getCloudStackConverter().convert(context.getStack());
            Image image = imageConverter.convert(imageService.getByStack(context.getStack()));
            PrepareImageRequest<Object> request = new PrepareImageRequest<>(context.getCloudContext(), context.getCloudCredential(), cloudStack, image);
            LOGGER.info("Prepare image: {}", image);
            sendEvent(context, request);
        }

        @Override
        protected Object getFailurePayload(ImageChangeEvent payload, Optional<StackContext> flowContext, Exception ex) {
            LOGGER.error("[PREPARE_IMAGE_STATE] failed", ex);
            return new StackFailureEvent(IMAGE_CHANGE_FAILED_EVENT.event(), payload.getResourceId(), ex);
        }
    };
}
Also used : Optional(java.util.Optional) StackFailureEvent(com.sequenceiq.freeipa.flow.stack.StackFailureEvent) ImageConverter(com.sequenceiq.freeipa.converter.image.ImageConverter) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Image(com.sequenceiq.cloudbreak.cloud.model.Image) ImageService(com.sequenceiq.freeipa.service.image.ImageService) ImageChangeEvent(com.sequenceiq.freeipa.flow.stack.image.change.event.ImageChangeEvent) StackContext(com.sequenceiq.freeipa.flow.stack.StackContext) Map(java.util.Map) PrepareImageRequest(com.sequenceiq.cloudbreak.cloud.event.setup.PrepareImageRequest) Bean(org.springframework.context.annotation.Bean)

Example 4 with ImageChangeEvent

use of com.sequenceiq.freeipa.flow.stack.image.change.event.ImageChangeEvent in project cloudbreak by hortonworks.

the class ImageChangeActionTest method testSameImage.

@Test
public void testSameImage() throws Exception {
    StackContext stackContext = mock(StackContext.class);
    Stack stack = new Stack();
    when(stackContext.getStack()).thenReturn(stack);
    when(stackContext.getFlowParameters()).thenReturn(new FlowParameters("flid", "userCrn", null));
    ImageEntity imageEntity = new ImageEntity();
    imageEntity.setId(2L);
    ImageSettingsRequest request = new ImageSettingsRequest();
    when(imageService.getByStackId(1L)).thenReturn(imageEntity);
    when(imageService.changeImage(stack, request)).thenReturn(imageEntity);
    when(auditReader.getRevisions(ImageEntity.class, imageEntity.getId())).thenReturn(List.of());
    Map<Object, Object> variables = new HashMap<>();
    underTest.doExecute(stackContext, new ImageChangeEvent(1L, request), variables);
    assertEquals(Boolean.TRUE, variables.get(IMAGE_CHANGED_IN_DB));
    assertFalse(variables.containsKey(ORIGINAL_IMAGE_REVISION));
    assertFalse(variables.containsKey(IMAGE_ENTITY_ID));
    assertEquals(imageEntity, variables.get(ORIGINAL_IMAGE));
    ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class);
    verify(reactorEventFactory).createEvent(anyMap(), captor.capture());
    ImageChangeEvent event = (ImageChangeEvent) captor.getValue();
    assertEquals(IMAGE_CHANGE_NOT_REQUIRED_EVENT.event(), event.selector());
    assertEquals(1L, event.getResourceId());
    assertEquals(request, event.getRequest());
}
Also used : FlowParameters(com.sequenceiq.flow.core.FlowParameters) ImageSettingsRequest(com.sequenceiq.freeipa.api.v1.freeipa.stack.model.common.image.ImageSettingsRequest) ImageEntity(com.sequenceiq.freeipa.entity.ImageEntity) HashMap(java.util.HashMap) ImageChangeEvent(com.sequenceiq.freeipa.flow.stack.image.change.event.ImageChangeEvent) StackContext(com.sequenceiq.freeipa.flow.stack.StackContext) Stack(com.sequenceiq.freeipa.entity.Stack) Test(org.junit.jupiter.api.Test)

Example 5 with ImageChangeEvent

use of com.sequenceiq.freeipa.flow.stack.image.change.event.ImageChangeEvent in project cloudbreak by hortonworks.

the class ImageChangeActionTest method testStoreRevision.

@Test
public void testStoreRevision() throws Exception {
    StackContext stackContext = mock(StackContext.class);
    Stack stack = new Stack();
    when(stackContext.getStack()).thenReturn(stack);
    when(stackContext.getFlowParameters()).thenReturn(new FlowParameters("flid", "userCrn", null));
    ImageEntity imageEntity = new ImageEntity();
    imageEntity.setId(2L);
    when(imageService.getByStackId(1L)).thenReturn(imageEntity);
    when(auditReader.getRevisions(ImageEntity.class, imageEntity.getId())).thenReturn(List.of(1, 2, 3));
    Map<Object, Object> variables = new HashMap<>();
    ImageSettingsRequest request = new ImageSettingsRequest();
    underTest.doExecute(stackContext, new ImageChangeEvent(1L, request), variables);
    assertEquals(Boolean.TRUE, variables.get(IMAGE_CHANGED_IN_DB));
    assertEquals(3, variables.get(ORIGINAL_IMAGE_REVISION));
    assertEquals(2L, variables.get(IMAGE_ENTITY_ID));
    assertFalse(variables.containsKey(ORIGINAL_IMAGE));
    ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class);
    verify(reactorEventFactory).createEvent(anyMap(), captor.capture());
    ImageChangeEvent event = (ImageChangeEvent) captor.getValue();
    assertEquals(IMAGE_CHANGED_IN_DB_EVENT.event(), event.selector());
    assertEquals(1L, event.getResourceId());
    assertEquals(request, event.getRequest());
}
Also used : FlowParameters(com.sequenceiq.flow.core.FlowParameters) ImageSettingsRequest(com.sequenceiq.freeipa.api.v1.freeipa.stack.model.common.image.ImageSettingsRequest) ImageEntity(com.sequenceiq.freeipa.entity.ImageEntity) HashMap(java.util.HashMap) ImageChangeEvent(com.sequenceiq.freeipa.flow.stack.image.change.event.ImageChangeEvent) StackContext(com.sequenceiq.freeipa.flow.stack.StackContext) Stack(com.sequenceiq.freeipa.entity.Stack) Test(org.junit.jupiter.api.Test)

Aggregations

ImageChangeEvent (com.sequenceiq.freeipa.flow.stack.image.change.event.ImageChangeEvent)7 ImageSettingsRequest (com.sequenceiq.freeipa.api.v1.freeipa.stack.model.common.image.ImageSettingsRequest)4 ImageEntity (com.sequenceiq.freeipa.entity.ImageEntity)4 Stack (com.sequenceiq.freeipa.entity.Stack)4 StackContext (com.sequenceiq.freeipa.flow.stack.StackContext)4 Test (org.junit.jupiter.api.Test)4 FlowParameters (com.sequenceiq.flow.core.FlowParameters)3 HashMap (java.util.HashMap)3 PrepareImageRequest (com.sequenceiq.cloudbreak.cloud.event.setup.PrepareImageRequest)1 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)1 Image (com.sequenceiq.cloudbreak.cloud.model.Image)1 Selectable (com.sequenceiq.cloudbreak.common.event.Selectable)1 FlowTriggerEventQueue (com.sequenceiq.flow.core.chain.config.FlowTriggerEventQueue)1 ImageConverter (com.sequenceiq.freeipa.converter.image.ImageConverter)1 DownscaleEvent (com.sequenceiq.freeipa.flow.freeipa.downscale.event.DownscaleEvent)1 ChangePrimaryGatewayEvent (com.sequenceiq.freeipa.flow.freeipa.repair.changeprimarygw.event.ChangePrimaryGatewayEvent)1 SaltUpdateTriggerEvent (com.sequenceiq.freeipa.flow.freeipa.salt.update.SaltUpdateTriggerEvent)1 UpgradeEvent (com.sequenceiq.freeipa.flow.freeipa.upgrade.UpgradeEvent)1 UpscaleEvent (com.sequenceiq.freeipa.flow.freeipa.upscale.event.UpscaleEvent)1 StackFailureEvent (com.sequenceiq.freeipa.flow.stack.StackFailureEvent)1