hosting asset validation for Cloud-Server to Webspace

This commit is contained in:
Michael Hoennig 2024-05-03 14:50:05 +02:00
parent 63e2be59f8
commit 1f02656aa8
9 changed files with 369 additions and 153 deletions

View File

@ -1,5 +1,6 @@
package net.hostsharing.hsadminng.hs.hosting.asset;
import net.hostsharing.hsadminng.hs.hosting.asset.validator.HsHostingAssetValidator;
import net.hostsharing.hsadminng.hs.hosting.generated.api.v1.api.HsHostingAssetsApi;
import net.hostsharing.hsadminng.context.Context;
@ -15,6 +16,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import jakarta.validation.ValidationException;
import java.util.List;
import java.util.UUID;
import java.util.function.BiConsumer;
@ -71,11 +73,6 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
return ResponseEntity.created(uri).body(mapped);
}
private HsHostingAssetEntity valid(final HsHostingAssetEntity entityToSave) {
HsHostingAssetValidator.forType(entityToSave.getType()).validate(entityToSave);
return entityToSave;
}
@Override
@Transactional(readOnly = true)
public ResponseEntity<HsHostingAssetResource> getAssetByUuid(
@ -125,6 +122,14 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
return ResponseEntity.ok(mapped);
}
private HsHostingAssetEntity valid(final HsHostingAssetEntity entityToSave) {
final var violations = HsHostingAssetValidator.forType(entityToSave.getType()).validate(entityToSave);
if (!violations.isEmpty()) {
throw new ValidationException(violations.toString());
}
return entityToSave;
}
@SuppressWarnings("unchecked")
final BiConsumer<HsHostingAssetInsertResource, HsHostingAssetEntity> RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> {
entity.putConfig(KeyValueMap.from(resource.getConfig()));

View File

@ -1,95 +0,0 @@
package net.hostsharing.hsadminng.hs.hosting.asset;
import lombok.With;
import lombok.experimental.SuperBuilder;
import java.util.Map;
class Validator {
}
@SuperBuilder
class PropertyValidator extends Validator {
String propertyName;
}
@SuperBuilder
class NumericPropertyValidator extends PropertyValidator {
private String unit;
private Integer min;
private Integer max;
private Integer step;
static NumericPropertyValidatorBuilder numericProperty(final String propertyName) {
return NumericPropertyValidator.builder().propertyName(propertyName);
}
}
@With
@SuperBuilder
class EnumPropertyValidator extends PropertyValidator {
private String[] values;
static EnumPropertyValidatorBuilderExtension enumProperty(final String propertyName) {
return new EnumPropertyValidatorBuilderExtension(propertyName);
}
static class EnumPropertyValidatorBuilderExtension extends ValidatorBuilder<EnumPropertyValidator, EnumPropertyValidatorBuilderExtension> {
private final String propertyName;
private String[] values;
EnumPropertyValidatorBuilderExtension(final String propertyName) {
this.propertyName = propertyName;
}
public EnumPropertyValidatorBuilderExtension values(final String... values) {
this.values = values;
return this;
}
@Override
protected EnumPropertyValidatorBuilderExtension self() {
return this;
}
@Override
public EnumPropertyValidator build() {
return EnumPropertyValidator.builder().propertyName(propertyName).values(values).build();
}
}
}
@SuperBuilder
class BooleanPropertyValidator extends PropertyValidator {
private Map.Entry<String, String> falseIf;
static BooleanPropertyValidatorBuilderExtension booleanProperty(final String propertyName) {
return new BooleanPropertyValidatorBuilderExtension(propertyName);
}
static class BooleanPropertyValidatorBuilderExtension extends PropertyValidatorBuilder<BooleanPropertyValidator, BooleanPropertyValidatorBuilderExtension> {
BooleanPropertyValidatorBuilderExtension(final String propertyName) {
super.propertyName(propertyName);
}
BooleanPropertyValidatorBuilderExtension falseIf(final String propertyName, final String propertyValue) {
return this;
}
@Override
protected BooleanPropertyValidatorBuilderExtension self() {
return this;
}
@Override
public BooleanPropertyValidator build() {
return new BooleanPropertyValidator()
}
}
}

View File

@ -1,51 +0,0 @@
package net.hostsharing.hsadminng.hs.hosting.asset;
import java.util.Map;
import static java.util.Map.entry;
import static net.hostsharing.hsadminng.hs.hosting.asset.BooleanPropertyValidator.booleanProperty;
import static net.hostsharing.hsadminng.hs.hosting.asset.EnumPropertyValidator.enumProperty;
import static net.hostsharing.hsadminng.hs.hosting.asset.NumericPropertyValidator.numericProperty;
public class HsHostingAssetValidator {
private static final Map<HsHostingAssetType, HsHostingAssetValidator> validators = Map.ofEntries(
entry(HsHostingAssetType.CLOUD_SERVER, new HsHostingAssetValidator(
numericProperty("CPUs").min(1).max(32).build(),
numericProperty("RAM").unit("GB").min(1).max(128).build(),
numericProperty("SSD").unit("GB").min(25).max(1000).step(25).build(),
numericProperty("HDD").unit("GB").min(0).max(4000).step(250).build(),
numericProperty("Traffic").unit("GB").min(250).max(10000).step(250).build(),
enumProperty("SLA-Infrastructure").values("BASIC", "EXT8H", "EXT4H", "EXT2H").build())),
entry(HsHostingAssetType.MANAGED_SERVER, new HsHostingAssetValidator(
numericProperty("CPUs").min(1).max(32).build(),
numericProperty("RAM").unit("GB").min(1).max(128).build(),
numericProperty("SSD").unit("GB").min(25).max(1000).step(25).build(),
numericProperty("HDD").unit("GB").min(0).max(4000).step(250).build(),
numericProperty("Traffic").unit("GB").min(250).max(10000).step(250).build(),
enumProperty("SLA-Platform").values("BASIC", "EXT8H", "EXT4H", "EXT2H").build(),
booleanProperty("SLA-EMail").falseIf("SLA-Platform", "BASIC").build(),
booleanProperty("SLA-Maria").falseIf("SLA-Platform", "BASIC").build(),
booleanProperty("SLA-PgSQL").falseIf("SLA-Platform", "BASIC").build(),
booleanProperty("SLA-Office").falseIf("SLA-Platform", "BASIC").build(),
booleanProperty("SLA-Web").falseIf("SLA-Platform", "BASIC").build())),
entry(HsHostingAssetType.MANAGED_WEBSPACE, new HsHostingAssetValidator(
numericProperty("SSD").unit("GB").min(1).max(100).step(1).build(),
numericProperty("HDD").unit("GB").min(0).max(250).step(10).build(),
numericProperty("Traffic").unit("GB").min(10).max(1000).step(10).build(),
enumProperty("SLA-Platform").values("BASIC", "EXT24H").build(),
numericProperty("Daemons").min(0).max(10).build(),
booleanProperty("Online Office Server").build())
));
public static HsHostingAssetValidator forType(final HsHostingAssetType type) {
return validators.get(type);
}
HsHostingAssetValidator(final Validator... validators) {
}
public void validate(final HsHostingAssetEntity entityToSave) {
}
}

View File

@ -0,0 +1,148 @@
package net.hostsharing.hsadminng.hs.hosting.asset.validator;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@RequiredArgsConstructor
public abstract class HsHostingAssetPropertyValidator<T> {
final Class<T> type;
final String propertyName;
private Boolean required;
public static <K, V> Map.Entry<K, V> defType(K k, V v) {
return new SimpleImmutableEntry<>(k, v);
}
public HsHostingAssetPropertyValidator<T> required() {
required = Boolean.TRUE;
return this;
}
public HsHostingAssetPropertyValidator<T> optional() {
required = Boolean.FALSE;
return this;
}
public final List<String> validate(final Map<String, Object> props) {
final var result = new ArrayList<String>();
final var propValue = props.get(propertyName);
if (propValue == null) {
if (required) {
result.add("'" + propertyName + "' is required but missing");
}
}
if (propValue != null){
if ( type.isInstance(propValue)) {
//noinspection unchecked
validate(result, (T) propValue, props);
} else {
result.add("'" + propertyName + "' is expected to be of type " + type + ", " +
"but is of type '" + propValue.getClass().getSimpleName() + "'");
}
}
return result;
}
protected abstract void validate(final ArrayList<String> result, final T propValue, final Map<String, Object> props);
public void verifyConsistency(final Map.Entry<HsHostingAssetType, HsHostingAssetValidator> typeDef) {
if (required == null ) {
throw new IllegalStateException(typeDef.getKey() + "[" + propertyName + "] not fully initialized, please call either .required() or .optional()" );
}
}
}
@Setter
class IntegerPropertyValidator extends HsHostingAssetPropertyValidator<Integer>{
private String unit;
private Integer min;
private Integer max;
private Integer step;
public static IntegerPropertyValidator integerProperty(final String propertyName) {
return new IntegerPropertyValidator(propertyName);
}
private IntegerPropertyValidator(final String propertyName) {
super(Integer.class, propertyName);
}
@Override
protected void validate(final ArrayList<String> result, final Integer propValue, final Map<String, Object> props) {
if (min != null && propValue < min) {
result.add("'" + propertyName + "' is expected to be >= " + min + " but is " + propValue);
}
if (max != null && propValue > max) {
result.add("'" + propertyName + "' is expected to be <= " + max + " but is " + propValue);
}
if (step != null && propValue % step != 0) {
result.add("'" + propertyName + "' is expected to be multiple of " + step + " but is " + propValue);
}
}
}
@Setter
class EnumPropertyValidator extends HsHostingAssetPropertyValidator<String> {
private String[] values;
private EnumPropertyValidator(final String propertyName) {
super(String.class, propertyName);
}
public static EnumPropertyValidator enumProperty(final String propertyName) {
return new EnumPropertyValidator(propertyName);
}
public HsHostingAssetPropertyValidator<String> values(final String... values) {
this.values = values;
return this;
}
@Override
protected void validate(final ArrayList<String> result, final String propValue, final Map<String, Object> props) {
if (Arrays.stream(values).noneMatch(v -> v.equals(propValue))) {
result.add("'" + propertyName + "' is expected to be one of " + Arrays.toString(values) + " but is '" + propValue + "'");
}
}
}
@Setter
class BooleanPropertyValidator extends HsHostingAssetPropertyValidator<Boolean> {
private Map.Entry<String, String> falseIf;
private BooleanPropertyValidator(final String propertyName) {
super(Boolean.class, propertyName);
}
public static BooleanPropertyValidator booleanProperty(final String propertyName) {
return new BooleanPropertyValidator(propertyName);
}
HsHostingAssetPropertyValidator<Boolean> falseIf(final String refPropertyName, final String refPropertyValue) {
this.falseIf = new SimpleImmutableEntry<>(refPropertyName, refPropertyValue);
return this;
}
@Override
protected void validate(final ArrayList<String> result, final Boolean propValue, final Map<String, Object> props) {
if (falseIf != null && !Objects.equals(props.get(falseIf.getKey()), falseIf.getValue())) {
if (propValue) {
result.add("'" + propertyName + "' is expected to be false because " +
falseIf.getKey()+ "=" + falseIf.getValue() + " but is " + propValue);
}
}
}
}

View File

@ -0,0 +1,75 @@
package net.hostsharing.hsadminng.hs.hosting.asset.validator;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static java.util.Arrays.stream;
import static net.hostsharing.hsadminng.hs.hosting.asset.validator.EnumPropertyValidator.enumProperty;
import static net.hostsharing.hsadminng.hs.hosting.asset.validator.HsHostingAssetPropertyValidator.defType;
import static net.hostsharing.hsadminng.hs.hosting.asset.validator.BooleanPropertyValidator.booleanProperty;
import static net.hostsharing.hsadminng.hs.hosting.asset.validator.IntegerPropertyValidator.integerProperty;
public class HsHostingAssetValidator {
private static final Map<HsHostingAssetType, HsHostingAssetValidator> validators = Map.ofEntries(
defType(HsHostingAssetType.CLOUD_SERVER, new HsHostingAssetValidator(
integerProperty("CPUs").min(1).max(32).required(),
integerProperty("RAM").unit("GB").min(1).max(128).required(),
integerProperty("SSD").unit("GB").min(25).max(1000).step(25).required(),
integerProperty("HDD").unit("GB").min(0).max(4000).step(250).optional(),
integerProperty("Traffic").unit("GB").min(250).max(10000).step(250).required(),
enumProperty("SLA-Infrastructure").values("BASIC", "EXT8H", "EXT4H", "EXT2H").optional())),
defType(HsHostingAssetType.MANAGED_SERVER, new HsHostingAssetValidator(
integerProperty("CPUs").min(1).max(32).required(),
integerProperty("RAM").unit("GB").min(1).max(128).required(),
integerProperty("SSD").unit("GB").min(25).max(1000).step(25).required(),
integerProperty("HDD").unit("GB").min(0).max(4000).step(250).optional(),
integerProperty("Traffic").unit("GB").min(250).max(10000).step(250).required(),
enumProperty("SLA-Platform").values("BASIC", "EXT8H", "EXT4H", "EXT2H").optional(),
booleanProperty("SLA-EMail").falseIf("SLA-Platform", "BASIC").optional(),
booleanProperty("SLA-Maria").falseIf("SLA-Platform", "BASIC").optional(),
booleanProperty("SLA-PgSQL").falseIf("SLA-Platform", "BASIC").optional(),
booleanProperty("SLA-Office").falseIf("SLA-Platform", "BASIC").optional(),
booleanProperty("SLA-Web").falseIf("SLA-Platform", "BASIC").optional())),
defType(HsHostingAssetType.MANAGED_WEBSPACE, new HsHostingAssetValidator(
integerProperty("SSD").unit("GB").min(1).max(100).step(1).required(),
integerProperty("HDD").unit("GB").min(0).max(250).step(10).optional(),
integerProperty("Traffic").unit("GB").min(10).max(1000).step(10).required(),
enumProperty("SLA-Platform").values("BASIC", "EXT24H").optional(),
integerProperty("Daemons").min(0).max(10).optional(),
booleanProperty("Online Office Server").optional())
));
static {
validators.entrySet().forEach(typeDef -> {
stream(typeDef.getValue().propertyValidators).forEach( entry -> {
entry.verifyConsistency(typeDef);
});
});
}
private final HsHostingAssetPropertyValidator<?>[] propertyValidators;
public static HsHostingAssetValidator forType(final HsHostingAssetType type) {
return validators.get(type);
}
HsHostingAssetValidator(final HsHostingAssetPropertyValidator<?>... validators) {
propertyValidators = validators;
}
public List<String> validate(final HsHostingAssetEntity assetEntity) {
final var result = new ArrayList<String>();
assetEntity.getConfig().keySet().forEach( givenPropName -> {
if (stream(propertyValidators).map(pv -> pv.propertyName).noneMatch(propName -> propName.equals(givenPropName))) {
result.add("'" + givenPropName + "' is not expected but is '" +assetEntity.getConfig().get(givenPropName) + "'");
}
});
stream(propertyValidators).forEach(pv -> {
result.addAll(pv.validate(assetEntity.getConfig()));
});
return result;
}
}

View File

@ -0,0 +1,3 @@
lombok.addLombokGeneratedAnnotation = true
lombok.accessors.chain = true
lombok.accessors.fluent = true

View File

@ -52,6 +52,7 @@ public class ArchitectureTest {
"..hs.office.sepamandate",
"..hs.booking.item",
"..hs.hosting.asset",
"..hs.hosting.asset.validator",
"..errors",
"..mapper",
"..ping",

View File

@ -174,7 +174,7 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
"type": "MANAGED_SERVER",
"identifier": "vm1400",
"caption": "some new CloudServer",
"config": { "CPU": 3, "extra": 42 }
"config": { "CPUs": 2, "RAM": 100, "SSD": 300, "Traffic": 250 }
}
""".formatted(givenBookingItem.getUuid()))
.port(port)
@ -188,7 +188,7 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
"type": "MANAGED_SERVER",
"identifier": "vm1400",
"caption": "some new CloudServer",
"config": { "CPU": 3, "extra": 42 }
"config": { "CPUs": 2, "RAM": 100, "SSD": 300, "Traffic": 250 }
}
"""))
.header("Location", matchesRegex("http://localhost:[1-9][0-9]*/api/hs/hosting/assets/[^/]*"))
@ -199,6 +199,39 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
location.substring(location.lastIndexOf('/') + 1));
assertThat(newUserUuid).isNotNull();
}
@Test
void additionalValidationsArePerformend_whenAddingAsset() {
context.define("superuser-alex@hostsharing.net");
final var givenBookingItem = givenBookingItem("First", "some PrivateCloud");
final var location = RestAssured // @formatter:off
.given()
.header("current-user", "superuser-alex@hostsharing.net")
.contentType(ContentType.JSON)
.body("""
{
"bookingItemUuid": "%s",
"type": "MANAGED_SERVER",
"identifier": "vm1400",
"caption": "some new CloudServer",
"config": { "CPUs": 0, "extra": 42 }
}
""".formatted(givenBookingItem.getUuid()))
.port(port)
.when()
.post("http://localhost/api/hs/hosting/assets")
.then().log().all().assertThat()
.statusCode(400)
.contentType(ContentType.JSON)
.body("", lenientlyEquals("""
{
"statusPhrase": "Bad Request",
"message": "['extra' is not expected but is '42', 'CPUs' is expected to be >= 1 but is 0, 'RAM' is required but missing, 'SSD' is required but missing, 'Traffic' is required but missing]"
}
""")); // @formatter:on
}
}
@Nested

