feat: edit volume

This commit is contained in:
Nicolas Meienberger
2025-09-03 21:42:18 +02:00
parent ca4bd4a619
commit 91020e6f23
17 changed files with 790 additions and 319 deletions

View File

@@ -5,10 +5,13 @@ import {
createVolumeBody,
createVolumeDto,
deleteVolumeDto,
getVolumeDto,
type ListVolumesResponseDto,
listVolumesDto,
testConnectionBody,
testConnectionDto,
updateVolumeBody,
updateVolumeDto,
} from "./volume.dto";
import { volumeService } from "./volume.service";
@@ -18,8 +21,8 @@ export const volumeController = new Hono()
const response = {
volumes: volumes.map((volume) => ({
name: volume.name,
path: volume.path,
...volume,
updatedAt: volume.updatedAt.getTime(),
createdAt: volume.createdAt.getTime(),
})),
} satisfies ListVolumesResponseDto;
@@ -54,12 +57,47 @@ export const volumeController = new Hono()
return c.json({ message: "Volume deleted" });
})
.get("/:name", (c) => {
return c.json({ message: `Details of volume ${c.req.param("name")}` });
.get("/:name", getVolumeDto, async (c) => {
const { name } = c.req.param();
const res = await volumeService.getVolume(name);
if (res.error) {
const { message, status } = handleServiceError(res.error);
return c.json(message, status);
}
const response = {
name: res.volume.name,
path: res.volume.path,
type: res.volume.type,
createdAt: res.volume.createdAt.getTime(),
updatedAt: res.volume.updatedAt.getTime(),
config: res.volume.config,
};
return c.json(response, 200);
})
.put("/:name", (c) => {
return c.json({ message: `Update volume ${c.req.param("name")}` });
})
.delete("/:name", (c) => {
return c.json({ message: `Delete volume ${c.req.param("name")}` });
.put("/:name", updateVolumeDto, validator("json", updateVolumeBody), async (c) => {
const { name } = c.req.param();
const body = c.req.valid("json");
const res = await volumeService.updateVolume(name, body.config);
if (res.error) {
const { message, status } = handleServiceError(res.error);
return c.json(message, status);
}
const response = {
message: "Volume updated",
volume: {
name: res.volume.name,
path: res.volume.path,
type: res.volume.type,
createdAt: res.volume.createdAt.getTime(),
updatedAt: res.volume.updatedAt.getTime(),
config: res.volume.config,
},
};
return c.json(response, 200);
});

View File

@@ -3,15 +3,20 @@ import { type } from "arktype";
import { describeRoute } from "hono-openapi";
import { resolver } from "hono-openapi/arktype";
const volumeSchema = type({
name: "string",
path: "string",
type: "string",
createdAt: "number",
updatedAt: "number",
config: volumeConfigSchema,
});
/**
* List all volumes
*/
export const listVolumesResponse = type({
volumes: type({
name: "string",
path: "string",
createdAt: "number",
}).array(),
volumes: volumeSchema.array(),
});
export type ListVolumesResponseDto = typeof listVolumesResponse.infer;
@@ -90,6 +95,68 @@ export const deleteVolumeDto = describeRoute({
},
});
/**
* Get a volume
*/
export const getVolumeDto = describeRoute({
description: "Get a volume by name",
operationId: "getVolume",
validateResponse: true,
tags: ["Volumes"],
responses: {
200: {
description: "Volume details",
content: {
"application/json": {
schema: resolver(volumeSchema),
},
},
},
404: {
description: "Volume not found",
},
},
});
/**
* Update a volume
*/
export const updateVolumeBody = type({
config: volumeConfigSchema,
});
export const updateVolumeResponse = type({
message: "string",
volume: type({
name: "string",
path: "string",
type: "string",
createdAt: "number",
updatedAt: "number",
config: volumeConfigSchema,
}),
});
export const updateVolumeDto = describeRoute({
description: "Update a volume's configuration",
operationId: "updateVolume",
validateResponse: true,
tags: ["Volumes"],
responses: {
200: {
description: "Volume updated successfully",
content: {
"application/json": {
schema: resolver(updateVolumeResponse),
},
},
},
404: {
description: "Volume not found",
},
},
});
/**
* Test connection
*/

View File

@@ -86,6 +86,55 @@ const mountVolume = async (name: string) => {
}
};
const getVolume = async (name: string) => {
const volume = await db.query.volumesTable.findFirst({
where: eq(volumesTable.name, name),
});
if (!volume) {
return { error: new NotFoundError("Volume not found") };
}
return { volume };
};
const updateVolume = async (name: string, backendConfig: BackendConfig) => {
try {
const existing = await db.query.volumesTable.findFirst({
where: eq(volumesTable.name, name),
});
if (!existing) {
return { error: new NotFoundError("Volume not found") };
}
const oldBackend = createVolumeBackend(existing);
await oldBackend.unmount();
const updated = await db
.update(volumesTable)
.set({
config: backendConfig,
type: backendConfig.backend,
updatedAt: new Date(),
})
.where(eq(volumesTable.name, name))
.returning();
// Mount with new configuration
const newBackend = createVolumeBackend(updated[0]);
await newBackend.mount();
return { volume: updated[0] };
} catch (error) {
return {
error: new InternalServerError("Failed to update volume", {
cause: error,
}),
};
}
};
const testConnection = async (backendConfig: BackendConfig) => {
let tempDir: string | null = null;
@@ -134,5 +183,7 @@ export const volumeService = {
createVolume,
mountVolume,
deleteVolume,
getVolume,
updateVolume,
testConnection,
};