From a9825b2074e60bcdc9250f842a01d4837bf16a27 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Thu, 2 May 2024 17:57:28 +0200 Subject: [PATCH] hosting asset validation for Cloud-Server to Webspace (WIP) --- .../asset/HsHostingAssetController.java | 7 +- .../HsHostingAssetPropertyValidator.java | 95 +++++++++++++++++++ .../asset/HsHostingAssetValidator.java | 51 ++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetPropertyValidator.java create mode 100644 src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetValidator.java diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetController.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetController.java index 62a62b34..b3411576 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetController.java @@ -60,7 +60,7 @@ public class HsHostingAssetController implements HsHostingAssetsApi { final var entityToSave = mapper.map(body, HsHostingAssetEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER); - final var saved = assetRepo.save(entityToSave); + final var saved = assetRepo.save(valid(entityToSave)); final var uri = MvcUriComponentsBuilder.fromController(getClass()) @@ -71,6 +71,11 @@ 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 getAssetByUuid( diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetPropertyValidator.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetPropertyValidator.java new file mode 100644 index 00000000..7dd4154d --- /dev/null +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetPropertyValidator.java @@ -0,0 +1,95 @@ +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 { + + 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 falseIf; + static BooleanPropertyValidatorBuilderExtension booleanProperty(final String propertyName) { + return new BooleanPropertyValidatorBuilderExtension(propertyName); + } + + static class BooleanPropertyValidatorBuilderExtension extends PropertyValidatorBuilder { + + + 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() + } + } +} diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetValidator.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetValidator.java new file mode 100644 index 00000000..95f94124 --- /dev/null +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetValidator.java @@ -0,0 +1,51 @@ +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 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) { + } +}