Search in sources :

Example 1 with CloudbreakApiException

use of com.sequenceiq.cloudbreak.json.CloudbreakApiException in project cloudbreak by hortonworks.

the class BlueprintV4RequestToBlueprintConverterTest method testConvertShouldThrowExceptionWhenTheBlueprintJsonIsInvalid.

@Test(expected = BadRequestException.class)
public void testConvertShouldThrowExceptionWhenTheBlueprintJsonIsInvalid() {
    BlueprintV4Request request = new BlueprintV4Request();
    String blueprint = "{}";
    request.setBlueprint(blueprint);
    when(jsonHelper.createJsonFromString(blueprint)).thenThrow(new CloudbreakApiException("Invalid Json"));
    underTest.convert(request);
}
Also used : BlueprintV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.blueprint.requests.BlueprintV4Request) CloudbreakApiException(com.sequenceiq.cloudbreak.json.CloudbreakApiException) Test(org.junit.Test) AbstractJsonConverterTest(com.sequenceiq.cloudbreak.converter.AbstractJsonConverterTest)

Example 2 with CloudbreakApiException

use of com.sequenceiq.cloudbreak.json.CloudbreakApiException in project cloudbreak by hortonworks.

the class BlueprintV4RequestToBlueprintConverter method convert.

public Blueprint convert(BlueprintV4Request json) {
    Blueprint blueprint = new Blueprint();
    if (StringUtils.isNotEmpty(json.getUrl())) {
        String sourceUrl = json.getUrl().trim();
        try {
            String urlText = URLUtils.readUrl(sourceUrl);
            jsonHelper.createJsonFromString(urlText);
            blueprint.setBlueprintText(urlText);
        } catch (IOException | CloudbreakApiException e) {
            throw new BadRequestException(String.format("Cannot download cluster template from: %s", sourceUrl), e);
        }
    } else if (!CollectionUtils.isEmpty(json.getServices()) && !Strings.isNullOrEmpty(json.getPlatform())) {
        GeneratedCmTemplate generatedCmTemplate = clusterTemplateGeneratorService.generateTemplateByServices(json.getServices(), json.getPlatform());
        blueprint.setBlueprintText(generatedCmTemplate.getTemplate());
    } else {
        blueprint.setBlueprintText(json.getBlueprint());
    }
    try {
        JsonNode blueprintJson = JsonUtil.readTree(blueprint.getBlueprintText());
        if (blueprintUtils.isBuiltinBlueprint(blueprintJson)) {
            LOGGER.info("Built-in blueprint format detected, applying embedded \"blueprint\" content");
            blueprintJson = blueprintUtils.getBuiltinBlueprintContent(blueprintJson);
            blueprint.setBlueprintText(JsonUtil.writeValueAsString(blueprintJson));
        }
        if (blueprintUtils.isClouderaManagerClusterTemplate(blueprintJson)) {
            blueprint.setStackName(blueprintUtils.getCDHDisplayName(blueprintJson));
            blueprint.setHostGroupCount(blueprintUtils.countHostTemplates(blueprintJson));
            blueprint.setStackVersion(blueprintUtils.getCDHStackVersion(blueprintJson));
            blueprint.setStackType("CDH");
        } else {
            throw new BadRequestException("Failed to determine cluster template format");
        }
    } catch (IOException e) {
        throw new BadRequestException(JSON_PARSE_EXCEPTION_MESSAGE, e);
    }
    blueprint.setName(getNameByItsAvailability(json.getName()));
    blueprint.setDescription(json.getDescription());
    blueprint.setStatus(ResourceStatus.USER_MANAGED);
    blueprint.setTags(createJsonFromTagsMap(json.getTags()));
    return blueprint;
}
Also used : Blueprint(com.sequenceiq.cloudbreak.domain.Blueprint) GeneratedCmTemplate(com.sequenceiq.cloudbreak.cmtemplate.generator.template.domain.GeneratedCmTemplate) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) CloudbreakApiException(com.sequenceiq.cloudbreak.json.CloudbreakApiException)

Aggregations

CloudbreakApiException (com.sequenceiq.cloudbreak.json.CloudbreakApiException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 BlueprintV4Request (com.sequenceiq.cloudbreak.api.endpoint.v4.blueprint.requests.BlueprintV4Request)1 GeneratedCmTemplate (com.sequenceiq.cloudbreak.cmtemplate.generator.template.domain.GeneratedCmTemplate)1 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)1 AbstractJsonConverterTest (com.sequenceiq.cloudbreak.converter.AbstractJsonConverterTest)1 Blueprint (com.sequenceiq.cloudbreak.domain.Blueprint)1 IOException (java.io.IOException)1 Test (org.junit.Test)1