| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package api
- import (
- "encoding/json"
- "net/http"
- "proxmox-router/internal/dhcp"
- )
- func (s *Server) handleListDHCPClients(w http.ResponseWriter, r *http.Request) {
- clients := s.dhcpMgr.ListClients()
- writeJSON(w, http.StatusOK, clients)
- }
- func (s *Server) handleGetDHCPSummary(w http.ResponseWriter, r *http.Request) {
- summary := s.dhcpMgr.GetSummary()
- writeJSON(w, http.StatusOK, summary)
- }
- func (s *Server) handleCreateDHCPClient(w http.ResponseWriter, r *http.Request) {
- var req dhcp.CreateDHCPClientRequest
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- client, err := s.dhcpMgr.AddClient(req)
- if err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusCreated, client)
- }
- func (s *Server) handleUpdateDHCPClient(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Client ID is required"})
- return
- }
- var req dhcp.UpdateDHCPClientRequest
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- client, err := s.dhcpMgr.UpdateClient(id, req)
- if err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, client)
- }
- func (s *Server) handleDeleteDHCPClient(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Client ID is required"})
- return
- }
- if err := s.dhcpMgr.DeleteClient(id); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
- }
- func (s *Server) handleToggleDHCPClientState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Client ID is required"})
- return
- }
- var payload struct {
- Enabled bool `json:"enabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- if err := s.dhcpMgr.ToggleState(id, payload.Enabled); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "updated"})
- }
- func (s *Server) handleRenewDHCPClient(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Client ID is required"})
- return
- }
- if err := s.dhcpMgr.RenewLease(id); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "renewed"})
- }
- func (s *Server) handleReleaseDHCPClient(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Client ID is required"})
- return
- }
- if err := s.dhcpMgr.ReleaseLease(id); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"status": "released"})
- }
|