Search in sources :

Example 6 with RegistryDeploymentData

use of org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class PanacheResourceStorage method createOrUpdateRegistryDeployment.

// *** RegistryDeployment
@Override
public boolean createOrUpdateRegistryDeployment(RegistryDeploymentData deployment) throws RegistryDeploymentStorageConflictException, RegistryDeploymentNotFoundException {
    // TODO Is this necessary if using @Valid?
    requireNonNull(deployment);
    Optional<RegistryDeploymentData> existing = empty();
    if (deployment.getId() != null) {
        // TODO investigate using locks, such as optimistic locks
        existing = deploymentRepository.findByIdOptional(deployment.getId());
        if (existing.isEmpty()) {
            throw new RegistryDeploymentNotFoundException(deployment.getId().toString());
        }
    }
    try {
        final Instant now = Instant.now();
        deployment.getStatus().setLastUpdated(now);
        if (existing.isEmpty()) {
            deploymentRepository.persistAndFlush(deployment);
        } else {
            em.merge(deployment);
            em.flush();
        }
    } catch (PersistenceException ex) {
        if (ex.getCause() instanceof ConstraintViolationException) {
            throw new RegistryDeploymentStorageConflictException();
        } else {
            throw ex;
        }
    }
    return existing.isEmpty();
}
Also used : Instant(java.time.Instant) PersistenceException(javax.persistence.PersistenceException) RegistryDeploymentNotFoundException(org.bf2.srs.fleetmanager.storage.RegistryDeploymentNotFoundException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) RegistryDeploymentData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData) RegistryDeploymentStorageConflictException(org.bf2.srs.fleetmanager.storage.RegistryDeploymentStorageConflictException)

Aggregations

RegistryDeploymentData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData)6 Transactional (javax.transaction.Transactional)4 ProvisionRegistryTenantTask (org.bf2.srs.fleetmanager.execution.impl.tasks.ProvisionRegistryTenantTask)3 RegistryData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData)3 List (java.util.List)2 Optional (java.util.Optional)2 Collectors.toList (java.util.stream.Collectors.toList)2 ApplicationScoped (javax.enterprise.context.ApplicationScoped)2 Inject (javax.inject.Inject)2 RegistryDeploymentStatusValue (org.bf2.srs.fleetmanager.rest.service.model.RegistryDeploymentStatusValue)2 TenantNotFoundServiceException (org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException)2 TenantManagerConfig (org.bf2.srs.fleetmanager.spi.tenants.model.TenantManagerConfig)2 ResourceStorage (org.bf2.srs.fleetmanager.storage.ResourceStorage)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 YAMLMapper (com.fasterxml.jackson.dataformat.yaml.YAMLMapper)1 File (java.io.File)1 IOException (java.io.IOException)1 Instant (java.time.Instant)1 HashSet (java.util.HashSet)1