333 lines
16 KiB
C++
333 lines
16 KiB
C++
#include "openrouter/admin.h"
|
|||
|
|
|
||
|
|
#include "llm/error.h"
|
||
|
|
#include "openrouter/account.h"
|
||
|
|
#include "openrouter/caps.h"
|
||
|
|
|
||
|
|
namespace molo::openrouter {
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid(kVendor, why); }
|
||
|
|
|
||
|
|
Json string_list_body(const char* key, const std::vector<std::string>& values, bool hashes = false) {
|
||
|
|
if (values.empty()) bad(std::string(key) + " must not be empty");
|
||
|
|
if (values.size() > Caps::kMaxBulkIds) bad(std::string(key) + " has too many entries");
|
||
|
|
Json a = Json::array();
|
||
|
|
for (const std::string& v : values) {
|
||
|
|
if (hashes ? !is_key_hash(v) : (v.empty() || v.size() > 256)) bad(std::string(key) + " has a malformed entry");
|
||
|
|
a.push(Json(v));
|
||
|
|
}
|
||
|
|
Json b = Json::object();
|
||
|
|
b.set(key, a);
|
||
|
|
return b;
|
||
|
|
}
|
||
|
|
|
||
|
|
void check_body(const Json& body, const char* what) {
|
||
|
|
if (!body.is_object() || body.as_object().empty()) bad(std::string(what) + " needs a non-empty JSON object");
|
||
|
|
}
|
||
|
|
|
||
|
|
Json secret_body(const std::string& value, const std::vector<std::string>& hosts) {
|
||
|
|
if (value.empty() || value.size() > 65536) bad("secret value must be 1..65536 characters");
|
||
|
|
if (hosts.empty() || hosts.size() > 100) bad("secret hosts must hold 1..100 hostnames");
|
||
|
|
Json h = Json::array();
|
||
|
|
for (const std::string& host : hosts) {
|
||
|
|
if (host.empty() || host.size() > 254) bad("secret host must be 1..254 characters");
|
||
|
|
for (char c : host)
|
||
|
|
if (c == '/' || c == ':' || c == '*' || c == ' ') bad("secret host must be a bare hostname");
|
||
|
|
h.push(Json(host));
|
||
|
|
}
|
||
|
|
Json b = Json::object();
|
||
|
|
b.set("value", Json(value));
|
||
|
|
b.set("hosts", h);
|
||
|
|
return b;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
Query page_query(std::optional<int> offset, std::optional<int> limit, int max_limit) {
|
||
|
|
if (offset && *offset < 0) bad("offset must be >= 0");
|
||
|
|
if (limit && (*limit < 1 || *limit > max_limit)) bad("limit must be 1.." + std::to_string(max_limit));
|
||
|
|
Query q;
|
||
|
|
if (offset) q.add("offset", *offset);
|
||
|
|
if (limit) q.add("limit", *limit);
|
||
|
|
return q;
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- guardrails ------------------------------------------------------------
|
||
|
|
|
||
|
|
Json GuardrailsService::list(std::optional<int> o, std::optional<int> l, const std::string& ws) const {
|
||
|
|
Query q = page_query(o, l);
|
||
|
|
if (!ws.empty() && !is_uuid(ws)) bad("workspace_id must be a uuid");
|
||
|
|
q.add_if("workspace_id", ws);
|
||
|
|
return run("listGuardrails", {}, q);
|
||
|
|
}
|
||
|
|
Json GuardrailsService::get(const std::string& id) const { return run("getGuardrail", {{"id", id}}); }
|
||
|
|
Json GuardrailsService::create(const Json& body) const {
|
||
|
|
check_body(body, "guardrail");
|
||
|
|
if (!body.has("name")) bad("guardrail needs a name");
|
||
|
|
return run("createGuardrail", {}, {}, body);
|
||
|
|
}
|
||
|
|
Json GuardrailsService::update(const std::string& id, const Json& body) const {
|
||
|
|
check_body(body, "guardrail update");
|
||
|
|
return run("updateGuardrail", {{"id", id}}, {}, body);
|
||
|
|
}
|
||
|
|
Json GuardrailsService::remove(const std::string& id) const { return run("deleteGuardrail", {{"id", id}}); }
|
||
|
|
Json GuardrailsService::key_assignments(const std::string& id, std::optional<int> o, std::optional<int> l) const {
|
||
|
|
return run("listGuardrailKeyAssignments", {{"id", id}}, page_query(o, l));
|
||
|
|
}
|
||
|
|
Json GuardrailsService::assign_keys(const std::string& id, const std::vector<std::string>& h) const {
|
||
|
|
return run("bulkAssignKeysToGuardrail", {{"id", id}}, {}, string_list_body("key_hashes", h, true));
|
||
|
|
}
|
||
|
|
Json GuardrailsService::unassign_keys(const std::string& id, const std::vector<std::string>& h) const {
|
||
|
|
return run("bulkUnassignKeysFromGuardrail", {{"id", id}}, {}, string_list_body("key_hashes", h, true));
|
||
|
|
}
|
||
|
|
Json GuardrailsService::member_assignments(const std::string& id, std::optional<int> o, std::optional<int> l) const {
|
||
|
|
return run("listGuardrailMemberAssignments", {{"id", id}}, page_query(o, l));
|
||
|
|
}
|
||
|
|
Json GuardrailsService::assign_members(const std::string& id, const std::vector<std::string>& u) const {
|
||
|
|
return run("bulkAssignMembersToGuardrail", {{"id", id}}, {}, string_list_body("member_user_ids", u));
|
||
|
|
}
|
||
|
|
Json GuardrailsService::unassign_members(const std::string& id, const std::vector<std::string>& u) const {
|
||
|
|
return run("bulkUnassignMembersFromGuardrail", {{"id", id}}, {}, string_list_body("member_user_ids", u));
|
||
|
|
}
|
||
|
|
Json GuardrailsService::all_key_assignments(std::optional<int> o, std::optional<int> l) const {
|
||
|
|
return run("listKeyAssignments", {}, page_query(o, l));
|
||
|
|
}
|
||
|
|
Json GuardrailsService::all_member_assignments(std::optional<int> o, std::optional<int> l) const {
|
||
|
|
return run("listMemberAssignments", {}, page_query(o, l));
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- BYOK ------------------------------------------------------------------
|
||
|
|
|
||
|
|
Json ByokService::list(std::optional<int> o, std::optional<int> l, const std::string& ws,
|
||
|
|
const std::string& provider) const {
|
||
|
|
Query q = page_query(o, l);
|
||
|
|
if (!ws.empty() && !is_uuid(ws)) bad("workspace_id must be a uuid");
|
||
|
|
q.add_if("workspace_id", ws);
|
||
|
|
q.add_if("provider", provider);
|
||
|
|
return run("listBYOKKeys", {}, q);
|
||
|
|
}
|
||
|
|
Json ByokService::get(const std::string& id) const { return run("getBYOKKey", {{"id", id}}); }
|
||
|
|
Json ByokService::create(const Json& body) const {
|
||
|
|
check_body(body, "BYOK credential");
|
||
|
|
if (!body.has("provider") || !body.has("key")) bad("BYOK credential needs provider and key");
|
||
|
|
return run("createBYOKKey", {}, {}, body);
|
||
|
|
}
|
||
|
|
Json ByokService::update(const std::string& id, const Json& body) const {
|
||
|
|
check_body(body, "BYOK update");
|
||
|
|
return run("updateBYOKKey", {{"id", id}}, {}, body);
|
||
|
|
}
|
||
|
|
Json ByokService::remove(const std::string& id) const { return run("deleteBYOKKey", {{"id", id}}); }
|
||
|
|
|
||
|
|
// --- workspaces ------------------------------------------------------------
|
||
|
|
|
||
|
|
bool WorkspacesService::valid_interval(const std::string& i) {
|
||
|
|
return i == "daily" || i == "weekly" || i == "monthly" || i == "lifetime";
|
||
|
|
}
|
||
|
|
Json WorkspacesService::list(std::optional<int> o, std::optional<int> l) const {
|
||
|
|
return run("listWorkspaces", {}, page_query(o, l));
|
||
|
|
}
|
||
|
|
Json WorkspacesService::get(const std::string& id) const { return run("getWorkspace", {{"id", id}}); }
|
||
|
|
Json WorkspacesService::create(const Json& body) const {
|
||
|
|
check_body(body, "workspace");
|
||
|
|
if (!body.has("name") || !body.has("slug")) bad("workspace needs name and slug");
|
||
|
|
return run("createWorkspace", {}, {}, body);
|
||
|
|
}
|
||
|
|
Json WorkspacesService::update(const std::string& id, const Json& body) const {
|
||
|
|
check_body(body, "workspace update");
|
||
|
|
return run("updateWorkspace", {{"id", id}}, {}, body);
|
||
|
|
}
|
||
|
|
Json WorkspacesService::remove(const std::string& id, bool confirm_default) const {
|
||
|
|
Query q;
|
||
|
|
if (confirm_default) q.add("confirm_default_workspace_deletion", true);
|
||
|
|
return run("deleteWorkspace", {{"id", id}}, q);
|
||
|
|
}
|
||
|
|
Json WorkspacesService::members(const std::string& id, std::optional<int> o, std::optional<int> l) const {
|
||
|
|
return run("listWorkspaceMembers", {{"id", id}}, page_query(o, l));
|
||
|
|
}
|
||
|
|
Json WorkspacesService::add_members(const std::string& id, const std::vector<std::string>& u) const {
|
||
|
|
return run("bulkAddWorkspaceMembers", {{"id", id}}, {}, string_list_body("user_ids", u));
|
||
|
|
}
|
||
|
|
Json WorkspacesService::remove_members(const std::string& id, const std::vector<std::string>& u) const {
|
||
|
|
return run("bulkRemoveWorkspaceMembers", {{"id", id}}, {}, string_list_body("user_ids", u));
|
||
|
|
}
|
||
|
|
Json WorkspacesService::budgets(const std::string& ref) const {
|
||
|
|
return run("listWorkspaceBudgets", {{"workspace_ref", ref}});
|
||
|
|
}
|
||
|
|
Json WorkspacesService::budget(const std::string& ref, const std::string& interval) const {
|
||
|
|
if (!valid_interval(interval)) bad("interval must be daily weekly monthly or lifetime");
|
||
|
|
return run("getWorkspaceBudget", {{"workspace_ref", ref}, {"interval", interval}});
|
||
|
|
}
|
||
|
|
Json WorkspacesService::set_budget(const std::string& ref, const std::string& interval, double limit_usd,
|
||
|
|
std::optional<bool> include_byok) const {
|
||
|
|
if (!valid_interval(interval)) bad("interval must be daily weekly monthly or lifetime");
|
||
|
|
if (!(limit_usd > 0)) bad("limit_usd must be greater than 0");
|
||
|
|
Json b = Json::object();
|
||
|
|
b.set("limit_usd", Json(limit_usd));
|
||
|
|
if (include_byok) b.set("include_byok_in_budgets", Json(*include_byok));
|
||
|
|
return run("upsertWorkspaceBudget", {{"workspace_ref", ref}, {"interval", interval}}, {}, b);
|
||
|
|
}
|
||
|
|
Json WorkspacesService::remove_budget(const std::string& ref, const std::string& interval) const {
|
||
|
|
if (!valid_interval(interval)) bad("interval must be daily weekly monthly or lifetime");
|
||
|
|
return run("deleteWorkspaceBudget", {{"workspace_ref", ref}, {"interval", interval}});
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- observability -----------------------------------------------------------
|
||
|
|
|
||
|
|
Json ObservabilityService::list(std::optional<int> o, std::optional<int> l, const std::string& ws) const {
|
||
|
|
Query q = page_query(o, l);
|
||
|
|
if (!ws.empty() && !is_uuid(ws)) bad("workspace_id must be a uuid");
|
||
|
|
q.add_if("workspace_id", ws);
|
||
|
|
return run("listObservabilityDestinations", {}, q);
|
||
|
|
}
|
||
|
|
Json ObservabilityService::get(const std::string& id) const {
|
||
|
|
return run("getObservabilityDestination", {{"id", id}});
|
||
|
|
}
|
||
|
|
Json ObservabilityService::create(const Json& body) const {
|
||
|
|
check_body(body, "observability destination");
|
||
|
|
if (!body.has("type") || !body.has("name") || !body.has("config"))
|
||
|
|
bad("observability destination needs type, name and config");
|
||
|
|
return run("createObservabilityDestination", {}, {}, body);
|
||
|
|
}
|
||
|
|
Json ObservabilityService::update(const std::string& id, const Json& body) const {
|
||
|
|
check_body(body, "observability update");
|
||
|
|
return run("updateObservabilityDestination", {{"id", id}}, {}, body);
|
||
|
|
}
|
||
|
|
Json ObservabilityService::remove(const std::string& id) const {
|
||
|
|
return run("deleteObservabilityDestination", {{"id", id}});
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- private endpoints ---------------------------------------------------------
|
||
|
|
|
||
|
|
Json PrivateEndpointsService::list() const { return run("listPrivateEndpoints"); }
|
||
|
|
Json PrivateEndpointsService::get(const std::string& id) const { return run("getPrivateEndpoint", {{"id", id}}); }
|
||
|
|
Json PrivateEndpointsService::create(const Json& body) const {
|
||
|
|
check_body(body, "private endpoint");
|
||
|
|
return run("createPrivateEndpoint", {}, {}, body);
|
||
|
|
}
|
||
|
|
Json PrivateEndpointsService::update(const std::string& id, const Json& body) const {
|
||
|
|
check_body(body, "private endpoint update");
|
||
|
|
return run("updatePrivateEndpoint", {{"id", id}}, {}, body);
|
||
|
|
}
|
||
|
|
Json PrivateEndpointsService::remove(const std::string& id, bool draft_only) const {
|
||
|
|
Query q;
|
||
|
|
if (draft_only) q.add("draft_only", true);
|
||
|
|
return run("deletePrivateEndpoint", {{"id", id}}, q);
|
||
|
|
}
|
||
|
|
Json PrivateEndpointsService::activate(const std::string& id) const { return run("activatePrivateEndpoint", {{"id", id}}); }
|
||
|
|
Json PrivateEndpointsService::enable(const std::string& id) const { return run("enablePrivateEndpoint", {{"id", id}}); }
|
||
|
|
Json PrivateEndpointsService::disable(const std::string& id) const { return run("disablePrivateEndpoint", {{"id", id}}); }
|
||
|
|
Json PrivateEndpointsService::set_pricing(const std::string& id, const Json& pricing) const {
|
||
|
|
check_body(pricing, "pricing");
|
||
|
|
Json b = Json::object();
|
||
|
|
b.set("pricing", pricing);
|
||
|
|
return run("updatePrivateEndpointPricing", {{"id", id}}, {}, b);
|
||
|
|
}
|
||
|
|
Json PrivateEndpointsService::validate(const std::string& id, const std::string& ws) const {
|
||
|
|
if (!ws.empty() && !is_uuid(ws)) bad("workspace_id must be a uuid");
|
||
|
|
if (ws.empty()) return run("validatePrivateEndpoint", {{"id", id}}, {}, Json::object());
|
||
|
|
Json b = Json::object();
|
||
|
|
b.set("workspace_id", Json(ws));
|
||
|
|
return run("validatePrivateEndpoint", {{"id", id}}, {}, b);
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- directory ---------------------------------------------------------------------
|
||
|
|
|
||
|
|
Json DirectoryService::external_users(std::optional<int> o, std::optional<int> l, const std::string& user,
|
||
|
|
bool include_inactive) const {
|
||
|
|
Query q = page_query(o, l);
|
||
|
|
q.add_if("user", user);
|
||
|
|
if (include_inactive) q.add("include_inactive", true);
|
||
|
|
return run("listExternalUsers", {}, q);
|
||
|
|
}
|
||
|
|
Json DirectoryService::external_user(const std::string& user) const { return run("getExternalUser", {{"user", user}}); }
|
||
|
|
Json DirectoryService::register_external_user(const std::string& user) const {
|
||
|
|
if (user.empty() || user.size() > 512 || user.find('\0') != std::string::npos)
|
||
|
|
bad("external user must be 1..512 characters without NUL");
|
||
|
|
Json b = Json::object();
|
||
|
|
b.set("user", Json(user));
|
||
|
|
return run("createExternalUser", {}, {}, b);
|
||
|
|
}
|
||
|
|
Json DirectoryService::set_external_user_active(const std::string& user, bool active) const {
|
||
|
|
Json b = Json::object();
|
||
|
|
b.set("is_active", Json(active));
|
||
|
|
return run("updateExternalUser", {{"user", user}}, {}, b);
|
||
|
|
}
|
||
|
|
Json DirectoryService::deactivate_external_user(const std::string& user) const {
|
||
|
|
return run("deleteExternalUser", {{"user", user}});
|
||
|
|
}
|
||
|
|
Json DirectoryService::organization_members(std::optional<int> o, std::optional<int> l) const {
|
||
|
|
return run("listOrganizationMembers", {}, page_query(o, l));
|
||
|
|
}
|
||
|
|
Json DirectoryService::organization_settings() const { return run("getOrganizationSettings"); }
|
||
|
|
Json DirectoryService::set_filtered_model_catalog(bool enabled) const {
|
||
|
|
Json b = Json::object();
|
||
|
|
b.set("is_filtered_model_catalog_enabled", Json(enabled));
|
||
|
|
return run("updateOrganizationSettings", {}, {}, b);
|
||
|
|
}
|
||
|
|
Json DirectoryService::scim_group_mappings(std::optional<int> o, std::optional<int> l) const {
|
||
|
|
return run("listScimGroupMappings", {}, page_query(o, l));
|
||
|
|
}
|
||
|
|
Json DirectoryService::scim_group_mapping(const std::string& id) const {
|
||
|
|
return run("getScimGroupMapping", {{"id", id}});
|
||
|
|
}
|
||
|
|
Json DirectoryService::create_scim_group_mapping(const Json& body) const {
|
||
|
|
check_body(body, "SCIM group mapping");
|
||
|
|
if (!body.has("scim_group_id") || !body.has("role")) bad("SCIM group mapping needs scim_group_id and role");
|
||
|
|
return run("createScimGroupMapping", {}, {}, body);
|
||
|
|
}
|
||
|
|
Json DirectoryService::update_scim_group_mapping(const std::string& id, const std::string& role) const {
|
||
|
|
if (role.empty()) bad("role must not be empty");
|
||
|
|
Json b = Json::object();
|
||
|
|
b.set("role", Json(role));
|
||
|
|
return run("updateScimGroupMapping", {{"id", id}}, {}, b);
|
||
|
|
}
|
||
|
|
Json DirectoryService::remove_scim_group_mapping(const std::string& id, bool keep_members) const {
|
||
|
|
Query q;
|
||
|
|
if (keep_members) q.add("keep_members", true);
|
||
|
|
return run("deleteScimGroupMapping", {{"id", id}}, q);
|
||
|
|
}
|
||
|
|
Json DirectoryService::scim_groups(std::optional<int> o, std::optional<int> l, const std::string& display_name,
|
||
|
|
const std::string& external_id) const {
|
||
|
|
Query q = page_query(o, l);
|
||
|
|
q.add_if("display_name", display_name);
|
||
|
|
q.add_if("external_id", external_id);
|
||
|
|
return run("listScimGroups", {}, q);
|
||
|
|
}
|
||
|
|
Json DirectoryService::start_scim_sync() const { return run("createScimSyncJob"); }
|
||
|
|
Json DirectoryService::scim_sync_job(const std::string& id) const { return run("getScimSyncJob", {{"id", id}}); }
|
||
|
|
|
||
|
|
// --- vault -----------------------------------------------------------------------------
|
||
|
|
|
||
|
|
Json VaultService::secrets(std::optional<int> o, std::optional<int> l) const {
|
||
|
|
return run("listVaultSecrets", {}, page_query(o, l));
|
||
|
|
}
|
||
|
|
Json VaultService::put_secret(const std::string& name, const std::string& value,
|
||
|
|
const std::vector<std::string>& hosts) const {
|
||
|
|
return run("storeVaultSecret", {{"name", name}}, {}, secret_body(value, hosts));
|
||
|
|
}
|
||
|
|
Json VaultService::delete_secret(const std::string& name) const { return run("deleteVaultSecret", {{"name", name}}); }
|
||
|
|
Json VaultService::intern_secrets(const std::string& intern, std::optional<int> o, std::optional<int> l) const {
|
||
|
|
return run("listInternVaultSecrets", {{"internId", intern}}, page_query(o, l));
|
||
|
|
}
|
||
|
|
Json VaultService::intern_effective_secrets(const std::string& intern, std::optional<int> o,
|
||
|
|
std::optional<int> l) const {
|
||
|
|
return run("listInternEffectiveVaultSecrets", {{"internId", intern}}, page_query(o, l));
|
||
|
|
}
|
||
|
|
Json VaultService::intern_connections(const std::string& intern) const {
|
||
|
|
return run("listInternConnections", {{"internId", intern}});
|
||
|
|
}
|
||
|
|
Json VaultService::put_intern_secret(const std::string& intern, const std::string& name,
|
||
|
|
const std::string& value, const std::vector<std::string>& hosts) const {
|
||
|
|
return run("storeInternVaultSecret", {{"internId", intern}, {"name", name}}, {}, secret_body(value, hosts));
|
||
|
|
}
|
||
|
|
Json VaultService::delete_intern_secret(const std::string& intern, const std::string& name) const {
|
||
|
|
return run("deleteInternVaultSecret", {{"internId", intern}, {"name", name}});
|
||
|
|
}
|
||
|
|
Json VaultService::copy_secrets_to_intern(const std::string& intern, const std::vector<std::string>& names) const {
|
||
|
|
return run("copyVaultSecretsToIntern", {{"internId", intern}}, {}, string_list_body("names", names));
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::openrouter
|