Search in sources :

Example 21 with BadRequestException

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

the class BlueprintService method validate.

private void validate(Blueprint blueprint, boolean internalCreation) {
    MapBindingResult errors = new MapBindingResult(new HashMap(), "blueprint");
    blueprintValidator.validate(blueprint, errors);
    if (errors.hasErrors()) {
        throw new BadRequestException(errors.getAllErrors().stream().map(e -> (e instanceof FieldError ? ((FieldError) e).getField() + ": " : "") + e.getDefaultMessage()).collect(Collectors.joining("; ")));
    }
    if (!internalCreation) {
        blueprintConfigValidator.validate(blueprint);
    }
    hueWorkaroundValidatorService.validateForBlueprintRequest(getHueHostGroups(blueprint.getBlueprintText()));
}
Also used : HashMap(java.util.HashMap) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) MapBindingResult(org.springframework.validation.MapBindingResult) FieldError(org.springframework.validation.FieldError)

Example 22 with BadRequestException

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

the class BlueprintService method prepareDeletion.

@Override
protected void prepareDeletion(Blueprint blueprint) {
    Set<Cluster> notDeletedClustersWithThisCd = getNotDeletedClustersWithBlueprint(blueprint);
    if (!notDeletedClustersWithThisCd.isEmpty()) {
        if (notDeletedClustersWithThisCd.size() > 1) {
            List<String> clusters = notDeletedClustersWithThisCd.stream().filter(it -> !it.getStack().getType().equals(StackType.TEMPLATE)).map(it -> it.getStack().getName()).collect(Collectors.toList());
            List<Long> stackTemplateIds = notDeletedClustersWithThisCd.stream().filter(it -> it.getStack().getType().equals(StackType.TEMPLATE)).map(it -> it.getStack().getId()).collect(Collectors.toList());
            Set<String> clusterDefinitions = getClusterDefinitionNameByStackTemplateIds(stackTemplateIds);
            throw new BadRequestException(String.format("There are clusters or cluster definitions associated with cluster template '%s'. " + "The cluster template used by %s cluster(s) (%s) and %s cluster definitions (%s). " + "Please remove these before deleting the cluster template.", blueprint.getName(), clusters.size(), String.join(", ", clusters), clusterDefinitions.size(), String.join(", ", clusterDefinitions)));
        }
        Cluster cluster = notDeletedClustersWithThisCd.iterator().next();
        String clusterType = getClusterType(cluster);
        String clusterDefinitionName = getClusterDefinitionNameByStackTemplateIds(List.of(cluster.getStack().getId())).stream().findFirst().orElseThrow(() -> new NotFoundException("Cannot find the cluster definition for the stack template"));
        throw new BadRequestException(String.format("There is a %s ['%s'] which uses cluster template '%s'. Please remove this " + "cluster before deleting the cluster template.", clusterType, clusterDefinitionName, blueprint.getName()));
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) ClusterService(com.sequenceiq.cloudbreak.service.cluster.ClusterService) StringUtils(org.apache.commons.lang3.StringUtils) ThreadBasedUserCrnProvider(com.sequenceiq.cloudbreak.auth.ThreadBasedUserCrnProvider) TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) CdpResourceType(com.sequenceiq.common.api.type.CdpResourceType) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) AuthorizationResourceType(com.sequenceiq.authorization.resource.AuthorizationResourceType) Blueprint(com.sequenceiq.cloudbreak.domain.Blueprint) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) EnumSet(java.util.EnumSet) CloudResourceAdvisor(com.sequenceiq.cloudbreak.service.stack.CloudResourceAdvisor) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) FieldError(org.springframework.validation.FieldError) WorkspaceResourceRepository(com.sequenceiq.cloudbreak.workspace.repository.workspace.WorkspaceResourceRepository) Collection(java.util.Collection) BlueprintView(com.sequenceiq.cloudbreak.domain.view.BlueprintView) Set(java.util.Set) DELETE_COMPLETED(com.sequenceiq.cloudbreak.api.endpoint.v4.common.Status.DELETE_COMPLETED) NotNull(javax.validation.constraints.NotNull) Crn(com.sequenceiq.cloudbreak.auth.crn.Crn) Collectors(java.util.stream.Collectors) DEFAULT(com.sequenceiq.cloudbreak.api.endpoint.v4.common.ResourceStatus.DEFAULT) SERVICE_MANAGED(com.sequenceiq.cloudbreak.api.endpoint.v4.common.ResourceStatus.SERVICE_MANAGED) List(java.util.List) CmCloudStorageConfigProvider(com.sequenceiq.cloudbreak.cmtemplate.cloudstorage.CmCloudStorageConfigProvider) MapBindingResult(org.springframework.validation.MapBindingResult) MDCUtils(com.sequenceiq.cloudbreak.logger.MDCUtils) ConfigQueryEntry(com.sequenceiq.common.api.cloudstorage.query.ConfigQueryEntry) Optional(java.util.Optional) HueWorkaroundValidatorService(com.sequenceiq.cloudbreak.validation.HueWorkaroundValidatorService) CmTemplateProcessor(com.sequenceiq.cloudbreak.cmtemplate.CmTemplateProcessor) BlueprintStatusView(com.sequenceiq.cloudbreak.domain.projection.BlueprintStatusView) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) PlatformRecommendation(com.sequenceiq.cloudbreak.cloud.model.PlatformRecommendation) ResourceStatus(com.sequenceiq.cloudbreak.api.endpoint.v4.common.ResourceStatus) ScaleRecommendation(com.sequenceiq.cloudbreak.cloud.model.ScaleRecommendation) BlueprintRepository(com.sequenceiq.cloudbreak.repository.BlueprintRepository) OwnerAssignmentService(com.sequenceiq.authorization.service.OwnerAssignmentService) MDCBuilder(com.sequenceiq.cloudbreak.logger.MDCBuilder) HashMap(java.util.HashMap) CompositeAuthResourcePropertyProvider(com.sequenceiq.authorization.service.CompositeAuthResourcePropertyProvider) BlueprintViewRepository(com.sequenceiq.cloudbreak.repository.BlueprintViewRepository) USER_MANAGED(com.sequenceiq.cloudbreak.api.endpoint.v4.common.ResourceStatus.USER_MANAGED) HashSet(java.util.HashSet) Inject(javax.inject.Inject) CrnResourceDescriptor(com.sequenceiq.cloudbreak.auth.crn.CrnResourceDescriptor) HueRoles(com.sequenceiq.cloudbreak.cmtemplate.configproviders.hue.HueRoles) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Service(org.springframework.stereotype.Service) FileSystemConfigQueryObject(com.sequenceiq.cloudbreak.template.filesystem.FileSystemConfigQueryObject) Builder(com.sequenceiq.cloudbreak.template.filesystem.FileSystemConfigQueryObject.Builder) CentralBlueprintParameterQueryService(com.sequenceiq.cloudbreak.cmtemplate.CentralBlueprintParameterQueryService) NotFoundException.notFound(com.sequenceiq.cloudbreak.common.exception.NotFoundException.notFound) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) Logger(org.slf4j.Logger) AutoscaleRecommendation(com.sequenceiq.cloudbreak.cloud.model.AutoscaleRecommendation) BlueprintLoaderService(com.sequenceiq.cloudbreak.init.blueprint.BlueprintLoaderService) DEFAULT_DELETED(com.sequenceiq.cloudbreak.api.endpoint.v4.common.ResourceStatus.DEFAULT_DELETED) RegionAwareCrnGenerator(com.sequenceiq.cloudbreak.auth.crn.RegionAwareCrnGenerator) CompactView(com.sequenceiq.cloudbreak.domain.view.CompactView) Workspace(com.sequenceiq.cloudbreak.workspace.model.Workspace) NameOrCrn(com.sequenceiq.cloudbreak.api.endpoint.v4.dto.NameOrCrn) ClusterTemplateViewService(com.sequenceiq.cloudbreak.service.template.ClusterTemplateViewService) User(com.sequenceiq.cloudbreak.workspace.model.User) BlueprintUtils(com.sequenceiq.cloudbreak.cmtemplate.utils.BlueprintUtils) StackType(com.sequenceiq.cloudbreak.api.endpoint.v4.common.StackType) AbstractWorkspaceAwareResourceService(com.sequenceiq.cloudbreak.service.AbstractWorkspaceAwareResourceService) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException)

