diff --git a/e2e/volume_ops_test.go b/e2e/volume_ops_test.go index 31fe5aa45..26442ee38 100644 --- a/e2e/volume_ops_test.go +++ b/e2e/volume_ops_test.go @@ -239,6 +239,19 @@ func TestVolumeOptions(t *testing.T) { r.NotNil(err) } + // test options that are settable and not settable + createReq.Options = nil + _, err = client.VolumeCreate(createReq) + r.Nil(err) + var optionReq api.VolOptionReq + settableKey := "afr.use-compound-fops" + optionReq.Options = map[string]string{settableKey: "on"} + r.Nil(client.VolumeSet(volname, optionReq)) + notSettableKey := "afr.consistent-io" + optionReq.Options = map[string]string{notSettableKey: "on"} + r.NotNil(client.VolumeSet(volname, optionReq)) + r.Nil(client.VolumeDelete(volname)) + // group option test cases groupOpKeys := []string{"profile.test"} for _, validKey := range groupOpKeys { diff --git a/glusterd2/commands/global/getglobaloptions.go b/glusterd2/commands/global/getglobaloptions.go index b4565e639..ebad2d93b 100644 --- a/glusterd2/commands/global/getglobaloptions.go +++ b/glusterd2/commands/global/getglobaloptions.go @@ -16,7 +16,7 @@ func getGlobalOptionsHandler(w http.ResponseWriter, r *http.Request) { c, err := cluster.GetCluster() // ErrClusterNotFound here implies that no global option has yet been explicitly set. Ignoring it. if err != nil && err != errors.ErrClusterNotFound { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, fmt.Sprintf("Problem retrieving cluster information from etcd store: %s", err.Error()), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, fmt.Sprintf("Problem retrieving cluster information from etcd store: %s", err.Error())) return } diff --git a/glusterd2/commands/global/setglobaloptions.go b/glusterd2/commands/global/setglobaloptions.go index 219da6ac8..6b1dae567 100644 --- a/glusterd2/commands/global/setglobaloptions.go +++ b/glusterd2/commands/global/setglobaloptions.go @@ -15,14 +15,14 @@ func setGlobalOptionsHandler(w http.ResponseWriter, r *http.Request) { var req api.GlobalOptionReq if err := restutils.UnmarshalRequest(r, &req); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed) return } c, err := cluster.GetCluster() // ErrClusterNotFound here implies that no global option has yet been explicitly set. Ignoring it. if err != nil && err != errors.ErrClusterNotFound { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, fmt.Sprintf("Problem retrieving cluster information from etcd store: %s", err.Error()), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, fmt.Sprintf("Problem retrieving cluster information from etcd store: %s", err.Error())) return } @@ -36,14 +36,14 @@ func setGlobalOptionsHandler(w http.ResponseWriter, r *http.Request) { } c.Options[k] = v } else { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, fmt.Sprintf("Invalid global option: %s", k), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, fmt.Sprintf("Invalid global option: %s", k)) continue } } if err := cluster.UpdateCluster(c); err != nil { restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, - fmt.Sprint("Failed to update store with cluster attributes %s", err.Error()), api.ErrCodeDefault) + fmt.Sprint("Failed to update store with cluster attributes %s", err.Error())) return } diff --git a/glusterd2/commands/peers/addpeer.go b/glusterd2/commands/peers/addpeer.go index dd0870069..af5bcb885 100644 --- a/glusterd2/commands/peers/addpeer.go +++ b/glusterd2/commands/peers/addpeer.go @@ -3,6 +3,7 @@ package peercommands import ( "fmt" "net/http" + "strings" "github.com/gluster/glusterd2/glusterd2/events" "github.com/gluster/glusterd2/glusterd2/gdctx" @@ -21,19 +22,27 @@ func addPeerHandler(w http.ResponseWriter, r *http.Request) { var req api.PeerAddReq if err := restutils.UnmarshalRequest(r, &req); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, err) return } + for key := range req.MetaData { + if strings.HasPrefix(key, "_") { + logger.WithField("metadata-key", key).Error("Key names starting with '_' are restricted in metadata field") + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Key names starting with '_' are restricted in metadata field") + return + } + } + if len(req.Addresses) < 1 { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrNoHostnamesPresent.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrNoHostnamesPresent) return } logger.WithField("addresses", req.Addresses).Debug("received request to add new peer with given addresses") p, _ := peer.GetPeerByAddrs(req.Addresses) if p != nil { - restutils.SendHTTPError(ctx, w, http.StatusConflict, fmt.Sprintf("Peer exists with given addresses (ID: %s)", p.ID.String()), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusConflict, fmt.Sprintf("Peer exists with given addresses (ID: %s)", p.ID.String())) return } @@ -42,14 +51,14 @@ func addPeerHandler(w http.ResponseWriter, r *http.Request) { remotePeerAddress, err := utils.FormRemotePeerAddress(req.Addresses[0]) if err != nil { logger.WithError(err).WithField("address", req.Addresses[0]).Error("failed to parse peer address") - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "failed to parse remote address", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "failed to parse remote address") return } // TODO: Try all addresses till the first one connects client, err := getPeerServiceClient(remotePeerAddress) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } defer client.conn.Close() @@ -62,12 +71,12 @@ func addPeerHandler(w http.ResponseWriter, r *http.Request) { rsp, err := client.JoinCluster(newconfig) if err != nil { logger.WithError(err).Error("sending Join request failed") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "failed to send join cluster request", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "failed to send join cluster request") return } else if Error(rsp.Err) != ErrNone { err = Error(rsp.Err) logger.WithError(err).Error("join request failed") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } logger = logger.WithField("peerid", rsp.PeerID) @@ -76,14 +85,25 @@ func addPeerHandler(w http.ResponseWriter, r *http.Request) { // Get the new peer information to reply back with newpeer, err := peer.GetPeer(rsp.PeerID) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "new peer was added, but could not find peer in store. Try again later.", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "new peer was added, but could not find peer in store. Try again later.") return } - newpeer.MetaData = req.MetaData + if p.MetaData == nil { + p.MetaData = make(map[string]string) + } + if req.Zone != "" { + newpeer.MetaData["_zone"] = req.Zone + } else { + newpeer.MetaData["_zone"] = string(p.ID) + } + for key, value := range req.MetaData { + newpeer.MetaData[key] = value + } + err = peer.AddOrUpdatePeer(newpeer) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Fail to add metadata to peer", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Fail to add metadata to peer") } resp := createPeerAddResp(newpeer) restutils.SendHTTPResponse(ctx, w, http.StatusCreated, resp) diff --git a/glusterd2/commands/peers/commands.go b/glusterd2/commands/peers/commands.go index 377a92926..418a38861 100644 --- a/glusterd2/commands/peers/commands.go +++ b/glusterd2/commands/peers/commands.go @@ -46,10 +46,19 @@ func (c *Command) Routes() route.Routes { ResponseType: utils.GetTypeString((*api.PeerAddResp)(nil)), HandlerFunc: addPeerHandler, }, + route.Route{ + Name: "EditPeer", + Method: "POST", + Pattern: "/peers/{peerid}", + Version: 1, + RequestType: utils.GetTypeString((*api.PeerEditReq)(nil)), + ResponseType: utils.GetTypeString((*api.PeerEditResp)(nil)), + HandlerFunc: editPeer, + }, } } // RegisterStepFuncs implements a required function for the Command interface func (c *Command) RegisterStepFuncs() { - return + registerPeerEditStepFuncs() } diff --git a/glusterd2/commands/peers/deletepeer.go b/glusterd2/commands/peers/deletepeer.go index 5a9de2bd5..8c95e2a0b 100644 --- a/glusterd2/commands/peers/deletepeer.go +++ b/glusterd2/commands/peers/deletepeer.go @@ -9,7 +9,6 @@ import ( restutils "github.com/gluster/glusterd2/glusterd2/servers/rest/utils" "github.com/gluster/glusterd2/glusterd2/store" "github.com/gluster/glusterd2/glusterd2/volume" - "github.com/gluster/glusterd2/pkg/api" "github.com/gluster/glusterd2/pkg/utils" "github.com/gorilla/mux" @@ -22,8 +21,8 @@ func deletePeerHandler(w http.ResponseWriter, r *http.Request) { logger := gdctx.GetReqLogger(ctx) id := mux.Vars(r)["peerid"] - if id == "" { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "peerid not present in the request", api.ErrCodeDefault) + if uuid.Parse(id) == nil { + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Invalid peer id passed") return } @@ -40,49 +39,49 @@ func deletePeerHandler(w http.ResponseWriter, r *http.Request) { p, err := peer.GetPeerF(id) if err != nil { logger.WithError(err).Error("failed to get peer") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "could not validate delete request", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "could not validate delete request") return } else if p == nil { logger.Debug("request denied, received request to remove unknown peer") - restutils.SendHTTPError(ctx, w, http.StatusNotFound, "peer not found in cluster", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, "peer not found in cluster") return } // You cannot remove yourself if id == gdctx.MyUUID.String() { logger.Debug("request denied, received request to delete self from cluster") - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "removing self is disallowed.", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "removing self is disallowed.") return } // Check if any volumes exist with bricks on this peer if exists, err := bricksExist(id); err != nil { logger.WithError(err).Error("failed to check if bricks exist on peer") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "could not validate delete request", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "could not validate delete request") return } else if exists { logger.Debug("request denied, peer has bricks") - restutils.SendHTTPError(ctx, w, http.StatusForbidden, "cannot delete peer, peer has bricks", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusForbidden, "cannot delete peer, peer has bricks") return } // Remove the peer details from the store if err := peer.DeletePeer(id); err != nil { logger.WithError(err).WithField("peer", id).Error("failed to remove peer from the store") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } remotePeerAddress, err := utils.FormRemotePeerAddress(p.PeerAddresses[0]) if err != nil { logger.WithError(err).WithField("address", p.PeerAddresses[0]).Error("failed to parse peer address") - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "failed to parse remote address", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "failed to parse remote address") return } client, err := getPeerServiceClient(remotePeerAddress) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } defer client.conn.Close() @@ -93,12 +92,12 @@ func deletePeerHandler(w http.ResponseWriter, r *http.Request) { rsp, err := client.LeaveCluster() if err != nil { logger.WithError(err).Error("sending Leave request failed") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "failed to send leave cluster request", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "failed to send leave cluster request") return } else if Error(rsp.Err) != ErrNone { err = Error(rsp.Err) logger.WithError(err).Error("leave request failed") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } logger.Debug("peer left cluster") diff --git a/glusterd2/commands/peers/editpeer.go b/glusterd2/commands/peers/editpeer.go new file mode 100644 index 000000000..83ff4b8e4 --- /dev/null +++ b/glusterd2/commands/peers/editpeer.go @@ -0,0 +1,145 @@ +package peercommands + +import ( + "net/http" + "strings" + + "github.com/gluster/glusterd2/glusterd2/gdctx" + "github.com/gluster/glusterd2/glusterd2/peer" + restutils "github.com/gluster/glusterd2/glusterd2/servers/rest/utils" + "github.com/gluster/glusterd2/glusterd2/transaction" + "github.com/gluster/glusterd2/pkg/api" + + "github.com/gorilla/mux" + "github.com/pborman/uuid" +) + +func editPeer(w http.ResponseWriter, r *http.Request) { + + ctx := r.Context() + logger := gdctx.GetReqLogger(ctx) + + var req api.PeerEditReq + if err := restutils.UnmarshalRequest(r, &req); err != nil { + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, err) + return + } + + peerID := mux.Vars(r)["peerid"] + if uuid.Parse(peerID) == nil { + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Invalid peerID passed in url") + return + } + + for key := range req.MetaData { + if strings.HasPrefix(key, "_") { + logger.WithField("metadata-key", key).Error("Key names starting with '_' are restricted in metadata field") + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Key names starting with '_' are restricted in metadata field") + return + } + } + + txn := transaction.NewTxn(ctx) + defer txn.Cleanup() + lock, unlock, err := transaction.CreateLockSteps(peerID) + if err != nil { + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) + return + } + + txn.Steps = []*transaction.Step{ + lock, + { + DoFunc: "peer-edit", + Nodes: []uuid.UUID{gdctx.MyUUID}, + }, + unlock, + } + err = txn.Ctx.Set("peerid", peerID) + if err != nil { + logger.WithError(err).WithField("key", "peerid").WithField("value", peerID).Error("Failed to set key in transaction context") + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) + return + } + err = txn.Ctx.Set("req", req) + if err != nil { + logger.WithError(err).WithField("key", "req").Error("Failed to set key in transaction context") + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) + return + } + err = txn.Do() + if err != nil { + logger.WithError(err).Error("Transaction to update peer failed") + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Transaction to update peer failed") + return + } + var peerInfo peer.Peer + if err := txn.Ctx.Get("peerInfo", &peerInfo); err != nil { + logger.WithError(err).WithField("key", "peerInfo").Error("Failed to get key from transaction context") + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Failed to get key from transaction context") + return + } + resp := createPeerEditResp(&peerInfo) + restutils.SendHTTPResponse(ctx, w, http.StatusCreated, resp) + +} + +func txnPeerEdit(c transaction.TxnCtx) error { + var peerID string + if err := c.Get("peerid", &peerID); err != nil { + c.Logger().WithError(err).WithField("key", "peerID").Error("Failed to get key from transaction context") + return err + } + + var req api.PeerEditReq + if err := c.Get("req", &req); err != nil { + c.Logger().WithError(err).WithField("key", "req").Error("Failed to get key from transaction context") + return err + } + peerInfo, err := peer.GetPeer(peerID) + if err != nil { + c.Logger().WithError(err).WithField("peerid", peerID).Error("Peer ID not found in store") + return err + } + for k, v := range req.MetaData { + if peerInfo.MetaData != nil { + peerInfo.MetaData[k] = v + } else { + peerInfo.MetaData = make(map[string]string) + peerInfo.MetaData[k] = v + } + } + err = peer.AddOrUpdatePeer(peerInfo) + if err != nil { + c.Logger().WithError(err).WithField("peerid", peerID).Error("Failed to update peer Info") + return err + } + err = c.Set("peerInfo", peerInfo) + if err != nil { + c.Logger().WithError(err).WithField("key", "peerInfo").Error("Failed to set key in transaction context") + return err + } + return nil +} + +func registerPeerEditStepFuncs() { + var sfs = []struct { + name string + sf transaction.StepFunc + }{ + {"peer-edit", txnPeerEdit}, + } + for _, sf := range sfs { + transaction.RegisterStepFunc(sf.sf, sf.name) + } +} + +func createPeerEditResp(p *peer.Peer) *api.PeerEditResp { + return &api.PeerEditResp{ + ID: p.ID, + Name: p.Name, + PeerAddresses: p.PeerAddresses, + ClientAddresses: p.ClientAddresses, + MetaData: p.MetaData, + } +} diff --git a/glusterd2/commands/peers/getpeer.go b/glusterd2/commands/peers/getpeer.go index eea48e3da..8a2ebdee6 100644 --- a/glusterd2/commands/peers/getpeer.go +++ b/glusterd2/commands/peers/getpeer.go @@ -8,6 +8,8 @@ import ( "github.com/gluster/glusterd2/glusterd2/store" "github.com/gluster/glusterd2/pkg/api" "github.com/gorilla/mux" + + "github.com/pborman/uuid" ) func getPeerHandler(w http.ResponseWriter, r *http.Request) { @@ -15,14 +17,14 @@ func getPeerHandler(w http.ResponseWriter, r *http.Request) { ctx := r.Context() id := mux.Vars(r)["peerid"] - if id == "" { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "peerid not present in request", api.ErrCodeDefault) + if uuid.Parse(id) == nil { + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Invalid peer id passed") return } peer, err := peer.GetPeerF(id) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, err) } resp := createPeerGetResp(peer) @@ -36,5 +38,6 @@ func createPeerGetResp(p *peer.Peer) *api.PeerGetResp { PeerAddresses: p.PeerAddresses, ClientAddresses: p.ClientAddresses, Online: store.Store.IsNodeAlive(p.ID), + MetaData: p.MetaData, } } diff --git a/glusterd2/commands/peers/getpeers.go b/glusterd2/commands/peers/getpeers.go index 81555f571..3eba4301d 100644 --- a/glusterd2/commands/peers/getpeers.go +++ b/glusterd2/commands/peers/getpeers.go @@ -15,7 +15,7 @@ func getPeersHandler(w http.ResponseWriter, r *http.Request) { peers, err := peer.GetPeersF() if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, err) } resp := createPeerListResp(peers) @@ -32,6 +32,7 @@ func createPeerListResp(peers []*peer.Peer) *api.PeerListResp { PeerAddresses: p.PeerAddresses, ClientAddresses: p.ClientAddresses, Online: store.Store.IsNodeAlive(p.ID), + MetaData: p.MetaData, }) } diff --git a/glusterd2/commands/volumes/bricks-status.go b/glusterd2/commands/volumes/bricks-status.go index 7e92afc11..26f82e0b6 100644 --- a/glusterd2/commands/volumes/bricks-status.go +++ b/glusterd2/commands/volumes/bricks-status.go @@ -71,7 +71,7 @@ func volumeBricksStatusHandler(w http.ResponseWriter, r *http.Request) { volname := mux.Vars(r)["volname"] vol, err := volume.GetVolume(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } @@ -92,7 +92,7 @@ func volumeBricksStatusHandler(w http.ResponseWriter, r *http.Request) { err = txn.Do() if err != nil { logger.WithError(err).WithField("volume", volname).Error("Failed to get volume status") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -100,7 +100,7 @@ func volumeBricksStatusHandler(w http.ResponseWriter, r *http.Request) { if err != nil { errMsg := "Failed to aggregate brick status results from multiple nodes." logger.WithField("error", err.Error()).Error("volumeStatusHandler:" + errMsg) - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, errMsg, api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, errMsg) return } diff --git a/glusterd2/commands/volumes/common.go b/glusterd2/commands/volumes/common.go index 39c32252e..3728506a7 100644 --- a/glusterd2/commands/volumes/common.go +++ b/glusterd2/commands/volumes/common.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "os" "path/filepath" @@ -35,6 +36,10 @@ func validateOptions(opts map[string]string) error { return err } + if !o.IsSettable() { + return fmt.Errorf("Option %s cannot be set", k) + } + if err := o.Validate(v); err != nil { return err } diff --git a/glusterd2/commands/volumes/optiongroup-create.go b/glusterd2/commands/volumes/optiongroup-create.go index 4081b2e04..701b79c5e 100644 --- a/glusterd2/commands/volumes/optiongroup-create.go +++ b/glusterd2/commands/volumes/optiongroup-create.go @@ -29,24 +29,24 @@ func optionGroupCreateHandler(w http.ResponseWriter, r *http.Request) { var req api.OptionGroupReq if err := restutils.UnmarshalRequest(r, &req); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed) return } if err := validateOptionSet(req); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, err) return } resp, err := store.Store.Get(context.TODO(), "groupoptions") if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } var groupOptions map[string][]api.VolumeOption if err := json.Unmarshal(resp.Kvs[0].Value, &groupOptions); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -58,11 +58,11 @@ func optionGroupCreateHandler(w http.ResponseWriter, r *http.Request) { groupOptionsJSON, err := json.Marshal(groupOptions) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if _, err := store.Store.Put(context.TODO(), "groupoptions", string(groupOptionsJSON)); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } diff --git a/glusterd2/commands/volumes/optiongroup-delete.go b/glusterd2/commands/volumes/optiongroup-delete.go index 873069ff2..7782105de 100644 --- a/glusterd2/commands/volumes/optiongroup-delete.go +++ b/glusterd2/commands/volumes/optiongroup-delete.go @@ -18,24 +18,24 @@ func optionGroupDeleteHandler(w http.ResponseWriter, r *http.Request) { resp, err := store.Store.Get(context.TODO(), "groupoptions") if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } var groupOptions map[string][]api.VolumeOption if err := json.Unmarshal(resp.Kvs[0].Value, &groupOptions); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } _, ok := groupOptions[groupName] if !ok { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "invalid group name specified", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "invalid group name specified") return } if _, ok := defaultGroupOptions[groupName]; ok { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "cannot delete builtin groups", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "cannot delete builtin groups") return } @@ -43,11 +43,11 @@ func optionGroupDeleteHandler(w http.ResponseWriter, r *http.Request) { groupOptionsJSON, err := json.Marshal(groupOptions) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if _, err := store.Store.Put(context.TODO(), "groupoptions", string(groupOptionsJSON)); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } diff --git a/glusterd2/commands/volumes/optiongroup-list.go b/glusterd2/commands/volumes/optiongroup-list.go index c5475f064..16e21d0bb 100644 --- a/glusterd2/commands/volumes/optiongroup-list.go +++ b/glusterd2/commands/volumes/optiongroup-list.go @@ -15,13 +15,13 @@ func optionGroupListHandler(w http.ResponseWriter, r *http.Request) { resp, err := store.Store.Get(context.TODO(), "groupoptions") if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } var groupOptions map[string][]api.VolumeOption if err := json.Unmarshal(resp.Kvs[0].Value, &groupOptions); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } diff --git a/glusterd2/commands/volumes/volfiles.go b/glusterd2/commands/volumes/volfiles.go index 3971bbaa5..3055f4305 100644 --- a/glusterd2/commands/volumes/volfiles.go +++ b/glusterd2/commands/volumes/volfiles.go @@ -5,7 +5,6 @@ import ( restutils "github.com/gluster/glusterd2/glusterd2/servers/rest/utils" volgen "github.com/gluster/glusterd2/glusterd2/volgen2" - "github.com/gluster/glusterd2/pkg/api" ) func volfilesGenerateHandler(w http.ResponseWriter, r *http.Request) { @@ -13,12 +12,12 @@ func volfilesGenerateHandler(w http.ResponseWriter, r *http.Request) { err := volgen.Generate() if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "unable to generate volfiles", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "unable to generate volfiles") return } volfiles, err := volgen.GetVolfiles() if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "unable to get list of volfiles", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "unable to get list of volfiles") return } restutils.SendHTTPResponse(ctx, w, http.StatusOK, volfiles) @@ -29,7 +28,7 @@ func volfilesListHandler(w http.ResponseWriter, r *http.Request) { volfiles, err := volgen.GetVolfiles() if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "unable to get list of volfiles", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "unable to get list of volfiles") return } restutils.SendHTTPResponse(ctx, w, http.StatusOK, volfiles) diff --git a/glusterd2/commands/volumes/volume-create.go b/glusterd2/commands/volumes/volume-create.go index 90827af62..e1f768c4d 100644 --- a/glusterd2/commands/volumes/volume-create.go +++ b/glusterd2/commands/volumes/volume-create.go @@ -197,31 +197,31 @@ func volumeCreateHandler(w http.ResponseWriter, r *http.Request) { httpStatus, err := unmarshalVolCreateRequest(req, r) if err != nil { logger.WithError(err).Error("Failed to unmarshal volume create request") - restutils.SendHTTPError(ctx, w, httpStatus, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, httpStatus, err) return } if volume.ExistsFunc(req.Name) { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, gderrors.ErrVolExists.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, gderrors.ErrVolExists) return } nodes, err := nodesFromVolumeCreateReq(req) if err != nil { logger.WithError(err).Error("could not prepare node list") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if req.Options, err = expandOptions(req.Options); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err := validateOptions(req.Options); err != nil { logger.WithField("option", err.Error()).Error("invalid volume option specified") msg := fmt.Sprintf("invalid volume option specified: %s", err.Error()) - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, msg, api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, msg) return } @@ -230,7 +230,7 @@ func volumeCreateHandler(w http.ResponseWriter, r *http.Request) { lock, unlock, err := transaction.CreateLockSteps(req.Name) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -256,20 +256,20 @@ func volumeCreateHandler(w http.ResponseWriter, r *http.Request) { vol, err := createVolinfo(req) if err != nil { logger.WithError(err).Error("failed to create volinfo") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err := validateXlatorOptions(req.Options, vol); err != nil { logger.WithError(err).Error("validation failed") - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, fmt.Sprintf("failed to set volume option: %s", err.Error()), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, fmt.Sprintf("failed to set volume option: %s", err.Error())) return } err = txn.Ctx.Set("bricks", vol.GetBricks()) if err != nil { logger.WithError(err).WithField("key", "bricks").Error("failed to set key in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -284,14 +284,14 @@ func volumeCreateHandler(w http.ResponseWriter, r *http.Request) { err = txn.Ctx.Set("brick-checks", &checks) if err != nil { logger.WithError(err).WithField("key", "brick-checks").Error("failed to set key in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } err = txn.Ctx.Set("volinfo", &vol) if err != nil { logger.WithError(err).WithField("key", "volinfo").Error("failed to set key in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -299,15 +299,15 @@ func volumeCreateHandler(w http.ResponseWriter, r *http.Request) { if err != nil { logger.WithError(err).Error("volume create transaction failed") if err == transaction.ErrLockTimeout { - restutils.SendHTTPError(ctx, w, http.StatusConflict, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusConflict, err) } else { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) } return } if err = txn.Ctx.Get("volinfo", &vol); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "failed to get volinfo", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "failed to get volinfo") return } diff --git a/glusterd2/commands/volumes/volume-delete.go b/glusterd2/commands/volumes/volume-delete.go index d45c7a83c..525915d3b 100644 --- a/glusterd2/commands/volumes/volume-delete.go +++ b/glusterd2/commands/volumes/volume-delete.go @@ -9,7 +9,6 @@ import ( "github.com/gluster/glusterd2/glusterd2/transaction" "github.com/gluster/glusterd2/glusterd2/volgen" "github.com/gluster/glusterd2/glusterd2/volume" - "github.com/gluster/glusterd2/pkg/api" "github.com/gorilla/mux" "github.com/pborman/uuid" @@ -68,12 +67,12 @@ func volumeDeleteHandler(w http.ResponseWriter, r *http.Request) { volname := mux.Vars(r)["volname"] volinfo, err := volume.GetVolume(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, err) return } if volinfo.State == volume.VolStarted { - restutils.SendHTTPError(ctx, w, http.StatusForbidden, "volume is not stopped", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusForbidden, "volume is not stopped") return } @@ -81,7 +80,7 @@ func volumeDeleteHandler(w http.ResponseWriter, r *http.Request) { defer txn.Cleanup() lock, unlock, err := transaction.CreateLockSteps(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -103,9 +102,9 @@ func volumeDeleteHandler(w http.ResponseWriter, r *http.Request) { logger.WithError(err).WithField( "volume", volname).Error("failed to delete the volume") if err == transaction.ErrLockTimeout { - restutils.SendHTTPError(ctx, w, http.StatusConflict, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusConflict, err) } else { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) } return } diff --git a/glusterd2/commands/volumes/volume-expand.go b/glusterd2/commands/volumes/volume-expand.go index bfaac60c1..1d7606208 100644 --- a/glusterd2/commands/volumes/volume-expand.go +++ b/glusterd2/commands/volumes/volume-expand.go @@ -195,13 +195,13 @@ func volumeExpandHandler(w http.ResponseWriter, r *http.Request) { volinfo, err := volume.GetVolume(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } var req api.VolExpandReq if err := restutils.UnmarshalRequest(r, &req); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed) return } @@ -219,24 +219,24 @@ func volumeExpandHandler(w http.ResponseWriter, r *http.Request) { } if newBrickCount%newReplicaCount != 0 { - restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, "Invalid number of bricks", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, "Invalid number of bricks") return } if volinfo.Type == volume.Replicate && req.ReplicaCount != 0 { // TODO: Only considered first sub volume's ReplicaCount if req.ReplicaCount < volinfo.Subvols[0].ReplicaCount { - restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, "Invalid number of bricks", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, "Invalid number of bricks") return } else if req.ReplicaCount == volinfo.Subvols[0].ReplicaCount { - restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, "Replica count is same", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, "Replica count is same") return } } lock, unlock, err := transaction.CreateLockSteps(volinfo.Name) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -246,7 +246,7 @@ func volumeExpandHandler(w http.ResponseWriter, r *http.Request) { nodes, err := nodesFromVolumeExpandReq(&req) if err != nil { logger.WithError(err).Error("could not prepare node list") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -280,12 +280,12 @@ func volumeExpandHandler(w http.ResponseWriter, r *http.Request) { newBricks, err := volume.NewBrickEntriesFunc(req.Bricks, volinfo.Name, volinfo.ID) if err != nil { logger.WithError(err).Error("failed to create new brick entries") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err := txn.Ctx.Set("bricks", newBricks); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -299,33 +299,33 @@ func volumeExpandHandler(w http.ResponseWriter, r *http.Request) { err = txn.Ctx.Set("brick-checks", &checks) if err != nil { logger.WithError(err).WithField("key", "brick-checks").Error("failed to set key in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err := txn.Ctx.Set("newreplicacount", newReplicaCount); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err := txn.Ctx.Set("oldvolinfo", volinfo); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err = txn.Do(); err != nil { logger.WithError(err).Error("volume expand transaction failed") if err == transaction.ErrLockTimeout { - restutils.SendHTTPError(ctx, w, http.StatusConflict, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusConflict, err) } else { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) } return } newvolinfo, err := volume.GetVolume(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } diff --git a/glusterd2/commands/volumes/volume-info.go b/glusterd2/commands/volumes/volume-info.go index 44d0b9b0b..f58205d63 100644 --- a/glusterd2/commands/volumes/volume-info.go +++ b/glusterd2/commands/volumes/volume-info.go @@ -18,7 +18,7 @@ func volumeInfoHandler(w http.ResponseWriter, r *http.Request) { volname := mux.Vars(r)["volname"] v, err := volume.GetVolume(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } diff --git a/glusterd2/commands/volumes/volume-list.go b/glusterd2/commands/volumes/volume-list.go index adcd59645..4b84a5455 100644 --- a/glusterd2/commands/volumes/volume-list.go +++ b/glusterd2/commands/volumes/volume-list.go @@ -14,7 +14,7 @@ func volumeListHandler(w http.ResponseWriter, r *http.Request) { volumes, err := volume.GetVolumes() if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, err) } resp := createVolumeListResp(volumes) diff --git a/glusterd2/commands/volumes/volume-option.go b/glusterd2/commands/volumes/volume-option.go index 7dac05c90..9dab24726 100644 --- a/glusterd2/commands/volumes/volume-option.go +++ b/glusterd2/commands/volumes/volume-option.go @@ -37,37 +37,37 @@ func volumeOptionsHandler(w http.ResponseWriter, r *http.Request) { volname := mux.Vars(r)["volname"] volinfo, err := volume.GetVolume(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrVolNotFound) return } var req api.VolOptionReq if err := restutils.UnmarshalRequest(r, &req); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed) return } var options map[string]string if options, err = expandOptions(req.Options); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err := validateOptions(options); err != nil { logger.WithError(err).Error("failed to set volume option") - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, fmt.Sprintf("failed to set volume option: %s", err.Error()), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, fmt.Sprintf("failed to set volume option: %s", err.Error())) return } if err := validateXlatorOptions(req.Options, volinfo); err != nil { logger.WithError(err).Error("validation failed") - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, fmt.Sprintf("failed to set volume option: %s", err.Error()), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, fmt.Sprintf("failed to set volume option: %s", err.Error())) return } lock, unlock, err := transaction.CreateLockSteps(volinfo.Name) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -76,7 +76,7 @@ func volumeOptionsHandler(w http.ResponseWriter, r *http.Request) { allNodes, err := peer.GetPeerIDs() if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -104,16 +104,16 @@ func volumeOptionsHandler(w http.ResponseWriter, r *http.Request) { if err := txn.Ctx.Set("volinfo", volinfo); err != nil { logger.WithError(err).Error("failed to set volinfo in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err := txn.Do(); err != nil { logger.WithError(err).Error("volume option transaction failed") if err == transaction.ErrLockTimeout { - restutils.SendHTTPError(ctx, w, http.StatusConflict, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusConflict, err) } else { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) } return } diff --git a/glusterd2/commands/volumes/volume-start.go b/glusterd2/commands/volumes/volume-start.go index 63300aa71..e1bf2a30e 100644 --- a/glusterd2/commands/volumes/volume-start.go +++ b/glusterd2/commands/volumes/volume-start.go @@ -8,7 +8,6 @@ import ( restutils "github.com/gluster/glusterd2/glusterd2/servers/rest/utils" "github.com/gluster/glusterd2/glusterd2/transaction" "github.com/gluster/glusterd2/glusterd2/volume" - "github.com/gluster/glusterd2/pkg/api" "github.com/gluster/glusterd2/pkg/errors" "github.com/gorilla/mux" @@ -89,11 +88,11 @@ func volumeStartHandler(w http.ResponseWriter, r *http.Request) { volname := mux.Vars(r)["volname"] vol, e := volume.GetVolume(volname) if e != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } if vol.State == volume.VolStarted { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrVolAlreadyStarted.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrVolAlreadyStarted) return } @@ -102,7 +101,7 @@ func volumeStartHandler(w http.ResponseWriter, r *http.Request) { defer txn.Cleanup() lock, unlock, err := transaction.CreateLockSteps(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -123,7 +122,7 @@ func volumeStartHandler(w http.ResponseWriter, r *http.Request) { "error": err.Error(), "volume": volname, }).Error("failed to start volume") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -131,7 +130,7 @@ func volumeStartHandler(w http.ResponseWriter, r *http.Request) { e = volume.AddOrUpdateVolumeFunc(vol) if e != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e) return } diff --git a/glusterd2/commands/volumes/volume-status.go b/glusterd2/commands/volumes/volume-status.go index 6a32e5f1c..dcd79d85d 100644 --- a/glusterd2/commands/volumes/volume-status.go +++ b/glusterd2/commands/volumes/volume-status.go @@ -19,19 +19,19 @@ func volumeStatusHandler(w http.ResponseWriter, r *http.Request) { v, err := volume.GetVolume(mux.Vars(r)["volname"]) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } if v.State != volume.VolStarted { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrVolNotStarted.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrVolNotStarted) return } s, err := volume.UsageInfo(v.Name) if err != nil { logger.WithError(err).WithField("volume", v.Name).Error("Failed to get volume size info") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Failed to get Volume size info", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Failed to get Volume size info") return } diff --git a/glusterd2/commands/volumes/volume-stop.go b/glusterd2/commands/volumes/volume-stop.go index 1a3e0eceb..1bc890939 100644 --- a/glusterd2/commands/volumes/volume-stop.go +++ b/glusterd2/commands/volumes/volume-stop.go @@ -10,7 +10,6 @@ import ( restutils "github.com/gluster/glusterd2/glusterd2/servers/rest/utils" "github.com/gluster/glusterd2/glusterd2/transaction" "github.com/gluster/glusterd2/glusterd2/volume" - "github.com/gluster/glusterd2/pkg/api" "github.com/gluster/glusterd2/pkg/errors" "github.com/gorilla/mux" @@ -86,11 +85,11 @@ func volumeStopHandler(w http.ResponseWriter, r *http.Request) { volname := mux.Vars(r)["volname"] vol, e := volume.GetVolume(volname) if e != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } if vol.State == volume.VolStopped { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrVolAlreadyStopped.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrVolAlreadyStopped) return } @@ -99,7 +98,7 @@ func volumeStopHandler(w http.ResponseWriter, r *http.Request) { defer txn.Cleanup() lock, unlock, err := transaction.CreateLockSteps(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } txn.Steps = []*transaction.Step{ @@ -116,9 +115,9 @@ func volumeStopHandler(w http.ResponseWriter, r *http.Request) { logger.WithError(err).WithField( "volume", volname).Error("failed to stop volume") if err == transaction.ErrLockTimeout { - restutils.SendHTTPError(ctx, w, http.StatusConflict, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusConflict, err) } else { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) } return } @@ -127,7 +126,7 @@ func volumeStopHandler(w http.ResponseWriter, r *http.Request) { e = volume.AddOrUpdateVolumeFunc(vol) if e != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e) return } events.Broadcast(newVolumeEvent(eventVolumeStopped, vol)) diff --git a/glusterd2/servers/rest/utils/utils.go b/glusterd2/servers/rest/utils/utils.go index fa91a53da..9a6b0232d 100644 --- a/glusterd2/servers/rest/utils/utils.go +++ b/glusterd2/servers/rest/utils/utils.go @@ -4,18 +4,13 @@ package utils import ( "context" "encoding/json" + "fmt" "net/http" "github.com/gluster/glusterd2/glusterd2/gdctx" "github.com/gluster/glusterd2/pkg/api" ) -// APIError is the placeholder for error string to report back to the client -type APIError struct { - Code api.ErrorCode `json:"error_code"` - Error string `json:"error"` -} - // UnmarshalRequest unmarshals JSON in `r` into `v` func UnmarshalRequest(r *http.Request, v interface{}) error { defer r.Body.Close() @@ -44,8 +39,14 @@ func SendHTTPResponse(ctx context.Context, w http.ResponseWriter, statusCode int } } -// SendHTTPError sends an error response to the client. -func SendHTTPError(ctx context.Context, w http.ResponseWriter, statusCode int, errMsg string, errCode api.ErrorCode) { +// SendHTTPError sends an error response to the client. The caller of this +// function can pass either the error or one or more error code(s) exported by +// api package. Example usage: +// SendHTTPError(ctx, http.StatusBadRequest, err) // Pass error as is +// SendHTTPError(ctx, http.StatusBadRequest, "", api.ErrorCode) // Specify error code +// SendHTTPError(ctx, http.StatusBadRequest, "custom error") // Pass specific error string +func SendHTTPError(ctx context.Context, w http.ResponseWriter, statusCode int, + err interface{}, errCodes ...api.ErrorCode) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("X-Gluster-Node-Id", gdctx.MyUUID.String()) @@ -53,7 +54,20 @@ func SendHTTPError(ctx context.Context, w http.ResponseWriter, statusCode int, e w.WriteHeader(statusCode) - resp := APIError{Code: errCode, Error: errMsg} + var resp api.ErrorResp + errMsg := fmt.Sprint(err) + if errMsg != "" || errMsg != "" || len(errCodes) == 0 { + resp.Errors = append(resp.Errors, api.HTTPError{ + Code: int(api.ErrCodeGeneric), + Message: errMsg}) + } else { + for _, code := range errCodes { + resp.Errors = append(resp.Errors, api.HTTPError{ + Code: int(code), + Message: api.ErrorCodeMap[code]}) + } + } + if err := json.NewEncoder(w).Encode(resp); err != nil { logger := gdctx.GetReqLogger(ctx) logger.WithError(err).Error("Failed to send the response -", resp) diff --git a/glusterd2/xlator/load.go b/glusterd2/xlator/load.go index 52a7438d9..6e3096a5b 100644 --- a/glusterd2/xlator/load.go +++ b/glusterd2/xlator/load.go @@ -60,7 +60,7 @@ func structifyOption(cOpt *C.volume_option_t) *options.Option { opt.DefaultValue = C.GoString(cOpt.default_value) opt.Description = C.GoString(cOpt.description) opt.ValidateType = options.OptionValidateType(cOpt.validate) - opt.Flags = uint32(cOpt.flags) + opt.Flags = options.OptionFlag(cOpt.flags) opt.SetKey = C.GoString(cOpt.setkey) opt.Level = options.OptionLevel(cOpt.level) diff --git a/glusterd2/xlator/options/options.go b/glusterd2/xlator/options/options.go index 97c78459d..8cc9d3ff2 100644 --- a/glusterd2/xlator/options/options.go +++ b/glusterd2/xlator/options/options.go @@ -90,12 +90,18 @@ type Option struct { ValidateType OptionValidateType OpVersion []uint32 Deprecated []uint32 - Flags uint32 + Flags OptionFlag Tags []string SetKey string Level OptionLevel } +// IsSettable returns true if the option can be set by a user, returns false +// otherwise. +func (o *Option) IsSettable() bool { + return (o.Flags & OptionFlagSettable) != 0 +} + // Validate checks if the given value string can be set as the value for the // Option. // Returns are error if it is not possible, nil otherwise. diff --git a/pkg/api/errors.go b/pkg/api/errors.go index a5042287d..df0b9ed5e 100644 --- a/pkg/api/errors.go +++ b/pkg/api/errors.go @@ -1,14 +1,26 @@ package api -// HTTPError represents HTTP error returned by glusterd2 +// HTTPError contains an error code and corresponding text which briefly +// describes the error in short. type HTTPError struct { - Error string `json:"Error"` + Code int `json:"code"` + Message string `json:"message"` +} + +// ErrorResp is an error response which may contain one or more error responses +type ErrorResp struct { + Errors []HTTPError `json:"errors"` } // ErrorCode represents API Error code Type type ErrorCode uint16 const ( - // ErrCodeDefault represents default error code for API responses - ErrCodeDefault ErrorCode = iota + 1 + // ErrCodeGeneric represents generic error code for API responses + ErrCodeGeneric ErrorCode = iota + 1 ) + +// ErrorCodeMap maps error code to it's textual message +var ErrorCodeMap = map[ErrorCode]string{ + ErrCodeGeneric: "generic error", +} diff --git a/pkg/api/peer_req_resp.go b/pkg/api/peer_req_resp.go index 5cd4f091d..4a04cbcbc 100644 --- a/pkg/api/peer_req_resp.go +++ b/pkg/api/peer_req_resp.go @@ -15,12 +15,21 @@ type Peer struct { // PeerAddReq represents an incoming request to add a peer to the cluster type PeerAddReq struct { Addresses []string `json:"addresses"` - MetaData map[string]string `json:"metadata"` + Zone string `json:"zone,omitempty"` + MetaData map[string]string `json:"metadata,omitempty"` +} + +// PeerEditReq represents an incoming request to edit metadata of peer +type PeerEditReq struct { + MetaData map[string]string `json:"metadata"` } // PeerAddResp is the success response sent to a PeerAddReq request type PeerAddResp Peer +// PeerEditResp is the success response sent to a MetaDataEditReq request +type PeerEditResp Peer + // PeerGetResp is the response sent for a peer get request type PeerGetResp Peer diff --git a/pkg/restclient/common.go b/pkg/restclient/common.go index 3f0da0ae5..949881cc8 100644 --- a/pkg/restclient/common.go +++ b/pkg/restclient/common.go @@ -35,12 +35,13 @@ func New(baseURL string, username string, password string, cacert string, insecu } func parseHTTPError(jsonData []byte) string { - var errstr api.HTTPError + var errstr api.ErrorResp err := json.Unmarshal(jsonData, &errstr) if err != nil { return "" } - return errstr.Error + // There's only a single error most of the times + return errstr.Errors[0].Message } func getAuthToken(username string, password string) string { diff --git a/plugins/bitrot/rest.go b/plugins/bitrot/rest.go index 8f27e5db8..6f9fec38c 100644 --- a/plugins/bitrot/rest.go +++ b/plugins/bitrot/rest.go @@ -9,7 +9,6 @@ import ( "github.com/gluster/glusterd2/glusterd2/transaction" "github.com/gluster/glusterd2/glusterd2/volume" "github.com/gluster/glusterd2/glusterd2/xlator" - "github.com/gluster/glusterd2/pkg/api" "github.com/gluster/glusterd2/pkg/errors" bitrotapi "github.com/gluster/glusterd2/plugins/bitrot/api" "github.com/gorilla/mux" @@ -29,19 +28,19 @@ func bitrotEnableHandler(w http.ResponseWriter, r *http.Request) { // Validate volume existence volinfo, err := volume.GetVolume(volName) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } // Check if volume is started if volinfo.State != volume.VolStarted { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrVolNotStarted.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrVolNotStarted) return } // Check if bitrot is already enabled if volume.IsBitrotEnabled(volinfo) { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrBitrotAlreadyEnabled.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrBitrotAlreadyEnabled) return } @@ -58,14 +57,14 @@ func bitrotEnableHandler(w http.ResponseWriter, r *http.Request) { if err := txn.Ctx.Set("volinfo", volinfo); err != nil { logger.WithError(err).Error("failed to set volinfo in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } //Lock on Volume Name lock, unlock, err := transaction.CreateLockSteps(volName) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -99,7 +98,7 @@ func bitrotEnableHandler(w http.ResponseWriter, r *http.Request) { "error": err.Error(), "volname": volName, }).Error("failed to enable bitrot") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } restutils.SendHTTPResponse(ctx, w, http.StatusOK, "Bitrot enabled successfully") @@ -116,13 +115,13 @@ func bitrotDisableHandler(w http.ResponseWriter, r *http.Request) { // Validate volume existence volinfo, err := volume.GetVolume(volName) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } // Check if bitrot is already disabled if !volume.IsBitrotEnabled(volinfo) { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrBitrotAlreadyDisabled.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrBitrotAlreadyDisabled) return } @@ -137,14 +136,14 @@ func bitrotDisableHandler(w http.ResponseWriter, r *http.Request) { if err := txn.Ctx.Set("volinfo", volinfo); err != nil { logger.WithError(err).Error("failed to set volinfo in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } //Lock on Volume Name lock, unlock, err := transaction.CreateLockSteps(volName) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -174,7 +173,7 @@ func bitrotDisableHandler(w http.ResponseWriter, r *http.Request) { "error": err.Error(), "volname": volName, }).Error("failed to disable bitrot") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -192,19 +191,19 @@ func bitrotScrubOndemandHandler(w http.ResponseWriter, r *http.Request) { // Validate volume existence volinfo, err := volume.GetVolume(volName) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } // Check if volume is started if volinfo.State != volume.VolStarted { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrVolNotStarted.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrVolNotStarted) return } // Check if bitrot is disabled if !volume.IsBitrotEnabled(volinfo) { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrBitrotNotEnabled.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrBitrotNotEnabled) return } @@ -215,7 +214,7 @@ func bitrotScrubOndemandHandler(w http.ResponseWriter, r *http.Request) { //Lock on Volume Name lock, unlock, err := transaction.CreateLockSteps(volName) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -236,7 +235,7 @@ func bitrotScrubOndemandHandler(w http.ResponseWriter, r *http.Request) { "error": err.Error(), "volname": volName, }).Error("failed to start scrubber") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } restutils.SendHTTPResponse(ctx, w, http.StatusOK, "Scrubber started successfully") @@ -254,21 +253,21 @@ func bitrotScrubStatusHandler(w http.ResponseWriter, r *http.Request) { volinfo, err := volume.GetVolume(volName) if err != nil { restutils.SendHTTPError(ctx, w, http.StatusNotFound, - errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + errors.ErrVolNotFound) return } // Check if volume is started if volinfo.State != volume.VolStarted { restutils.SendHTTPError(ctx, w, http.StatusBadRequest, - errors.ErrVolNotStarted.Error(), api.ErrCodeDefault) + errors.ErrVolNotStarted) return } // Check if bitrot is disabled if !volume.IsBitrotEnabled(volinfo) { restutils.SendHTTPError(ctx, w, http.StatusBadRequest, - errors.ErrBitrotNotEnabled.Error(), api.ErrCodeDefault) + errors.ErrBitrotNotEnabled) return } @@ -280,7 +279,7 @@ func bitrotScrubStatusHandler(w http.ResponseWriter, r *http.Request) { lock, unlock, err := transaction.CreateLockSteps(volName) if err != nil { restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, - err.Error(), api.ErrCodeDefault) + err) return } @@ -306,7 +305,7 @@ func bitrotScrubStatusHandler(w http.ResponseWriter, r *http.Request) { "volname": volName, }).Error("failed to get scrubber status") restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, - err.Error(), api.ErrCodeDefault) + err) return } @@ -315,7 +314,7 @@ func bitrotScrubStatusHandler(w http.ResponseWriter, r *http.Request) { errMsg := "Failed to aggregate scrub status results from multiple nodes." logger.WithField("error", err.Error()).Error("bitrotScrubStatusHandler:" + errMsg) restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, - errMsg, api.ErrCodeDefault) + errMsg) return } restutils.SendHTTPResponse(ctx, w, http.StatusOK, result) diff --git a/plugins/device/api/req.go b/plugins/device/api/req.go index 1e2deabc8..655a1027d 100644 --- a/plugins/device/api/req.go +++ b/plugins/device/api/req.go @@ -16,7 +16,7 @@ type AddDeviceReq struct { Devices []string `json:"devices"` } -// PeerEditGroupReq structure -type PeerEditGroupReq struct { - Group string `json:"group"` +// PeerEditZoneReq structure +type PeerEditZoneReq struct { + Zone string `json:"zone"` } diff --git a/plugins/device/api/resp.go b/plugins/device/api/resp.go index 67a5e71ca..2bf61983f 100644 --- a/plugins/device/api/resp.go +++ b/plugins/device/api/resp.go @@ -13,5 +13,5 @@ type Info struct { // AddDeviceResp is the success response sent to a AddDeviceReq request type AddDeviceResp api.Peer -// PeerEditGroupResp is the success response sent to a EditGroup request -type PeerEditGroupResp api.Peer +// PeerEditZoneResp is the success response sent to a EditGroup request +type PeerEditZoneResp api.Peer diff --git a/plugins/device/init.go b/plugins/device/init.go index 1ef83ca35..6d32e609c 100644 --- a/plugins/device/init.go +++ b/plugins/device/init.go @@ -35,13 +35,13 @@ func (p *Plugin) RestRoutes() route.Routes { HandlerFunc: deviceAddHandler, }, route.Route{ - Name: "PeerEditGroup", + Name: "PeerEditZone", Method: "POST", - Pattern: "/peers/{peerid}/group", + Pattern: "/peers/{peerid}/zone", Version: 1, - RequestType: utils.GetTypeString((*deviceapi.PeerEditGroupReq)(nil)), - ResponseType: utils.GetTypeString((*deviceapi.PeerEditGroupResp)(nil)), - HandlerFunc: peerEditGroupHandler, + RequestType: utils.GetTypeString((*deviceapi.PeerEditZoneReq)(nil)), + ResponseType: utils.GetTypeString((*deviceapi.PeerEditZoneResp)(nil)), + HandlerFunc: peerEditZoneHandler, }, } } @@ -50,5 +50,4 @@ func (p *Plugin) RestRoutes() route.Routes { // Glusterd Transaction framework func (p *Plugin) RegisterStepFuncs() { transaction.RegisterStepFunc(txnPrepareDevice, "prepare-device") - transaction.RegisterStepFunc(txnPeerEditGroup, "peer-edit-group") } diff --git a/plugins/device/rest.go b/plugins/device/rest.go index f907f51d1..b3a2201da 100644 --- a/plugins/device/rest.go +++ b/plugins/device/rest.go @@ -18,27 +18,31 @@ func deviceAddHandler(w http.ResponseWriter, r *http.Request) { ctx := r.Context() logger := gdctx.GetReqLogger(ctx) + peerID := mux.Vars(r)["peerid"] + if uuid.Parse(peerID) == nil { + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Invalid peer-id passed in url") + return + } req := new(deviceapi.AddDeviceReq) if err := restutils.UnmarshalRequest(r, req); err != nil { - logger.WithError(err).Error("Failed to unmarshal request") - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Unable to unmarshal request", api.ErrCodeDefault) - return - } - peerID := mux.Vars(r)["peerid"] - if uuid.Parse(peerID) == nil { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Invalid peer id passed in request", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, err) return } + peerInfo, err := peer.GetPeer(peerID) if err != nil { - logger.WithError(err).WithField("peerid", peerID).Error("Peer ID not found in store") - restutils.SendHTTPError(ctx, w, http.StatusNotFound, "Peer Id not found in store", api.ErrCodeDefault) + logger.WithError(err).WithField("peerid", peerID).Error("Peer-id not found in store") + restutils.SendHTTPError(ctx, w, http.StatusNotFound, "Peer-id not found in store") return } txn := transaction.NewTxn(ctx) defer txn.Cleanup() - lock, unlock, err := transaction.CreateLockSteps(peerInfo.ID.String()) + lock, unlock, err := transaction.CreateLockSteps(peerID) + if err != nil { + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) + return + } txn.Nodes = []uuid.UUID{peerInfo.ID} txn.Steps = []*transaction.Step{ lock, @@ -50,75 +54,85 @@ func deviceAddHandler(w http.ResponseWriter, r *http.Request) { } err = txn.Ctx.Set("peerid", peerID) if err != nil { - logger.WithError(err).WithField("peerid", peerID).Error("Failed to set peerid in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + logger.WithError(err).WithField("key", "peerid").WithField("value", peerID).Error("Failed to set key in transaction context") + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } err = txn.Ctx.Set("req", req) if err != nil { - logger.WithError(err).WithField("req-key", req).Error("Failed to set unmarshalled request information in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + logger.WithError(err).WithField("key", "req").Error("Failed to set key in transaction context") + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } err = txn.Do() if err != nil { logger.WithError(err).Error("Transaction to prepare device failed") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Transaction to prepare device failed", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Transaction to prepare device failed") return } peerInfo, err = peer.GetPeer(peerID) if err != nil { logger.WithError(err).WithField("peerid", peerID).Error("Failed to get peer from store") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Failed to get peer from store", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Failed to get peer from store") return } restutils.SendHTTPResponse(ctx, w, http.StatusOK, peerInfo) } -func peerEditGroupHandler(w http.ResponseWriter, r *http.Request) { +func peerEditZoneHandler(w http.ResponseWriter, r *http.Request) { ctx := r.Context() logger := gdctx.GetReqLogger(ctx) - req := new(deviceapi.PeerEditGroupReq) - if err := restutils.UnmarshalRequest(r, req); err != nil { - logger.WithError(err).Error("Failed to Unmarshal request") - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Unable to unmarshal request", api.ErrCodeDefault) + peerID := mux.Vars(r)["peerid"] + if uuid.Parse(peerID) == nil { + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Invalid peer id passed in request url") return } - peerID := mux.Vars(r)["peerid"] - if uuid.Parse(peerID) == nil { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Invalid peer id passed in request", api.ErrCodeDefault) + req := new(deviceapi.PeerEditZoneReq) + if err := restutils.UnmarshalRequest(r, req); err != nil { + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, err) return } + txn := transaction.NewTxn(ctx) defer txn.Cleanup() lock, unlock, err := transaction.CreateLockSteps(peerID) + if err != nil { + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) + return + } txn.Steps = []*transaction.Step{ lock, { - DoFunc: "peer-edit-group", + DoFunc: "peer-edit", Nodes: []uuid.UUID{gdctx.MyUUID}, }, unlock, } + err = txn.Ctx.Set("peerid", peerID) if err != nil { - logger.WithError(err).WithField("PeerID", peerID).Error("Failed to set peerid data in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + logger.WithError(err).WithField("key", "peerid").WithField("value", peerID).Error("Failed to set key in transaction context") + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } - err = txn.Ctx.Set("req", req) + + var editPeerReq api.PeerEditReq + editPeerReq.MetaData = make(map[string]string) + editPeerReq.MetaData["_zone"] = req.Zone + + err = txn.Ctx.Set("req", editPeerReq) if err != nil { - logger.WithError(err).WithField("req", req).Error("Failed to set unmarshalled request data in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + logger.WithError(err).WithField("key", "req").Error("Failed to set key in transaction context") + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } err = txn.Do() if err != nil { - logger.WithError(err).Error("Transaction to edit group failed") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Transaction to edit group failed", api.ErrCodeDefault) + logger.WithError(err).Error("Transaction to edit zone failed") + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Transaction to edit zone failed") return } restutils.SendHTTPResponse(ctx, w, http.StatusOK, nil) diff --git a/plugins/device/store-utils.go b/plugins/device/store-utils.go index 9903165c3..02609e16e 100644 --- a/plugins/device/store-utils.go +++ b/plugins/device/store-utils.go @@ -40,7 +40,7 @@ func AddDevices(devices []deviceapi.Info, peerID string) error { if err != nil { return err } - peerInfo.MetaData["devices"] = string(deviceJSON) + peerInfo.MetaData["_devices"] = string(deviceJSON) err = peer.AddOrUpdatePeer(peerInfo) if err != nil { return err diff --git a/plugins/device/transaction.go b/plugins/device/transaction.go index 54cb48fb4..71b110525 100644 --- a/plugins/device/transaction.go +++ b/plugins/device/transaction.go @@ -4,7 +4,6 @@ import ( "os/exec" "strings" - "github.com/gluster/glusterd2/glusterd2/peer" "github.com/gluster/glusterd2/glusterd2/transaction" deviceapi "github.com/gluster/glusterd2/plugins/device/api" @@ -16,12 +15,13 @@ func txnPrepareDevice(c transaction.TxnCtx) error { var peerID uuid.UUID var req deviceapi.AddDeviceReq var deviceList []deviceapi.Info + if err := c.Get("peerid", peerID); err != nil { - c.Logger().WithError(err).Error("Failed transaction, cannot find peer-id") + c.Logger().WithError(err).WithField("key", "peerid").Error("Failed to get key from transaction context") return err } if err := c.Get("req", req); err != nil { - c.Logger().WithError(err).Error("Failed transaction, cannot find device-details") + c.Logger().WithError(err).WithField("key", "req").Error("Failed to get key from transaction context") return err } for _, name := range req.Devices { @@ -52,29 +52,3 @@ func txnPrepareDevice(c transaction.TxnCtx) error { } return nil } - -func txnPeerEditGroup(c transaction.TxnCtx) error { - - var peerID string - if err := c.Get("peerid", peerID); err != nil { - c.Logger().WithError(err).WithField("PeerID", peerID).Error("Failed transaction, cannot find peer-id") - return err - } - var req deviceapi.PeerEditGroupReq - if err := c.Get("req", req); err != nil { - c.Logger().WithError(err).WithField("req", req).Error("Failed transaction, cannot find req") - return err - } - peerInfo, err := peer.GetPeer(peerID) - if err != nil { - c.Logger().WithError(err).WithField("peerid", peerID).Error("Peer ID not found in store") - return err - } - peerInfo.MetaData["_group"] = req.Group - err = peer.AddOrUpdatePeer(peerInfo) - if err != nil { - c.Logger().WithError(err).WithField("GroupID", req.Group).WithField("peerid", peerID).Error("Failed to update peer Info") - return err - } - return nil -} diff --git a/plugins/events/rest.go b/plugins/events/rest.go index 504eef2b9..a1ab486a7 100644 --- a/plugins/events/rest.go +++ b/plugins/events/rest.go @@ -1,11 +1,11 @@ package events import ( - "github.com/gluster/glusterd2/pkg/errors" "net/http" + "github.com/gluster/glusterd2/pkg/errors" + restutils "github.com/gluster/glusterd2/glusterd2/servers/rest/utils" - "github.com/gluster/glusterd2/pkg/api" eventsapi "github.com/gluster/glusterd2/plugins/events/api" ) @@ -20,12 +20,12 @@ func webhookAddHandler(w http.ResponseWriter, r *http.Request) { if err := restutils.UnmarshalRequest(r, &req); err != nil { restutils.SendHTTPError( ctx, w, http.StatusUnprocessableEntity, - errors.ErrJSONParsingFailed.Error(), api.ErrCodeDefault) + errors.ErrJSONParsingFailed) return } if req.URL == "" { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Webhook Url is required field", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Webhook Url is required field") return } @@ -34,18 +34,18 @@ func webhookAddHandler(w http.ResponseWriter, r *http.Request) { if err != nil { restutils.SendHTTPError( ctx, w, http.StatusInternalServerError, - "Could not check if webhook already exists", api.ErrCodeDefault) + "Could not check if webhook already exists") return } if exists { - restutils.SendHTTPError(ctx, w, http.StatusConflict, "Webhook already exists", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusConflict, "Webhook already exists") return } if err := addWebhook(req); err != nil { restutils.SendHTTPError( ctx, w, http.StatusInternalServerError, - "Could not add webhook", api.ErrCodeDefault) + "Could not add webhook") return } @@ -58,12 +58,12 @@ func webhookDeleteHandler(w http.ResponseWriter, r *http.Request) { var req eventsapi.Webhook if err := restutils.UnmarshalRequest(r, &req); err != nil { restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, - errors.ErrJSONParsingFailed.Error(), api.ErrCodeDefault) + errors.ErrJSONParsingFailed) return } if req.URL == "" { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Webhook Url is required field", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Webhook Url is required field") return } @@ -72,20 +72,18 @@ func webhookDeleteHandler(w http.ResponseWriter, r *http.Request) { if err != nil { restutils.SendHTTPError( ctx, w, http.StatusInternalServerError, - "Could not check if webhook already exists", - api.ErrCodeDefault) + "Could not check if webhook already exists") return } if !exists { - restutils.SendHTTPError(ctx, w, http.StatusConflict, "Webhook does not exist", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusConflict, "Webhook does not exist") return } if err := deleteWebhook(req); err != nil { restutils.SendHTTPError( ctx, w, http.StatusInternalServerError, - "Could not delete webhook", - api.ErrCodeDefault) + "Could not delete webhook") return } restutils.SendHTTPResponse(ctx, w, http.StatusOK, "Webhook Deleted") @@ -98,8 +96,7 @@ func webhookListHandler(w http.ResponseWriter, r *http.Request) { if err != nil { restutils.SendHTTPError( ctx, w, http.StatusInternalServerError, - "Could not retrive webhook list", - api.ErrCodeDefault) + "Could not retrive webhook list") return } diff --git a/plugins/georeplication/rest.go b/plugins/georeplication/rest.go index 4f44f12fe..9726e8dc8 100644 --- a/plugins/georeplication/rest.go +++ b/plugins/georeplication/rest.go @@ -13,7 +13,6 @@ import ( restutils "github.com/gluster/glusterd2/glusterd2/servers/rest/utils" "github.com/gluster/glusterd2/glusterd2/transaction" "github.com/gluster/glusterd2/glusterd2/volume" - "github.com/gluster/glusterd2/pkg/api" "github.com/gluster/glusterd2/pkg/errors" georepapi "github.com/gluster/glusterd2/plugins/georeplication/api" @@ -52,14 +51,14 @@ func validateMasterAndRemoteIDFormat(ctx context.Context, w http.ResponseWriter, // Validate UUID format of Master and Remote Volume ID masterid := uuid.Parse(masteridRaw) if masterid == nil { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Invalid Master Volume ID", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Invalid Master Volume ID") return nil, nil, errs.New("Invalid Master Volume ID") } // Validate UUID format of Remote Volume ID remoteid := uuid.Parse(remoteidRaw) if remoteid == nil { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Invalid Remote Volume ID", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Invalid Remote Volume ID") return nil, nil, errs.New("Invalid Remote Volume ID") } @@ -82,43 +81,43 @@ func georepCreateHandler(w http.ResponseWriter, r *http.Request) { } if uuid.Equal(masterid, remoteid) { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Master and Remote Volume can't be same", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Master and Remote Volume can't be same") return } // Parse the JSON body to get additional details of request var req georepapi.GeorepCreateReq if err := restutils.UnmarshalRequest(r, &req); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed) return } // Required fields are MasterVol, RemoteHosts and RemoteVol if req.MasterVol == "" { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Master volume name is required field", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Master volume name is required field") return } if len(req.RemoteHosts) == 0 { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Atleast one Remote host is required", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Atleast one Remote host is required") return } if req.RemoteVol == "" { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Remote volume name is required field", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Remote volume name is required field") return } // Check if Master volume exists and Matches with passed Volume ID vol, e := volume.GetVolume(req.MasterVol) if e != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } // Check if Master Volume ID from store matches the input Master Volume ID if !uuid.Equal(vol.ID, masterid) { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Master volume ID doesn't match", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Master volume ID doesn't match") return } @@ -129,7 +128,7 @@ func georepCreateHandler(w http.ResponseWriter, r *http.Request) { if err == nil { sessionExists = true if !req.Force { - restutils.SendHTTPError(ctx, w, http.StatusConflict, "Session already exists", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusConflict, "Session already exists") return } } @@ -138,7 +137,7 @@ func georepCreateHandler(w http.ResponseWriter, r *http.Request) { // error while fetching from store or JSON marshal errors if err != nil { if _, ok := err.(*ErrGeorepSessionNotFound); !ok { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } } @@ -160,7 +159,7 @@ func georepCreateHandler(w http.ResponseWriter, r *http.Request) { // Lock on Master Volume name lock, unlock, err := transaction.CreateLockSteps(geoSession.MasterVol) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -192,12 +191,12 @@ func georepCreateHandler(w http.ResponseWriter, r *http.Request) { if err = txn.Ctx.Set("geosession", geoSession); err != nil { logger.WithError(err).Error("failed to set geosession in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err = txn.Ctx.Set("volinfo", vol); err != nil { logger.WithError(err).Error("failed to set volinfo in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -208,7 +207,7 @@ func georepCreateHandler(w http.ResponseWriter, r *http.Request) { "mastervolid": masterid, "remotevolid": remoteid, }).Error("failed to create geo-replication session") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e) return } @@ -233,7 +232,7 @@ func georepActionHandler(w http.ResponseWriter, r *http.Request, action actionTy // Parse the JSON body to get additional details of request var req georepapi.GeorepCommandsReq if err := restutils.UnmarshalRequest(r, &req); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed) return } @@ -241,42 +240,42 @@ func georepActionHandler(w http.ResponseWriter, r *http.Request, action actionTy geoSession, err := getSession(masterid.String(), remoteid.String()) if err != nil { if _, ok := err.(*ErrGeorepSessionNotFound); !ok { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "geo-replication session not found", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "geo-replication session not found") return } if action == actionStart && geoSession.Status == georepapi.GeorepStatusStarted && !req.Force { - restutils.SendHTTPError(ctx, w, http.StatusConflict, "session already started", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusConflict, "session already started") return } if action == actionStop && geoSession.Status == georepapi.GeorepStatusStopped && !req.Force { - restutils.SendHTTPError(ctx, w, http.StatusConflict, "session already stopped", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusConflict, "session already stopped") return } if action == actionPause && geoSession.Status != georepapi.GeorepStatusStarted && !req.Force { - restutils.SendHTTPError(ctx, w, http.StatusConflict, "session is not in started state", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusConflict, "session is not in started state") return } if action == actionResume && geoSession.Status != georepapi.GeorepStatusPaused && !req.Force { - restutils.SendHTTPError(ctx, w, http.StatusConflict, "session not in paused state", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusConflict, "session not in paused state") return } // Fetch Volume details and check if Volume is in started state vol, e := volume.GetVolume(geoSession.MasterVol) if e != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } if action == actionStart && vol.State != volume.VolStarted { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "master volume not started", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "master volume not started") return } @@ -285,7 +284,7 @@ func georepActionHandler(w http.ResponseWriter, r *http.Request, action actionTy lock, unlock, err := transaction.CreateLockSteps(geoSession.MasterVol) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -305,7 +304,7 @@ func georepActionHandler(w http.ResponseWriter, r *http.Request, action actionTy doFunc = "georeplication-stop.Commit" stateToSet = georepapi.GeorepStatusStopped default: - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Unknown action", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Unknown action") return } @@ -320,13 +319,13 @@ func georepActionHandler(w http.ResponseWriter, r *http.Request, action actionTy if err = txn.Ctx.Set("mastervolid", masterid.String()); err != nil { logger.WithError(err).Error("failed to set mastervolid in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err = txn.Ctx.Set("remotevolid", remoteid.String()); err != nil { logger.WithError(err).Error("failed to set remotevolid in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -337,7 +336,7 @@ func georepActionHandler(w http.ResponseWriter, r *http.Request, action actionTy "mastervolid": masterid, "remotevolid": remoteid, }).Error("failed to " + action.String() + " geo-replication session") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -345,7 +344,7 @@ func georepActionHandler(w http.ResponseWriter, r *http.Request, action actionTy e = addOrUpdateSession(geoSession) if e != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e) return } @@ -387,17 +386,17 @@ func georepDeleteHandler(w http.ResponseWriter, r *http.Request) { geoSession, err := getSession(masterid.String(), remoteid.String()) if err != nil { if _, ok := err.(*ErrGeorepSessionNotFound); !ok { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "geo-replication session not found", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "geo-replication session not found") return } // Fetch Volume details and check if Volume exists _, e := volume.GetVolume(geoSession.MasterVol) if e != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } @@ -406,7 +405,7 @@ func georepDeleteHandler(w http.ResponseWriter, r *http.Request) { lock, unlock, err := transaction.CreateLockSteps(geoSession.MasterVol) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -422,13 +421,13 @@ func georepDeleteHandler(w http.ResponseWriter, r *http.Request) { if err = txn.Ctx.Set("mastervolid", masterid.String()); err != nil { logger.WithError(err).Error("failed to set mastervolid in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err = txn.Ctx.Set("remotevolid", remoteid.String()); err != nil { logger.WithError(err).Error("failed to set remotevolid in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -439,7 +438,7 @@ func georepDeleteHandler(w http.ResponseWriter, r *http.Request) { "mastervolid": masterid, "remotevolid": remoteid, }).Error("failed to delete geo-replication session") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e) return } @@ -464,7 +463,7 @@ func georepStatusHandler(w http.ResponseWriter, r *http.Request) { geoSession, err := getSession(masterid.String(), remoteid.String()) if err != nil { if _, ok := err.(*ErrGeorepSessionNotFound); !ok { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } restutils.SendHTTPResponse(ctx, w, http.StatusOK, []georepapi.GeorepSession{}) @@ -481,7 +480,7 @@ func georepStatusHandler(w http.ResponseWriter, r *http.Request) { // Get Volume info, which is required to get the Bricks list vol, e := volume.GetVolume(geoSession.MasterVol) if e != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } @@ -499,13 +498,13 @@ func georepStatusHandler(w http.ResponseWriter, r *http.Request) { if err = txn.Ctx.Set("mastervolid", masterid.String()); err != nil { logger.WithError(err).Error("failed to set mastervolid in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err = txn.Ctx.Set("remotevolid", remoteid.String()); err != nil { logger.WithError(err).Error("failed to set remotevolid in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -518,7 +517,7 @@ func georepStatusHandler(w http.ResponseWriter, r *http.Request) { "mastervolid": masterid, "remotevolid": remoteid, }).Error("failed to get status of geo-replication session") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e) return } @@ -527,7 +526,7 @@ func georepStatusHandler(w http.ResponseWriter, r *http.Request) { if err != nil { errMsg := "Failed to aggregate gsyncd status results from multiple nodes." logger.WithField("error", err.Error()).Error("gsyncdStatusHandler:" + errMsg) - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, errMsg, api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, errMsg) return } @@ -619,10 +618,10 @@ func georepConfigGetHandler(w http.ResponseWriter, r *http.Request) { geoSession, err := getSession(masterid.String(), remoteid.String()) if err != nil { if _, ok := err.(*ErrGeorepSessionNotFound); !ok { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "geo-replication session not found", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "geo-replication session not found") return } @@ -641,7 +640,7 @@ func georepConfigGetHandler(w http.ResponseWriter, r *http.Request) { "mastervolid": masterid, "remotevolid": remoteid, }).Error("failed to get session configurations") - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Failed to get session configurations", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Failed to get session configurations") return } @@ -652,7 +651,7 @@ func georepConfigGetHandler(w http.ResponseWriter, r *http.Request) { "mastervolid": masterid, "remotevolid": remoteid, }).Error("failed to parse configurations") - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Failed to parse configurations", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Failed to parse configurations") return } @@ -698,7 +697,7 @@ func georepConfigSetHandler(w http.ResponseWriter, r *http.Request) { // Parse the JSON body to get additional details of request var req map[string]string if err := restutils.UnmarshalRequest(r, &req); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed) return } @@ -708,10 +707,10 @@ func georepConfigSetHandler(w http.ResponseWriter, r *http.Request) { // Continue only if NotFound error, return if other errors like // error while fetching from store or JSON marshal errors if _, ok := err.(*ErrGeorepSessionNotFound); !ok { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "geo-replication session not found", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "geo-replication session not found") return } @@ -724,7 +723,7 @@ func georepConfigSetHandler(w http.ResponseWriter, r *http.Request) { configWillChange = true err = checkConfig(k, v) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Invalid Config Name/Value", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Invalid Config Name/Value") return } @@ -734,7 +733,7 @@ func georepConfigSetHandler(w http.ResponseWriter, r *http.Request) { vol, e := volume.GetVolume(geoSession.MasterVol) if e != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } @@ -758,7 +757,7 @@ func georepConfigSetHandler(w http.ResponseWriter, r *http.Request) { // TODO: change the lock key lock, unlock, err := transaction.CreateLockSteps(geoSession.MasterVol) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -778,25 +777,25 @@ func georepConfigSetHandler(w http.ResponseWriter, r *http.Request) { if err = txn.Ctx.Set("mastervolid", masterid.String()); err != nil { logger.WithError(err).Error("failed to set mastervolid in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err = txn.Ctx.Set("remotevolid", remoteid.String()); err != nil { logger.WithError(err).Error("failed to set remotevolid in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err = txn.Ctx.Set("session", geoSession); err != nil { logger.WithError(err).Error("failed to set geosession in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err = txn.Ctx.Set("restartRequired", restartRequired); err != nil { logger.WithError(err).Error("failed to set restartrequired in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -807,7 +806,7 @@ func georepConfigSetHandler(w http.ResponseWriter, r *http.Request) { "mastervolid": masterid, "remotevolid": remoteid, }).Error("failed to update geo-replication session config") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e) return } @@ -831,7 +830,7 @@ func georepConfigResetHandler(w http.ResponseWriter, r *http.Request) { // Parse the JSON body to get additional details of request var req []string if err := restutils.UnmarshalRequest(r, &req); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed) return } @@ -841,10 +840,10 @@ func georepConfigResetHandler(w http.ResponseWriter, r *http.Request) { // Continue only if NotFound error, return if other errors like // error while fetching from store or JSON marshal errors if _, ok := err.(*ErrGeorepSessionNotFound); !ok { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "geo-replication session not found", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "geo-replication session not found") return } @@ -873,7 +872,7 @@ func georepConfigResetHandler(w http.ResponseWriter, r *http.Request) { vol, e := volume.GetVolume(geoSession.MasterVol) if e != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } @@ -886,7 +885,7 @@ func georepConfigResetHandler(w http.ResponseWriter, r *http.Request) { // TODO: change the lock key lock, unlock, err := transaction.CreateLockSteps(geoSession.MasterVol) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -906,25 +905,25 @@ func georepConfigResetHandler(w http.ResponseWriter, r *http.Request) { if err = txn.Ctx.Set("mastervolid", masterid.String()); err != nil { logger.WithError(err).Error("failed to set mastervolid in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err = txn.Ctx.Set("remotevolid", remoteid.String()); err != nil { logger.WithError(err).Error("failed to set remotevolid in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err = txn.Ctx.Set("session", geoSession); err != nil { logger.WithError(err).Error("failed to set geosession in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err = txn.Ctx.Set("restartRequired", restartRequired); err != nil { logger.WithError(err).Error("failed to set restartrequired in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -935,7 +934,7 @@ func georepConfigResetHandler(w http.ResponseWriter, r *http.Request) { "mastervolid": masterid, "remotevolid": remoteid, }).Error("failed to update geo-replication session config") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e) return } @@ -947,7 +946,7 @@ func georepStatusListHandler(w http.ResponseWriter, r *http.Request) { sessions, err := getSessionList() if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -965,7 +964,7 @@ func georepSSHKeyGenerateHandler(w http.ResponseWriter, r *http.Request) { // Check if Volume exists vol, e := volume.GetVolume(volname) if e != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } @@ -976,7 +975,7 @@ func georepSSHKeyGenerateHandler(w http.ResponseWriter, r *http.Request) { // Lock on Master Volume name lock, unlock, err := transaction.CreateLockSteps(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -992,7 +991,7 @@ func georepSSHKeyGenerateHandler(w http.ResponseWriter, r *http.Request) { if err = txn.Ctx.Set("volname", volname); err != nil { logger.WithError(err).Error("failed to set volname in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -1002,13 +1001,13 @@ func georepSSHKeyGenerateHandler(w http.ResponseWriter, r *http.Request) { "error": err.Error(), "volname": volname, }).Error("failed to generate SSH Keys") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } sshkeys, err := getSSHPublicKeys(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -1029,7 +1028,7 @@ func georepSSHKeyGetHandler(w http.ResponseWriter, r *http.Request) { "error": err.Error(), "volname": volname, }).Error("failed to get SSH public Keys") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -1050,14 +1049,14 @@ func georepSSHKeyPushHandler(w http.ResponseWriter, r *http.Request) { // Check if Volume exists vol, e := volume.GetVolume(volname) if e != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } // Parse the JSON body to get additional details of request var sshkeys []georepapi.GeorepSSHPublicKey if err := restutils.UnmarshalRequest(r, &sshkeys); err != nil { - restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusUnprocessableEntity, errors.ErrJSONParsingFailed) return } @@ -1068,7 +1067,7 @@ func georepSSHKeyPushHandler(w http.ResponseWriter, r *http.Request) { // Lock on Master Volume name lock, unlock, err := transaction.CreateLockSteps(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -1084,13 +1083,13 @@ func georepSSHKeyPushHandler(w http.ResponseWriter, r *http.Request) { if err = txn.Ctx.Set("sshkeys", sshkeys); err != nil { logger.WithError(err).Error("failed to set sshkeys in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err = txn.Ctx.Set("user", user); err != nil { logger.WithError(err).Error("failed to set user in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -1100,7 +1099,7 @@ func georepSSHKeyPushHandler(w http.ResponseWriter, r *http.Request) { "error": e.Error(), "volname": volname, }).Error("failed to push SSH Keys") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e) return } diff --git a/plugins/glustershd/rest.go b/plugins/glustershd/rest.go index f9188edba..e0476abaf 100644 --- a/plugins/glustershd/rest.go +++ b/plugins/glustershd/rest.go @@ -7,7 +7,6 @@ import ( restutils "github.com/gluster/glusterd2/glusterd2/servers/rest/utils" "github.com/gluster/glusterd2/glusterd2/transaction" "github.com/gluster/glusterd2/glusterd2/volume" - "github.com/gluster/glusterd2/pkg/api" "github.com/gluster/glusterd2/pkg/errors" "github.com/gorilla/mux" @@ -34,7 +33,7 @@ func glustershEnableHandler(w http.ResponseWriter, r *http.Request) { //validate volume name v, err := volume.GetVolume(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } // Store initial volinfo before changing the HealFlag @@ -43,12 +42,12 @@ func glustershEnableHandler(w http.ResponseWriter, r *http.Request) { // validate volume type if !isVolReplicate(v.Type) { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Volume Type not supported", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Volume Type not supported") return } if v.State != volume.VolStarted { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Volume should be in started state.", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Volume should be in started state.") return } @@ -60,7 +59,7 @@ func glustershEnableHandler(w http.ResponseWriter, r *http.Request) { //Lock on Volume Name lock, unlock, err := transaction.CreateLockSteps(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -87,20 +86,20 @@ func glustershEnableHandler(w http.ResponseWriter, r *http.Request) { if err := txn.Ctx.Set("volinfo", v); err != nil { logger.WithError(err).Error("failed to set volinfo in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err := txn.Ctx.Set("oldvolinfo", oldvolinfo); err != nil { logger.WithError(err).Error("failed to set volinfo in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } err = txn.Do() if err != nil { logger.WithError(err).Error("failed to start self heal daemon") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -117,7 +116,7 @@ func glustershDisableHandler(w http.ResponseWriter, r *http.Request) { //validate volume name v, err := volume.GetVolume(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } // Store initial volinfo before changing the HealFlag @@ -126,7 +125,7 @@ func glustershDisableHandler(w http.ResponseWriter, r *http.Request) { // validate volume type if !isVolReplicate(v.Type) { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Volume Type not supported", api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Volume Type not supported") return } @@ -138,7 +137,7 @@ func glustershDisableHandler(w http.ResponseWriter, r *http.Request) { // Lock on volume name. lock, unlock, err := transaction.CreateLockSteps(volname) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -166,13 +165,13 @@ func glustershDisableHandler(w http.ResponseWriter, r *http.Request) { if err := txn.Ctx.Set("volinfo", v); err != nil { logger.WithError(err).Error("failed to set volinfo in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } if err := txn.Ctx.Set("oldvolinfo", oldvolinfo); err != nil { logger.WithError(err).Error("failed to set volinfo in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -182,7 +181,7 @@ func glustershDisableHandler(w http.ResponseWriter, r *http.Request) { "error": err.Error(), "volname": volname, }).Error("failed to stop self heal daemon") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } diff --git a/plugins/quota/rest.go b/plugins/quota/rest.go index 36de7cf2d..55d386aa7 100644 --- a/plugins/quota/rest.go +++ b/plugins/quota/rest.go @@ -10,7 +10,6 @@ import ( restutils "github.com/gluster/glusterd2/glusterd2/servers/rest/utils" "github.com/gluster/glusterd2/glusterd2/transaction" "github.com/gluster/glusterd2/glusterd2/volume" - "github.com/gluster/glusterd2/pkg/api" "github.com/gluster/glusterd2/pkg/errors" "github.com/gorilla/mux" "github.com/pborman/uuid" @@ -51,19 +50,19 @@ func quotaEnableHandler(w http.ResponseWriter, r *http.Request) { // Validate volume existence vol, err := volume.GetVolume(volName) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound) return } // Check if volume is started if vol.State != volume.VolStarted { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrVolNotStarted.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrVolNotStarted) return } // Check if quota is already enabled if volume.IsQuotaEnabled(vol) { - restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrProcessAlreadyRunning.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrProcessAlreadyRunning) return } @@ -75,13 +74,13 @@ func quotaEnableHandler(w http.ResponseWriter, r *http.Request) { if err := txn.Ctx.Set("volinfo", vol); err != nil { logger.WithError(err).Error("failed to set volinfo in transaction context") - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } lock, unlock, err := transaction.CreateLockSteps(volName) if err != nil { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) return } @@ -102,9 +101,9 @@ func quotaEnableHandler(w http.ResponseWriter, r *http.Request) { if err != nil { logger.WithError(err).Error("quota enable transaction failed") if err == transaction.ErrLockTimeout { - restutils.SendHTTPError(ctx, w, http.StatusConflict, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusConflict, err) } else { - restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault) + restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) } return }