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 15, 2018
1 parent 3caf951 commit ae88792
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 30 deletions.
4 changes: 0 additions & 4 deletions plugins/device/api/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,3 @@ const (
type AddDeviceReq struct {
Devices []string `json:"devices"`
}

type EditGroupReq struct {
Group string `json:"group"`
}
1 change: 1 addition & 0 deletions 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

// EditGroupResp is the success response sent to a EditGroup request
type EditGroupResp api.Peer
1 change: 0 additions & 1 deletion plugins/device/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func (p *Plugin) RestRoutes() route.Routes {
Method: "POST",
Pattern: "/peers/{peerid}/group/{group_id}",
Version: 1,
RequestType: utils.GetTypeString((*deviceapi.EditGroupReq)(nil)),
ResponseType: utils.GetTypeString((*deviceapi.EditGroupResp)(nil)),
HandlerFunc: groupEditHandler,
},
Expand Down
19 changes: 3 additions & 16 deletions plugins/device/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ 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)
Expand All @@ -106,19 +99,13 @@ func groupEditHandler(w http.ResponseWriter, r *http.Request) {
}
err = txn.Ctx.Set("peerid", peerID)
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("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")
logger.WithError(err).WithField("GroupID", groupID).Error("Failed to set data for transaction")
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err.Error(), api.ErrCodeDefault)
return
}
Expand All @@ -130,7 +117,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
13 changes: 4 additions & 9 deletions plugins/device/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,23 @@ func txnPrepareDevice(c transaction.TxnCtx) error {
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")
c.Logger().WithError(err).WithField("PeerID", peerID).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")
c.Logger().WithError(err).WithField("GroupID", groupID).Error("Failed transaction, cannot find group-id")
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
peerInfo.MetaData["_group"] = groupID
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", groupID).WithField("peerid", peerID).Error("Failed to update peer Info")
return err
}
return nil
Expand Down

0 comments on commit ae88792

Please sign in to comment.