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;
}
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;
}
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;
}
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;
}
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());
}
}
Aggregations