Example 23 with BadRequestException

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

the class ClusterService method getStackRepositoryJson.

public String getStackRepositoryJson(Long stackId) {
    Stack stack = stackService.getById(stackId);
    Cluster cluster = stack.getCluster();
    if (cluster == null) {
        throw new BadRequestException(String.format("There is no cluster installed on stack '%s'.", stack.getName()));
    }
    StackRepoDetails repoDetails = clusterComponentConfigProvider.getStackRepoDetails(cluster.getId());
    String stackRepoId = repoDetails.getStack().get(REPO_ID_TAG);
    return clusterApiConnectors.getConnector(stack).clusterModificationService().getStackRepositoryJson(repoDetails, stackRepoId);
}
Also used : StackRepoDetails(com.sequenceiq.cloudbreak.cloud.model.component.StackRepoDetails) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack)

Example 24 with BadRequestException

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

the class StackCommonService method putStopInWorkspace.

public FlowIdentifier putStopInWorkspace(NameOrCrn nameOrCrn, Long workspaceId) {
    Stack stack = stackService.getByNameOrCrnInWorkspace(nameOrCrn, workspaceId);
    MDCBuilder.buildMdcContext(stack);
    if (!cloudParameterCache.isStartStopSupported(stack.cloudPlatform())) {
        throw new BadRequestException(String.format("Stop is not supported on %s cloudplatform", stack.cloudPlatform()));
    }
    UpdateStackV4Request updateStackJson = new UpdateStackV4Request();
    updateStackJson.setStatus(StatusRequest.STOPPED);
    updateStackJson.setWithClusterEvent(true);
    return put(stack, updateStackJson);
}
Also used : UpdateStackV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.autoscales.request.UpdateStackV4Request) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack)

