Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.

Commit

Permalink
Updating code
Browse files Browse the repository at this point in the history
  • Loading branch information
rishubhjain committed Mar 26, 2018
2 parents fe15576 + 40e0c6f commit de27fcf
Show file tree
Hide file tree
Showing 41 changed files with 559 additions and 349 deletions.
13 changes: 13 additions & 0 deletions e2e/volume_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion glusterd2/commands/global/getglobaloptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions glusterd2/commands/global/setglobaloptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
35 changes: 26 additions & 9 deletions glusterd2/commands/peers/addpeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package peercommands
import (
"fmt"
"net/http"
"strings"

"github.com/gluster/glusterd2/glusterd2/events"
"github.com/gluster/glusterd2/glusterd2/gdctx"
Expand All @@ -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
}

Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -76,14 +85,22 @@ 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 req.Zone != "" {
if newpeer.MetaData != nil {
newpeer.MetaData["_zone"] = req.Zone
} else {
newpeer.MetaData = make(map[string]string)
newpeer.MetaData["_zone"] = req.Zone
}
}
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)
Expand Down
11 changes: 10 additions & 1 deletion glusterd2/commands/peers/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
25 changes: 12 additions & 13 deletions glusterd2/commands/peers/deletepeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand All @@ -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()
Expand All @@ -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")
Expand Down
Loading

0 comments on commit de27fcf

Please sign in to comment.