Search in sources :

Example 16 with BadRequestException

use of com.sequenceiq.cloudbreak.controller.BadRequestException in project cloudbreak by hortonworks.

the class TopologyService method delete.

private void delete(Topology topology) {
    authorizationService.hasWritePermission(topology);
    if (0L != credentialRepository.countByTopology(topology) + templateRepository.countByTopology(topology) + networkRepository.countByTopology(topology)) {
        throw new BadRequestException(String.format("Topology '%d' is in use, cannot be deleted.", topology.getId()));
    }
    LOGGER.debug("Deleting topology. {} - {}", new Object[] { topology.getId(), topology.getName() });
    Date now = new Date();
    String terminatedName = topology.getName() + DELIMITER + now.getTime();
    topology.setName(terminatedName);
    topology.setDeleted(true);
    topologyRepository.save(topology);
}
Also used : BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) Date(java.util.Date)

Example 17 with BadRequestException

use of com.sequenceiq.cloudbreak.controller.BadRequestException in project cloudbreak by hortonworks.

the class StackServiceTest method testRemoveInstanceWhenTheHostIsGatewayTypeThenWeShoulNotAllowTerminationWithException.

@Test
public void testRemoveInstanceWhenTheHostIsGatewayTypeThenWeShoulNotAllowTerminationWithException() {
    String exceptionMessage = String.format("Downscale for node [public IP: %s] is prohibited because it maintains the Ambari server", INSTANCE_PUBLIC_IP);
    when(stackRepository.findOne(STACK_ID)).thenReturn(stack);
    when(instanceMetaDataRepository.findByInstanceId(STACK_ID, INSTANCE_ID)).thenReturn(instanceMetaData);
    when(instanceMetaData.getInstanceMetadataType()).thenReturn(GATEWAY);
    when(instanceMetaData.getPublicIp()).thenReturn(INSTANCE_PUBLIC_IP);
    doThrow(new BadRequestException(exceptionMessage)).when(downscaleValidatorService).checkInstanceIsTheAmbariServerOrNot(INSTANCE_PUBLIC_IP, GATEWAY);
    expectedException.expect(BadRequestException.class);
    expectedException.expectMessage(exceptionMessage);
    underTest.removeInstance(user, STACK_ID, INSTANCE_ID);
    verify(instanceMetaDataRepository, times(1)).findByInstanceId(STACK_ID, INSTANCE_ID);
    verify(downscaleValidatorService, times(1)).checkInstanceIsTheAmbariServerOrNot(INSTANCE_PUBLIC_IP, GATEWAY);
    verify(downscaleValidatorService, times(0)).checkUserHasRightToTerminateInstance(anyBoolean(), anyString(), anyString(), anyLong());
    verify(flowManager, times(0)).triggerStackRemoveInstance(anyLong(), anyString(), anyString());
}
Also used : BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 18 with BadRequestException

use of com.sequenceiq.cloudbreak.controller.BadRequestException in project cloudbreak by hortonworks.

the class UpdateStackRequestV2ToUpdateStackRequestConverter method convert.

@Override
public UpdateStackJson convert(StackScaleRequestV2 source) {
    UpdateStackJson updateStackJson = new UpdateStackJson();
    updateStackJson.setWithClusterEvent(true);
    InstanceGroup instanceGroup = instanceGroupRepository.findOneByGroupNameInStack(source.getStackId(), source.getGroup());
    if (instanceGroup != null) {
        InstanceGroupAdjustmentJson instanceGroupAdjustmentJson = new InstanceGroupAdjustmentJson();
        instanceGroupAdjustmentJson.setInstanceGroup(source.getGroup());
        int scaleNumber = source.getDesiredCount() - instanceGroup.getNodeCount();
        instanceGroupAdjustmentJson.setScalingAdjustment(scaleNumber);
        updateStackJson.setInstanceGroupAdjustment(instanceGroupAdjustmentJson);
    } else {
        throw new BadRequestException(String.format("Group '%s' not available on stack", source.getGroup()));
    }
    return updateStackJson;
}
Also used : UpdateStackJson(com.sequenceiq.cloudbreak.api.model.UpdateStackJson) BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) InstanceGroupAdjustmentJson(com.sequenceiq.cloudbreak.api.model.InstanceGroupAdjustmentJson) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup)

Example 19 with BadRequestException

use of com.sequenceiq.cloudbreak.controller.BadRequestException in project cloudbreak by hortonworks.

the class StackValidationRequestToStackValidationConverter method convertHostGroupsFromJson.

