use of org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class CheckDeletePermissionsInterceptor method intercept.
@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
if (isResolvable(securityIdentity)) {
final AccountInfo accountInfo = authService.extractAccountInfo();
final Optional<RegistryData> registry = storage.getRegistryById(context.getParameters()[0].toString());
if (userCanDeleteInstance(accountInfo, registry)) {
return context.proceed();
}
} else {
return context.proceed();
}
log.info("Attempt to delete registry instance without the proper permissions");
throw new ForbiddenException();
}
use of org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class PanacheResourceStorage method createOrUpdateRegistry.
@Override
public boolean createOrUpdateRegistry(RegistryData registry) throws RegistryStorageConflictException {
requireNonNull(registry);
Optional<RegistryData> existing = empty();
if (registry.getId() != null) {
// TODO investigate using locks, such as optimistic locks
existing = registryRepository.findByIdOptional(registry.getId());
}
try {
final Instant now = Instant.now();
if (existing.isEmpty()) {
registry.setCreatedAt(now);
}
registry.setUpdatedAt(now);
registryRepository.persistAndFlush(registry);
} catch (PersistenceException ex) {
if (ex.getCause() instanceof ConstraintViolationException) {
throw new RegistryStorageConflictException();
} else {
throw ex;
}
}
return existing.isEmpty();
}
use of org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class FileQuotaPlansService method reconcile.
private void reconcile() {
log.info("Performing quota plan reconciliation");
var allRegistries = storage.getAllRegistries();
var updatedCount = 0;
for (RegistryData registry : allRegistries) {
var tid = registry.getId();
var tmc = Utils.createTenantManagerConfig(registry.getRegistryDeployment());
try {
var tenant = tmClient.getTenantById(tmc, tid).orElseThrow();
Map<String, Long> tenantLimits = new HashMap<>();
for (TenantLimit resource : tenant.getResources()) {
tenantLimits.put(resource.getType(), resource.getLimit());
}
var targetPlan = determineQuotaPlan(registry.getOrgId());
var requiresUpdate = false;
// Compare limits
for (TenantLimit targetLimit : targetPlan.getResources()) {
var v = tenantLimits.get(targetLimit.getType());
if (v == null || !v.equals(targetLimit.getLimit())) {
requiresUpdate = true;
break;
}
}
if (requiresUpdate) {
UpdateTenantRequest utr = UpdateTenantRequest.builder().id(tid).status(tenant.getStatus()).resources(targetPlan.getResources()).build();
tmClient.updateTenant(tmc, utr);
updatedCount++;
}
} catch (TenantManagerServiceException | NoSuchElementException | TenantNotFoundServiceException e) {
log.warn("Could not get or update tenant " + tid + " during quota plan reconciliation", e);
}
}
log.info("Quota plan reconciliation successful. Updated {} out of {} tenants", updatedCount, allRegistries.size());
}
use of org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class CheckReadPermissionsInterceptor method intercept.
@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
if (isResolvable(securityIdentity)) {
final AccountInfo accountInfo = authService.extractAccountInfo();
final Optional<RegistryData> registry = storage.getRegistryById(context.getParameters()[0].toString());
if (userCanReadInstance(accountInfo, registry)) {
return context.proceed();
}
} else {
return context.proceed();
}
// TODO Refactor for readability
log.info("Attempt to read registry instance without the proper permissions");
throw new ForbiddenException();
}
use of org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class StartDeprovisionRegistryWorker method execute.
@Transactional
@Override
public void execute(Task aTask, WorkerContext ctl) throws RegistryStorageConflictException {
StartDeprovisionRegistryTask task = (StartDeprovisionRegistryTask) aTask;
Optional<RegistryData> registryOptional = storage.getRegistryById(task.getRegistryId());
if (registryOptional.isPresent()) {
// FAILURE POINT 1
var registry = registryOptional.get();
var force = Instant.now().isAfter(registry.getCreatedAt().plus(props.getDeprovisionStuckInstanceTimeout()));
if (force) {
log.warn("Registry instance '{}' is forced to be deprovisioned.", registry);
}
var deprovision = force;
var status = RegistryStatusValueDto.of(registry.getStatus());
switch(status) {
case ACCEPTED:
case PROVISIONING:
if (!force) {
log.debug("Provisioning in progress. Retrying.");
ctl.retry();
}
break;
case READY:
case FAILED:
deprovision = true;
break;
case REQUESTED_DEPROVISIONING:
case DEPROVISIONING_DELETING:
if (!force) {
log.debug("Deprovisioning is already in progress. Stopping. Registry = {}", registry);
ctl.stop();
}
break;
default:
throw new IllegalStateException("Unexpected status value: " + status);
}
if (deprovision) {
registry.setStatus(RegistryStatusValueDto.DEPROVISIONING_DELETING.value());
// FAILURE POINT 2
storage.createOrUpdateRegistry(registry);
ctl.delay(() -> tasks.submit(DeprovisionRegistryTask.builder().registryId(registry.getId()).build()));
}
} else {
log.warn("Registry id='{}' not found. Stopping.", task.getRegistryId());
ctl.stop();
}
}
Aggregations