Search in sources :

Example 41 with BadRequestException

use of com.sequenceiq.cloudbreak.common.exception.BadRequestException in project cloudbreak by hortonworks.

the class StackToCreateFreeIpaRequestConverter method getBackupLocation.

String getBackupLocation(Stack stack, String location) {
    String originalLocation = getLocation(location, CLUSTER_BACKUP_PREFIX);
    // Example: convert https://storage1.dfs.core.windows.net/logs-fs into abfs://logs-fs@storage1.dfs.core.windows.net
    try {
        if (CloudPlatform.AZURE.equalsIgnoreCase(stack.getCloudPlatform()) && originalLocation != null) {
            URI uri = new URI(originalLocation);
            if (AZURE_BLOB_STORAGE_SCHEMA.equals(uri.getScheme())) {
                String uriPath = uri.getPath();
                int firstSeparator = uriPath.indexOf(PATH_DELIMETER);
                if (firstSeparator != -1) {
                    int secondSeparator = uriPath.indexOf(PATH_DELIMETER, firstSeparator + 1);
                    String bucketName;
                    String bucketPath = "";
                    if (secondSeparator == -1) {
                        bucketName = uriPath.substring(firstSeparator + 1);
                    } else {
                        bucketName = uriPath.substring(firstSeparator + 1, secondSeparator);
                        bucketPath = uriPath.substring(secondSeparator);
                    }
                    originalLocation = String.format("%s%s@%s%s", ORIGINAL_AZURE_BLOB_STORAGE_SCHEMA, bucketName, uri.getHost(), bucketPath);
                }
            }
        }
    } catch (URISyntaxException e) {
        String error = String.format("Unable to parse URI for backup location %s", originalLocation);
        LOGGER.error(error);
        throw new BadRequestException(error, e);
    }
    LOGGER.debug("Created backup location {} location {}", originalLocation, location);
    return originalLocation;
}
Also used : BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 42 with BadRequestException

use of com.sequenceiq.cloudbreak.common.exception.BadRequestException in project cloudbreak by hortonworks.

the class CreateFreeIpaRequestToStackConverter method getRegion.

private String getRegion(CreateFreeIpaRequest source, String cloudPlatform) {
    if (source.getPlacement() == null) {
        return null;
    }
    if (isEmpty(source.getPlacement().getRegion())) {
        Map<Platform, Region> regions = Maps.newHashMap();
        if (isNotEmpty(defaultRegions)) {
            for (String entry : defaultRegions.split(",")) {
                String[] keyValue = entry.split(":");
                regions.put(platform(keyValue[0]), Region.region(keyValue[1]));
            }
            Region platformRegion = regions.get(platform(cloudPlatform));
            if (platformRegion == null || isEmpty(platformRegion.value())) {
                throw new BadRequestException(String.format("No default region specified for: %s. Region cannot be empty.", cloudPlatform));
            }
            return platformRegion.value();
        } else {
            throw new BadRequestException("No default region is specified. Region cannot be empty.");
        }
    }
    return source.getPlacement().getRegion();
}
Also used : Platform(com.sequenceiq.cloudbreak.cloud.model.Platform) CloudPlatform(com.sequenceiq.cloudbreak.common.mappable.CloudPlatform) Region(com.sequenceiq.cloudbreak.cloud.model.Region) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException)

Example 43 with BadRequestException

use of com.sequenceiq.cloudbreak.common.exception.BadRequestException in project cloudbreak by hortonworks.

the class FreeIpaV1Controller method create.

@Override
@CheckPermissionByRequestProperty(path = "environmentCrn", type = CRN, action = EDIT_ENVIRONMENT)
public DescribeFreeIpaResponse create(@RequestObject @Valid CreateFreeIpaRequest request) {
    ValidationResult validationResult = createFreeIpaRequestValidator.validate(request);
    if (validationResult.getState() == State.ERROR) {
        LOGGER.debug("FreeIPA request has validation error(s): {}.", validationResult.getFormattedErrors());
        throw new BadRequestException(validationResult.getFormattedErrors());
    }
    String accountId = crnService.getCurrentAccountId();
    return freeIpaCreationService.launchFreeIpa(request, accountId);
}
Also used : BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) ValidationResult(com.sequenceiq.cloudbreak.validation.ValidationResult) CheckPermissionByRequestProperty(com.sequenceiq.authorization.annotation.CheckPermissionByRequestProperty)

