Skip to content

Commit df7268c

Browse files
committed
return farms, nodes and accounts without extra db fields
1 parent 23e194a commit df7268c

File tree

1 file changed

+116
-19
lines changed

1 file changed

+116
-19
lines changed

node-registrar/pkg/server/handlers.go

Lines changed: 116 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ const (
2020
OnlineCutoffTime = 40 * time.Minute
2121
)
2222

23+
type Farm struct {
24+
FarmID uint64 `json:"farm_id"`
25+
FarmName string `json:"farm_name"`
26+
TwinID uint64 `json:"twin_id"`
27+
Dedicated bool `json:"dedicated"`
28+
StellarAddress string `json:"stellar_address"`
29+
}
30+
2331
// @title Node Registrar API
2432
// @version 1.0
2533
// @description API for managing TFGrid node registration
@@ -35,7 +43,7 @@ const (
3543
// @Param twin_id query int false "Filter by twin ID"
3644
// @Param page query int false "Page number" default(1)
3745
// @Param size query int false "Results per page" default(10)
38-
// @Success 200 {object} []db.Farm "List of farms"
46+
// @Success 200 {object} []Farm "List of farms"
3947
// @Failure 400 {object} map[string]any "Bad request"
4048
// @Router /farms [get]
4149
func (s Server) listFarmsHandler(c *gin.Context) {
@@ -54,7 +62,19 @@ func (s Server) listFarmsHandler(c *gin.Context) {
5462
return
5563
}
5664

57-
c.JSON(http.StatusOK, farms)
65+
// dorp extra db field
66+
var res []Farm
67+
for _, farm := range farms {
68+
res = append(res, Farm{
69+
FarmID: farm.FarmID,
70+
FarmName: farm.FarmName,
71+
TwinID: farm.TwinID,
72+
Dedicated: farm.Dedicated,
73+
StellarAddress: farm.StellarAddress,
74+
})
75+
}
76+
77+
c.JSON(http.StatusOK, res)
5878
}
5979

6080
// @Summary Get farm details
@@ -63,7 +83,7 @@ func (s Server) listFarmsHandler(c *gin.Context) {
6383
// @Accept json
6484
// @Produce json
6585
// @Param farm_id path int true "Farm ID"
66-
// @Success 200 {object} db.Farm "Farm details"
86+
// @Success 200 {object} Farm "Farm details"
6787
// @Failure 400 {object} map[string]any "Invalid farm ID"
6888
// @Failure 404 {object} map[string]any "Farm not found"
6989
// @Router /farms/{farm_id} [get]
@@ -88,7 +108,16 @@ func (s Server) getFarmHandler(c *gin.Context) {
88108
return
89109
}
90110

91-
c.JSON(http.StatusOK, farm)
111+
// dorp extra db field
112+
res := Farm{
113+
FarmID: farm.FarmID,
114+
FarmName: farm.FarmName,
115+
TwinID: farm.TwinID,
116+
Dedicated: farm.Dedicated,
117+
StellarAddress: farm.StellarAddress,
118+
}
119+
120+
c.JSON(http.StatusOK, res)
92121
}
93122

94123
// @Summary Create new farm
@@ -203,6 +232,21 @@ func (s Server) updateFarmHandler(c *gin.Context) {
203232
})
204233
}
205234

