File size: 5,492 Bytes
ea775bd | 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | package api
import (
"cmp"
"math"
"net/http"
"slices"
"strings"
"time"
"github.com/Resinat/Resin/internal/service"
)
func nodeTagSortKey(n service.NodeSummary) string {
if n.DisplayTag != "" {
return n.DisplayTag
}
if len(n.Tags) == 0 {
return ""
}
bestCreated := int64(math.MaxInt64)
bestTag := ""
for _, t := range n.Tags {
if t.SubscriptionCreatedAtNs < bestCreated {
bestCreated = t.SubscriptionCreatedAtNs
bestTag = t.Tag
continue
}
if t.SubscriptionCreatedAtNs == bestCreated && (bestTag == "" || t.Tag < bestTag) {
bestTag = t.Tag
}
}
return bestTag
}
func compareNodeSummaries(sortBy string, a, b service.NodeSummary) int {
order := 0
switch sortBy {
case "created_at":
order = strings.Compare(a.CreatedAt, b.CreatedAt)
case "failure_count":
order = cmp.Compare(a.FailureCount, b.FailureCount)
case "region":
order = strings.Compare(a.Region, b.Region)
default:
order = strings.Compare(nodeTagSortKey(a), nodeTagSortKey(b))
}
if order != 0 {
return order
}
return strings.Compare(a.NodeHash, b.NodeHash)
}
func sortNodeSummaries(nodes []service.NodeSummary, sorting Sorting) {
slices.SortStableFunc(nodes, func(a, b service.NodeSummary) int {
return applySortOrder(compareNodeSummaries(sorting.SortBy, a, b), sorting.SortOrder)
})
}
type nodeListPageResponse struct {
Items []service.NodeSummary `json:"items"`
Total int `json:"total"`
Limit int `json:"limit"`
Offset int `json:"offset"`
UniqueEgressIPs int `json:"unique_egress_ips"`
UniqueHealthyEgressIPs int `json:"unique_healthy_egress_ips"`
}
func countUniqueEgressIPs(nodes []service.NodeSummary) int {
seen := make(map[string]struct{})
for _, n := range nodes {
if n.EgressIP == "" {
continue
}
seen[n.EgressIP] = struct{}{}
}
return len(seen)
}
func countUniqueHealthyAndEnabledEgressIPs(nodes []service.NodeSummary) int {
seen := make(map[string]struct{})
for _, n := range nodes {
if n.EgressIP == "" {
continue
}
if !n.IsHealthyAndEnabled() {
continue
}
seen[n.EgressIP] = struct{}{}
}
return len(seen)
}
// HandleListNodes returns a handler for GET /api/v1/nodes.
func HandleListNodes(cp *service.ControlPlaneService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
filters := service.NodeFilters{}
platformID, ok := parseOptionalUUIDQuery(w, r, "platform_id", "platform_id")
if !ok {
return
}
filters.PlatformID = platformID
subscriptionID, ok := parseOptionalUUIDQuery(w, r, "subscription_id", "subscription_id")
if !ok {
return
}
filters.SubscriptionID = subscriptionID
if v := q.Get("region"); v != "" {
filters.Region = &v
}
if v := q.Get("egress_ip"); v != "" {
filters.EgressIP = &v
}
if v := strings.TrimSpace(q.Get("tag_keyword")); v != "" {
filters.TagKeyword = &v
}
circuitOpen, ok := parseBoolQueryOrWriteInvalid(w, r, "circuit_open")
if !ok {
return
}
filters.CircuitOpen = circuitOpen
hasOutbound, ok := parseBoolQueryOrWriteInvalid(w, r, "has_outbound")
if !ok {
return
}
filters.HasOutbound = hasOutbound
enabled, ok := parseBoolQueryOrWriteInvalid(w, r, "enabled")
if !ok {
return
}
filters.Enabled = enabled
if v := q.Get("probed_since"); v != "" {
t, err := time.Parse(time.RFC3339Nano, v)
if err != nil {
writeInvalidArgument(w, "probed_since: invalid RFC3339 timestamp")
return
}
filters.ProbedSince = &t
}
nodes, err := cp.ListNodes(filters)
if err != nil {
writeServiceError(w, err)
return
}
sorting, ok := parseSortingOrWriteInvalid(w, r, []string{"tag", "created_at", "failure_count", "region"}, "tag", "asc")
if !ok {
return
}
sortNodeSummaries(nodes, sorting)
pg, ok := parsePaginationOrWriteInvalid(w, r)
if !ok {
return
}
WriteJSON(w, http.StatusOK, nodeListPageResponse{
Items: PaginateSlice(nodes, pg),
Total: len(nodes),
Limit: pg.Limit,
Offset: pg.Offset,
UniqueEgressIPs: countUniqueEgressIPs(nodes),
UniqueHealthyEgressIPs: countUniqueHealthyAndEnabledEgressIPs(nodes),
})
}
}
// HandleGetNode returns a handler for GET /api/v1/nodes/{hash}.
func HandleGetNode(cp *service.ControlPlaneService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
hash := PathParam(r, "hash")
n, err := cp.GetNode(hash)
if err != nil {
writeServiceError(w, err)
return
}
WriteJSON(w, http.StatusOK, n)
}
}
// HandleProbeEgress returns a handler for POST /api/v1/nodes/{hash}/actions/probe-egress.
func HandleProbeEgress(cp *service.ControlPlaneService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
hash := PathParam(r, "hash")
result, err := cp.ProbeEgress(hash)
if err != nil {
writeServiceError(w, err)
return
}
WriteJSON(w, http.StatusOK, result)
}
}
// HandleProbeLatency returns a handler for POST /api/v1/nodes/{hash}/actions/probe-latency.
func HandleProbeLatency(cp *service.ControlPlaneService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
hash := PathParam(r, "hash")
result, err := cp.ProbeLatency(hash)
if err != nil {
writeServiceError(w, err)
return
}
WriteJSON(w, http.StatusOK, result)
}
}
|