Search in sources :

Example 6 with RegistryData

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();
}
Also used : ForbiddenException(javax.ws.rs.ForbiddenException) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) AccountInfo(org.bf2.srs.fleetmanager.spi.common.model.AccountInfo) AroundInvoke(javax.interceptor.AroundInvoke)

Example 7 with RegistryData

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();
}
Also used : Instant(java.time.Instant) PersistenceException(javax.persistence.PersistenceException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) RegistryStorageConflictException(org.bf2.srs.fleetmanager.storage.RegistryStorageConflictException)

Example 8 with RegistryData

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());
}
Also used : TenantNotFoundServiceException(org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UpdateTenantRequest(org.bf2.srs.fleetmanager.spi.tenants.model.UpdateTenantRequest) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) TenantLimit(org.bf2.srs.fleetmanager.spi.tenants.model.TenantLimit) TenantManagerServiceException(org.bf2.srs.fleetmanager.spi.tenants.TenantManagerServiceException) NoSuchElementException(java.util.NoSuchElementException)

Example 9 with RegistryData

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();
}
Also used : ForbiddenException(javax.ws.rs.ForbiddenException) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) AccountInfo(org.bf2.srs.fleetmanager.spi.common.model.AccountInfo) AroundInvoke(javax.interceptor.AroundInvoke)

Example 10 with RegistryData

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();
    }
}
Also used : StartDeprovisionRegistryTask(org.bf2.srs.fleetmanager.execution.impl.tasks.deprovision.StartDeprovisionRegistryTask) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) Transactional(javax.transaction.Transactional)

Aggregations

RegistryData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData)13 Transactional (javax.transaction.Transactional)6 AccountInfo (org.bf2.srs.fleetmanager.spi.common.model.AccountInfo)4 ProvisionRegistryTenantTask (org.bf2.srs.fleetmanager.execution.impl.tasks.ProvisionRegistryTenantTask)3 RegistryDeploymentData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData)3 AroundInvoke (javax.interceptor.AroundInvoke)2 ForbiddenException (javax.ws.rs.ForbiddenException)2 ScheduleRegistryTask (org.bf2.srs.fleetmanager.execution.impl.tasks.ScheduleRegistryTask)2 StartDeprovisionRegistryTask (org.bf2.srs.fleetmanager.execution.impl.tasks.deprovision.StartDeprovisionRegistryTask)2 TenantNotFoundServiceException (org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException)2 RegistryStorageConflictException (org.bf2.srs.fleetmanager.storage.RegistryStorageConflictException)2 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 Collectors.toList (java.util.stream.Collectors.toList)1