Example 44 with BadRequestException

use of com.sequenceiq.cloudbreak.common.exception.BadRequestException in project cloudbreak by hortonworks.

the class StackV4RequestToStackConverterTest method testWhenRegionIsEmptyButDefaultRegionsAreEmptyThenBadRequestExceptionComes.

@Test
public void testWhenRegionIsEmptyButDefaultRegionsAreEmptyThenBadRequestExceptionComes() {
    setDefaultRegions(null);
    StackV4Request request = getRequest("stack.json");
    request.setCloudPlatform(MOCK);
    request.getPlacement().setRegion(null);
    BadRequestException resultException = assertThrows(BadRequestException.class, () -> underTest.convert(request));
    assertEquals("No default region is specified. Region cannot be empty.", resultException.getMessage());
}
Also used : StackV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) AbstractJsonConverterTest(com.sequenceiq.cloudbreak.converter.AbstractJsonConverterTest) Test(org.junit.jupiter.api.Test)

Example 45 with BadRequestException

use of com.sequenceiq.cloudbreak.common.exception.BadRequestException in project cloudbreak by hortonworks.

the class StackCreatorServiceRecipeValidationTest method testIfMultipleRecipesDoesNotExistsWhichHasGivenInOneOfTheHostgroupsThenBadRequestExceptionShouldCome.

@Test
void testIfMultipleRecipesDoesNotExistsWhichHasGivenInOneOfTheHostgroupsThenBadRequestExceptionShouldCome() {
    String notExistingRecipeName = "someNotExistingRecipe";
    String someOtherNotExistingRecipeName = "someOtherNotExistingRecipe";
    StackV4Request request = new StackV4Request();
    request.setInstanceGroups(List.of(getInstanceGroupWithRecipe(INSTANCE_GROUP_MASTER, Set.of(notExistingRecipeName, someOtherNotExistingRecipeName))));
    when(recipeService.get(any(NameOrCrn.class), eq(WORKSPACE_ID))).thenThrow(new NotFoundException("Recipe not found"));
    BadRequestException exception = Assertions.assertThrows(BadRequestException.class, () -> underTest.createStack(user, workspace, request, false));
    Assert.assertNotNull(exception);
    Assertions.assertTrue(exception.getMessage().matches(String.format("The given recipes does not exists for the instance group \"%s\": (\\w+), (\\w+)", INSTANCE_GROUP_MASTER)));
    verify(recipeService, times(2)).get(any(NameOrCrn.class), anyLong());
}
Also used : StackV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) NameOrCrn(com.sequenceiq.cloudbreak.api.endpoint.v4.dto.NameOrCrn) Test(org.junit.jupiter.api.Test)

Aggregations

BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)298 Test (org.junit.jupiter.api.Test)134 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)45 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)34 Cluster (com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster)26 SdxCluster (com.sequenceiq.datalake.entity.SdxCluster)23 ValidationResult (com.sequenceiq.cloudbreak.validation.ValidationResult)22 StackV4Request (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request)21 DetailedEnvironmentResponse (com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse)21 Blueprint (com.sequenceiq.cloudbreak.domain.Blueprint)19 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)19 Stack (com.sequenceiq.freeipa.entity.Stack)18 BaseDiagnosticsCollectionRequest (com.sequenceiq.common.api.diagnostics.BaseDiagnosticsCollectionRequest)14 FlowIdentifier (com.sequenceiq.flow.api.model.FlowIdentifier)14 SdxClusterRequest (com.sequenceiq.sdx.api.model.SdxClusterRequest)14 Set (java.util.Set)14 NotFoundException (com.sequenceiq.cloudbreak.common.exception.NotFoundException)13 NameOrCrn (com.sequenceiq.cloudbreak.api.endpoint.v4.dto.NameOrCrn)12 Json (com.sequenceiq.cloudbreak.common.json.Json)12 TransactionExecutionException (com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException)12