use of org.bf2.srs.fleetmanager.storage.RegistryDeploymentNotFoundException in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class RegistryDeploymentServiceImpl method init.
@Override
public void init() throws IOException, RegistryDeploymentStorageConflictException, RegistryDeploymentNotFoundException {
if (deploymentsConfigFile.isEmpty()) {
return;
}
log.info("Loading registry deployments config file from {}", deploymentsConfigFile.get().getAbsolutePath());
YAMLMapper mapper = new YAMLMapper();
RegistryDeploymentsConfigList deploymentsConfigList = mapper.readValue(deploymentsConfigFile.get(), RegistryDeploymentsConfigList.class);
List<RegistryDeploymentCreate> staticDeployments = deploymentsConfigList.getDeployments();
Set<String> names = new HashSet<>();
List<String> duplicatedNames = staticDeployments.stream().map(d -> {
Set<ConstraintViolation<RegistryDeploymentCreate>> errors = validator.validate(d);
if (!errors.isEmpty()) {
throw new ConstraintViolationException(errors);
}
return d;
}).filter(d -> !names.add(d.getName())).map(d -> d.getName()).collect(Collectors.toList());
if (!duplicatedNames.isEmpty()) {
throw new IllegalArgumentException("Error in static deployments config, duplicated deployments name: " + duplicatedNames.toString());
}
Map<String, RegistryDeploymentData> currentDeployments = storage.getAllRegistryDeployments().stream().collect(Collectors.toMap(d -> d.getName(), d -> d));
for (RegistryDeploymentCreate dep : staticDeployments) {
RegistryDeploymentData deploymentData = currentDeployments.get(dep.getName());
if (deploymentData == null) {
// deployment is new
deploymentData = convertRegistryDeployment.convert(dep);
} else {
if (deploymentData.getRegistryDeploymentUrl().equals(dep.getRegistryDeploymentUrl()) && deploymentData.getTenantManagerUrl().equals(dep.getTenantManagerUrl())) {
// no changes in the deployment
continue;
}
deploymentData.setRegistryDeploymentUrl(dep.getRegistryDeploymentUrl());
deploymentData.setTenantManagerUrl(dep.getTenantManagerUrl());
}
createOrUpdateRegistryDeployment(deploymentData);
}
}
use of org.bf2.srs.fleetmanager.storage.RegistryDeploymentNotFoundException 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();
}
Aggregations