235+
type Node struct {
236+
NodeID uint64 `json:"node_id"`
237+
FarmID uint64 `json:"farm_id"`
238+
TwinID uint64 `json:"twin_id"`
239+
Location db.Location `json:"location"`
240+
Resources db.Resources `json:"resources"`
241+
Interfaces []db.Interface `json:"interfaces"`
242+
SecureBoot bool `json:"secure_boot"`
243+
Virtualized bool `json:"virtualized"`
244+
SerialNumber string `json:"serial_number"`
245+
LastSeen time.Time `json:"last_seen"`
246+
Online bool `json:"online"`
247+
Approved bool
248+
}
249+
206250
// @Summary List nodes
207251
// @Description Get a list of nodes with optional filters
208252
// @Tags nodes
@@ -217,7 +261,7 @@ func (s Server) updateFarmHandler(c *gin.Context) {
217261
// @Param last_seen query int false "Filter nodes last seen within this many minutes"
218262
// @Param page query int false "Page number" default(1)
219263
// @Param size query int false "Results per page" default(10)
220-
// @Success 200 {object} []db.Node "List of nodes with online status"
264+
// @Success 200 {object} []Node "List of nodes with online status"
221265
// @Failure 400 {object} map[string]any "Bad request"
222266
// @Router /nodes [get]
223267
func (s Server) listNodesHandler(c *gin.Context) {
@@ -236,13 +280,27 @@ func (s Server) listNodesHandler(c *gin.Context) {
236280
return
237281
}
238282

239-
// Set online status for each node
240283
cutoffTime := time.Now().Add(-OnlineCutoffTime)
241-
for i := range nodes {
242-
nodes[i].Online = !nodes[i].LastSeen.IsZero() && nodes[i].LastSeen.After(cutoffTime)
284+
var res []Node
285+
for i, node := range nodes {
286+
res = append(res, Node{
287+
NodeID: node.NodeID,
288+
FarmID: node.FarmID,
289+
TwinID: node.TwinID,
290+
Location: node.Location,
291+
Resources: node.Resources,
292+
Interfaces: node.Interfaces,
293+
SecureBoot: node.SecureBoot,
294+
SerialNumber: node.SerialNumber,
295+
Virtualized: node.Virtualized,
296+
LastSeen: node.LastSeen,
297+
Approved: node.Approved,
298+
// Set online status for each node
299+
Online: !nodes[i].LastSeen.IsZero() && nodes[i].LastSeen.After(cutoffTime),
300+
})
243301
}
244302

245-
c.JSON(http.StatusOK, nodes)
303+
c.JSON(http.StatusOK, res)
246304
}
247305

248306
// @Summary Get node details
@@ -251,7 +309,7 @@ func (s Server) listNodesHandler(c *gin.Context) {
251309
// @Accept json
252310
// @Produce json
253311
// @Param node_id path int true "Node ID"
254-
// @Success 200 {object} db.Node "Node details with online status and last_seen information"
312+
// @Success 200 {object} Node "Node details with online status and last_seen information"
255313
// @Failure 400 {object} map[string]any "Invalid node ID"
256314
// @Failure 404 {object} map[string]any "Node not found"
257315
// @Router /nodes/{node_id} [get]
@@ -275,11 +333,24 @@ func (s Server) getNodeHandler(c *gin.Context) {
275333
return
276334
}
277335

278-
// Determine if the node is online (has sent an uptime report in the last 30 minutes)
279336
cutoffTime := time.Now().Add(-OnlineCutoffTime)
280-
node.Online = !node.LastSeen.IsZero() && node.LastSeen.After(cutoffTime)
281-
282-
c.JSON(http.StatusOK, node)
337+
res := Node{
338+
NodeID: node.NodeID,
339+
FarmID: node.FarmID,
340+
TwinID: node.TwinID,
341+
Location: node.Location,
342+
Resources: node.Resources,
343+
Interfaces: node.Interfaces,
344+
SecureBoot: node.SecureBoot,
345+
SerialNumber: node.SerialNumber,
346+
Virtualized: node.Virtualized,
347+
LastSeen: node.LastSeen,
348+
Approved: node.Approved,
349+
// Determine if the node is online (has sent an uptime report in the last 30 minutes)
350+
Online: !node.LastSeen.IsZero() && node.LastSeen.After(cutoffTime),
351+
}
352+
353+
c.JSON(http.StatusOK, res)
283354
}
284355

285356
type NodeRegistrationRequest struct {
@@ -517,13 +588,20 @@ type AccountCreationRequest struct {
517588
RMBEncKey string `json:"rmb_enc_key,omitempty"`
518589
}
519590

591+
type Account struct {
592+
TwinID uint64 `json:"twin_id"`
593+
Relays []string `json:"relays"` // Optional list of relay domains
594+
RMBEncKey string `json:"rmb_enc_key"` // Optional base64 encoded public key for rmb communication
595+
PublicKey string `json:"public_key"`
596+
}
597+
520598
// @Summary Create new account
521599
// @Description Create a new twin account with cryptographic verification
522600
// @Tags accounts
523601
// @Accept json
524602
// @Produce json
525603
// @Param request body AccountCreationRequest true "Account creation data"
526-
// @Success 201 {object} db.Account "Created account details"
604+
// @Success 201 {object} Account "Created account details"
527605
// @Failure 400 {object} map[string]any "Invalid request"
528606
// @Failure 409 {object} map[string]any "Account already exists"
529607
// @Router /accounts [post]
@@ -593,7 +671,13 @@ func (s *Server) createAccountHandler(c *gin.Context) {
593671
return
594672
}
595673

596-
c.JSON(http.StatusCreated, account)
674+
res := Account{
675+
TwinID: account.TwinID,
676+
PublicKey: account.PublicKey,
677+
RMBEncKey: account.RMBEncKey,
678+
Relays: account.Relays,
679+
}
680+
c.JSON(http.StatusCreated, res)
597681
}
598682

599683
type UpdateAccountRequest struct {
@@ -654,7 +738,7 @@ func (s *Server) updateAccountHandler(c *gin.Context) {
654738
// @Produce json
655739
// @Param twin_id query uint64 false "Twin ID of the account"
656740
// @Param public_key query string false "Base64 decoded Public key of the account"
657-
// @Success 200 {object} db.Account "Account details"
741+
// @Success 200 {object} Account "Account details"
658742
// @Failure 400 {object} map[string]any "Invalid request"
659743
// @Failure 404 {object} map[string]any "Account not found"
660744
// @Router /accounts [get]
@@ -694,7 +778,13 @@ func (s *Server) getAccountHandler(c *gin.Context) {
694778
return
695779
}
696780

697-
c.JSON(http.StatusOK, account)
781+
res := Account{
782+
TwinID: account.TwinID,
783+
PublicKey: account.PublicKey,
784+
RMBEncKey: account.RMBEncKey,
785+
Relays: account.Relays,
786+
}
787+
c.JSON(http.StatusCreated, res)
698788
return
699789
}
700790

@@ -708,7 +798,14 @@ func (s *Server) getAccountHandler(c *gin.Context) {
708798
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get account"})
709799
return
710800
}
711-
c.JSON(http.StatusOK, account)
801+
802+
res := Account{
803+
TwinID: account.TwinID,
804+
PublicKey: account.PublicKey,
805+
RMBEncKey: account.RMBEncKey,
806+
Relays: account.Relays,
807+
}
808+
c.JSON(http.StatusCreated, res)
712809
return
713810
}
714811
}

0 commit comments

Comments
 (0)