private Set<HostGroup> convertHostGroupsFromJson(Collection<InstanceGroup> instanceGroups, Iterable<HostGroupRequest> hostGroupsJsons) {
    Set<HostGroup> hostGroups = new HashSet<>();
    for (HostGroupRequest json : hostGroupsJsons) {
        HostGroup hostGroup = new HostGroup();
        hostGroup.setName(json.getName());
        Constraint constraint = getConversionService().convert(json.getConstraint(), Constraint.class);
        String instanceGroupName = json.getConstraint().getInstanceGroupName();
        if (instanceGroupName != null) {
            InstanceGroup instanceGroup = instanceGroups.stream().filter(instanceGroup1 -> instanceGroup1.getGroupName().equals(instanceGroupName)).findFirst().get();
            if (instanceGroup == null) {
                throw new BadRequestException(String.format("Cannot find instance group named '%s' in instance group list", instanceGroupName));
            }
            constraint.setInstanceGroup(instanceGroup);
        }
        hostGroup.setConstraint(constraint);
        hostGroups.add(hostGroup);
    }
    return hostGroups;
}
Also used : Constraint(com.sequenceiq.cloudbreak.domain.Constraint) HostGroupRequest(com.sequenceiq.cloudbreak.api.model.HostGroupRequest) HostGroup(com.sequenceiq.cloudbreak.domain.HostGroup) BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) HashSet(java.util.HashSet) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup)

Example 20 with BadRequestException

use of com.sequenceiq.cloudbreak.controller.BadRequestException in project cloudbreak by hortonworks.

the class StackService method validateScalingAdjustment.

private void validateScalingAdjustment(InstanceGroupAdjustmentJson instanceGroupAdjustmentJson, Stack stack) {
    if (0 == instanceGroupAdjustmentJson.getScalingAdjustment()) {
        throw new BadRequestException(String.format("Requested scaling adjustment on stack '%s' is 0. Nothing to do.", stack.getName()));
    }
    if (0 > instanceGroupAdjustmentJson.getScalingAdjustment()) {
        InstanceGroup instanceGroup = stack.getInstanceGroupByInstanceGroupName(instanceGroupAdjustmentJson.getInstanceGroup());
        if (-1 * instanceGroupAdjustmentJson.getScalingAdjustment() > instanceGroup.getNodeCount()) {
            throw new BadRequestException(String.format("There are %s instances in instance group '%s'. Cannot remove %s instances.", instanceGroup.getNodeCount(), instanceGroup.getGroupName(), -1 * instanceGroupAdjustmentJson.getScalingAdjustment()));
        }
        int removableHosts = instanceMetaDataRepository.findRemovableInstances(stack.getId(), instanceGroupAdjustmentJson.getInstanceGroup()).size();
        if (removableHosts < -1 * instanceGroupAdjustmentJson.getScalingAdjustment()) {
            throw new BadRequestException(String.format("There are %s unregistered instances in instance group '%s' but %s were requested. Decommission nodes from the cluster!", removableHosts, instanceGroup.getGroupName(), instanceGroupAdjustmentJson.getScalingAdjustment() * -1));
        }
    }
}
Also used : BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) Blueprint(com.sequenceiq.cloudbreak.domain.Blueprint) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup)

Aggregations

BadRequestException (com.sequenceiq.cloudbreak.controller.BadRequestException)87 Stack (com.sequenceiq.cloudbreak.domain.Stack)16 Transactional (javax.transaction.Transactional)13 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)12 Cluster (com.sequenceiq.cloudbreak.domain.Cluster)12 Json (com.sequenceiq.cloudbreak.domain.json.Json)12 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)12 Blueprint (com.sequenceiq.cloudbreak.domain.Blueprint)11 HostGroup (com.sequenceiq.cloudbreak.domain.HostGroup)9 InstanceGroup (com.sequenceiq.cloudbreak.domain.InstanceGroup)9 IOException (java.io.IOException)7 Credential (com.sequenceiq.cloudbreak.domain.Credential)6 HashMap (java.util.HashMap)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 Constraint (com.sequenceiq.cloudbreak.domain.Constraint)5 BlueprintParameterJson (com.sequenceiq.cloudbreak.api.model.BlueprintParameterJson)4 Platform (com.sequenceiq.cloudbreak.cloud.model.Platform)4 CloudbreakException (com.sequenceiq.cloudbreak.service.CloudbreakException)4 HashSet (java.util.HashSet)4 BlueprintInputJson (com.sequenceiq.cloudbreak.api.model.BlueprintInputJson)3