155 lines
5.1 KiB
Go
155 lines
5.1 KiB
Go
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) getRunnerPolicy(w http.ResponseWriter, r *http.Request) {
|
|
item, err := s.store.GetActiveRunnerPolicy(r.Context())
|
|
if err != nil {
|
|
s.logger.Error("get runner policy failed", "error", err)
|
|
writeError(w, http.StatusInternalServerError, "get runner policy failed")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, item)
|
|
}
|
|
|
|
func (s *Server) updateRunnerPolicy(w http.ResponseWriter, r *http.Request) {
|
|
var input store.RunnerPolicyInput
|
|
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid json body")
|
|
return
|
|
}
|
|
item, err := s.store.UpsertDefaultRunnerPolicy(r.Context(), input)
|
|
if err != nil {
|
|
s.logger.Error("update runner policy failed", "error", err)
|
|
writeError(w, http.StatusInternalServerError, "update runner policy failed")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, item)
|
|
}
|
|
|
|
type updatePlatformDynamicPriorityRequest struct {
|
|
DynamicPriority *int `json:"dynamicPriority"`
|
|
Reset bool `json:"reset"`
|
|
}
|
|
|
|
func (s *Server) updatePlatformDynamicPriority(w http.ResponseWriter, r *http.Request) {
|
|
var input updatePlatformDynamicPriorityRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid json body")
|
|
return
|
|
}
|
|
var dynamicPriority *int
|
|
if input.Reset {
|
|
dynamicPriority = nil
|
|
} else {
|
|
if input.DynamicPriority == nil {
|
|
writeError(w, http.StatusBadRequest, "dynamicPriority is required unless reset is true")
|
|
return
|
|
}
|
|
if *input.DynamicPriority < 0 {
|
|
writeError(w, http.StatusBadRequest, "dynamicPriority must be greater than or equal to 0")
|
|
return
|
|
}
|
|
dynamicPriority = input.DynamicPriority
|
|
}
|
|
item, err := s.store.UpdatePlatformDynamicPriority(r.Context(), r.PathValue("platformID"), dynamicPriority)
|
|
if err != nil {
|
|
if store.IsNotFound(err) {
|
|
writeError(w, http.StatusNotFound, "platform not found")
|
|
return
|
|
}
|
|
s.logger.Error("update platform dynamic priority failed", "error", err)
|
|
writeError(w, http.StatusInternalServerError, "update platform dynamic priority failed")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, item)
|
|
}
|
|
|
|
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) != ""
|
|
}
|