-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
nodes_controller.go
94 lines (78 loc) · 2.44 KB
/
nodes_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package web
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
"github.com/manyminds/api2go/jsonapi"
"github.com/smartcontractkit/chainlink-common/pkg/types"
"github.com/smartcontractkit/chainlink/v2/core/logger/audit"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/relay"
)
type NodesController interface {
// Index lists nodes, and optionally filters by chain id.
Index(c *gin.Context, size, page, offset int)
}
type NetworkScopedNodeStatuser struct {
network relay.Network
relayers chainlink.RelayerChainInteroperators
}
func NewNetworkScopedNodeStatuser(relayers chainlink.RelayerChainInteroperators, network relay.Network) *NetworkScopedNodeStatuser {
scoped := relayers.List(chainlink.FilterRelayersByType(network))
return &NetworkScopedNodeStatuser{
network: network,
relayers: scoped,
}
}
func (n *NetworkScopedNodeStatuser) NodeStatuses(ctx context.Context, offset, limit int, relayIDs ...relay.ID) (nodes []types.NodeStatus, count int, err error) {
return n.relayers.NodeStatuses(ctx, offset, limit, relayIDs...)
}
type nodesController[R jsonapi.EntityNamer] struct {
nodeSet *NetworkScopedNodeStatuser
errNotEnabled error
newResource func(status types.NodeStatus) R
auditLogger audit.AuditLogger
}
func newNodesController[R jsonapi.EntityNamer](
nodeSet *NetworkScopedNodeStatuser,
errNotEnabled error,
newResource func(status types.NodeStatus) R,
auditLogger audit.AuditLogger,
) NodesController {
return &nodesController[R]{
nodeSet: nodeSet,
errNotEnabled: errNotEnabled,
newResource: newResource,
auditLogger: auditLogger,
}
}
func (n *nodesController[R]) Index(c *gin.Context, size, page, offset int) {
if n.nodeSet == nil {
jsonAPIError(c, http.StatusBadRequest, n.errNotEnabled)
return
}
id := c.Param("ID")
var nodes []types.NodeStatus
var count int
var err error
if id == "" {
// fetch all nodes
nodes, count, err = n.nodeSet.NodeStatuses(c, offset, size)
} else {
// fetch nodes for chain ID
// backward compatibility
var rid relay.ID
err = rid.UnmarshalString(id)
if err != nil {
rid.ChainID = id
rid.Network = n.nodeSet.network
}
nodes, count, err = n.nodeSet.NodeStatuses(c, offset, size, rid)
}
var resources []R
for _, node := range nodes {
res := n.newResource(node)
resources = append(resources, res)
}
paginatedResponse(c, "node", size, page, resources, count, err)
}