@@ -12,6 +12,7 @@ import (
12
12
13
13
"github.com/gin-gonic/gin"
14
14
"github.com/lib/pq"
15
+ "github.com/rs/zerolog/log"
15
16
"github.com/threefoldtech/tfgrid4-sdk-go/node-registrar/pkg/db"
16
17
)
17
18
@@ -36,7 +37,7 @@ const (
36
37
// @Param twin_id query int false "Filter by twin ID"
37
38
// @Param page query int false "Page number" default(1)
38
39
// @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"]
40
41
// @Failure 400 {object} map[string]any "Bad request"
41
42
// @Router /farms [get]
42
43
func (s Server ) listFarmsHandler (c * gin.Context ) {
@@ -224,21 +225,6 @@ func (s Server) updateFarmHandler(c *gin.Context) {
224
225
})
225
226
}
226
227
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
-
242
228
// @Summary List nodes
243
229
// @Description Get a list of nodes with optional filters
244
230
// @Tags nodes
@@ -253,7 +239,7 @@ type Node struct {
253
239
// @Param last_seen query int false "Filter nodes last seen within this many minutes"
254
240
// @Param page query int false "Page number" default(1)
255
241
// @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"
257
243
// @Failure 400 {object} map[string]any "Bad request"
258
244
// @Router /nodes [get]
259
245
func (s Server ) listNodesHandler (c * gin.Context ) {
@@ -273,23 +259,19 @@ func (s Server) listNodesHandler(c *gin.Context) {
273
259
}
274
260
275
261
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 )
293
275
}
294
276
295
277
c .JSON (http .StatusOK , res )
@@ -301,7 +283,7 @@ func (s Server) listNodesHandler(c *gin.Context) {
301
283
// @Accept json
302
284
// @Produce json
303
285
// @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"
305
287
// @Failure 400 {object} map[string]any "Invalid node ID"
306
288
// @Failure 404 {object} map[string]any "Node not found"
307
289
// @Router /nodes/{node_id} [get]
@@ -326,23 +308,18 @@ func (s Server) getNodeHandler(c *gin.Context) {
326
308
}
327
309
328
310
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
343
318
}
344
319
345
- c .JSON (http .StatusOK , res )
320
+ delete (data , "uptime" )
321
+
322
+ c .JSON (http .StatusOK , data )
346
323
}
347
324
348
325
type NodeRegistrationRequest struct {
@@ -580,20 +557,13 @@ type AccountCreationRequest struct {
580
557
RMBEncKey string `json:"rmb_enc_key,omitempty"`
581
558
}
582
559
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
-
590
560
// @Summary Create new account
591
561
// @Description Create a new twin account with cryptographic verification
592
562
// @Tags accounts
593
563
// @Accept json
594
564
// @Produce json
595
565
// @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"
597
567
// @Failure 400 {object} map[string]any "Invalid request"
598
568
// @Failure 409 {object} map[string]any "Account already exists"
599
569
// @Router /accounts [post]
@@ -663,13 +633,15 @@ func (s *Server) createAccountHandler(c *gin.Context) {
663
633
return
664
634
}
665
635
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
671
641
}
672
- c .JSON (http .StatusCreated , res )
642
+
643
+ delete (data , "farms" )
644
+ c .JSON (http .StatusCreated , data )
673
645
}
674
646
675
647
type UpdateAccountRequest struct {
@@ -730,7 +702,7 @@ func (s *Server) updateAccountHandler(c *gin.Context) {
730
702
// @Produce json
731
703
// @Param twin_id query uint64 false "Twin ID of the account"
732
704
// @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"
734
706
// @Failure 400 {object} map[string]any "Invalid request"
735
707
// @Failure 404 {object} map[string]any "Account not found"
736
708
// @Router /accounts [get]
@@ -770,13 +742,17 @@ func (s *Server) getAccountHandler(c *gin.Context) {
770
742
return
771
743
}
772
744
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
778
750
}
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 )
780
756
return
781
757
}
782
758
@@ -791,13 +767,15 @@ func (s *Server) getAccountHandler(c *gin.Context) {
791
767
return
792
768
}
793
769
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
799
775
}
800
- c .JSON (http .StatusCreated , res )
776
+
777
+ delete (data , "farms" )
778
+ c .JSON (http .StatusCreated , data )
801
779
return
802
780
}
803
781
}
0 commit comments