Files
moloapp/src/openai/admin/governance.cpp
T
retoorandClaude Sonnet 5.5 9e550889cc feat(clients): openai admin: organization admin API, usage, costs, audit logs, webhooks
121-call table checked against the OpenAI OpenAPI document, admin-key-only
client, destructive guards, WebhookVerifier (Standard Webhooks HMAC-SHA256),
demo routes under /ai/openai/admin, local and opt-in live tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-24 07:43:17 +02:00

161 lines
5.7 KiB
C++

#include "openai/admin/governance.h"
#include "openai/admin/caps.h"
#include "openai/admin/validate.h"
namespace molo::openai::admin {
using J = AdminService::Json;
// ---- certificates ----
void Certificates::require_org(const char* what) const {
if (!project_id_.empty())
Validate::fail(std::string(what) + " exists at organization scope only");
}
J Certificates::list(const ListOptions& options) {
if (project_id_.empty()) return execute(Call::kCertificatesList, {}, options.to_query());
return execute(Call::kProjectsCertificatesList, {project_id_}, options.to_query());
}
J Certificates::get(const std::string& certificate_id, bool include_content) {
require_org("get certificate");
Query q;
if (include_content) q.set("include", "content");
return execute(Call::kCertificatesGet, {certificate_id}, q);
}
J Certificates::upload(const CertificateUpload& certificate) {
require_org("upload certificate");
Json body = certificate.to_json();
return execute(Call::kCertificatesCreate, {}, {}, &body);
}
J Certificates::rename(const std::string& certificate_id, const std::string& name) {
require_org("rename certificate");
Validate::text("name", name, AdminCaps::kMaxNameChars);
Json body = object_of({{"name", Json(name)}});
return execute(Call::kCertificatesUpdate, {certificate_id}, {}, &body);
}
J Certificates::destroy(const std::string& certificate_id, bool confirm) {
require_org("delete certificate");
return execute(Call::kCertificatesDelete, {certificate_id}, {}, nullptr, confirm);
}
J Certificates::toggle(Call org_call, Call project_call, const std::vector<std::string>& ids,
bool confirm) {
Validate::count("certificate_ids", ids.size(), AdminCaps::kMaxCertificateIds, false);
Json list = Json::array();
for (const std::string& id : ids) {
Validate::id("certificate id", id);
list.push(Json(id));
}
Json body = Json::object();
body.set("certificate_ids", list);
if (project_id_.empty()) return execute(org_call, {}, {}, &body, confirm);
return execute(project_call, {project_id_}, {}, &body, confirm);
}
J Certificates::activate(const std::vector<std::string>& certificate_ids) {
return toggle(Call::kCertificatesActivate, Call::kProjectsCertificatesActivate, certificate_ids,
false);
}
J Certificates::deactivate(const std::vector<std::string>& certificate_ids, bool confirm) {
return toggle(Call::kCertificatesDeactivate, Call::kProjectsCertificatesDeactivate,
certificate_ids, confirm);
}
// ---- spend alerts ----
std::vector<std::string> SpendAlerts::ids(const std::string& alert_id) const {
std::vector<std::string> out;
if (!project_id_.empty()) out.push_back(project_id_);
if (!alert_id.empty()) out.push_back(alert_id);
return out;
}
J SpendAlerts::list(const ListOptions& options) {
return execute(project_id_.empty() ? Call::kSpendAlertsList : Call::kProjectsSpendAlertsList,
ids(), options.to_query());
}
J SpendAlerts::get(const std::string& alert_id) {
Validate::id("alert_id", alert_id);
return execute(project_id_.empty() ? Call::kSpendAlertsGet : Call::kProjectsSpendAlertsGet,
ids(alert_id));
}
J SpendAlerts::create(const SpendAlertSpec& spec) {
Json body = spec.to_json();
return execute(project_id_.empty() ? Call::kSpendAlertsCreate : Call::kProjectsSpendAlertsCreate,
ids(), {}, &body);
}
J SpendAlerts::update(const std::string& alert_id, const SpendAlertSpec& spec) {
Validate::id("alert_id", alert_id);
Json body = spec.to_json();
return execute(project_id_.empty() ? Call::kSpendAlertsUpdate : Call::kProjectsSpendAlertsUpdate,
ids(alert_id), {}, &body);
}
J SpendAlerts::destroy(const std::string& alert_id, bool confirm) {
Validate::id("alert_id", alert_id);
return execute(project_id_.empty() ? Call::kSpendAlertsDelete : Call::kProjectsSpendAlertsDelete,
ids(alert_id), {}, nullptr, confirm);
}
// ---- organization policy ----
J OrganizationPolicies::spend_limit() { return execute(Call::kSpendLimitGet); }
J OrganizationPolicies::set_spend_limit(const SpendLimitSpec& limit) {
Json body = limit.to_json();
return execute(Call::kSpendLimitUpdate, {}, {}, &body);
}
J OrganizationPolicies::clear_spend_limit(bool confirm) {
return execute(Call::kSpendLimitDelete, {}, {}, nullptr, confirm);
}
J OrganizationPolicies::data_retention() { return execute(Call::kDataRetentionGet); }
J OrganizationPolicies::set_data_retention(const std::string& retention_type, bool confirm) {
Json body = DataRetention::organization(retention_type);
return execute(Call::kDataRetentionUpdate, {}, {}, &body, confirm);
}
// ---- external storage ----
J ExternalStorage::list(const ListOptions& options, const std::string& project_id) {
if (!options.before.empty())
Validate::fail("external storage lists take limit, after and order only");
Query q = options.to_query();
if (!project_id.empty()) {
Validate::id("project_id", project_id);
q.set("project_id", project_id);
}
return execute(Call::kExternalStorageList, {}, q);
}
J ExternalStorage::get(const std::string& external_storage_id) {
return execute(Call::kExternalStorageGet, {external_storage_id});
}
J ExternalStorage::create(const ExternalStorageSpec& spec) {
Json body = spec.to_json();
return execute(Call::kExternalStorageCreate, {}, {}, &body);
}
J ExternalStorage::validate(const std::string& external_storage_id) {
return execute(Call::kExternalStorageValidate, {external_storage_id});
}
J ExternalStorage::destroy(const std::string& external_storage_id, bool confirm) {
return execute(Call::kExternalStorageDelete, {external_storage_id}, {}, nullptr, confirm);
}
} // namespace molo::openai::admin