refactor HsBookingItemCreatedListener

This commit is contained in:
Michael Hoennig 2024-10-01 09:34:40 +02:00
parent 0223cc1929
commit 63e026d4d5

View File

@ -4,9 +4,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import net.hostsharing.hsadminng.hs.booking.generated.api.v1.model.HsHostingAssetAutoInsertResource;
import net.hostsharing.hsadminng.hs.booking.generated.api.v1.model.HsHostingAssetSubInsertResource;
import net.hostsharing.hsadminng.hs.booking.generated.api.v1.model.HsHostingAssetTypeResource;
import net.hostsharing.hsadminng.hs.booking.item.BookingItemCreatedAppEvent;
import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemRealEntity;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetRealEntity.HsHostingAssetRealEntityBuilder;
import net.hostsharing.hsadminng.hs.hosting.asset.validators.HostingAssetEntitySaveProcessor;
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRealEntity;
import net.hostsharing.hsadminng.mapper.StandardMapper;
@ -17,11 +19,10 @@ import org.springframework.stereotype.Component;
import jakarta.validation.ValidationException;
import java.net.IDN;
import java.util.Map;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import static java.util.Map.entry;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_DNS_SETUP;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_HTTP_SETUP;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_MBOX_SETUP;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_SMTP_SETUP;
@ -44,9 +45,7 @@ public class HsBookingItemCreatedListener implements ApplicationListener<Booking
final var newBookingItemRealEntity = event.getEntity().getBookingItem();
final var asset = jsonMapper.readValue(event.getEntity().getAssetJson(), HsHostingAssetAutoInsertResource.class);
final var factory = switch (newBookingItemRealEntity.getType()) {
case PRIVATE_CLOUD -> null;
case CLOUD_SERVER -> null;
case MANAGED_SERVER -> null;
case PRIVATE_CLOUD, CLOUD_SERVER, MANAGED_SERVER -> null; // for now, no automatic HostingAsset possible
case MANAGED_WEBSPACE -> null; // TODO.impl: implement ManagedWebspace HostingAsset creation, where possible
case DOMAIN_SETUP -> new DomainSetupHostingAssetFactory(newBookingItemRealEntity, asset);
};
@ -100,52 +99,34 @@ public class HsBookingItemCreatedListener implements ApplicationListener<Booking
@Override
protected HsHostingAsset create() {
final String domainName = asset.getIdentifier();
final var domainSetupAsset = createDomainSetupAsset(domainName);
final var domainSetupAsset = createDomainSetupAsset(getDomainName());
// TODO.legacy: as long as we need to be compatible, we always do all technical domain-setups
final var subHostingAssetResources = asset.getSubHostingAssets();
final var domainHttpSetupAssetResource = subHostingAssetResources.stream()
.filter(ha -> ha.getType() == HsHostingAssetTypeResource.DOMAIN_HTTP_SETUP)
.findFirst().orElseThrow(() -> new ValidationException(
domainName + ": missing target unix user (assignedToHostingAssetUuid) for DOMAIN_HTTP_SETUP "));
final var domainHttpSetupAsset = domainSetupAsset.getSubHostingAssets().stream().filter(sha -> sha.getType() == DOMAIN_HTTP_SETUP).findFirst().orElseThrow();
domainHttpSetupAsset.setParentAsset(domainSetupAsset);
final HsHostingAssetRealEntity assignedToUnixUserAsset =
emw.find(HsHostingAssetRealEntity.class, domainHttpSetupAssetResource.getAssignedToAssetUuid());
domainHttpSetupAsset.setAssignedToAsset(assignedToUnixUserAsset);
final var subHostingAssetResources = getSubHostingAssetResources();
final var domainHttpSetupAsset = createDomainHttpSetupAssetEntity(
subHostingAssetResources,
getDomainName(),
domainSetupAsset);
final var assignedToUnixUserAsset = domainHttpSetupAsset.getAssignedToAsset();
if (subHostingAssetResources.stream().noneMatch(ha -> ha.getType() == HsHostingAssetTypeResource.DOMAIN_DNS_SETUP)) {
domainSetupAsset.getSubHostingAssets().add(HsHostingAssetRealEntity.builder()
.type(DOMAIN_DNS_SETUP)
.parentAsset(domainSetupAsset)
.assignedToAsset(assignedToUnixUserAsset.getParentAsset()) // FIXME: why is that needed?
.identifier(domainName + "|DNS")
.config(Map.ofEntries(
// FIXME:
entry("TTL", 21600),
entry("auto-SOA", true)
))
.build());
}
if (subHostingAssetResources.stream().noneMatch(ha -> ha.getType() == HsHostingAssetTypeResource.DOMAIN_MBOX_SETUP)) {
domainSetupAsset.getSubHostingAssets().add(HsHostingAssetRealEntity.builder()
.type(DOMAIN_MBOX_SETUP)
.parentAsset(domainSetupAsset)
// TODO.legacy: don't create DNS setup as long as we need to remain support legacy web-ui etc.
createDomainSubSetupAssetEntity(
domainSetupAsset,
DOMAIN_MBOX_SETUP,
builder -> builder
.assignedToAsset(assignedToUnixUserAsset.getParentAsset())
.identifier(domainName + "|MBOX")
.caption("HTTP-Setup für " + IDN.toUnicode(domainName))
.build());
}
if (subHostingAssetResources.stream().noneMatch(ha -> ha.getType() == HsHostingAssetTypeResource.DOMAIN_SMTP_SETUP)) {
domainSetupAsset.getSubHostingAssets().add(HsHostingAssetRealEntity.builder()
.type(DOMAIN_SMTP_SETUP)
.parentAsset(domainSetupAsset)
.identifier(getDomainName() + "|MBOX")
.caption("HTTP-Setup für " + IDN.toUnicode(getDomainName())));
createDomainSubSetupAssetEntity(
domainSetupAsset,
DOMAIN_SMTP_SETUP,
builder -> builder
.assignedToAsset(assignedToUnixUserAsset.getParentAsset())
.identifier(domainName + "|SMTP")
.caption("HTTP-Setup für " + IDN.toUnicode(domainName))
.build());
}
.identifier(getDomainName() + "|SMTP")
.caption("HTTP-Setup für " + IDN.toUnicode(getDomainName())));
return domainSetupAsset;
}
@ -157,13 +138,55 @@ public class HsBookingItemCreatedListener implements ApplicationListener<Booking
.caption(asset.getCaption() != null ? asset.getCaption() : domainName)
.parentAsset(ref(HsHostingAssetRealEntity.class, asset.getParentAssetUuid()))
.alarmContact(ref(HsOfficeContactRealEntity.class, asset.getAlarmContactUuid()))
// FIXME .config(asset.getConfig())
.subHostingAssets(
standardMapper.mapList(asset.getSubHostingAssets(), HsHostingAssetRealEntity.class)
standardMapper.mapList(getSubHostingAssetResources(), HsHostingAssetRealEntity.class)
)
.build();
}
private HsHostingAssetRealEntity createDomainHttpSetupAssetEntity(
final List<HsHostingAssetSubInsertResource> subHostingAssetResources,
final String domainName,
final HsHostingAssetRealEntity domainSetupAsset) {
final var domainHttpSetupAssetResource = subHostingAssetResources.stream()
.filter(ha -> ha.getType() == HsHostingAssetTypeResource.DOMAIN_HTTP_SETUP)
.findFirst().orElseThrow(() -> new ValidationException(
domainName + ": missing target unix user (assignedToHostingAssetUuid) for DOMAIN_HTTP_SETUP "));
final var domainHttpSetupAsset = domainSetupAsset.getSubHostingAssets()
.stream()
.filter(sha -> sha.getType() == DOMAIN_HTTP_SETUP)
.findFirst()
.orElseThrow();
domainHttpSetupAsset.setParentAsset(domainSetupAsset);
final var assignedToUnixUserAssetX =
emw.find(HsHostingAssetRealEntity.class, domainHttpSetupAssetResource.getAssignedToAssetUuid());
domainHttpSetupAsset.setAssignedToAsset(assignedToUnixUserAssetX);
return domainHttpSetupAsset;
}
private void createDomainSubSetupAssetEntity(
final HsHostingAssetRealEntity domainSetupAsset,
final HsHostingAssetType subAssetType,
final Function<HsHostingAssetRealEntityBuilder<?, ?>, HsHostingAssetRealEntityBuilder<?, ?>> builderTransformer) {
final var resourceType = HsHostingAssetTypeResource.valueOf(subAssetType.name());
if (getSubHostingAssetResources().stream().noneMatch(ha -> ha.getType() == resourceType)) {
final var subAssetEntity = builderTransformer.apply(
HsHostingAssetRealEntity.builder()
.type(HsHostingAssetType.valueOf(subAssetType.name()))
.parentAsset(domainSetupAsset))
.build();
domainSetupAsset.getSubHostingAssets().add(subAssetEntity);
}
}
private String getDomainName() {
return asset.getIdentifier();
}
private List<HsHostingAssetSubInsertResource> getSubHostingAssetResources() {
return asset.getSubHostingAssets();
}
@Override
protected void persist(final HsHostingAsset newHostingAsset) {
super.persist(newHostingAsset);