-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from CodeKaio/72-mandatory-variables
🚸 : mandatory variables
- Loading branch information
Showing
23 changed files
with
886 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/io/codeka/gaia/stacks/bo/MandatoryStackVariablesValidation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.codeka.gaia.stacks.bo; | ||
|
||
import io.codeka.gaia.stacks.service.MandatoryStackVariablesValidator; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Constraint(validatedBy = MandatoryStackVariablesValidator.class) | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface MandatoryStackVariablesValidation { | ||
|
||
String message() default "mandatory variables should not be blank"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/io/codeka/gaia/stacks/bo/RegexStackVariablesValidation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.codeka.gaia.stacks.bo; | ||
|
||
import io.codeka.gaia.stacks.service.RegexStackVariablesValidator; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Constraint(validatedBy = RegexStackVariablesValidator.class) | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface RegexStackVariablesValidation { | ||
|
||
String message() default "variables should match the regex"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/io/codeka/gaia/stacks/controller/StackValidationExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.codeka.gaia.stacks.controller; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.validation.ObjectError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
@ControllerAdvice | ||
public class StackValidationExceptionHandler { | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
@ResponseBody | ||
public Map<String, String> handleValidationExceptions(MethodArgumentNotValidException ex) { | ||
var message = ex.getBindingResult().getAllErrors().stream() | ||
.map(this::getMessage) | ||
.collect(Collectors.joining("\n")); | ||
|
||
return Map.of("message", message); | ||
} | ||
|
||
private String getMessage(ObjectError error){ | ||
if(error instanceof FieldError){ | ||
var fieldError = ((FieldError)error); | ||
return fieldError.getField() + " " + fieldError.getDefaultMessage(); | ||
} | ||
return error.getDefaultMessage(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/io/codeka/gaia/stacks/service/MandatoryStackVariablesValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.codeka.gaia.stacks.service; | ||
|
||
import io.codeka.gaia.modules.bo.TerraformVariable; | ||
import io.codeka.gaia.modules.repository.TerraformModuleRepository; | ||
import io.codeka.gaia.stacks.bo.Stack; | ||
import io.codeka.gaia.stacks.bo.MandatoryStackVariablesValidation; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
@Service | ||
public class MandatoryStackVariablesValidator implements ConstraintValidator<MandatoryStackVariablesValidation, Stack> { | ||
|
||
@Autowired | ||
private TerraformModuleRepository moduleRepository; | ||
|
||
@Override | ||
public void initialize(MandatoryStackVariablesValidation constraintAnnotation) { | ||
// nothing to do here | ||
} | ||
|
||
@Override | ||
public boolean isValid(Stack stack, ConstraintValidatorContext constraintValidatorContext) { | ||
if(stack.getModuleId() == null || stack.getModuleId().isBlank()){ | ||
// no module defined, so variables are valid ! | ||
return true; | ||
} | ||
// getting the module | ||
var module = this.moduleRepository.findById(stack.getModuleId()).orElseThrow(); | ||
|
||
return module.getVariables().stream() | ||
.filter(TerraformVariable::isMandatory) | ||
.allMatch(variable -> { | ||
var variableValue = stack.getVariableValues().get(variable.getName()); | ||
return variableValue != null && !variableValue.isBlank(); | ||
}); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/io/codeka/gaia/stacks/service/RegexStackVariablesValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.codeka.gaia.stacks.service; | ||
|
||
import io.codeka.gaia.modules.repository.TerraformModuleRepository; | ||
import io.codeka.gaia.stacks.bo.RegexStackVariablesValidation; | ||
import io.codeka.gaia.stacks.bo.Stack; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.util.Pair; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
import java.util.regex.Pattern; | ||
|
||
@Service | ||
public class RegexStackVariablesValidator implements ConstraintValidator<RegexStackVariablesValidation, Stack> { | ||
|
||
@Autowired | ||
private TerraformModuleRepository moduleRepository; | ||
|
||
@Override | ||
public void initialize(RegexStackVariablesValidation constraintAnnotation) { | ||
// nothing to do here | ||
} | ||
|
||
@Override | ||
public boolean isValid(Stack stack, ConstraintValidatorContext constraintValidatorContext) { | ||
if(stack.getModuleId() == null || stack.getModuleId().isBlank()){ | ||
// no module defined, so variables are valid ! | ||
return true; | ||
} | ||
// getting the module | ||
var module = this.moduleRepository.findById(stack.getModuleId()).orElseThrow(); | ||
|
||
return module.getVariables().stream() | ||
// not checking non regex values | ||
.filter(terraformVariable -> terraformVariable.getValidationRegex() != null) | ||
// not checking blank values (doing it first because Pair want non-null values) | ||
.filter(terraformVariable -> { | ||
var value = stack.getVariableValues().get(terraformVariable.getName()); | ||
return value != null && ! value.isBlank(); | ||
}) | ||
// associating each variable with its value | ||
.map(terraformVariable -> Pair.of(terraformVariable, stack.getVariableValues().get(terraformVariable.getName()))) | ||
.allMatch(pair -> { | ||
var pattern = Pattern.compile(pair.getFirst().getValidationRegex()); | ||
return pattern.matcher(pair.getSecond()).matches(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -304,8 +304,8 @@ a.btn { | |
color: #000; | ||
} | ||
|
||
button i { | ||
color: #fff; | ||
#sidebarCollapse i { | ||
color: #FFF; | ||
} | ||
|
||
.lead { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.