File size: 5,920 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
215
216
217
218
219
220
221
222
223
224
225
package api

import (
	"net/http"
	"slices"
	"strings"

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

func platformMatchesKeyword(p service.PlatformResponse, keyword string) bool {
	contains := func(v string) bool {
		return strings.Contains(strings.ToLower(v), keyword)
	}

	if contains(p.ID) || contains(p.Name) {
		return true
	}
	for _, item := range p.RegionFilters {
		if contains(item) {
			return true
		}
	}
	return false
}

func filterPlatformsByKeyword(platforms []service.PlatformResponse, rawKeyword string) []service.PlatformResponse {
	keyword := strings.ToLower(strings.TrimSpace(rawKeyword))
	if keyword == "" {
		return platforms
	}
	filtered := make([]service.PlatformResponse, 0, len(platforms))
	for _, p := range platforms {
		if platformMatchesKeyword(p, keyword) {
			filtered = append(filtered, p)
		}
	}
	return filtered
}

func platformSortKey(sortBy string, p service.PlatformResponse) string {
	switch sortBy {
	case "id":
		return p.ID
	case "updated_at":
		return p.UpdatedAt
	default:
		return p.Name
	}
}

func comparePlatformsForList(a, b service.PlatformResponse, sorting Sorting) int {
	aBuiltin := a.ID == platform.DefaultPlatformID
	bBuiltin := b.ID == platform.DefaultPlatformID
	if aBuiltin != bBuiltin {
		if aBuiltin {
			return -1
		}
		return 1
	}

	primary := strings.Compare(platformSortKey(sorting.SortBy, a), platformSortKey(sorting.SortBy, b))
	if sorting.SortOrder == "desc" {
		primary = -primary
	}
	if primary != 0 {
		return primary
	}
	// keep stable deterministic output when primary sort key is equal
	return strings.Compare(a.ID, b.ID)
}

// HandleListPlatforms returns a handler for GET /api/v1/platforms.
func HandleListPlatforms(cp *service.ControlPlaneService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		platforms, err := cp.ListPlatforms()
		if err != nil {
			writeServiceError(w, err)
			return
		}
		platforms = filterPlatformsByKeyword(platforms, r.URL.Query().Get("keyword"))

		sorting, ok := parseSortingOrWriteInvalid(w, r, []string{"name", "id", "updated_at"}, "name", "asc")
		if !ok {
			return
		}
		slices.SortStableFunc(platforms, func(a, b service.PlatformResponse) int {
			return comparePlatformsForList(a, b, sorting)
		})

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

// HandleGetPlatform returns a handler for GET /api/v1/platforms/{id}.
func HandleGetPlatform(cp *service.ControlPlaneService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		id, ok := requireUUIDPathParam(w, r, "id", "platform_id")
		if !ok {
			return
		}

		p, err := cp.GetPlatform(id)
		if err != nil {
			writeServiceError(w, err)
			return
		}
		WriteJSON(w, http.StatusOK, p)
	}
}

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

// HandleUpdatePlatform returns a handler for PATCH /api/v1/platforms/{id}.
func HandleUpdatePlatform(cp *service.ControlPlaneService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		id, ok := requireUUIDPathParam(w, r, "id", "platform_id")
		if !ok {
			return
		}

		body, ok := readRawBodyOrWriteInvalid(w, r)
		if !ok {
			return
		}
		p, err := cp.UpdatePlatform(id, body)
		if err != nil {
			writeServiceError(w, err)
			return
		}
		WriteJSON(w, http.StatusOK, p)
	}
}

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

// HandleResetPlatform returns a handler for POST /api/v1/platforms/{id}/actions/reset-to-default.
func HandleResetPlatform(cp *service.ControlPlaneService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		id, ok := requireUUIDPathParam(w, r, "id", "platform_id")
		if !ok {
			return
		}
		p, err := cp.ResetPlatformToDefault(id)
		if err != nil {
			writeServiceError(w, err)
			return
		}
		WriteJSON(w, http.StatusOK, p)
	}
}

// HandleRebuildPlatform returns a handler for POST /api/v1/platforms/{id}/actions/rebuild-routable-view.
func HandleRebuildPlatform(cp *service.ControlPlaneService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		id, ok := requireUUIDPathParam(w, r, "id", "platform_id")
		if !ok {
			return
		}
		if err := cp.RebuildPlatformView(id); err != nil {
			writeServiceError(w, err)
			return
		}
		WriteJSON(w, http.StatusOK, map[string]string{"status": "ok"})
	}
}

// HandlePreviewFilter returns a handler for POST /api/v1/platforms/preview-filter.
func HandlePreviewFilter(cp *service.ControlPlaneService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req service.PreviewFilterRequest
		if err := DecodeBody(r, &req); err != nil {
			writeDecodeBodyError(w, err)
			return
		}
		if req.PlatformID != nil && *req.PlatformID != "" && !ValidateUUID(*req.PlatformID) {
			writeInvalidArgument(w, "platform_id: must be a valid UUID")
			return
		}
		nodes, err := cp.PreviewFilter(req)
		if err != nil {
			writeServiceError(w, err)
			return
		}
		pg, ok := parsePaginationOrWriteInvalid(w, r)
		if !ok {
			return
		}
		WritePage(w, http.StatusOK, nodes, pg)
	}
}