Search in sources :

Example 1 with TenantNotFoundServiceException

use of org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class DeprovisionRegistryWorker method execute.

@Transactional
@Override
public void execute(Task aTask, WorkerContext ctl) throws RegistryStorageConflictException, RegistryNotFoundException, AccountManagementServiceException, TenantManagerServiceException {
    var task = (DeprovisionRegistryTask) aTask;
    var registryOptional = storage.getRegistryById(task.getRegistryId());
    if (registryOptional.isPresent()) {
        // FAILURE POINT 1
        var registry = registryOptional.get();
        RegistryDeploymentData registryDeployment = registry.getRegistryDeployment();
        // FAILURE POINT 2
        if (task.getRegistryTenantId() == null) {
            final var tenantId = registry.getId();
            TenantManagerConfig tenantManagerConfig = Utils.createTenantManagerConfig(registryDeployment);
            try {
                tms.deleteTenant(tenantManagerConfig, tenantId);
                log.debug("Tenant id='{}' delete request send.", tenantId);
            } catch (TenantNotFoundServiceException ex) {
                log.info("Tenant id='{}' does not exist (already deleted?).", tenantId);
            }
            task.setRegistryTenantId(tenantId);
        }
        /* Return AMS entitlement
             * FAILURE POINT 3
             * Recovery: We recover by setting the registry status to failed so we don't lose information
             *   and the process can be initiated again.
             * Reentrancy: If the registry was already returned, (i.e. failed in #3)
             *   we need to continue without raising an error, otherwise we will keep retrying.
             */
        if (!task.isAmsSuccess()) {
            final String subscriptionId = registry.getSubscriptionId();
            // TODO Workaround: Remove this once we have RHOSRTrial working.
            if (subscriptionId != null && RegistryInstanceTypeValueDto.of(registry.getInstanceType()) != RegistryInstanceTypeValueDto.EVAL) {
                try {
                    ams.deleteSubscription(subscriptionId);
                } catch (SubscriptionNotFoundServiceException ex) {
                    log.info("Subscription ID '{}' for tenant ID '{}' does not exist (already deleted?).", subscriptionId, task.getRegistryTenantId());
                }
            } else {
                log.debug("Deleting an eval instance {} without calling AMS.", registry.getId());
            }
            task.setAmsSuccess(true);
            log.debug("Subscription (id='{}') for Registry (id='{}') deleted.", subscriptionId, registry.getId());
        }
        /* Delete the registry from DB
             * FAILURE POINT 4
             * Recovery: We set the status to failed so it can be retried.
             * Reentrancy: This is the last step, so nothing to do.
             */
        storage.deleteRegistry(registry.getId());
    } else {
        log.warn("Registry id='{}' not found. Stopping.", task.getRegistryId());
        ctl.stop();
    }
}
Also used : DeprovisionRegistryTask(org.bf2.srs.fleetmanager.execution.impl.tasks.deprovision.DeprovisionRegistryTask) TenantNotFoundServiceException(org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException) SubscriptionNotFoundServiceException(org.bf2.srs.fleetmanager.spi.ams.SubscriptionNotFoundServiceException) RegistryDeploymentData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData) TenantManagerConfig(org.bf2.srs.fleetmanager.spi.tenants.model.TenantManagerConfig) Transactional(javax.transaction.Transactional)

Example 2 with TenantNotFoundServiceException

use of org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class ProvisionRegistryTenantWorker method finallyExecute.

@Transactional
@Override
public void finallyExecute(Task aTask, WorkerContext ctl, Optional<Exception> error) throws RegistryNotFoundException, RegistryStorageConflictException, SubscriptionNotFoundServiceException, AccountManagementServiceException, TenantManagerServiceException {
    ProvisionRegistryTenantTask task = (ProvisionRegistryTenantTask) aTask;
    RegistryData registry = storage.getRegistryById(task.getRegistryId()).orElse(null);
    RegistryDeploymentData registryDeployment = null;
    if (registry != null)
        registryDeployment = registry.getRegistryDeployment();
    // SUCCESS STATE
    if (registry != null && registry.getRegistryUrl() != null)
        return;
    // Cleanup orphan susbcription, if it's null, it's not needed since it will likely be an eval instance
    if (registry != null && registryDeployment != null && registry.getSubscriptionId() != null) {
        accountManagementService.deleteSubscription(registry.getSubscriptionId());
    }
    // Cleanup orphan tenant
    if (registry != null && registryDeployment != null && task.getRegistryTenantId() != null) {
        try {
            tmClient.deleteTenant(Utils.createTenantManagerConfig(registryDeployment), registry.getId());
        } catch (TenantNotFoundServiceException e) {
            log.warn("Could not delete tenant '{}'. Tenant does not exist and may have been already deleted.", registry.getId());
        }
    }
    // Remove registry entity
    if (registry != null) {
        storage.deleteRegistry(registry.getId());
    }
}
Also used : TenantNotFoundServiceException(org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException) ProvisionRegistryTenantTask(org.bf2.srs.fleetmanager.execution.impl.tasks.ProvisionRegistryTenantTask) RegistryDeploymentData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) Transactional(javax.transaction.Transactional)