View File

@ -0,0 +1,97 @@
package net.hostsharing.hsadminng.hs.hosting.asset;
import net.hostsharing.hsadminng.hs.hosting.asset.validator.HsHostingAssetValidator;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static java.util.Collections.emptyMap;
import static java.util.Map.entry;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_SERVER;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_WEBSPACE;
import static org.assertj.core.api.Assertions.assertThat;
class HsHostingAssetValidatorUnitTest {
@Test
void validatesMissingProperties() {
// given
final var validator = HsHostingAssetValidator.forType(MANAGED_WEBSPACE);
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
.type(MANAGED_WEBSPACE)
.config(emptyMap())
.build();
// when
final var result = validator.validate(mangedWebspaceHostingAssetEntity);
// then
assertThat(result).containsExactlyInAnyOrder(
"'SSD' is required but missing",
"'Traffic' is required but missing"
);
}
@Test
void validatesUnknownProperties() {
// given
final var validator = HsHostingAssetValidator.forType(MANAGED_WEBSPACE);
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
.type(MANAGED_WEBSPACE)
.config(Map.ofEntries(
entry("HDD", 0),
entry("SSD", 1),
entry("Traffic", 10),
entry("unknown", "some value")
))
.build();
// when
final var result = validator.validate(mangedWebspaceHostingAssetEntity);
// then
assertThat(result).containsExactly("'unknown' is not expected but is 'some value'");
}
@Test
void validatesDependentProperties() {
// given
final var validator = HsHostingAssetValidator.forType(MANAGED_SERVER);
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
.type(MANAGED_SERVER)
.config(Map.ofEntries(
entry("CPUs", 2),
entry("RAM", 25),
entry("SSD", 25),
entry("Traffic", 250),
entry("SLA-EMail", true)
))
.build();
// when
final var result = validator.validate(mangedWebspaceHostingAssetEntity);
// then
assertThat(result).containsExactly("'SLA-EMail' is expected to be false because SLA-Platform=BASIC but is true");
}
@Test
void validatesValidProperties() {
// given
final var validator = HsHostingAssetValidator.forType(MANAGED_WEBSPACE);
final var mangedWebspaceHostingAssetEntity = HsHostingAssetEntity.builder()
.type(MANAGED_WEBSPACE)
.config(Map.ofEntries(
entry("HDD", 200),
entry("SSD", 25),
entry("Traffic", 250)
))
.build();
// when
final var result = validator.validate(mangedWebspaceHostingAssetEntity);
// then
assertThat(result).isEmpty();
}
}