Search in sources :

Example 1 with VmImage

use of com.sequenceiq.cloudbreak.domain.VmImage in project cloudbreak by hortonworks.

the class CustomImageCatalogService method updateCustomImage.

public CustomImage updateCustomImage(Long workspaceId, String creator, String imageCatalogName, CustomImage customImage) {
    LOGGER.debug(String.format("Update custom image '%s' in catalog '%s' in workspace '%d'", customImage.getName(), imageCatalogName, workspaceId));
    try {
        return transactionService.required(() -> {
            ImageCatalog imageCatalog = getImageCatalog(workspaceId, imageCatalogName);
            CustomImage savedCustomImage = getCustomImageFromCatalog(imageCatalog, customImage.getName());
            if (customImage.getCustomizedImageId() != null) {
                savedCustomImage.setCustomizedImageId(customImage.getCustomizedImageId());
            }
            if (customImage.getImageType() != null) {
                savedCustomImage.setImageType(customImage.getImageType());
            }
            if (customImage.getBaseParcelUrl() != null) {
                savedCustomImage.setBaseParcelUrl(customImage.getBaseParcelUrl());
            }
            if (customImage.getVmImage() != null) {
                Set<VmImage> vmImagesToSave = new HashSet<>(customImage.getVmImage());
                for (VmImage savedVmImage : savedCustomImage.getVmImage()) {
                    Optional<VmImage> vmImage = vmImagesToSave.stream().filter(vm -> vm.getRegion().equals(savedVmImage.getRegion())).findFirst();
                    if (vmImage.isPresent()) {
                        savedVmImage.setRegion(vmImage.get().getRegion());
                        savedVmImage.setImageReference(vmImage.get().getImageReference());
                        vmImagesToSave.remove(vmImage.get());
                    } else {
                        savedCustomImage.getVmImage().remove(savedVmImage);
                    }
                }
                for (VmImage vmImage : vmImagesToSave) {
                    vmImage.setCreator(creator);
                    vmImage.setCustomImage(savedCustomImage);
                    savedCustomImage.getVmImage().add(vmImage);
                }
            }
            validateSourceImage(savedCustomImage);
            ImageCatalog savedImageCatalog = imageCatalogService.pureSave(imageCatalog);
            return getCustomImageFromCatalog(savedImageCatalog, customImage.getName());
        });
    } catch (TransactionService.TransactionExecutionException e) {
        LOGGER.error(String.format("Custom image '%s' update failed in custom image catalog '%s' in workspace '%d': %s", customImage.getName(), imageCatalogName, workspaceId, e.getMessage()));
        throw new TransactionService.TransactionRuntimeExecutionException(e);
    }
}
Also used : BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) RegionAwareCrnGenerator(com.sequenceiq.cloudbreak.auth.crn.RegionAwareCrnGenerator) Set(java.util.Set) VmImage(com.sequenceiq.cloudbreak.domain.VmImage) UUID(java.util.UUID) Image(com.sequenceiq.cloudbreak.cloud.model.catalog.Image) ImageCatalog(com.sequenceiq.cloudbreak.domain.ImageCatalog) CloudbreakImageNotFoundException(com.sequenceiq.cloudbreak.core.CloudbreakImageNotFoundException) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) CrnResourceDescriptor(com.sequenceiq.cloudbreak.auth.crn.CrnResourceDescriptor) Component(org.springframework.stereotype.Component) TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) CustomImage(com.sequenceiq.cloudbreak.domain.CustomImage) Optional(java.util.Optional) CloudbreakImageCatalogException(com.sequenceiq.cloudbreak.core.CloudbreakImageCatalogException) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) CustomImage(com.sequenceiq.cloudbreak.domain.CustomImage) VmImage(com.sequenceiq.cloudbreak.domain.VmImage) ImageCatalog(com.sequenceiq.cloudbreak.domain.ImageCatalog) HashSet(java.util.HashSet)

Example 2 with VmImage

use of com.sequenceiq.cloudbreak.domain.VmImage in project cloudbreak by hortonworks.

the class CustomImageCatalogV4UpdateImageRequestToCustomImageConverterTest method shouldConvert.

@Test
public void shouldConvert() {
    CustomImageCatalogV4UpdateImageRequest source = new CustomImageCatalogV4UpdateImageRequest();
    source.setSourceImageId(SOURCE_IMAGE_ID);
    source.setBaseParcelUrl(BASE_PARCEL_URL);
    source.setImageType(VALID_IMAGE_TYPE);
    source.setVmImages(Collections.singleton(getVmImageRequest(REGION, IMAGE_REFERENCE)));
    CustomImage result = victim.convert(source);
    assertEquals(SOURCE_IMAGE_ID, result.getCustomizedImageId());
    assertEquals(BASE_PARCEL_URL, result.getBaseParcelUrl());
    assertEquals(ImageType.RUNTIME, result.getImageType());
    assertEquals(1, result.getVmImage().size());
    VmImage vmImage = result.getVmImage().stream().findFirst().get();
    assertEquals(REGION, vmImage.getRegion());
    assertEquals(IMAGE_REFERENCE, vmImage.getImageReference());
}
Also used : CustomImage(com.sequenceiq.cloudbreak.domain.CustomImage) VmImage(com.sequenceiq.cloudbreak.domain.VmImage) CustomImageCatalogV4UpdateImageRequest(com.sequenceiq.cloudbreak.api.endpoint.v4.customimage.request.CustomImageCatalogV4UpdateImageRequest) Test(org.junit.jupiter.api.Test)

Example 3 with VmImage

use of com.sequenceiq.cloudbreak.domain.VmImage in project cloudbreak by hortonworks.

the class CustomImageToCustomImageCatalogV4UpdateImageResponseConverterTest method getVmImage.

