@ -2,6 +2,7 @@ package handlers
import (
"bytes"
"errors"
"net/http"
"time"
@ -9,9 +10,23 @@ import (
"github.com/seaweedfs/seaweedfs/weed/admin/dash"
"github.com/seaweedfs/seaweedfs/weed/admin/view/app"
"github.com/seaweedfs/seaweedfs/weed/admin/view/layout"
"github.com/seaweedfs/seaweedfs/weed/credential"
"github.com/seaweedfs/seaweedfs/weed/glog"
)
func groupErrorToHTTPStatus ( err error ) int {
if errors . Is ( err , credential . ErrGroupNotFound ) {
return http . StatusNotFound
}
if errors . Is ( err , credential . ErrGroupAlreadyExists ) {
return http . StatusConflict
}
if errors . Is ( err , credential . ErrUserNotInGroup ) {
return http . StatusBadRequest
}
return http . StatusInternalServerError
}
type GroupHandlers struct {
adminServer * dash . AdminServer
}
@ -59,7 +74,7 @@ func (h *GroupHandlers) CreateGroup(w http.ResponseWriter, r *http.Request) {
group , err := h . adminServer . CreateGroup ( r . Context ( ) , req . Name )
if err != nil {
glog . Errorf ( "Failed to create group: %v" , err )
writeJSONError ( w , http . StatusInternalServerError , "Failed to create group: " + err . Error ( ) )
writeJSONError ( w , groupErrorToHTTPStatus ( err ) , "Failed to create group: " + err . Error ( ) )
return
}
writeJSON ( w , http . StatusOK , group )
@ -70,7 +85,7 @@ func (h *GroupHandlers) GetGroupDetails(w http.ResponseWriter, r *http.Request)
group , err := h . adminServer . GetGroupDetails ( r . Context ( ) , name )
if err != nil {
glog . Errorf ( "Failed to get group details: %v" , err )
writeJSONError ( w , http . StatusNotFound , "Group not found" )
writeJSONError ( w , groupErrorToHTTPStatus ( err ) , "Group not found" )
return
}
writeJSON ( w , http . StatusOK , group )
@ -80,7 +95,7 @@ func (h *GroupHandlers) DeleteGroup(w http.ResponseWriter, r *http.Request) {
name := mux . Vars ( r ) [ "name" ]
if err := h . adminServer . DeleteGroup ( r . Context ( ) , name ) ; err != nil {
glog . Errorf ( "Failed to delete group: %v" , err )
writeJSONError ( w , http . StatusInternalServerError , "Failed to delete group: " + err . Error ( ) )
writeJSONError ( w , groupErrorToHTTPStatus ( err ) , "Failed to delete group: " + err . Error ( ) )
return
}
writeJSON ( w , http . StatusOK , map [ string ] string { "message" : "Group deleted successfully" } )
@ -110,7 +125,7 @@ func (h *GroupHandlers) AddGroupMember(w http.ResponseWriter, r *http.Request) {
return
}
if err := h . adminServer . AddGroupMember ( r . Context ( ) , name , req . Username ) ; err != nil {
writeJSONError ( w , http . StatusInternalServerError , "Failed to add member: " + err . Error ( ) )
writeJSONError ( w , groupErrorToHTTPStatus ( err ) , "Failed to add member: " + err . Error ( ) )
return
}
writeJSON ( w , http . StatusOK , map [ string ] string { "message" : "Member added successfully" } )
@ -120,7 +135,7 @@ func (h *GroupHandlers) RemoveGroupMember(w http.ResponseWriter, r *http.Request
name := mux . Vars ( r ) [ "name" ]
username := mux . Vars ( r ) [ "username" ]
if err := h . adminServer . RemoveGroupMember ( r . Context ( ) , name , username ) ; err != nil {
writeJSONError ( w , http . StatusInternalServerError , "Failed to remove member: " + err . Error ( ) )
writeJSONError ( w , groupErrorToHTTPStatus ( err ) , "Failed to remove member: " + err . Error ( ) )
return
}
writeJSON ( w , http . StatusOK , map [ string ] string { "message" : "Member removed successfully" } )
@ -150,7 +165,7 @@ func (h *GroupHandlers) AttachGroupPolicy(w http.ResponseWriter, r *http.Request
return
}
if err := h . adminServer . AttachGroupPolicy ( r . Context ( ) , name , req . PolicyName ) ; err != nil {
writeJSONError ( w , http . StatusInternalServerError , "Failed to attach policy: " + err . Error ( ) )
writeJSONError ( w , groupErrorToHTTPStatus ( err ) , "Failed to attach policy: " + err . Error ( ) )
return
}
writeJSON ( w , http . StatusOK , map [ string ] string { "message" : "Policy attached successfully" } )
@ -160,7 +175,7 @@ func (h *GroupHandlers) DetachGroupPolicy(w http.ResponseWriter, r *http.Request
name := mux . Vars ( r ) [ "name" ]
policyName := mux . Vars ( r ) [ "policyName" ]
if err := h . adminServer . DetachGroupPolicy ( r . Context ( ) , name , policyName ) ; err != nil {
writeJSONError ( w , http . StatusInternalServerError , "Failed to detach policy: " + err . Error ( ) )
writeJSONError ( w , groupErrorToHTTPStatus ( err ) , "Failed to detach policy: " + err . Error ( ) )
return
}
writeJSON ( w , http . StatusOK , map [ string ] string { "message" : "Policy detached successfully" } )
@ -169,14 +184,18 @@ func (h *GroupHandlers) DetachGroupPolicy(w http.ResponseWriter, r *http.Request
func ( h * GroupHandlers ) SetGroupStatus ( w http . ResponseWriter , r * http . Request ) {
name := mux . Vars ( r ) [ "name" ]
var req struct {
Enabled bool ` json:"enabled" `
Enabled * bool ` json:"enabled" `
}
if err := decodeJSONBody ( newJSONMaxReader ( w , r ) , & req ) ; err != nil {
writeJSONError ( w , http . StatusBadRequest , "Invalid request: " + err . Error ( ) )
return
}
if err := h . adminServer . SetGroupStatus ( r . Context ( ) , name , req . Enabled ) ; err != nil {
writeJSONError ( w , http . StatusInternalServerError , "Failed to update group status: " + err . Error ( ) )
if req . Enabled == nil {
writeJSONError ( w , http . StatusBadRequest , "enabled field is required" )
return
}
if err := h . adminServer . SetGroupStatus ( r . Context ( ) , name , * req . Enabled ) ; err != nil {
writeJSONError ( w , groupErrorToHTTPStatus ( err ) , "Failed to update group status: " + err . Error ( ) )
return
}
writeJSON ( w , http . StatusOK , map [ string ] string { "message" : "Group status updated" } )