Search in sources :

Example 1 with Pipeline

use of com.netflix.spinnaker.front50.api.model.pipeline.Pipeline 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());
    }
}
Also used : ValidationException(com.netflix.spinnaker.kork.web.exceptions.ValidationException) PipelineTemplateDAO(com.netflix.spinnaker.front50.model.pipeline.PipelineTemplateDAO) NotFoundException(com.netflix.spinnaker.kork.web.exceptions.NotFoundException) BadRequestException(com.netflix.spinnaker.front50.exception.BadRequestException) V2TemplateConfiguration(com.netflix.spinnaker.front50.model.pipeline.V2TemplateConfiguration) TemplateConfiguration(com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration) ValidatorErrors(com.netflix.spinnaker.front50.api.validator.ValidatorErrors) InvalidEntityException(com.netflix.spinnaker.front50.exceptions.InvalidEntityException) V2TemplateConfiguration(com.netflix.spinnaker.front50.model.pipeline.V2TemplateConfiguration)

Example 2 with Pipeline

use of com.netflix.spinnaker.front50.api.model.pipeline.Pipeline in project front50 by spinnaker.

the class PipelineController method checkForStalePipeline.

private void checkForStalePipeline(Pipeline pipeline, ValidatorErrors errors) {
    Pipeline existingPipeline = pipelineDAO.findById(pipeline.getId());
    Long storedUpdateTs = existingPipeline.getLastModified();
    Long submittedUpdateTs = pipeline.getLastModified();
    if (!submittedUpdateTs.equals(storedUpdateTs)) {
        errors.reject("The submitted pipeline is stale.  submitted updateTs " + submittedUpdateTs + " does not match stored updateTs " + storedUpdateTs);
    }
}
Also used : Pipeline(com.netflix.spinnaker.front50.api.model.pipeline.Pipeline)

Example 3 with Pipeline

use of com.netflix.spinnaker.front50.api.model.pipeline.Pipeline in project front50 by spinnaker.

the class PipelineController method listByApplication.

@PreAuthorize("hasPermission(#application, 'APPLICATION', 'READ')")
@RequestMapping(value = "{application:.+}", method = RequestMethod.GET)
public List<Pipeline> listByApplication(@PathVariable(value = "application") String application, @RequestParam(required = false, value = "refresh", defaultValue = "true") boolean refresh) {
    List<Pipeline> pipelines = new ArrayList<>(pipelineDAO.getPipelinesByApplication(application, refresh));
    pipelines.sort((p1, p2) -> {
        if (p1.getIndex() != null && p2.getIndex() == null) {
            return -1;
        }
        if (p1.getIndex() == null && p2.getIndex() != null) {
            return 1;
        }
        if (p1.getIndex() != null && p2.getIndex() != null && !p1.getIndex().equals(p2.getIndex())) {
            return p1.getIndex() - p2.getIndex();
        }
        return Optional.ofNullable(p1.getName()).orElse(p1.getId()).compareToIgnoreCase(Optional.ofNullable(p2.getName()).orElse(p2.getId()));
    });
    int i = 0;
    for (Pipeline p : pipelines) {
        p.setIndex(i);
        i++;
    }
    return pipelines;
}
Also used : ArrayList(java.util.ArrayList) Pipeline(com.netflix.spinnaker.front50.api.model.pipeline.Pipeline) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with Pipeline

use of com.netflix.spinnaker.front50.api.model.pipeline.Pipeline in project front50 by spinnaker.

the class PipelineController method update.

@PreAuthorize("hasPermission(#pipeline.application, 'APPLICATION', 'WRITE')")
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public Pipeline update(@PathVariable final String id, @RequestParam(value = "staleCheck", required = false, defaultValue = "false") Boolean staleCheck, @RequestBody Pipeline pipeline) {
    Pipeline existingPipeline = pipelineDAO.findById(id);
    if (!pipeline.getId().equals(existingPipeline.getId())) {
        throw new InvalidRequestException(format("The provided id %s doesn't match the pipeline id %s", pipeline.getId(), existingPipeline.getId()));
    }
    validatePipeline(pipeline, staleCheck);
    pipeline.setName(pipeline.getName().trim());
    pipeline.setLastModified(System.currentTimeMillis());
    pipeline = ensureCronTriggersHaveIdentifier(pipeline);
    pipelineDAO.update(id, pipeline);
    return pipeline;
}
Also used : InvalidRequestException(com.netflix.spinnaker.front50.exceptions.InvalidRequestException) Pipeline(com.netflix.spinnaker.front50.api.model.pipeline.Pipeline) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with Pipeline