private VmImage getVmImage(String region, String imageReference) {
    VmImage request = new VmImage();
    request.setRegion(region);
    request.setImageReference(imageReference);
    return request;
}
Also used : VmImage(com.sequenceiq.cloudbreak.domain.VmImage)

Example 4 with VmImage

use of com.sequenceiq.cloudbreak.domain.VmImage in project cloudbreak by hortonworks.

the class CustomImageCatalogV4CreateImageRequestToCustomImageConverterTest method shouldConvert.

@Test
public void shouldConvert() {
    CustomImageCatalogV4CreateImageRequest source = new CustomImageCatalogV4CreateImageRequest();
    source.setSourceImageId(SOURCE_IMAGE_ID);
    source.setBaseParcelUrl(BASE_PARCEL_URL);
    source.setImageType(VALID_IMAGE_TYPE);
    source.setVmImages(Collections.singleton(getVmImageRequest(REGION, IMAGE_REFERENCE)));
    CustomImage result = victim.convert(source);
    assertEquals(SOURCE_IMAGE_ID, result.getCustomizedImageId());
    assertEquals(BASE_PARCEL_URL, result.getBaseParcelUrl());
    assertEquals(ImageType.RUNTIME, result.getImageType());
    assertEquals(1, result.getVmImage().size());
    VmImage vmImage = result.getVmImage().stream().findFirst().get();
    assertEquals(REGION, vmImage.getRegion());
    assertEquals(IMAGE_REFERENCE, vmImage.getImageReference());
}
Also used : CustomImageCatalogV4CreateImageRequest(com.sequenceiq.cloudbreak.api.endpoint.v4.customimage.request.CustomImageCatalogV4CreateImageRequest) CustomImage(com.sequenceiq.cloudbreak.domain.CustomImage) VmImage(com.sequenceiq.cloudbreak.domain.VmImage) Test(org.junit.jupiter.api.Test)

Example 5 with VmImage

use of com.sequenceiq.cloudbreak.domain.VmImage in project cloudbreak by hortonworks.

the class CustomImageCatalogServiceTest method testUpdateCustomImageWithUpdatedVmImage.

@Test
public void testUpdateCustomImageWithUpdatedVmImage() throws TransactionService.TransactionExecutionException {
    CustomImage updatedCustomImage = aCustomImage();
    CustomImage savedCustomImage = new CustomImage();
    VmImage vmImage = new VmImage();
    vmImage.setRegion(REGION);
    savedCustomImage.getVmImage().add(vmImage);
    savedCustomImage.setName(IMAGE_NAME);
    ImageCatalog imageCatalog = new ImageCatalog();
    imageCatalog.getCustomImages().add(savedCustomImage);
    doAnswer(invocation -> ((Supplier<CustomImage>) invocation.getArgument(0)).get()).when(transactionService).required(any(Supplier.class));
    when(imageCatalogService.getImageCatalogByName(WORKSPACE_ID, IMAGE_CATALOG_NAME)).thenReturn(imageCatalog);
    when(imageCatalogService.pureSave(imageCatalog)).thenReturn(imageCatalog);
    CustomImage actual = victim.updateCustomImage(WORKSPACE_ID, CREATOR, IMAGE_CATALOG_NAME, updatedCustomImage);
    assertEquals(1, imageCatalog.getCustomImages().size());
    assertEquals(CUSTOMIZED_IMAGE_ID, actual.getCustomizedImageId());
    assertEquals(BASE_PARCEL_URL, actual.getBaseParcelUrl());
    assertEquals(ImageType.RUNTIME, actual.getImageType());
    assertEquals(1, actual.getVmImage().size());
    VmImage actualVmImage = actual.getVmImage().stream().findFirst().get();
    assertNull(actualVmImage.getCreator());
    assertEquals(REGION, actualVmImage.getRegion());
    assertEquals(IMAGE_REFERENCE, actualVmImage.getImageReference());
}
Also used : CustomImage(com.sequenceiq.cloudbreak.domain.CustomImage) Supplier(java.util.function.Supplier) VmImage(com.sequenceiq.cloudbreak.domain.VmImage) ImageCatalog(com.sequenceiq.cloudbreak.domain.ImageCatalog) Test(org.junit.jupiter.api.Test)

Aggregations

VmImage (com.sequenceiq.cloudbreak.domain.VmImage)13 CustomImage (com.sequenceiq.cloudbreak.domain.CustomImage)10 Test (org.junit.jupiter.api.Test)7 ImageCatalog (com.sequenceiq.cloudbreak.domain.ImageCatalog)6 Supplier (java.util.function.Supplier)5 Strings (com.google.common.base.Strings)1 CustomImageCatalogV4CreateImageRequest (com.sequenceiq.cloudbreak.api.endpoint.v4.customimage.request.CustomImageCatalogV4CreateImageRequest)1 CustomImageCatalogV4UpdateImageRequest (com.sequenceiq.cloudbreak.api.endpoint.v4.customimage.request.CustomImageCatalogV4UpdateImageRequest)1 CrnResourceDescriptor (com.sequenceiq.cloudbreak.auth.crn.CrnResourceDescriptor)1 RegionAwareCrnGenerator (com.sequenceiq.cloudbreak.auth.crn.RegionAwareCrnGenerator)1 Image (com.sequenceiq.cloudbreak.cloud.model.catalog.Image)1 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)1 NotFoundException (com.sequenceiq.cloudbreak.common.exception.NotFoundException)1 TransactionService (com.sequenceiq.cloudbreak.common.service.TransactionService)1 CloudbreakImageCatalogException (com.sequenceiq.cloudbreak.core.CloudbreakImageCatalogException)1 CloudbreakImageNotFoundException (com.sequenceiq.cloudbreak.core.CloudbreakImageNotFoundException)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1