Garage UI : interface web d'administration pour Garage (API v2 + CLI)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
cedricandClaude Sonnet 5.5 committed 2026-11-20 20:18:24 +01:00
commit fdd5555100
36 files changed
+8486

No files matched your search

+179
View File
@@ -0,0 +1,179 @@
// Client de l'API : le serveur Go relaie /api/garage/v2/... vers l'API d'administration de Garage.
export class ApiError extends Error {
status: number;
constructor(status: number, message: string) {
super(message);
this.status = status;
}
}
async function req<T>(url: string, init: RequestInit = {}): Promise<T> {
const r = await fetch(url, {
...init,
credentials: "same-origin",
headers: { "X-Requested-With": "garage-ui", ...(init.body ? { "Content-Type": "application/json" } : {}) },
});
const text = await r.text();
let data: unknown = null;
try {
data = text ? JSON.parse(text) : null;
} catch {
data = text;
}
if (!r.ok) {
const d = data as { message?: string; error?: string } | string | null;
const msg = (d && typeof d === "object" ? d.message || d.error : d) || r.statusText;
throw new ApiError(r.status, String(msg));
}
return data as T;
}
const qs = (p?: Record<string, string | number | boolean | undefined>) => {
const u = new URLSearchParams();
for (const [k, v] of Object.entries(p ?? {})) if (v !== undefined && v !== "") u.set(k, String(v));
const s = u.toString();
return s ? `?${s}` : "";
};
type Params = Record<string, string | number | boolean | undefined>;
export const garage = {
get: <T>(op: string, params?: Params) => req<T>(`/api/garage/v2/${op}${qs(params)}`),
post: <T = unknown>(op: string, params?: Params, body?: unknown) =>
req<T>(`/api/garage/v2/${op}${qs(params)}`, { method: "POST", body: JSON.stringify(body ?? {}) }),
};
// --- Types (partiels : seuls les champs utilisés) ---------------------------------
export interface Perm {
read: boolean;
write: boolean;
owner: boolean;
}
export interface BucketListItem {
id: string;
created: string;
globalAliases: string[];
localAliases: { accessKeyId: string; alias: string }[];
}
export interface BucketKey {
accessKeyId: string;
name: string;
permissions: Perm;
bucketLocalAliases?: string[];
}
export interface BucketInfo extends BucketListItem {
websiteAccess: boolean;
websiteConfig?: { indexDocument?: string; errorDocument?: string } | null;
keys: BucketKey[];
objects: number;
bytes: number;
unfinishedUploads: number;
unfinishedMultipartUploads: number;
unfinishedMultipartUploadParts: number;
unfinishedMultipartUploadBytes: number;
quotas: { maxSize?: number | null; maxObjects?: number | null };
}
export interface KeyListItem {
id: string;
name: string;
created?: string;
expiration?: string | null;
expired?: boolean;
}
export interface KeyInfo {
accessKeyId: string;
name: string;
created?: string;
expiration?: string | null;
expired?: boolean;
secretAccessKey?: string | null;
permissions: { createBucket: boolean };
buckets: { id: string; globalAliases: string[]; localAliases: string[]; permissions: Perm }[];
}
export interface NodeInfo {
id: string;
role?: { zone: string; tags: string[]; capacity: number | null } | null;
addr?: string | null;
hostname?: string | null;
isUp: boolean;
lastSeenSecsAgo?: number | null;
draining?: boolean;
dataPartition?: { available: number; total: number } | null;
metadataPartition?: { available: number; total: number } | null;
garageVersion?: string | null;
}
export interface ClusterStatus {
layoutVersion: number;
nodes: NodeInfo[];
}
export interface ClusterHealth {
status: string;
knownNodes: number;
connectedNodes: number;
storageNodes: number;
storageNodesUp: number;
partitions: number;
partitionsQuorum: number;
partitionsAllOk: number;
}
export interface LayoutRole {
id: string;
zone: string;
tags: string[];
capacity: number | null;
}
export interface StagedRole {
id: string;
remove?: boolean;
zone?: string;
tags?: string[];
capacity?: number | null;
}
export interface ClusterLayout {
version: number;
roles: LayoutRole[];
parameters?: { zoneRedundancy?: unknown };
partitionSize?: number;
stagedRoleChanges: StagedRole[];
}
/** Réponse d'une opération diffusée aux nœuds (?node=*). */
export interface MultiResponse<T> {
success: Record<string, T>;
error: Record<string, string>;
}
export interface Config {
title: string;
version: string;
auth: boolean;
user: string;
cli: boolean;
s3Endpoint: string;
s3Region: string;
}
export const appApi = {
config: () => req<Config>("/api/config"),
login: (user: string, password: string) => req<{ user: string }>("/api/login", { method: "POST", body: JSON.stringify({ user, password }) }),
logout: () => req("/api/logout", { method: "POST", body: "{}" }),
cli: (args: string[]) => req<{ exitCode: number; output: string; ms: number }>("/api/cli", { method: "POST", body: JSON.stringify({ args }) }),
};
export const bucketName = (b: { id: string; globalAliases: string[]; localAliases: { alias: string }[] }) =>
b.globalAliases[0] ?? b.localAliases[0]?.alias ?? b.id.slice(0, 12);
export const errMsg = (e: unknown) => (e instanceof Error ? e.message : String(e));