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

Commit

Permalink
Edit Group API
Browse files Browse the repository at this point in the history
  • Loading branch information
rishubhjain committed Mar 15, 2018
1 parent f983ca3 commit 3caf951
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
4 changes: 4 additions & 0 deletions plugins/device/api/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ const (
type AddDeviceReq struct {
Devices []string `json:"devices"`
}

type EditGroupReq struct {
Group string `json:"group"`
}
2 changes: 2 additions & 0 deletions plugins/device/api/resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ type Info struct {

// AddDeviceResp is the success response sent to a AddDeviceReq request
type AddDeviceResp api.Peer

type EditGroupResp api.Peer
13 changes: 12 additions & 1 deletion plugins/device/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,23 @@ func (p *Plugin) RestRoutes() route.Routes {
Version: 1,
RequestType: utils.GetTypeString((*deviceapi.AddDeviceReq)(nil)),
ResponseType: utils.GetTypeString((*deviceapi.AddDeviceResp)(nil)),
HandlerFunc: deviceAddHandler},
HandlerFunc: deviceAddHandler,
},
route.Route{
Name: "EditGroup",
Method: "POST",
Pattern: "/peers/{peerid}/group/{group_id}",
Version: 1,
RequestType: utils.GetTypeString((*deviceapi.EditGroupReq)(nil)),
ResponseType: utils.GetTypeString((*deviceapi.EditGroupResp)(nil)),
HandlerFunc: groupEditHandler,
},
}
}

// RegisterStepFuncs registers transaction step functions with
// Glusterd Transaction framework
func (p *Plugin) RegisterStepFuncs() {
transaction.RegisterStepFunc(txnPrepareDevice, "prepare-device")
transaction.RegisterStepFunc(txnEditGroup, "edit-group")
}
62 changes: 62 additions & 0 deletions plugins/device/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,65 @@ func deviceAddHandler(w http.ResponseWriter, r *http.Request) {
}
restutils.SendHTTPResponse(ctx, w, http.StatusOK, peerInfo)
}

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

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

req := new(deviceapi.EditGroupReq)
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)
return
}

peerID := mux.Vars(r)["peerid"]
if peerID == "" {
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",
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")
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")
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault)
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)
return
}
peerInfo, err := peer.GetPeer(peerID)
if err != nil {
logger.WithError(err).Error("Failed to get peer from store")
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Failed to get peer from store", api.ErrCodeDefault)
return
}
restutils.SendHTTPResponse(ctx, w, http.StatusOK, peerInfo)
}
31 changes: 31 additions & 0 deletions plugins/device/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os/exec"
"strings"

"github.com/gluster/glusterd2/glusterd2/peer"
"github.com/gluster/glusterd2/glusterd2/transaction"
deviceapi "github.com/gluster/glusterd2/plugins/device/api"

Expand Down Expand Up @@ -51,3 +52,33 @@ func txnPrepareDevice(c transaction.TxnCtx) error {
}
return nil
}

func txnEditGroup(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")
return err
}
if err := c.Get("req", req); err != nil {
c.Logger().WithError(err).Error("Failed transaction, cannot find group details")
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("peerid", peerID).Error("Failed to update peer Info")
return err
}
return nil
}

0 comments on commit 3caf951

Please sign in to comment.