Search in sources :

Example 1 with Tenant

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

the class DeprovisionRegistryWorker method finallyExecute.

@Transactional
@Override
public void finallyExecute(Task aTask, WorkerContext ctl, Optional<Exception> error) throws RegistryNotFoundException, RegistryStorageConflictException {
    DeprovisionRegistryTask task = (DeprovisionRegistryTask) aTask;
    Optional<RegistryData> registry = storage.getRegistryById(task.getRegistryId());
    if (registry.isPresent()) {
        var reg = registry.get();
        // Failure - Could not delete tenant or update status
        // Try updating status to failed, otherwise user can retry.
        reg.setStatus(RegistryStatusValueDto.FAILED.value());
        // TODO Add failed_reason
        storage.createOrUpdateRegistry(reg);
        log.warn("Failed to deprovision Registry: {}", registry);
    } else {
        // SUCCESS
        log.debug("Registry (ID = {}) has been deleted.", task.getRegistryId());
    }
}
Also used : DeprovisionRegistryTask(org.bf2.srs.fleetmanager.execution.impl.tasks.deprovision.DeprovisionRegistryTask) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) Transactional(javax.transaction.Transactional)

Example 2 with Tenant

use of org.bf2.srs.fleetmanager.spi.tenants.model.Tenant 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 3 with Tenant

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

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

the class QuotaPlanIT method testQuotaPlan.

@Test
void testQuotaPlan() throws Exception {
    var alice = new AccountInfo("alice", "alice", false, 1L);
    var registry1 = new RegistryCreate();
    registry1.setName("registry-basic");
    var registry1Result = FleetManagerApi.createRegistry(registry1, alice);
    assertNotEquals(RegistryStatusValue.failed, registry1Result.getStatus());
    Awaitility.await("registry available").atMost(30, TimeUnit.SECONDS).pollInterval(5, TimeUnit.SECONDS).until(() -> {
        var reg = FleetManagerApi.getRegistry(registry1Result.getId(), alice);
        return reg.getStatus().equals(RegistryStatusValue.ready);
    });
    var bob = new AccountInfo("bob", "bob", false, 2L);
    var registry2 = new RegistryCreate();
    registry2.setName("registry-premium");
    var registry2Result = FleetManagerApi.createRegistry(registry2, bob);
    assertNotEquals(RegistryStatusValue.failed, registry2Result.getStatus());
    Awaitility.await("registry available").atMost(30, TimeUnit.SECONDS).pollInterval(5, TimeUnit.SECONDS).until(() -> {
        var reg = FleetManagerApi.getRegistry(registry2Result.getId(), bob);
        return reg.getStatus().equals(RegistryStatusValue.ready);
    });
    TenantManagerClient tenantManager = Utils.createTenantManagerClient();
    // basic
    var tenant = tenantManager.getTenant(registry1Result.getId());
    var resources = tenant.getResources();
    Long l = null;
    for (var r : resources) {
        if (r.getType() == ResourceType.MAX_TOTAL_SCHEMAS_COUNT) {
            l = r.getLimit();
        }
    }
    assertNotNull(l);
    assertEquals(10, l);
    // premium
    tenant = tenantManager.getTenant(registry2Result.getId());
    resources = tenant.getResources();
    l = null;
    for (var r : resources) {
        if (r.getType() == ResourceType.MAX_TOTAL_SCHEMAS_COUNT) {
            l = r.getLimit();
        }
    }
    assertNotNull(l);
    assertEquals(100, l);
    // Update the limit value and recheck after forced reconciliation
    for (var r : resources) {
        if (r.getType() == ResourceType.MAX_TOTAL_SCHEMAS_COUNT) {
            r.setLimit(-1L);
        }
    }
    var ur = new UpdateRegistryTenantRequest();
    ur.setResources(resources);
    tenantManager.updateTenant(registry2Result.getId(), ur);
    // Check updated
    tenant = tenantManager.getTenant(registry2Result.getId());
    resources = tenant.getResources();
    l = null;
    for (var r : resources) {
        if (r.getType() == ResourceType.MAX_TOTAL_SCHEMAS_COUNT) {
            l = r.getLimit();
        }
    }
    assertNotNull(l);
    assertEquals(-1, l);
    // Restart fleet manager(s) so the quota plan is reconciled
    TestInfraManager.getInstance().restartFleetManager();
    tenant = tenantManager.getTenant(registry2Result.getId());
    resources = tenant.getResources();
    l = null;
    for (var r : resources) {
        if (r.getType() == ResourceType.MAX_TOTAL_SCHEMAS_COUNT) {
            l = r.getLimit();
        }
    }
    assertNotNull(l);
    assertEquals(100, l);
    // Delete
    FleetManagerApi.deleteRegistry(registry1Result.getId(), alice);
    FleetManagerApi.deleteRegistry(registry2Result.getId(), bob);
}
Also used : TenantManagerClient(io.apicurio.multitenant.client.TenantManagerClient) RegistryCreate(org.bf2.srs.fleetmanager.rest.publicapi.beans.RegistryCreate) AccountInfo(org.bf2.srs.fleetmanager.spi.common.model.AccountInfo) UpdateRegistryTenantRequest(io.apicurio.multitenant.api.datamodel.UpdateRegistryTenantRequest) Test(org.junit.jupiter.api.Test)

Example 5 with Tenant

use of org.bf2.srs.fleetmanager.spi.tenants.model.Tenant 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)

Aggregations

TenantNotFoundServiceException (org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException)4 Transactional (javax.transaction.Transactional)3 RegistryData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData)3 Timed (io.micrometer.core.annotation.Timed)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 ConnectorNamespace (org.bf2.cos.fleet.manager.model.ConnectorNamespace)2 ConnectorNamespaceStatus1 (org.bf2.cos.fleet.manager.model.ConnectorNamespaceStatus1)2 ConnectorNamespaceTenant (org.bf2.cos.fleet.manager.model.ConnectorNamespaceTenant)2 Audited (org.bf2.srs.fleetmanager.common.operation.auditing.Audited)2 DeprovisionRegistryTask (org.bf2.srs.fleetmanager.execution.impl.tasks.deprovision.DeprovisionRegistryTask)2 Tenant (org.bf2.srs.fleetmanager.spi.tenants.model.Tenant)2 TenantManagerConfig (org.bf2.srs.fleetmanager.spi.tenants.model.TenantManagerConfig)2 RegistryDeploymentData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData)2 Test (org.junit.jupiter.api.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 NewRegistryTenantRequest (io.apicurio.multitenant.api.datamodel.NewRegistryTenantRequest)1 RegistryTenant (io.apicurio.multitenant.api.datamodel.RegistryTenant)1 TenantResource (io.apicurio.multitenant.api.datamodel.TenantResource)1 UpdateRegistryTenantRequest (io.apicurio.multitenant.api.datamodel.UpdateRegistryTenantRequest)1