-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackfill_handler.go
More file actions
310 lines (263 loc) · 9.79 KB
/
backfill_handler.go
File metadata and controls
310 lines (263 loc) · 9.79 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package store
import (
"encoding/json"
"errors"
"log/slog"
"net/http"
"time"
"github.com/google/uuid"
)
// BackfillMockDiffHandler provides HTTP endpoints for backfill, step mock,
// and execution diff management.
type BackfillMockDiffHandler struct {
backfillStore BackfillStore
mockStore StepMockStore
diffCalc *DiffCalculator
logger *slog.Logger
}
// NewBackfillMockDiffHandler creates a new handler with the given stores and calculator.
func NewBackfillMockDiffHandler(
backfillStore BackfillStore,
mockStore StepMockStore,
diffCalc *DiffCalculator,
logger *slog.Logger,
) *BackfillMockDiffHandler {
if logger == nil {
logger = slog.Default()
}
return &BackfillMockDiffHandler{
backfillStore: backfillStore,
mockStore: mockStore,
diffCalc: diffCalc,
logger: logger,
}
}
// RegisterRoutes registers all backfill, mock, and diff API routes on the given mux.
func (h *BackfillMockDiffHandler) RegisterRoutes(mux *http.ServeMux) {
// Backfill routes
mux.HandleFunc("GET /api/v1/admin/backfill", h.handleBackfillList)
mux.HandleFunc("POST /api/v1/admin/backfill", h.handleBackfillCreate)
mux.HandleFunc("GET /api/v1/admin/backfill/{id}", h.handleBackfillGet)
mux.HandleFunc("POST /api/v1/admin/backfill/{id}/cancel", h.handleBackfillCancel)
// Mock routes
mux.HandleFunc("GET /api/v1/admin/mocks", h.handleMockList)
mux.HandleFunc("POST /api/v1/admin/mocks", h.handleMockSet)
mux.HandleFunc("DELETE /api/v1/admin/mocks", h.handleMockClearAll)
mux.HandleFunc("GET /api/v1/admin/mocks/{pipeline}", h.handleMockListPipeline)
mux.HandleFunc("DELETE /api/v1/admin/mocks/{pipeline}/{step}", h.handleMockRemove)
// Diff routes
mux.HandleFunc("GET /api/v1/admin/executions/diff", h.handleExecutionDiff)
}
// ---------------------------------------------------------------------------
// Backfill handlers
// ---------------------------------------------------------------------------
func (h *BackfillMockDiffHandler) handleBackfillList(w http.ResponseWriter, r *http.Request) {
requests, err := h.backfillStore.List(r.Context())
if err != nil {
h.logger.Error("list backfill requests", "error", err)
writeHandlerError(w, http.StatusInternalServerError, "internal error")
return
}
writeHandlerJSON(w, http.StatusOK, requests)
}
func (h *BackfillMockDiffHandler) handleBackfillCreate(w http.ResponseWriter, r *http.Request) {
var req BackfillRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeHandlerError(w, http.StatusBadRequest, "invalid request body")
return
}
if req.PipelineName == "" {
writeHandlerError(w, http.StatusBadRequest, "pipeline_name is required")
return
}
if err := h.backfillStore.Create(r.Context(), &req); err != nil {
h.logger.Error("create backfill request", "error", err)
writeHandlerError(w, http.StatusInternalServerError, "internal error")
return
}
writeHandlerJSON(w, http.StatusCreated, req)
}
func (h *BackfillMockDiffHandler) handleBackfillGet(w http.ResponseWriter, r *http.Request) {
id, err := uuid.Parse(r.PathValue("id"))
if err != nil {
writeHandlerError(w, http.StatusBadRequest, "invalid id")
return
}
req, err := h.backfillStore.Get(r.Context(), id)
if err != nil {
if errors.Is(err, ErrNotFound) {
writeHandlerError(w, http.StatusNotFound, "backfill request not found")
return
}
h.logger.Error("get backfill request", "error", err)
writeHandlerError(w, http.StatusInternalServerError, "internal error")
return
}
writeHandlerJSON(w, http.StatusOK, req)
}
func (h *BackfillMockDiffHandler) handleBackfillCancel(w http.ResponseWriter, r *http.Request) {
id, err := uuid.Parse(r.PathValue("id"))
if err != nil {
writeHandlerError(w, http.StatusBadRequest, "invalid id")
return
}
if err := h.backfillStore.Cancel(r.Context(), id); err != nil {
if errors.Is(err, ErrNotFound) {
writeHandlerError(w, http.StatusNotFound, "backfill request not found")
return
}
if errors.Is(err, ErrConflict) {
writeHandlerError(w, http.StatusConflict, err.Error())
return
}
h.logger.Error("cancel backfill request", "error", err)
writeHandlerError(w, http.StatusInternalServerError, "internal error")
return
}
writeHandlerJSON(w, http.StatusOK, map[string]any{"status": "cancelled"})
}
// ---------------------------------------------------------------------------
// Mock handlers
// ---------------------------------------------------------------------------
func (h *BackfillMockDiffHandler) handleMockList(w http.ResponseWriter, r *http.Request) {
// List all mocks across all pipelines — iterate known pipelines is not possible
// with the current interface, so we use an empty pipeline to signal "all".
// For this endpoint, the store needs to handle listing all.
// Since the interface takes a pipeline name, we list with empty string
// which won't match any. Instead we use a different approach: return all mocks
// from the in-memory store.
//
// We call List with each unique pipeline but there's no ListAll interface method.
// For the HTTP API, we'll list with a query parameter.
pipeline := r.URL.Query().Get("pipeline")
mocks, err := h.mockStore.List(r.Context(), pipeline)
if err != nil {
h.logger.Error("list mocks", "error", err)
writeHandlerError(w, http.StatusInternalServerError, "internal error")
return
}
writeHandlerJSON(w, http.StatusOK, mocks)
}
func (h *BackfillMockDiffHandler) handleMockListPipeline(w http.ResponseWriter, r *http.Request) {
pipeline := r.PathValue("pipeline")
if pipeline == "" {
writeHandlerError(w, http.StatusBadRequest, "pipeline is required")
return
}
mocks, err := h.mockStore.List(r.Context(), pipeline)
if err != nil {
h.logger.Error("list mocks for pipeline", "error", err)
writeHandlerError(w, http.StatusInternalServerError, "internal error")
return
}
writeHandlerJSON(w, http.StatusOK, mocks)
}
// mockSetRequest is the JSON body for the set mock endpoint.
type mockSetRequest struct {
PipelineName string `json:"pipeline_name"`
StepName string `json:"step_name"`
Response map[string]any `json:"response"`
ErrorResponse string `json:"error_response,omitempty"`
Delay time.Duration `json:"delay,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
}
func (h *BackfillMockDiffHandler) handleMockSet(w http.ResponseWriter, r *http.Request) {
var body mockSetRequest
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeHandlerError(w, http.StatusBadRequest, "invalid request body")
return
}
if body.PipelineName == "" || body.StepName == "" {
writeHandlerError(w, http.StatusBadRequest, "pipeline_name and step_name are required")
return
}
enabled := true
if body.Enabled != nil {
enabled = *body.Enabled
}
mock := &StepMock{
PipelineName: body.PipelineName,
StepName: body.StepName,
Response: body.Response,
ErrorResponse: body.ErrorResponse,
Delay: body.Delay,
Enabled: enabled,
}
if err := h.mockStore.Set(r.Context(), mock); err != nil {
h.logger.Error("set mock", "error", err)
writeHandlerError(w, http.StatusInternalServerError, "internal error")
return
}
writeHandlerJSON(w, http.StatusCreated, mock)
}
func (h *BackfillMockDiffHandler) handleMockRemove(w http.ResponseWriter, r *http.Request) {
pipeline := r.PathValue("pipeline")
step := r.PathValue("step")
if pipeline == "" || step == "" {
writeHandlerError(w, http.StatusBadRequest, "pipeline and step are required")
return
}
if err := h.mockStore.Remove(r.Context(), pipeline, step); err != nil {
if errors.Is(err, ErrNotFound) {
writeHandlerError(w, http.StatusNotFound, "mock not found")
return
}
h.logger.Error("remove mock", "error", err)
writeHandlerError(w, http.StatusInternalServerError, "internal error")
return
}
writeHandlerJSON(w, http.StatusOK, map[string]any{"status": "removed"})
}
func (h *BackfillMockDiffHandler) handleMockClearAll(w http.ResponseWriter, r *http.Request) {
if err := h.mockStore.ClearAll(r.Context()); err != nil {
h.logger.Error("clear all mocks", "error", err)
writeHandlerError(w, http.StatusInternalServerError, "internal error")
return
}
writeHandlerJSON(w, http.StatusOK, map[string]any{"status": "cleared"})
}
// ---------------------------------------------------------------------------
// Diff handler
// ---------------------------------------------------------------------------
func (h *BackfillMockDiffHandler) handleExecutionDiff(w http.ResponseWriter, r *http.Request) {
aStr := r.URL.Query().Get("a")
bStr := r.URL.Query().Get("b")
if aStr == "" || bStr == "" {
writeHandlerError(w, http.StatusBadRequest, "query parameters 'a' and 'b' are required")
return
}
execA, err := uuid.Parse(aStr)
if err != nil {
writeHandlerError(w, http.StatusBadRequest, "invalid execution id 'a'")
return
}
execB, err := uuid.Parse(bStr)
if err != nil {
writeHandlerError(w, http.StatusBadRequest, "invalid execution id 'b'")
return
}
diff, err := h.diffCalc.Compare(r.Context(), execA, execB)
if err != nil {
if errors.Is(err, ErrNotFound) {
writeHandlerError(w, http.StatusNotFound, err.Error())
return
}
h.logger.Error("compare executions", "error", err)
writeHandlerError(w, http.StatusInternalServerError, "internal error")
return
}
writeHandlerJSON(w, http.StatusOK, diff)
}
// ---------------------------------------------------------------------------
// Response helpers
// ---------------------------------------------------------------------------
func writeHandlerJSON(w http.ResponseWriter, status int, data any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(data)
}
func writeHandlerError(w http.ResponseWriter, status int, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(map[string]string{"error": message})
}