chore: commit pending gateway changes
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/easyai/easyai-ai-gateway/apps/api/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) listRuntimePolicySets(w http.ResponseWriter, r *http.Request) {
|
||||
items, err := s.store.ListRuntimePolicySets(r.Context())
|
||||
if err != nil {
|
||||
s.logger.Error("list runtime policy sets failed", "error", err)
|
||||
writeError(w, http.StatusInternalServerError, "list runtime policy sets failed")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"items": items})
|
||||
}
|
||||
|
||||
func (s *Server) createRuntimePolicySet(w http.ResponseWriter, r *http.Request) {
|
||||
var input store.RuntimePolicySetInput
|
||||
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid json body")
|
||||
return
|
||||
}
|
||||
if !validRuntimePolicyInput(input) {
|
||||
writeError(w, http.StatusBadRequest, "policyKey and name are required")
|
||||
return
|
||||
}
|
||||
item, err := s.store.CreateRuntimePolicySet(r.Context(), input)
|
||||
if err != nil {
|
||||
if store.IsUniqueViolation(err) {
|
||||
writeError(w, http.StatusConflict, "runtime policy key already exists")
|
||||
return
|
||||
}
|
||||
s.logger.Error("create runtime policy set failed", "error", err)
|
||||
writeError(w, http.StatusInternalServerError, "create runtime policy set failed")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, item)
|
||||
}
|
||||
|
||||
func (s *Server) updateRuntimePolicySet(w http.ResponseWriter, r *http.Request) {
|
||||
var input store.RuntimePolicySetInput
|
||||
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid json body")
|
||||
return
|
||||
}
|
||||
if !validRuntimePolicyInput(input) {
|
||||
writeError(w, http.StatusBadRequest, "policyKey and name are required")
|
||||
return
|
||||
}
|
||||
item, err := s.store.UpdateRuntimePolicySet(r.Context(), r.PathValue("policySetID"), input)
|
||||
if err != nil {
|
||||
if store.IsNotFound(err) {
|
||||
writeError(w, http.StatusNotFound, "runtime policy set not found")
|
||||
return
|
||||
}
|
||||
if store.IsUniqueViolation(err) {
|
||||
writeError(w, http.StatusConflict, "runtime policy key already exists")
|
||||
return
|
||||
}
|
||||
s.logger.Error("update runtime policy set failed", "error", err)
|
||||
writeError(w, http.StatusInternalServerError, "update runtime policy set failed")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, item)
|
||||
}
|
||||
|
||||
func (s *Server) deleteRuntimePolicySet(w http.ResponseWriter, r *http.Request) {
|
||||
if err := s.store.DeleteRuntimePolicySet(r.Context(), r.PathValue("policySetID")); err != nil {
|
||||
if store.IsNotFound(err) {
|
||||
writeError(w, http.StatusNotFound, "runtime policy set not found")
|
||||
return
|
||||
}
|
||||
if errors.Is(err, store.ErrProtectedDefault) {
|
||||
writeError(w, http.StatusForbidden, "default runtime policy set cannot be deleted")
|
||||
return
|
||||
}
|
||||
s.logger.Error("delete runtime policy set failed", "error", err)
|
||||
writeError(w, http.StatusInternalServerError, "delete runtime policy set failed")
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func validRuntimePolicyInput(input store.RuntimePolicySetInput) bool {
|
||||
return strings.TrimSpace(input.PolicyKey) != "" && strings.TrimSpace(input.Name) != ""
|
||||
}
|
||||
Reference in New Issue
Block a user