ai-wpa/app/api/services/download-kubernetes/route.ts

54 lines
1.9 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { authMiddleware } from '@/lib/auth-middleware'
export async function GET(request: NextRequest) {
try {
// Check authentication
const user = await authMiddleware(request)
if (!user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
// Get cluster ID from query parameters
const { searchParams } = new URL(request.url)
const clusterId = searchParams.get('cluster_id')
if (!clusterId) {
return NextResponse.json({ error: 'Cluster ID is required' }, { status: 400 })
}
// Mock Kubernetes configuration
const kubeConfig = `apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURCVENDQWUyZ0F3SUJBZ0lJRWRtTFUzZUNCUXN3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB5TkRBeE1EUXhOekF6TXpCYUZ3MHpOREF4TURFeE56QXpNekJhTUJVeApFekFSQmdOVkJBTVRDbXQxWW1WeWJtVjBaWE13Z2dFaU1BMEdDU3FHU0liM0RRRUJBUVVBQTRJQkR3QXdnZ0VLLQEKQVFJREFRQUI=
server: https://k8s-api.siliconpin.com:6443
name: ${clusterId}
contexts:
- context:
cluster: ${clusterId}
user: ${clusterId}-admin
name: ${clusterId}
current-context: ${clusterId}
kind: Config
preferences: {}
users:
- name: ${clusterId}-admin
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0t
client-key-data: LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQ==`
// Return the config as a downloadable file
return new NextResponse(kubeConfig, {
status: 200,
headers: {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename="kubeconfig-${clusterId}.yaml"`,
'Cache-Control': 'no-cache',
},
})
} catch (error) {
console.error('Download error:', error)
return NextResponse.json({ error: 'Download failed' }, { status: 500 })
}
}