Example 3 with TenantNotFoundServiceException

use of org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException 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 4 with TenantNotFoundServiceException

use of org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class MigrationService method runMigration.

public void runMigration() throws TenantManagerServiceException {
    if (deleteAll.isPresent() && deleteAll.get()) {
        log.warn("Removing all data first");
        TenantManagerConfig tm = TenantManagerConfig.builder().tenantManagerUrl("http://tenant-manager:8585").registryDeploymentUrl("https://service-registry-stage.apps.app-sre-stage-0.k3s7.p1.openshiftapps.com").build();
        for (Tenant t : tenantManagerClient.getAllTenants(tm)) {
            log.warn("Deleting tenant '{}'", t.getId());
            try {
                tenantManagerClient.deleteTenant(tm, t.getId());
            } catch (TenantNotFoundServiceException ex) {
                log.warn("Could not delete tenant '{}'. Tenant does not exist and may have been already deleted.", t.getId());
            }
        }
        CleanResult cleanResult = flyway.clean();
        log.info("Database clean result: " + "flywayVersion = '{}', " + "database = '{}', " + "schemasCleaned = '{}', " + "schemasDropped = '{}', " + "warnings = '{}'.", cleanResult.flywayVersion, cleanResult.database, cleanResult.schemasCleaned, cleanResult.schemasDropped, cleanResult.warnings);
    }
    MigrateResult migrateResult = flyway.migrate();
    log.info("Database migrate result: " + "flywayVersion = '{}', " + "database = '{}', " + "initialSchemaVersion = '{}', " + "targetSchemaVersion = '{}', " + "migrations = '{}', " + "warnings = '{}'.", migrateResult.flywayVersion, migrateResult.database, migrateResult.initialSchemaVersion, migrateResult.targetSchemaVersion, migrateResult.migrations, migrateResult.warnings);
}
Also used : TenantNotFoundServiceException(org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException) Tenant(org.bf2.srs.fleetmanager.spi.tenants.model.Tenant) MigrateResult(org.flywaydb.core.api.output.MigrateResult) TenantManagerConfig(org.bf2.srs.fleetmanager.spi.tenants.model.TenantManagerConfig) CleanResult(org.flywaydb.core.api.output.CleanResult)

Aggregations

TenantNotFoundServiceException (org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException)4 Transactional (javax.transaction.Transactional)2 TenantManagerConfig (org.bf2.srs.fleetmanager.spi.tenants.model.TenantManagerConfig)2 RegistryData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData)2 RegistryDeploymentData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData)2 HashMap (java.util.HashMap)1 NoSuchElementException (java.util.NoSuchElementException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ProvisionRegistryTenantTask (org.bf2.srs.fleetmanager.execution.impl.tasks.ProvisionRegistryTenantTask)1 DeprovisionRegistryTask (org.bf2.srs.fleetmanager.execution.impl.tasks.deprovision.DeprovisionRegistryTask)1 SubscriptionNotFoundServiceException (org.bf2.srs.fleetmanager.spi.ams.SubscriptionNotFoundServiceException)1 TenantManagerServiceException (org.bf2.srs.fleetmanager.spi.tenants.TenantManagerServiceException)1 Tenant (org.bf2.srs.fleetmanager.spi.tenants.model.Tenant)1 TenantLimit (org.bf2.srs.fleetmanager.spi.tenants.model.TenantLimit)1 UpdateTenantRequest (org.bf2.srs.fleetmanager.spi.tenants.model.UpdateTenantRequest)1 CleanResult (org.flywaydb.core.api.output.CleanResult)1 MigrateResult (org.flywaydb.core.api.output.MigrateResult)1