Call across tenants
Call a tenant's lunas from your platform, let a tenant call the lunas you grant it, and know what a tenant can never reach.
Each tenant is walled off, so ctx.call reaches only lunas of the same tenant. A platform still needs to call into its customers’ tenants, and to let their code use some lunas of its own.
Call into a tenant
From a luna of the default tenant, ctx.tenants.get(name) reaches a tenant added to the environment. Name the luna type and the key, then call a handler by name.
import { defineLuna, t, tenants } from "luna";
export const User = defineLuna({
name: "User",
key: { id: t.string() },
handlers: (h) => ({
addFor: h({ args: { text: t.string() } }, async (ctx, { text }) => {
const list = ctx.tenants.get(ctx.key.id).luna("List").at({ id: "inbox" });
return list.call("add", { text }, { timeout: "5s" });
}),
}),
});
The call reaches private luna types too. Its arguments and result are not typed, since the tenant’s code is not part of your app, so check what comes back.
In the tenant, ctx.caller is { external: { tenant } }, with the full name of the tenant that called.
Grant a tenant a service
A tenant cannot call the default tenant’s lunas until you grant it one. A grant names a service and the luna type that serves it. Set it with configure from Manage tenants from code. It replaces every setting, so send the tenant’s quotas with it.
grant: h({}, async (ctx) => {
const platform = tenants({ token: ctx.env.PLATFORM_TOKEN, parent: ctx.tenant });
await platform.configure(ctx.key.id, { grants: { wallet: { luna: "Wallet" } } });
}),
The tenant’s code calls the service by its name with ctx.services.get(name), then names the key of the luna it wants.
import { defineLuna, t } from "luna";
export const Shop = defineLuna({
name: "Shop",
key: { id: t.string() },
handlers: (h) => ({
buy: h({ args: { amount: t.int() } }, async (ctx, { amount }) => {
const wallet = ctx.services.get("wallet").at({ id: ctx.key.id });
return wallet.call("charge", { amount });
}),
}),
});
Each grant covers one luna type of yours. A service that was never granted fails with a LunaCallError of kind Invalid.
Know who called the service
In your luna, ctx.caller.external.tenant names the tenant that called. Check it before you act for that tenant.
import { defineLuna, t, table } from "luna";
export const Wallet = defineLuna({
name: "Wallet",
key: { id: t.string() },
schema: { charges: table({ id: t.id(), amount: t.int(), tenant: t.string() }) },
handlers: (h) => ({
charge: h({ args: { amount: t.int() } }, (ctx, { amount }) => {
const caller = ctx.caller as { external?: { tenant?: string } };
ctx.db.charges.insert({ amount, tenant: caller.external?.tenant ?? "" });
}),
}),
});
A tenant chooses the key it passes, so a tenant can name a key that belongs to another customer. Make the key include the tenant, or compare the two.
Know what a tenant can never reach
A tenant’s code reaches its own lunas and the services granted to it. Nothing else answers.
- The other tenants of the environment, even with a name that exists.
- The default tenant’s luna types that no grant names.
- Lunas of another environment or another project.
The default tenant has no services of its own. It calls its added tenants with ctx.tenants instead.
Follow a call across tenants
A call into a tenant carries its trace. In the tenant’s traces, the call’s span sits under the span of the platform’s luna that made it, so Follow traces shows both sides.
Next steps
- Manage tenants from code sets the grants and limits of each tenant.
- Call another luna covers calls within one tenant.
- Read a tenant’s API lists the handlers a tenant’s code exposes.