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