Example 25 with BadRequestException

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

the class StackDecorator method setupDatabaseAttachedVolume.

private void setupDatabaseAttachedVolume(Stack subject, InstanceGroup instanceGroup, Template template) {
    InstanceGroupType instanceGroupType = instanceGroup.getInstanceGroupType();
    if (instanceGroupType == InstanceGroupType.GATEWAY && embeddedDatabaseService.isEmbeddedDatabaseOnAttachedDiskEnabled(subject, subject.getCluster())) {
        String databaseVolumeType = embeddedDatabaseConfig.getPlatformVolumeType(subject.cloudPlatform()).orElseThrow(() -> new BadRequestException(String.format("If embedded db is enabled on attached disk, database volumetype" + " have to be defined for cloudprovider in app config! Missing database volumetype on %s provider", subject.cloudPlatform())));
        VolumeTemplate databaseVolumeTemplate = new VolumeTemplate();
        databaseVolumeTemplate.setUsageType(VolumeUsageType.DATABASE);
        databaseVolumeTemplate.setVolumeSize(embeddedDatabaseConfig.getSize());
        databaseVolumeTemplate.setVolumeCount(1);
        databaseVolumeTemplate.setVolumeType(databaseVolumeType);
        databaseVolumeTemplate.setTemplate(template);
        template.getVolumeTemplates().add(databaseVolumeTemplate);
        LOGGER.debug("Extra disk is attached with {} type and {}GB size for the embedded database.", databaseVolumeTemplate.getVolumeType(), databaseVolumeTemplate.getVolumeSize());
    } else {
        if (instanceGroupType == InstanceGroupType.GATEWAY) {
            LOGGER.debug("No extra disk will be attached to the gateway instance as db volume because it is disabled or external database is used.");
        } else {
            LOGGER.debug("No extra disk will be attached to the instance as db volume because it is CORE instance.");
        }
    }
}
Also used : InstanceGroupType(com.sequenceiq.common.api.type.InstanceGroupType) VolumeTemplate(com.sequenceiq.cloudbreak.domain.VolumeTemplate) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException)

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