File size: 4,948 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
package api

import (
	"net/http"
	"strings"

	"github.com/Resinat/Resin/internal/service"
)

func subscriptionMatchesKeyword(s service.SubscriptionResponse, keyword string) bool {
	contains := func(v string) bool {
		return strings.Contains(strings.ToLower(v), keyword)
	}

	return contains(s.ID) || contains(s.Name) || contains(s.URL) || contains(s.SourceType)
}

func filterSubscriptionsByKeyword(subs []service.SubscriptionResponse, rawKeyword string) []service.SubscriptionResponse {
	keyword := strings.ToLower(strings.TrimSpace(rawKeyword))
	if keyword == "" {
		return subs
	}
	filtered := make([]service.SubscriptionResponse, 0, len(subs))
	for _, sub := range subs {
		if subscriptionMatchesKeyword(sub, keyword) {
			filtered = append(filtered, sub)
		}
	}
	return filtered
}

func subscriptionSortKey(sortBy string, s service.SubscriptionResponse) string {
	switch sortBy {
	case "created_at":
		return s.CreatedAt
	case "last_checked":
		return s.LastChecked
	case "last_updated":
		return s.LastUpdated
	default:
		return s.Name
	}
}

// HandleListSubscriptions returns a handler for GET /api/v1/subscriptions.
func HandleListSubscriptions(cp *service.ControlPlaneService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		enabled, ok := parseBoolQueryOrWriteInvalid(w, r, "enabled")
		if !ok {
			return
		}
		subs, err := cp.ListSubscriptions(enabled)
		if err != nil {
			writeServiceError(w, err)
			return
		}
		subs = filterSubscriptionsByKeyword(subs, r.URL.Query().Get("keyword"))

		sorting, ok := parseSortingOrWriteInvalid(
			w,
			r,
			[]string{"name", "created_at", "last_checked", "last_updated"},
			"created_at",
			"asc",
		)
		if !ok {
			return
		}
		SortSlice(subs, sorting, func(s service.SubscriptionResponse) string {
			return subscriptionSortKey(sorting.SortBy, s)
		})

		pg, ok := parsePaginationOrWriteInvalid(w, r)
		if !ok {
			return
		}
		WritePage(w, http.StatusOK, subs, pg)
	}
}

// HandleGetSubscription returns a handler for GET /api/v1/subscriptions/{id}.
func HandleGetSubscription(cp *service.ControlPlaneService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		id, ok := requireUUIDPathParam(w, r, "id", "subscription_id")
		if !ok {
			return
		}
		s, err := cp.GetSubscription(id)
		if err != nil {
			writeServiceError(w, err)
			return
		}
		WriteJSON(w, http.StatusOK, s)
	}
}

// HandleCreateSubscription returns a handler for POST /api/v1/subscriptions.
func HandleCreateSubscription(cp *service.ControlPlaneService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req service.CreateSubscriptionRequest
		if err := DecodeBody(r, &req); err != nil {
			writeDecodeBodyError(w, err)
			return
		}
		s, err := cp.CreateSubscription(req)
		if err != nil {
			writeServiceError(w, err)
			return
		}
		WriteJSON(w, http.StatusCreated, s)
	}
}

// HandleUpdateSubscription returns a handler for PATCH /api/v1/subscriptions/{id}.
func HandleUpdateSubscription(cp *service.ControlPlaneService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		id, ok := requireUUIDPathParam(w, r, "id", "subscription_id")
		if !ok {
			return
		}
		body, ok := readRawBodyOrWriteInvalid(w, r)
		if !ok {
			return
		}
		s, err := cp.UpdateSubscription(id, body)
		if err != nil {
			writeServiceError(w, err)
			return
		}
		WriteJSON(w, http.StatusOK, s)
	}
}

// HandleDeleteSubscription returns a handler for DELETE /api/v1/subscriptions/{id}.
func HandleDeleteSubscription(cp *service.ControlPlaneService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		id, ok := requireUUIDPathParam(w, r, "id", "subscription_id")
		if !ok {
			return
		}
		if err := cp.DeleteSubscription(id); err != nil {
			writeServiceError(w, err)
			return
		}
		w.WriteHeader(http.StatusNoContent)
	}
}

// HandleRefreshSubscription returns a handler for POST /api/v1/subscriptions/{id}/actions/refresh.
func HandleRefreshSubscription(cp *service.ControlPlaneService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		id, ok := requireUUIDPathParam(w, r, "id", "subscription_id")
		if !ok {
			return
		}
		if err := cp.RefreshSubscription(id); err != nil {
			writeServiceError(w, err)
			return
		}
		WriteJSON(w, http.StatusOK, map[string]string{"status": "ok"})
	}
}

// HandleCleanupSubscriptionCircuitOpenNodes returns a handler for
// POST /api/v1/subscriptions/{id}/actions/cleanup-circuit-open-nodes.
func HandleCleanupSubscriptionCircuitOpenNodes(cp *service.ControlPlaneService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		id, ok := requireUUIDPathParam(w, r, "id", "subscription_id")
		if !ok {
			return
		}
		cleanedCount, err := cp.CleanupSubscriptionCircuitOpenNodes(id)
		if err != nil {
			writeServiceError(w, err)
			return
		}
		WriteJSON(w, http.StatusOK, map[string]int{"cleaned_count": cleanedCount})
	}
}