mirror of
https://github.com/khoaliber/dockhand.git
synced 2026-03-07 21:29:06 +00:00
Initial commit
This commit is contained in:
53
routes/api/containers/[id]/rename/+server.ts
Normal file
53
routes/api/containers/[id]/rename/+server.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { renameContainer, inspectContainer } from '$lib/server/docker';
|
||||
import { renameAutoUpdateSchedule } from '$lib/server/db';
|
||||
import { authorize } from '$lib/server/authorize';
|
||||
import { auditContainer } from '$lib/server/audit';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
export const POST: RequestHandler = async (event) => {
|
||||
const { params, request, url, cookies } = event;
|
||||
const auth = await authorize(cookies);
|
||||
|
||||
const envId = url.searchParams.get('env');
|
||||
const envIdNum = envId ? parseInt(envId) : undefined;
|
||||
|
||||
// Permission check with environment context (renaming requires create permission)
|
||||
if (auth.authEnabled && !await auth.can('containers', 'create', envIdNum)) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const { name } = await request.json();
|
||||
if (!name || typeof name !== 'string') {
|
||||
return json({ error: 'New name is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Get old container name before renaming
|
||||
let oldName = params.id;
|
||||
try {
|
||||
const details = await inspectContainer(params.id, envIdNum);
|
||||
oldName = details.Name?.replace(/^\//, '') || params.id;
|
||||
} catch {
|
||||
// Container might not exist or other error, use ID
|
||||
}
|
||||
|
||||
await renameContainer(params.id, name, envIdNum);
|
||||
|
||||
// Audit log
|
||||
await auditContainer(event, 'rename', params.id, name, envIdNum, { previousId: params.id, newName: name });
|
||||
|
||||
// Update schedule if exists
|
||||
try {
|
||||
await renameAutoUpdateSchedule(oldName, name, envIdNum);
|
||||
} catch (error) {
|
||||
console.error('Failed to update schedule name:', error);
|
||||
// Don't fail the rename if schedule update fails
|
||||
}
|
||||
|
||||
return json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error renaming container:', error);
|
||||
return json({ error: 'Failed to rename container' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user