Search in sources :

Example 56 with BadRequestException

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

the class TemplateRequestToTemplateConverter method convert.

@Override
public Template convert(TemplateRequest source) {
    Template template = new Template();
    if (Strings.isNullOrEmpty(source.getName())) {
        template.setName(missingResourceNameGenerator.generateName(APIResourceType.TEMPLATE));
    } else {
        template.setName(source.getName());
    }
    template.setDescription(source.getDescription());
    template.setStatus(ResourceStatus.USER_MANAGED);
    convertVolume(source, template);
    template.setCloudPlatform(source.getCloudPlatform());
    template.setInstanceType(source.getInstanceType() == null ? "" : source.getInstanceType());
    Map<String, Object> parameters = source.getParameters() == null ? Maps.newHashMap() : source.getParameters();
    CustomInstanceType customInstanceType = source.getCustomInstanceType();
    if (customInstanceType != null) {
        parameters.put(PlatformParametersConsts.CUSTOM_INSTANCETYPE_MEMORY, customInstanceType.getMemory());
        parameters.put(PlatformParametersConsts.CUSTOM_INSTANCETYPE_CPUS, customInstanceType.getCpus());
    }
    if (!parameters.isEmpty()) {
        try {
            template.setAttributes(new Json(parameters));
        } catch (JsonProcessingException ignored) {
            throw new BadRequestException("Invalid parameters");
        }
    }
    if (source.getTopologyId() != null) {
        template.setTopology(topologyService.getById(source.getTopologyId()));
    }
    return template;
}
Also used : BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) CustomInstanceType(com.sequenceiq.cloudbreak.api.model.CustomInstanceType) Json(com.sequenceiq.cloudbreak.domain.json.Json) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Template(com.sequenceiq.cloudbreak.domain.Template)

Example 57 with BadRequestException

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

the class ClusterTemplateRequestToClusterTemplateConverter method convert.

@Override
public ClusterTemplate convert(ClusterTemplateRequest json) {
    ClusterTemplate clusterTemplate = new ClusterTemplate();
    clusterTemplate.setName(json.getName());
    try {
        clusterTemplate.setTemplate(new Json(json.getTemplate()));
    } catch (JsonProcessingException e) {
        LOGGER.error("Cloudtemplate cannot be converted to JSON: " + json.getTemplate(), e);
        throw new BadRequestException("Cloudtemplate cannot be converted to JSON", e);
    }
    clusterTemplate.setType(json.getType());
    return clusterTemplate;
}
Also used : ClusterTemplate(com.sequenceiq.cloudbreak.domain.ClusterTemplate) BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) Json(com.sequenceiq.cloudbreak.domain.json.Json) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 58 with BadRequestException

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

the class BlueprintRequestToBlueprintConverter method convert.

public Blueprint convert(String name, String blueprintText, boolean publicInAccount) {
    Blueprint blueprint = new Blueprint();
    blueprint.setName(name);
    blueprint.setBlueprintText(blueprintText);
    blueprint.setPublicInAccount(publicInAccount);
    validateBlueprint(blueprint.getBlueprintText());
    try {
        JsonNode root = JsonUtil.readTree(blueprint.getBlueprintText());
        blueprint.setAmbariName(blueprintUtils.getBlueprintName(root));
        blueprint.setHostGroupCount(blueprintUtils.countHostGroups(root));
    } catch (IOException e) {
        throw new BadRequestException("Invalid Blueprint: Failed to parse JSON.", e);
    }
    return blueprint;
}
Also used : Blueprint(com.sequenceiq.cloudbreak.domain.Blueprint) BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 59 with BadRequestException

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

the class BlueprintRequestToBlueprintConverter method convert.

@Override
public Blueprint convert(BlueprintRequest json) {
    Blueprint blueprint = new Blueprint();
    if (json.getUrl() != null && !json.getUrl().isEmpty()) {
        String sourceUrl = json.getUrl().trim();
        try {
            String urlText = URLUtils.readUrl(sourceUrl);
            jsonHelper.createJsonFromString(urlText);
            blueprint.setBlueprintText(urlText);
        } catch (Exception e) {
            throw new BadRequestException("Cannot download ambari validation from: " + sourceUrl, e);
        }
    } else {
        blueprint.setBlueprintText(json.getAmbariBlueprint());
    }
    validateBlueprint(blueprint.getBlueprintText());
    if (Strings.isNullOrEmpty(json.getName())) {
        blueprint.setName(missingResourceNameGenerator.generateName(APIResourceType.BLUEPRINT));
    } else {
        blueprint.setName(json.getName());
    }
    blueprint.setDescription(json.getDescription());
    blueprint.setStatus(ResourceStatus.USER_MANAGED);
    prepareBlueprintInputs(json, blueprint);
    try {
        JsonNode root = JsonUtil.readTree(blueprint.getBlueprintText());
        blueprint.setAmbariName(blueprintUtils.getBlueprintName(root));
        blueprint.setHostGroupCount(blueprintUtils.countHostGroups(root));
    } catch (IOException e) {
        throw new BadRequestException("Invalid Blueprint: Failed to parse JSON.", e);
    }
    return blueprint;
}
Also used : Blueprint(com.sequenceiq.cloudbreak.domain.Blueprint) BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 60 with BadRequestException

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

the class BlueprintRequestToBlueprintConverter method validateHostGroups.

private void validateHostGroups(JsonNode root) {
    JsonNode hostGroups = root.path("host_groups");
    if (hostGroups.isMissingNode() || !hostGroups.isArray() || hostGroups.size() == 0) {
        throw new BadRequestException("Invalid validation: 'host_groups' node is missing from JSON or is not an array or empty.");
    }
    for (JsonNode hostGroup : hostGroups) {
        JsonNode hostGroupName = hostGroup.path("name");
        if (hostGroupName.isMissingNode() || !hostGroupName.isTextual() || hostGroupName.asText().isEmpty()) {
            throw new BadRequestException("Invalid validation: one of the 'host_groups' has no name.");
        }
        validateComponentsInHostgroup(hostGroup, hostGroupName.asText());
    }
}
Also used : BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode)

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