Search in sources :

Example 1 with TemplateConfiguration

use of com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration 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 TemplateConfiguration

use of com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration 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)

Example 3 with TemplateConfiguration

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

the class V2PipelineTemplateController method getDependentConfigs.

@VisibleForTesting
List<String> getDependentConfigs(String templateId) {
    List<String> dependentConfigIds = new ArrayList<>();
    String prefixedId = SPINNAKER_PREFIX + templateId;
    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 && source.equalsIgnoreCase(prefixedId)) {
            dependentConfigIds.add(templatedPipeline.getId());
        }
    });
    return dependentConfigIds;
}
Also used : PipelineTemplate(com.netflix.spinnaker.front50.model.pipeline.PipelineTemplate) PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) 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) MessageDigest(java.security.MessageDigest) Pipeline(com.netflix.spinnaker.front50.api.model.pipeline.Pipeline) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Hex(org.apache.commons.codec.binary.Hex) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) RequestBody(org.springframework.web.bind.annotation.RequestBody) BadRequestException(com.netflix.spinnaker.front50.exception.BadRequestException) Map(java.util.Map) 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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) TreeMap(java.util.TreeMap) SPINNAKER_PREFIX(com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration.TemplateSource.SPINNAKER_PREFIX) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) 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) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NotFoundException(com.netflix.spinnaker.kork.web.exceptions.NotFoundException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

BadRequestException (com.netflix.spinnaker.front50.exception.BadRequestException)3 InvalidEntityException (com.netflix.spinnaker.front50.exceptions.InvalidEntityException)3 PipelineTemplateDAO (com.netflix.spinnaker.front50.model.pipeline.PipelineTemplateDAO)3 TemplateConfiguration (com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration)3 NotFoundException (com.netflix.spinnaker.kork.web.exceptions.NotFoundException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Pipeline (com.netflix.spinnaker.front50.api.model.pipeline.Pipeline)2 TYPE_TEMPLATED (com.netflix.spinnaker.front50.api.model.pipeline.Pipeline.TYPE_TEMPLATED)2 DuplicateEntityException (com.netflix.spinnaker.front50.exceptions.DuplicateEntityException)2 InvalidRequestException (com.netflix.spinnaker.front50.exceptions.InvalidRequestException)2 PipelineDAO (com.netflix.spinnaker.front50.model.pipeline.PipelineDAO)2 PipelineTemplate (com.netflix.spinnaker.front50.model.pipeline.PipelineTemplate)2 SPINNAKER_PREFIX (com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration.TemplateSource.SPINNAKER_PREFIX)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 PathVariable (org.springframework.web.bind.annotation.PathVariable)2