From 63e026d4d53e2a1b3879c1d5d5c0b1202b4defd9 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 1 Oct 2024 09:34:40 +0200 Subject: [PATCH] refactor HsBookingItemCreatedListener --- .../asset/HsBookingItemCreatedListener.java | 125 +++++++++++------- 1 file changed, 74 insertions(+), 51 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsBookingItemCreatedListener.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsBookingItemCreatedListener.java index b4eb59df..72720601 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsBookingItemCreatedListener.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsBookingItemCreatedListener.java @@ -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 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 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(); + + // 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(getDomainName() + "|MBOX") + .caption("HTTP-Setup für " + IDN.toUnicode(getDomainName()))); + + createDomainSubSetupAssetEntity( + domainSetupAsset, + DOMAIN_SMTP_SETUP, + builder -> builder + .assignedToAsset(assignedToUnixUserAsset.getParentAsset()) + .identifier(getDomainName() + "|SMTP") + .caption("HTTP-Setup für " + IDN.toUnicode(getDomainName()))); - 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) - .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) - .assignedToAsset(assignedToUnixUserAsset.getParentAsset()) - .identifier(domainName + "|SMTP") - .caption("HTTP-Setup für " + IDN.toUnicode(domainName)) - .build()); - } return domainSetupAsset; } @@ -157,13 +138,55 @@ public class HsBookingItemCreatedListener implements ApplicationListener 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> 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 getSubHostingAssetResources() { + return asset.getSubHostingAssets(); + } + @Override protected void persist(final HsHostingAsset newHostingAsset) { super.persist(newHostingAsset);