Skip to content

Commit 8e48ae0

Browse files
committed
remove only farms from account and uptime from node
1 parent cb2684c commit 8e48ae0

File tree

1 file changed

+55
-77
lines changed

1 file changed

+55
-77
lines changed

node-registrar/pkg/server/handlers.go

Lines changed: 55 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/gin-gonic/gin"
1414
"github.com/lib/pq"
15+
"github.com/rs/zerolog/log"
1516
"github.com/threefoldtech/tfgrid4-sdk-go/node-registrar/pkg/db"
1617
)
1718

@@ -36,7 +37,7 @@ const (
3637
// @Param twin_id query int false "Filter by twin ID"
3738
// @Param page query int false "Page number" default(1)
3839
// @Param size query int false "Results per page" default(10)
39-
// @Success 200 {object} []map[string]any "List of farms"]
40+
// @Success 200 {object} []db.Farm "List of farms"]
4041
// @Failure 400 {object} map[string]any "Bad request"
4142
// @Router /farms [get]
4243
func (s Server) listFarmsHandler(c *gin.Context) {
@@ -224,21 +225,6 @@ func (s Server) updateFarmHandler(c *gin.Context) {
224225
})
225226
}
226227

227-
type Node struct {
228-
NodeID uint64 `json:"node_id"`
229-
FarmID uint64 `json:"farm_id"`
230-
TwinID uint64 `json:"twin_id"`
231-
Location db.Location `json:"location"`
232-
Resources db.Resources `json:"resources"`
233-
Interfaces []db.Interface `json:"interfaces"`
234-
SecureBoot bool `json:"secure_boot"`
235-
Virtualized bool `json:"virtualized"`
236-
SerialNumber string `json:"serial_number"`
237-
LastSeen time.Time `json:"last_seen"`
238-
Online bool `json:"online"`
239-
Approved bool
240-
}
241-
242228
// @Summary List nodes
243229
// @Description Get a list of nodes with optional filters
244230
// @Tags nodes
@@ -253,7 +239,7 @@ type Node struct {
253239
// @Param last_seen query int false "Filter nodes last seen within this many minutes"
254240
// @Param page query int false "Page number" default(1)
255241
// @Param size query int false "Results per page" default(10)
256-
// @Success 200 {object} []Node "List of nodes with online status"
242+
// @Success 200 {object} []db.Node "List of nodes with online status"
257243
// @Failure 400 {object} map[string]any "Bad request"
258244
// @Router /nodes [get]
259245
func (s Server) listNodesHandler(c *gin.Context) {
@@ -273,23 +259,19 @@ func (s Server) listNodesHandler(c *gin.Context) {
273259
}
274260

275261
cutoffTime := time.Now().Add(-OnlineCutoffTime)
276-
var res []Node
277-
for i, node := range nodes {
278-
res = append(res, Node{
279-
NodeID: node.NodeID,
280-
FarmID: node.FarmID,
281-
TwinID: node.TwinID,
282-
Location: node.Location,
283-
Resources: node.Resources,
284-
Interfaces: node.Interfaces,
285-
SecureBoot: node.SecureBoot,
286-
SerialNumber: node.SerialNumber,
287-
Virtualized: node.Virtualized,
288-
LastSeen: node.LastSeen,
289-
Approved: node.Approved,
290-
// Set online status for each node
291-
Online: !nodes[i].LastSeen.IsZero() && nodes[i].LastSeen.After(cutoffTime),
292-
})
262+
// dorp extra db field
263+
var res []map[string]any
264+
for _, node := range nodes {
265+
node.Online = !node.LastSeen.IsZero() && node.LastSeen.After(cutoffTime)
266+
267+
data, err := toMap(node)
268+
if err != nil {
269+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
270+
return
271+
}
272+
273+
delete(data, "uptime")
274+
res = append(res, data)
293275
}
294276

295277
c.JSON(http.StatusOK, res)
@@ -301,7 +283,7 @@ func (s Server) listNodesHandler(c *gin.Context) {
301283
// @Accept json
302284
// @Produce json
303285
// @Param node_id path int true "Node ID"
304-
// @Success 200 {object} Node "Node details with online status and last_seen information"
286+
// @Success 200 {object} db.Node "Node details with online status and last_seen information"
305287
// @Failure 400 {object} map[string]any "Invalid node ID"
306288
// @Failure 404 {object} map[string]any "Node not found"
307289
// @Router /nodes/{node_id} [get]
@@ -326,23 +308,18 @@ func (s Server) getNodeHandler(c *gin.Context) {
326308
}
327309

328310
cutoffTime := time.Now().Add(-OnlineCutoffTime)
329-
res := Node{
330-
NodeID: node.NodeID,
331-
FarmID: node.FarmID,
332-
TwinID: node.TwinID,
333-
Location: node.Location,
334-
Resources: node.Resources,
335-
Interfaces: node.Interfaces,
336-
SecureBoot: node.SecureBoot,
337-
SerialNumber: node.SerialNumber,
338-
Virtualized: node.Virtualized,
339-
LastSeen: node.LastSeen,
340-
Approved: node.Approved,
341-
// Determine if the node is online (has sent an uptime report in the last 30 minutes)
342-
Online: !node.LastSeen.IsZero() && node.LastSeen.After(cutoffTime),
311+
node.Online = !node.LastSeen.IsZero() && node.LastSeen.After(cutoffTime)
312+
313+
// dorp extra db field
314+
data, err := toMap(node)
315+
if err != nil {
316+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
317+
return
343318
}
344319

345-
c.JSON(http.StatusOK, res)
320+
delete(data, "uptime")
321+
322+
c.JSON(http.StatusOK, data)
346323
}
347324

348325
type NodeRegistrationRequest struct {
@@ -580,20 +557,13 @@ type AccountCreationRequest struct {
580557
RMBEncKey string `json:"rmb_enc_key,omitempty"`
581558
}
582559

583-
type Account struct {
584-
TwinID uint64 `json:"twin_id"`
585-
Relays []string `json:"relays"` // Optional list of relay domains
586-
RMBEncKey string `json:"rmb_enc_key"` // Optional base64 encoded public key for rmb communication
587-
PublicKey string `json:"public_key"`
588-
}
589-
590560
// @Summary Create new account
591561
// @Description Create a new twin account with cryptographic verification
592562
// @Tags accounts
593563
// @Accept json
594564
// @Produce json
595565
// @Param request body AccountCreationRequest true "Account creation data"
596-
// @Success 201 {object} Account "Created account details"
566+
// @Success 201 {object} db.Account "Created account details"
597567
// @Failure 400 {object} map[string]any "Invalid request"
598568
// @Failure 409 {object} map[string]any "Account already exists"
599569
// @Router /accounts [post]
@@ -663,13 +633,15 @@ func (s *Server) createAccountHandler(c *gin.Context) {
663633
return
664634
}
665635

666-
res := Account{
667-
TwinID: account.TwinID,
668-
PublicKey: account.PublicKey,
669-
RMBEncKey: account.RMBEncKey,
670-
Relays: account.Relays,
636+
// dorp extra db field
637+
data, err := toMap(account)
638+
if err != nil {
639+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
640+
return
671641
}
672-
c.JSON(http.StatusCreated, res)
642+
643+
delete(data, "farms")
644+
c.JSON(http.StatusCreated, data)
673645
}
674646

675647
type UpdateAccountRequest struct {
@@ -730,7 +702,7 @@ func (s *Server) updateAccountHandler(c *gin.Context) {
730702
// @Produce json
731703
// @Param twin_id query uint64 false "Twin ID of the account"
732704
// @Param public_key query string false "Base64 decoded Public key of the account"
733-
// @Success 200 {object} Account "Account details"
705+
// @Success 200 {object} db.Account "Account details"
734706
// @Failure 400 {object} map[string]any "Invalid request"
735707
// @Failure 404 {object} map[string]any "Account not found"
736708
// @Router /accounts [get]
@@ -770,13 +742,17 @@ func (s *Server) getAccountHandler(c *gin.Context) {
770742
return
771743
}
772744

773-
res := Account{
774-
TwinID: account.TwinID,
775-
PublicKey: account.PublicKey,
776-
RMBEncKey: account.RMBEncKey,
777-
Relays: account.Relays,
745+
// dorp extra db field
746+
data, err := toMap(account)
747+
if err != nil {
748+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
749+
return
778750
}
779-
c.JSON(http.StatusCreated, res)
751+
752+
log.Info().Any("farms", data).Send()
753+
delete(data, "farms")
754+
log.Info().Any("farms", data).Send()
755+
c.JSON(http.StatusCreated, data)
780756
return
781757
}
782758

@@ -791,13 +767,15 @@ func (s *Server) getAccountHandler(c *gin.Context) {
791767
return
792768
}
793769

794-
res := Account{
795-
TwinID: account.TwinID,
796-
PublicKey: account.PublicKey,
797-
RMBEncKey: account.RMBEncKey,
798-
Relays: account.Relays,
770+
// dorp extra db field
771+
data, err := toMap(account)
772+
if err != nil {
773+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
774+
return
799775
}
800-
c.JSON(http.StatusCreated, res)
776+
777+
delete(data, "farms")
778+
c.JSON(http.StatusCreated, data)
801779
return
802780
}
803781
}

0 commit comments

Comments
 (0)