Search in sources :

Example 1 with Tenant

use of org.folio.okapi.bean.Tenant in project okapi by folio-org.

the class ProxyService method proxy.

public void proxy(RoutingContext ctx) {
    ctx.request().pause();
    ReadStream<Buffer> stream = ctx.request();
    // Pause the request data stream before doing any slow ops, otherwise
    // it will get read into a buffer somewhere.
    ProxyContext pc = new ProxyContext(ctx);
    // It would be nice to pass the request-id to the client, so it knows what
    // to look for in Okapi logs. But that breaks the schemas, and RMB-based
    // modules will not accept the response. Maybe later...
    String tenantId = tenantHeader(pc);
    if (tenantId == null) {
        stream.resume();
        // Error code already set in ctx
        return;
    }
    sanitizeAuthHeaders(ctx.request().headers());
    tenantManager.get(tenantId, gres -> {
        if (gres.failed()) {
            stream.resume();
            pc.responseText(400, "No such Tenant " + tenantId);
            return;
        }
        Tenant tenant = gres.result();
        moduleManager.getEnabledModules(tenant, mres -> {
            if (mres.failed()) {
                stream.resume();
                pc.responseError(mres.getType(), mres.cause());
                return;
            }
            List<ModuleDescriptor> enabledModules = mres.result();
            String metricKey = "proxy." + tenantId + "." + ctx.request().method() + "." + ctx.normalisedPath();
            DropwizardHelper.markEvent(metricKey);
            List<ModuleInstance> l = getModulesForRequest(pc, enabledModules);
            if (l == null) {
                stream.resume();
                // ctx already set up
                return;
            }
            pc.setModList(l);
            pc.logRequest(ctx, tenantId);
            ctx.request().headers().add(XOkapiHeaders.URL, okapiUrl);
            ctx.request().headers().remove(XOkapiHeaders.MODULE_ID);
            resolveUrls(l.iterator(), res -> {
                if (res.failed()) {
                    stream.resume();
                    pc.responseError(res.getType(), res.cause());
                } else {
                    proxyR(l.iterator(), pc, stream, null);
                }
            });
        });
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ModuleDescriptor(org.folio.okapi.bean.ModuleDescriptor) Tenant(org.folio.okapi.bean.Tenant) ProxyContext(org.folio.okapi.util.ProxyContext) ModuleInstance(org.folio.okapi.bean.ModuleInstance)

Example 2 with Tenant

use of org.folio.okapi.bean.Tenant in project okapi by folio-org.

the class TenantManager method getModuleUserR.

private void getModuleUserR(String mod, Iterator<String> it, Handler<ExtendedAsyncResult<Void>> fut) {
    if (!it.hasNext()) {
        // no problems found
        fut.handle(new Success<>());
    } else {
        String tid = it.next();
        tenants.get(tid, gres -> {
            if (gres.failed()) {
                fut.handle(new Failure<>(gres.getType(), gres.cause()));
            } else {
                Tenant t = gres.result();
                if (t.isEnabled(mod)) {
                    fut.handle(new Failure<>(ANY, tid));
                } else {
                    getModuleUserR(mod, it, fut);
                }
            }
        });
    }
}
Also used : Tenant(org.folio.okapi.bean.Tenant)

Example 3 with Tenant

use of org.folio.okapi.bean.Tenant in project okapi by folio-org.

the class TenantManager method enableAndDisableModule.

/**
 * Enable a module for a tenant and disable another. Checks dependencies,
 * invokes the tenant interface, and the tenantPermissions interface, and
 * finally marks the modules as enabled and disabled.
 *
 * @param tenantId - id of the the tenant in question
 * @param moduleFrom id of the module to be disabled, or null
 * @param moduleTo id of the module to be enabled, or null
 * @param pc proxyContext for proper logging, etc
 * @param fut callback with success, or various errors
 *
 * To avoid too much callback hell, this has been split into several helpers.
 */
public void enableAndDisableModule(String tenantId, String moduleFrom, String moduleTo, ProxyContext pc, Handler<ExtendedAsyncResult<String>> fut) {
    tenants.get(tenantId, tres -> {
        if (tres.failed()) {
            fut.handle(new Failure<>(tres.getType(), tres.cause()));
        } else {
            Tenant tenant = tres.result();
            enableAndDisableModule(tenant, moduleFrom, moduleTo, pc, fut);
        }
    });
}
Also used : Tenant(org.folio.okapi.bean.Tenant)

Example 4 with Tenant

use of org.folio.okapi.bean.Tenant in project okapi by folio-org.

the class TenantManager method updateDescriptor.

public void updateDescriptor(TenantDescriptor td, Handler<ExtendedAsyncResult<Void>> fut) {
    final String id = td.getId();
    tenants.get(id, gres -> {
        if (gres.failed() && gres.getType() != NOT_FOUND) {
            logger.warn("TenantManager: UpDesc: getting " + id + " FAILED: ", gres);
            fut.handle(new Failure<>(INTERNAL, ""));
        }
        Tenant t;
        if (gres.succeeded()) {
            t = new Tenant(td, gres.result().getEnabled());
        } else {
            t = new Tenant(td);
        }
        if (tenantStore == null) {
            // no database. handles success directly
            tenants.add(id, t, fut);
        } else {
            tenantStore.updateDescriptor(td, upres -> {
                if (upres.failed()) {
                    logger.warn("TenantManager: Updating database for " + id + " FAILED: ", upres);
                    fut.handle(new Failure<>(INTERNAL, ""));
                } else {
                    // handles success
                    tenants.add(id, t, fut);
                }
            });
        }
    });
}
Also used : Tenant(org.folio.okapi.bean.Tenant)

Example 5 with Tenant

use of org.folio.okapi.bean.Tenant in project okapi by folio-org.

the class TenantManager method installUpgradeModules.

public void installUpgradeModules(String tenantId, ProxyContext pc, TenantInstallOptions options, List<TenantModuleDescriptor> tml, Handler<ExtendedAsyncResult<List<TenantModuleDescriptor>>> fut) {
    tenants.get(tenantId, gres -> {
        if (gres.failed()) {
            fut.handle(new Failure<>(gres.getType(), gres.cause()));
            return;
        }
        Tenant t = gres.result();
        moduleManager.getModulesWithFilter(null, options.getPreRelease(), mres -> {
            if (mres.failed()) {
                fut.handle(new Failure<>(mres.getType(), mres.cause()));
                return;
            }
            List<ModuleDescriptor> modResult = mres.result();
            HashMap<String, ModuleDescriptor> modsAvailable = new HashMap<>(modResult.size());
            HashMap<String, ModuleDescriptor> modsEnabled = new HashMap<>();
            for (ModuleDescriptor md : modResult) {
                modsAvailable.put(md.getId(), md);
                logger.info("mod available: " + md.getId());
                if (t.isEnabled(md.getId())) {
                    logger.info("mod enabled: " + md.getId());
                    modsEnabled.put(md.getId(), md);
                }
            }
            List<TenantModuleDescriptor> tml2 = prepareTenantModuleList(modsAvailable, modsEnabled, tml);
            installUpgradeModules2(t, pc, options, modsAvailable, modsEnabled, tml2, fut);
        });
    });
}
Also used : TenantModuleDescriptor(org.folio.okapi.bean.TenantModuleDescriptor) ModuleDescriptor(org.folio.okapi.bean.ModuleDescriptor) TenantModuleDescriptor(org.folio.okapi.bean.TenantModuleDescriptor) Tenant(org.folio.okapi.bean.Tenant) HashMap(java.util.HashMap)

Aggregations

Tenant (org.folio.okapi.bean.Tenant)17 JsonObject (io.vertx.core.json.JsonObject)3 ModuleDescriptor (org.folio.okapi.bean.ModuleDescriptor)3 TenantDescriptor (org.folio.okapi.bean.TenantDescriptor)3 TenantModuleDescriptor (org.folio.okapi.bean.TenantModuleDescriptor)3 DecodeException (io.vertx.core.json.DecodeException)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Failure (org.folio.okapi.common.Failure)2 Buffer (io.vertx.core.buffer.Buffer)1 JsonArray (io.vertx.core.json.JsonArray)1 HashMap (java.util.HashMap)1 List (java.util.List)1 InterfaceDescriptor (org.folio.okapi.bean.InterfaceDescriptor)1 ModuleInstance (org.folio.okapi.bean.ModuleInstance)1 PermissionList (org.folio.okapi.bean.PermissionList)1 CompList (org.folio.okapi.util.CompList)1 ProxyContext (org.folio.okapi.util.ProxyContext)1