use of com.netflix.spinnaker.front50.exception.BadRequestException in project front50 by spinnaker.
the class PipelineController method validatePipeline.
/**
* Ensure basic validity of the pipeline. Invalid pipelines will raise runtime exceptions.
*
* @param pipeline The Pipeline to validate
*/
private void validatePipeline(final Pipeline pipeline, Boolean staleCheck) {
// Pipelines must have an application and a name
if (StringUtils.isAnyBlank(pipeline.getApplication(), pipeline.getName())) {
throw new InvalidEntityException("A pipeline requires name and application fields");
}
// Check if pipeline type is templated
if (TYPE_TEMPLATED.equals(pipeline.getType())) {
PipelineTemplateDAO templateDAO = getTemplateDAO();
// Check templated pipelines to ensure template is valid
String source;
switch(pipeline.getSchema()) {
case "v2":
V2TemplateConfiguration v2Config = objectMapper.convertValue(pipeline, V2TemplateConfiguration.class);
source = v2Config.getTemplate().getReference();
break;
default:
TemplateConfiguration v1Config = objectMapper.convertValue(pipeline.getConfig(), TemplateConfiguration.class);
source = v1Config.getPipeline().getTemplate().getSource();
break;
}
// Check if template id which is after :// is in the store
if (source.startsWith(SPINNAKER_PREFIX)) {
String templateId = source.substring(SPINNAKER_PREFIX.length());
try {
templateDAO.findById(templateId);
} catch (NotFoundException notFoundEx) {
throw new BadRequestException("Configured pipeline template not found", notFoundEx);
}
}
}
checkForDuplicatePipeline(pipeline.getApplication(), pipeline.getName().trim(), pipeline.getId());
final ValidatorErrors errors = new ValidatorErrors();
pipelineValidators.forEach(it -> it.validate(pipeline, errors));
if (staleCheck && !Strings.isNullOrEmpty(pipeline.getId()) && pipeline.getLastModified() != null) {
checkForStalePipeline(pipeline, errors);
}
if (errors.hasErrors()) {
String message = errors.getAllErrorsMessage();
throw new ValidationException(message, errors.getAllErrors());
}
}
use of com.netflix.spinnaker.front50.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class JsonDataRestController method replaceNode.
JsonNode replaceNode(final JsonNode existingData, final String[] dataPath, final JsonNode newData) {
if (dataPath.length == 0) {
return newData;
}
String[] parentPath = Arrays.copyOfRange(dataPath, 0, dataPath.length - 1);
String fieldName = dataPath[dataPath.length - 1];
JsonNode parent = getNode(existingData, parentPath);
if (parent.isObject()) {
ObjectNode parentObject = (ObjectNode) parent;
parentObject.set(fieldName, newData);
} else if (parent.isArray()) {
ArrayNode parentArray = (ArrayNode) parent;
int index = toArrayIndex(fieldName);
parentArray.set(index, newData);
} else {
throw new BadRequestException(new TranslatableMessage("rest.error.cantSetFieldOfNodeType", parent.getNodeType()));
}
return existingData;
}
use of com.netflix.spinnaker.front50.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class JsonDataRestController method getNode.
JsonNode getNode(final JsonNode existingData, final String[] dataPath) {
JsonNode node = existingData;
for (int i = 0; i < dataPath.length; i++) {
String fieldName = dataPath[i];
if (node.isObject()) {
ObjectNode objectNode = (ObjectNode) node;
node = objectNode.get(fieldName);
} else if (node.isArray()) {
ArrayNode arrayNode = (ArrayNode) node;
int index = toArrayIndex(fieldName);
node = arrayNode.get(index);
} else {
throw new BadRequestException(new TranslatableMessage("rest.error.cantGetFieldOfNodeType", node.getNodeType()));
}
if (node == null) {
throw new NotFoundRestException();
}
}
return node;
}
use of com.netflix.spinnaker.front50.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class JsonDataRestController method deleteNode.
boolean deleteNode(final JsonNode existingData, final String[] dataPath) {
if (dataPath.length == 0)
throw new IllegalArgumentException();
String[] parentPath = Arrays.copyOfRange(dataPath, 0, dataPath.length - 1);
String fieldName = dataPath[dataPath.length - 1];
JsonNode parent = getNode(existingData, parentPath);
JsonNode deletedValue = null;
if (parent.isObject()) {
ObjectNode parentObject = (ObjectNode) parent;
deletedValue = parentObject.remove(fieldName);
} else if (parent.isArray()) {
ArrayNode parentArray = (ArrayNode) parent;
int index = toArrayIndex(fieldName);
deletedValue = parentArray.remove(index);
} else {
throw new BadRequestException(new TranslatableMessage("rest.error.cantDeleteFieldOfNodeType", parent.getNodeType()));
}
return deletedValue != null;
}
use of com.netflix.spinnaker.front50.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class DataPointRestController method updateDataPoint.
@ApiOperation(value = "Update an existing data point")
@RequestMapping(method = RequestMethod.PUT, value = "/{xid}")
public ResponseEntity<DataPointModel> updateDataPoint(@PathVariable String xid, @ApiParam(value = "Updated data point model", required = true) @RequestBody(required = true) DataPointModel model, @AuthenticationPrincipal User user, UriComponentsBuilder builder) {
DataPointVO dataPoint = DataPointDao.instance.getByXid(xid);
if (dataPoint == null) {
throw new NotFoundRestException();
}
Permissions.ensureDataSourcePermission(user, dataPoint.getDataSourceId());
// check if they are trying to move it to another data source
String newDataSourceXid = model.getDataSourceXid();
if (newDataSourceXid != null && !newDataSourceXid.isEmpty() && !newDataSourceXid.equals(dataPoint.getDataSourceXid())) {
throw new BadRequestException(new TranslatableMessage("rest.error.pointChangeDataSource"));
}
DataPointPropertiesTemplateVO template = null;
if (model.isTemplateXidWasSet()) {
if (model.getTemplateXid() != null) {
template = (DataPointPropertiesTemplateVO) TemplateDao.instance.getByXid(model.getTemplateXid());
if (template == null) {
throw new BadRequestException(new TranslatableMessage("invalidTemplateXid"));
}
}
} else if (dataPoint.getTemplateId() != null) {
template = (DataPointPropertiesTemplateVO) TemplateDao.instance.get(dataPoint.getTemplateId());
}
DataPointDao.instance.loadPartialRelationalData(dataPoint);
model.copyPropertiesTo(dataPoint);
// load the template after copying the properties, template properties override the ones in the data point
if (template != null) {
dataPoint.withTemplate(template);
}
dataPoint.ensureValid();
// have to load any existing event detectors for the data point as we are about to replace the VO in the runtime manager
DataPointDao.instance.setEventDetectors(dataPoint);
Common.runtimeManager.saveDataPoint(dataPoint);
URI location = builder.path("/v2/data-points/{xid}").buildAndExpand(xid).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(new DataPointModel(dataPoint), headers, HttpStatus.OK);
}
Aggregations