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 19, 2018
1 parent 3caf951 commit 76324c9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 31 deletions.
3 changes: 2 additions & 1 deletion plugins/device/api/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type AddDeviceReq struct {
Devices []string `json:"devices"`
}

type EditGroupReq struct {
// PeerEditGroupReq structure
type PeerEditGroupReq struct {
Group string `json:"group"`
}
3 changes: 2 additions & 1 deletion plugins/device/api/resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ type Info struct {
// AddDeviceResp is the success response sent to a AddDeviceReq request
type AddDeviceResp api.Peer

type EditGroupResp api.Peer
// PeerEditGroupResp is the success response sent to a EditGroup request
type PeerEditGroupResp api.Peer
12 changes: 6 additions & 6 deletions plugins/device/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func (p *Plugin) RestRoutes() route.Routes {
HandlerFunc: deviceAddHandler,
},
route.Route{
Name: "EditGroup",
Name: "PeerEditGroup",
Method: "POST",
Pattern: "/peers/{peerid}/group/{group_id}",
Pattern: "/peers/{peerid}/group",
Version: 1,
RequestType: utils.GetTypeString((*deviceapi.EditGroupReq)(nil)),
ResponseType: utils.GetTypeString((*deviceapi.EditGroupResp)(nil)),
HandlerFunc: groupEditHandler,
RequestType: utils.GetTypeString((*deviceapi.PeerEditGroupReq)(nil)),
ResponseType: utils.GetTypeString((*deviceapi.PeerEditGroupResp)(nil)),
HandlerFunc: peerEditGroupHandler,
},
}
}
Expand All @@ -50,5 +50,5 @@ func (p *Plugin) RestRoutes() route.Routes {
// Glusterd Transaction framework
func (p *Plugin) RegisterStepFuncs() {
transaction.RegisterStepFunc(txnPrepareDevice, "prepare-device")
transaction.RegisterStepFunc(txnEditGroup, "edit-group")
transaction.RegisterStepFunc(txnPeerEditGroup, "peer-edit-group")
}
19 changes: 6 additions & 13 deletions plugins/device/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func deviceAddHandler(w http.ResponseWriter, r *http.Request) {
restutils.SendHTTPResponse(ctx, w, http.StatusOK, peerInfo)
}

func groupEditHandler(w http.ResponseWriter, r *http.Request) {
func peerEditGroupHandler(w http.ResponseWriter, r *http.Request) {

ctx := r.Context()
logger := gdctx.GetReqLogger(ctx)

req := new(deviceapi.EditGroupReq)
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 marshal request", api.ErrCodeDefault)
Expand All @@ -92,33 +92,26 @@ func groupEditHandler(w http.ResponseWriter, r *http.Request) {
restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "peerid not present in request", api.ErrCodeDefault)
return
}
groupID := mux.Vars(r)["group_id"]
txn := transaction.NewTxn(ctx)
defer txn.Cleanup()
lock, unlock, err := transaction.CreateLockSteps(peerID)
txn.Steps = []*transaction.Step{
lock,
{
DoFunc: "edit-group",
DoFunc: "peer-edit-group",
Nodes: []uuid.UUID{gdctx.MyUUID},
},
unlock,
}
err = txn.Ctx.Set("peerid", peerID)
if err != nil {
logger.WithError(err).Error("Failed to set data for transaction")
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault)
return
}
err = txn.Ctx.Set("groupid", groupID)
if err != nil {
logger.WithError(err).Error("Failed to set data for transaction")
logger.WithError(err).WithField("PeerID", peerID).Error("Failed to set data for transaction")
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault)
return
}
err = txn.Ctx.Set("req", req)
if err != nil {
logger.WithError(err).Error("Failed to set data for transaction")
logger.WithError(err).WithField("req", req).Error("Failed to set data for transaction")
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault)
return
}
Expand All @@ -130,7 +123,7 @@ func groupEditHandler(w http.ResponseWriter, r *http.Request) {
}
peerInfo, err := peer.GetPeer(peerID)
if err != nil {
logger.WithError(err).Error("Failed to get peer from store")
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)
return
}
Expand Down
16 changes: 6 additions & 10 deletions plugins/device/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,16 @@ func txnPrepareDevice(c transaction.TxnCtx) error {
return nil
}

func txnEditGroup(c transaction.TxnCtx) error {
func txnPeerEditGroup(c transaction.TxnCtx) error {

var peerID string
var groupID string
var req deviceapi.EditGroupReq
if err := c.Get("peerid", peerID); err != nil {
c.Logger().WithError(err).Error("Failed transaction, cannot find peer-id")
return err
}
if err := c.Get("groupid", groupID); err != nil {
c.Logger().WithError(err).Error("Failed transaction, cannot find group-id")
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).Error("Failed transaction, cannot find group details")
c.Logger().WithError(err).WithField("req", req).Error("Failed transaction, cannot find req")
return err
}
peerInfo, err := peer.GetPeer(peerID)
Expand All @@ -77,7 +73,7 @@ func txnEditGroup(c transaction.TxnCtx) error {
peerInfo.MetaData["_group"] = req.Group
err = peer.AddOrUpdatePeer(peerInfo)
if err != nil {
c.Logger().WithError(err).WithField("peerid", peerID).Error("Failed to update peer Info")
c.Logger().WithError(err).WithField("GroupID", req.Group).WithField("peerid", peerID).Error("Failed to update peer Info")
return err
}
return nil
Expand Down

0 comments on commit 76324c9

Please sign in to comment.