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());
}
}
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();
}
}
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());
}
}
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);
}
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());
}
Aggregations