-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
meshsync_handler.go
262 lines (222 loc) · 7.5 KB
/
meshsync_handler.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
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
package handlers
import (
"encoding/json"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/layer5io/meshery/server/models"
"github.com/layer5io/meshsync/pkg/model"
"gorm.io/gorm/clause"
)
// swagger:route GET /api/system/meshsync/resources GetMeshSyncResources idGetMeshSyncResources
// Handle GET request for meshsync discovered resources
//
// ```?apiVersion={apiVersion}``` If apiVersion is unspecified then all models are returned
//
// ```?search={componentname}``` If search is non empty then a greedy search is performed
//
// ```?order={field}``` orders on the passed field
//
// ```?sort={[asc/desc]}``` Default behavior is asc
//
// ```?page={page-number}``` Default page number is 1
//
// ```?pagesize={pagesize}``` Default pagesize is 25. To return all results: ```pagesize=all```
//
// ```?annotation={annotaion}``` annotation is a boolean value. If true then annotations are returned
//
// ```?labels={labels}``` labels is a boolean value. If true then labels are returned
//
// ```?spec={spec}``` spec is a boolean value. If true then spec is returned
//
// ```?status={status}``` status is a boolean value. If true then status is returned
//
// ```?clusterId={[clusterId]}``` clusterId is array of string values. Required.
//
// responses:
// 200: []meshsyncResourcesResponseWrapper
func (h *Handler) GetMeshSyncResources(rw http.ResponseWriter, r *http.Request, _ *models.Preference, _ *models.User, provider models.Provider) {
rw.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(rw)
var resources []model.KubernetesResource
var totalCount int64
limitstr := r.URL.Query().Get("pagesize")
var limit int
if limitstr != "all" {
limit, _ = strconv.Atoi(limitstr)
if limit <= 0 {
limit = defaultPageSize
}
}
pagestr := r.URL.Query().Get("page")
page, _ := strconv.Atoi(pagestr)
if page <= 0 {
page = 1
}
offset := (page - 1) * limit
order := r.URL.Query().Get("order")
sort := r.URL.Query().Get("sort")
search := r.URL.Query().Get("search")
apiVersion := r.URL.Query().Get("apiVersion")
spec, _ := strconv.ParseBool(r.URL.Query().Get("spec"))
status, _ := strconv.ParseBool(r.URL.Query().Get("status"))
isAnnotaion, _ := strconv.ParseBool(r.URL.Query().Get("annotations"))
isLabels, _ := strconv.ParseBool(r.URL.Query().Get("labels"))
kind := r.URL.Query().Get("kind")
filter := struct {
ClusterIds []string `json:"clusterIds"`
}{}
clusterIds := r.URL.Query().Get("clusterIds")
if clusterIds != "" {
err := json.Unmarshal([]byte(clusterIds), &filter.ClusterIds)
if err != nil {
h.log.Error(ErrFetchMeshSyncResources(err))
http.Error(rw, ErrFetchMeshSyncResources(err).Error(), http.StatusInternalServerError)
return
}
} else {
filter.ClusterIds = []string{}
}
result := provider.GetGenericPersister().Model(&model.KubernetesResource{}).
Preload("KubernetesResourceMeta").
Where("kubernetes_resources.cluster_id IN (?)", filter.ClusterIds)
if kind != "" {
result = result.Where(&model.KubernetesResource{Kind: kind})
}
if apiVersion != "" {
result = result.Where(&model.KubernetesResource{APIVersion: apiVersion})
}
if isLabels {
result = result.Preload("KubernetesResourceMeta.Labels", "kind = ?", model.KindLabel)
}
if isAnnotaion {
result = result.Preload("KubernetesResourceMeta.Annotations", "kind = ?", model.KindAnnotation)
}
if spec {
result = result.Preload("Spec")
}
if status {
result = result.Preload("Status")
}
if search != "" {
result = result.
Joins("JOIN kubernetes_resource_object_meta ON kubernetes_resource_object_meta.id = kubernetes_resources.id").
Where("kubernetes_resource_object_meta.name LIKE ?", "%"+search+"%")
}
result.Count(&totalCount)
if limit != 0 {
result = result.Limit(limit)
}
if offset != 0 {
result = result.Offset(offset)
}
order = models.SanitizeOrderInput(order, []string{"created_at", "updated_at", "name"})
if order != "" {
if sort == "desc" {
result = result.Order(clause.OrderByColumn{Column: clause.Column{Name: order}, Desc: true})
} else {
result = result.Order(order)
}
}
err := result.Find(&resources).Error
if err != nil {
h.log.Error(ErrFetchMeshSyncResources(err))
http.Error(rw, ErrFetchMeshSyncResources(err).Error(), http.StatusInternalServerError)
return
}
var pgSize int
if limitstr == "all" {
pgSize = len(resources)
} else {
pgSize = limit
}
response := &models.MeshSyncResourcesAPIResponse{
Page: page,
PageSize: pgSize,
TotalCount: totalCount,
Resources: resources,
}
if err := enc.Encode(response); err != nil {
h.log.Error(ErrFetchMeshSyncResources(err))
http.Error(rw, ErrFetchMeshSyncResources(err).Error(), http.StatusInternalServerError)
}
}
// swagger:route GET /api/system/meshsync/resources/kinds GetMeshSyncResourcesKinds idGetMeshSyncResourcesKinds
// Handle GET request for meshsync discovered resources kinds
//
// ```?clusterId={[clusterId]}``` clusterId is array of string values. Required.
//
// ```?page = {page-number}``` Default page number is 1
//
// ```?pagesize = {pagesize}``` Default pagesize is 25. To return all results: ```pagesize=all```
//
// ```?search = {componentname}``` If search is non empty then a greedy search is performed
//
// ```?order = {field}``` orders on the passed field
//
// responses:
// 200: []meshsyncResourcesKindsResponseWrapper
func (h *Handler) GetMeshSyncResourcesKinds(rw http.ResponseWriter, r *http.Request, _ *models.Preference, _ *models.User, provider models.Provider) {
rw.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(rw)
page, offset, limit,
search, order, sort, _ := getPaginationParams(r)
filter := struct {
ClusterIds []string `json:"clusterIds"`
}{}
var kinds []string
var totalCount int64
clusterIds := r.URL.Query().Get("clusterIds")
if clusterIds != "" {
err := json.Unmarshal([]byte(clusterIds), &filter.ClusterIds)
if err != nil {
h.log.Error(ErrFetchMeshSyncResources(err))
http.Error(rw, ErrFetchMeshSyncResources(err).Error(), http.StatusInternalServerError)
return
}
} else {
filter.ClusterIds = []string{}
}
result := provider.GetGenericPersister().Model(&model.KubernetesResource{}).Distinct("kind").
Where("kubernetes_resources.cluster_id IN (?)", filter.ClusterIds)
if search != "" {
result = result.Where("kubernetes_resources.kind LIKE ?", "%"+search+"%")
}
if limit != 0 {
result = result.Limit(limit)
}
if offset != 0 {
result = result.Offset(offset)
}
if order != "" {
if sort == "desc" {
result = result.Order(clause.OrderByColumn{Column: clause.Column{Name: order}, Desc: true})
} else {
result = result.Order(order)
}
}
err := result.Pluck("kinds", &kinds).Error
if err != nil {
h.log.Error(ErrFetchMeshSyncResources(err))
http.Error(rw, ErrFetchMeshSyncResources(err).Error(), http.StatusInternalServerError)
return
}
response := &models.MeshSyncResourcesKindsAPIResponse{
Kinds: kinds,
TotalCount: totalCount,
Page: page,
PageSize: limit,
}
if err := enc.Encode(response); err != nil {
h.log.Error(ErrFetchMeshSyncResources(err))
http.Error(rw, ErrFetchMeshSyncResources(err).Error(), http.StatusInternalServerError)
}
}
func (h *Handler) DeleteMeshSyncResource(rw http.ResponseWriter, r *http.Request, _ *models.Preference, _ *models.User, provider models.Provider) {
resourceID := mux.Vars(r)["id"]
db := provider.GetGenericPersister()
err := db.Model(&model.KubernetesResource{}).Delete(&model.KubernetesResource{ID: resourceID}).Error
if err != nil {
h.log.Error(models.ErrDelete(err, "meshsync data", http.StatusInternalServerError))
}
}