use of com.netflix.spinnaker.front50.api.model.pipeline.Pipeline in project front50 by spinnaker.

the class PipelineTemplateController method getDependentConfigs.

@VisibleForTesting
List<String> getDependentConfigs(String templateId, boolean recursive) {
    List<String> dependentConfigIds = new ArrayList<>();
    List<String> templateIds = convertAllTemplateIdsToSources(templateId, recursive);
    pipelineDAO.all().stream().filter(pipeline -> pipeline.getType() != null && pipeline.getType().equals(TYPE_TEMPLATED)).forEach(templatedPipeline -> {
        String source;
        try {
            TemplateConfiguration config = objectMapper.convertValue(templatedPipeline.getConfig(), TemplateConfiguration.class);
            source = config.getPipeline().getTemplate().getSource();
        } catch (Exception e) {
            return;
        }
        if (source != null && containsAnyIgnoreCase(source, templateIds)) {
            dependentConfigIds.add(templatedPipeline.getId());
        }
    });
    return dependentConfigIds;
}
Also used : PipelineTemplate(com.netflix.spinnaker.front50.model.pipeline.PipelineTemplate) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) InvalidRequestException(com.netflix.spinnaker.front50.exceptions.InvalidRequestException) PipelineDAO(com.netflix.spinnaker.front50.model.pipeline.PipelineDAO) PipelineTemplateDAO(com.netflix.spinnaker.front50.model.pipeline.PipelineTemplateDAO) Pipeline(com.netflix.spinnaker.front50.api.model.pipeline.Pipeline) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) RequestBody(org.springframework.web.bind.annotation.RequestBody) BadRequestException(com.netflix.spinnaker.front50.exception.BadRequestException) InvalidEntityException(com.netflix.spinnaker.front50.exceptions.InvalidEntityException) TYPE_TEMPLATED(com.netflix.spinnaker.front50.api.model.pipeline.Pipeline.TYPE_TEMPLATED) DuplicateEntityException(com.netflix.spinnaker.front50.exceptions.DuplicateEntityException) TemplateConfiguration(com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) List(java.util.List) SPINNAKER_PREFIX(com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration.TemplateSource.SPINNAKER_PREFIX) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NotFoundException(com.netflix.spinnaker.kork.web.exceptions.NotFoundException) ArrayList(java.util.ArrayList) TemplateConfiguration(com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration) InvalidRequestException(com.netflix.spinnaker.front50.exceptions.InvalidRequestException) BadRequestException(com.netflix.spinnaker.front50.exception.BadRequestException) InvalidEntityException(com.netflix.spinnaker.front50.exceptions.InvalidEntityException) DuplicateEntityException(com.netflix.spinnaker.front50.exceptions.DuplicateEntityException) NotFoundException(com.netflix.spinnaker.kork.web.exceptions.NotFoundException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

Pipeline (com.netflix.spinnaker.front50.api.model.pipeline.Pipeline)9 InvalidRequestException (com.netflix.spinnaker.front50.exceptions.InvalidRequestException)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 NotFoundException (com.netflix.spinnaker.kork.web.exceptions.NotFoundException)4 ArrayList (java.util.ArrayList)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 BadRequestException (com.netflix.spinnaker.front50.exception.BadRequestException)3 DuplicateEntityException (com.netflix.spinnaker.front50.exceptions.DuplicateEntityException)3 InvalidEntityException (com.netflix.spinnaker.front50.exceptions.InvalidEntityException)3 PipelineTemplate (com.netflix.spinnaker.front50.model.pipeline.PipelineTemplate)3 PipelineTemplateDAO (com.netflix.spinnaker.front50.model.pipeline.PipelineTemplateDAO)3 TemplateConfiguration (com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 TYPE_TEMPLATED (com.netflix.spinnaker.front50.api.model.pipeline.Pipeline.TYPE_TEMPLATED)2 PipelineDAO (com.netflix.spinnaker.front50.model.pipeline.PipelineDAO)2