@Test public void rejectsBuiltinWithoutContent() { BlueprintV4Request request = new BlueprintV4Request(); request.setBlueprint("{ \"blueprint\": {}"); thrown.expect(BadRequestException.class); thrown.expectMessage("Invalid cluster template: Failed to parse JSON."); underTest.convert(request); }
@Override 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; }
BlueprintV4RequestToBlueprintConverter extends AbstractConversionServiceAwareConverter<BlueprintV4Request, Blueprint> { @Override 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; } }
BlueprintV4RequestToBlueprintConverter extends AbstractConversionServiceAwareConverter<BlueprintV4Request, Blueprint> { @Override 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; }  }
BlueprintV4RequestToBlueprintConverter extends AbstractConversionServiceAwareConverter<BlueprintV4Request, Blueprint> { @Override 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; }  @Override Blueprint convert(BlueprintV4Request json); }
BlueprintV4RequestToBlueprintConverter extends AbstractConversionServiceAwareConverter<BlueprintV4Request, Blueprint> { @Override 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; }  @Override Blueprint convert(BlueprintV4Request json);  }