hosting asset validation for Cloud-Server to Webspace (WIP)

This commit is contained in:
Michael Hoennig 2024-05-02 17:57:28 +02:00
parent 6c25dddcda
commit a9825b2074
3 changed files with 152 additions and 1 deletions

View File

@ -60,7 +60,7 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
final var entityToSave = mapper.map(body, HsHostingAssetEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER); 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 = final var uri =
MvcUriComponentsBuilder.fromController(getClass()) MvcUriComponentsBuilder.fromController(getClass())
@ -71,6 +71,11 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
return ResponseEntity.created(uri).body(mapped); return ResponseEntity.created(uri).body(mapped);
} }
private HsHostingAssetEntity valid(final HsHostingAssetEntity entityToSave) {
HsHostingAssetValidator.forType(entityToSave.getType()).validate(entityToSave);
return entityToSave;
}
@Override @Override
@Transactional(readOnly = true) @Transactional(readOnly = true)
public ResponseEntity<HsHostingAssetResource> getAssetByUuid( public ResponseEntity<HsHostingAssetResource> getAssetByUuid(

View File

@ -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<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

@ -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<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) {
}
}