handler.go 23.9 KB
Newer Older
P
pengcong06 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
Copyright 2020 The KubeSphere Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

Z
zryfish 已提交
14 15
package v1

Z
zryfish 已提交
16
import (
H
hongming 已提交
17
	"fmt"
Z
zryfish 已提交
18
	"github.com/emicklei/go-restful"
H
hongming 已提交
19 20 21
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	k8sinformers "k8s.io/client-go/informers"
H
hongming 已提交
22
	"k8s.io/klog"
Z
zryfish 已提交
23 24 25
	"kubesphere.io/kubesphere/pkg/api"
	"kubesphere.io/kubesphere/pkg/constants"
	"kubesphere.io/kubesphere/pkg/informers"
H
hongming 已提交
26 27
	"kubesphere.io/kubesphere/pkg/models/openpitrix"
	"kubesphere.io/kubesphere/pkg/server/errors"
Z
zryfish 已提交
28
	"kubesphere.io/kubesphere/pkg/server/params"
H
hongming 已提交
29 30 31
	op "kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
	"strconv"
	"strings"
Z
zryfish 已提交
32
)
Z
zryfish 已提交
33 34

type openpitrixHandler struct {
H
hongming 已提交
35 36
	openpitrix openpitrix.Interface
	informers  k8sinformers.SharedInformerFactory
Z
zryfish 已提交
37 38
}

39
func newOpenpitrixHandler(factory informers.InformerFactory, opClient op.Client) *openpitrixHandler {
H
hongming 已提交
40

Z
zryfish 已提交
41
	return &openpitrixHandler{
H
hongming 已提交
42 43
		openpitrix: openpitrix.NewOpenpitrixOperator(factory.KubernetesSharedInformerFactory(), opClient),
		informers:  factory.KubernetesSharedInformerFactory(),
Z
zryfish 已提交
44
	}
Z
zryfish 已提交
45 46
}

Z
Zhengyi Lai 已提交
47 48 49 50 51 52 53
func (h *openpitrixHandler) ListApplications(req *restful.Request, resp *restful.Response) {
	limit, offset := params.ParsePaging(req)
	clusterName := req.PathParameter("cluster")
	namespace := req.PathParameter("namespace")
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
	conditions, err := params.ParseConditions(req)
Z
zryfish 已提交
54 55

	if err != nil {
H
hongming 已提交
56
		klog.V(4).Infoln(err)
Z
Zhengyi Lai 已提交
57
		api.HandleBadRequest(resp, nil, err)
Z
zryfish 已提交
58 59
		return
	}
P
pengcong06 已提交
60
	conditions.Match[openpitrix.Zone] = namespace
Z
Zhengyi Lai 已提交
61 62
	// in openpitrix, runtime id is the cluster name
	conditions.Match[openpitrix.RuntimeId] = clusterName
Z
zryfish 已提交
63

H
hongming 已提交
64
	result, err := h.openpitrix.ListApplications(conditions, limit, offset, orderBy, reverse)
Z
zryfish 已提交
65 66

	if err != nil {
H
hongming 已提交
67
		klog.Errorln(err)
Z
Zhengyi Lai 已提交
68
		api.HandleInternalError(resp, nil, err)
Z
zryfish 已提交
69 70 71
		return
	}

Z
Zhengyi Lai 已提交
72
	resp.WriteAsJson(result)
H
hongming 已提交
73 74 75
}

func (h *openpitrixHandler) DescribeApplication(req *restful.Request, resp *restful.Response) {
Z
Zhengyi Lai 已提交
76
	clusterName := req.PathParameter("cluster")
H
hongming 已提交
77
	namespace := req.PathParameter("namespace")
Z
Zhengyi Lai 已提交
78
	applicationId := req.PathParameter("application")
H
hongming 已提交
79

Z
Zhengyi Lai 已提交
80
	app, err := h.openpitrix.DescribeApplication(namespace, applicationId, clusterName)
H
hongming 已提交
81 82

	if err != nil {
H
hongming 已提交
83 84
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
85 86 87 88 89 90 91 92
		return
	}

	resp.WriteEntity(app)
	return
}

func (h *openpitrixHandler) CreateApplication(req *restful.Request, resp *restful.Response) {
Z
Zhengyi Lai 已提交
93
	clusterName := req.PathParameter("cluster")
H
hongming 已提交
94 95 96 97
	namespace := req.PathParameter("namespace")
	var createClusterRequest openpitrix.CreateClusterRequest
	err := req.ReadEntity(&createClusterRequest)
	if err != nil {
H
hongming 已提交
98
		klog.V(4).Infoln(err)
Z
zryfish 已提交
99
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
100 101 102 103 104
		return
	}

	createClusterRequest.Username = req.HeaderParameter(constants.UserNameHeader)

Z
Zhengyi Lai 已提交
105
	err = h.openpitrix.CreateApplication(clusterName, namespace, createClusterRequest)
H
hongming 已提交
106 107

	if err != nil {
H
hongming 已提交
108
		klog.Errorln(err)
Z
zryfish 已提交
109
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
110 111 112 113 114 115 116 117
		return
	}

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) ModifyApplication(req *restful.Request, resp *restful.Response) {
	var modifyClusterAttributesRequest openpitrix.ModifyClusterAttributesRequest
Z
Zhengyi Lai 已提交
118 119
	clusterName := req.PathParameter("cluster")
	applicationId := req.PathParameter("application")
H
hongming 已提交
120 121 122
	namespace := req.PathParameter("namespace")
	err := req.ReadEntity(&modifyClusterAttributesRequest)
	if err != nil {
H
hongming 已提交
123
		klog.V(4).Infoln(err)
Z
zryfish 已提交
124
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
125 126 127
		return
	}

Z
Zhengyi Lai 已提交
128
	app, err := h.openpitrix.DescribeApplication(namespace, applicationId, clusterName)
H
hongming 已提交
129 130

	if err != nil {
H
hongming 已提交
131 132
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
133 134 135
		return
	}

Z
Zhengyi Lai 已提交
136 137
	if clusterName != app.Cluster.RuntimeId {
		err = fmt.Errorf("runtime and cluster not match %s,%s", app.Cluster.RuntimeId, clusterName)
H
hongming 已提交
138
		klog.V(4).Infoln(err)
Z
zryfish 已提交
139
		api.HandleForbidden(resp, nil, err)
H
hongming 已提交
140 141 142 143 144 145
		return
	}

	err = h.openpitrix.ModifyApplication(modifyClusterAttributesRequest)

	if err != nil {
H
hongming 已提交
146 147
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
148 149 150 151 152 153 154
		return
	}

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) DeleteApplication(req *restful.Request, resp *restful.Response) {
Z
Zhengyi Lai 已提交
155 156
	clusterName := req.PathParameter("cluster")
	applicationId := req.PathParameter("application")
H
hongming 已提交
157
	namespace := req.PathParameter("namespace")
Z
Zhengyi Lai 已提交
158
	app, err := h.openpitrix.DescribeApplication(namespace, applicationId, clusterName)
H
hongming 已提交
159 160

	if err != nil {
H
hongming 已提交
161 162
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
163 164 165
		return
	}

Z
Zhengyi Lai 已提交
166 167
	if clusterName != app.Cluster.RuntimeId {
		err = fmt.Errorf("runtime and cluster not match %s,%s", app.Cluster.RuntimeId, clusterName)
P
pengcong06 已提交
168 169 170 171 172
		klog.V(4).Infoln(err)
		api.HandleForbidden(resp, nil, err)
		return
	}

Z
Zhengyi Lai 已提交
173
	err = h.openpitrix.DeleteApplication(applicationId)
H
hongming 已提交
174 175

	if err != nil {
H
hongming 已提交
176
		klog.Errorln(err)
P
pengcong06 已提交
177
		handleOpenpitrixError(resp, err)
H
hongming 已提交
178 179 180
		return
	}

P
pengcong06 已提交
181 182 183 184
	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) UpgradeApplication(req *restful.Request, resp *restful.Response) {
Z
Zhengyi Lai 已提交
185
	clusterName := req.PathParameter("cluster")
P
pengcong06 已提交
186
	namespace := req.PathParameter("namespace")
Z
Zhengyi Lai 已提交
187
	applicationId := req.PathParameter("application")
P
pengcong06 已提交
188 189 190 191 192 193 194 195 196 197
	var upgradeClusterRequest openpitrix.UpgradeClusterRequest
	err := req.ReadEntity(&upgradeClusterRequest)
	if err != nil {
		klog.V(4).Infoln(err)
		api.HandleBadRequest(resp, nil, err)
		return
	}

	upgradeClusterRequest.Username = req.HeaderParameter(constants.UserNameHeader)

Z
Zhengyi Lai 已提交
198
	app, err := h.openpitrix.DescribeApplication(namespace, applicationId, clusterName)
P
pengcong06 已提交
199 200 201 202 203 204

	if err != nil {
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
		return
	}
H
hongming 已提交
205

Z
Zhengyi Lai 已提交
206 207
	if clusterName != app.Cluster.RuntimeId {
		err = fmt.Errorf("runtime and cluster not match %s,%s", app.Cluster.RuntimeId, clusterName)
H
hongming 已提交
208
		klog.V(4).Infoln(err)
Z
zryfish 已提交
209
		api.HandleForbidden(resp, nil, err)
H
hongming 已提交
210 211 212
		return
	}

P
pengcong06 已提交
213
	err = h.openpitrix.UpgradeApplication(upgradeClusterRequest)
H
hongming 已提交
214 215

	if err != nil {
H
hongming 已提交
216
		klog.Errorln(err)
P
pengcong06 已提交
217
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230
		return
	}

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) GetAppVersionPackage(req *restful.Request, resp *restful.Response) {
	appId := req.PathParameter("app")
	versionId := req.PathParameter("version")

	result, err := h.openpitrix.GetAppVersionPackage(appId, versionId)

	if err != nil {
H
hongming 已提交
231
		klog.Errorln(err)
Z
zryfish 已提交
232
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
233 234 235 236 237 238 239 240 241 242
		return
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) DoAppAction(req *restful.Request, resp *restful.Response) {
	var doActionRequest openpitrix.ActionRequest
	err := req.ReadEntity(&doActionRequest)
	if err != nil {
H
hongming 已提交
243
		klog.V(4).Infoln(err)
Z
zryfish 已提交
244
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
245 246 247 248 249 250 251 252
		return
	}

	appId := req.PathParameter("app")

	err = h.openpitrix.DoAppAction(appId, &doActionRequest)

	if err != nil {
H
hongming 已提交
253 254
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
255 256 257 258 259 260 261 262 263 264
		return
	}

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) DoAppVersionAction(req *restful.Request, resp *restful.Response) {
	var doActionRequest openpitrix.ActionRequest
	err := req.ReadEntity(&doActionRequest)
	if err != nil {
H
hongming 已提交
265
		klog.V(4).Infoln(err)
Z
zryfish 已提交
266
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
267 268 269 270 271 272 273 274 275
		return
	}
	doActionRequest.Username = req.HeaderParameter(constants.UserNameHeader)

	versionId := req.PathParameter("version")

	err = h.openpitrix.DoAppVersionAction(versionId, &doActionRequest)

	if err != nil {
H
hongming 已提交
276 277
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
		return
	}

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) GetAppVersionFiles(req *restful.Request, resp *restful.Response) {
	versionId := req.PathParameter("version")
	getAppVersionFilesRequest := &openpitrix.GetAppVersionFilesRequest{}
	if f := req.QueryParameter("files"); f != "" {
		getAppVersionFilesRequest.Files = strings.Split(f, ",")
	}

	result, err := h.openpitrix.GetAppVersionFiles(versionId, getAppVersionFilesRequest)

	if err != nil {
H
hongming 已提交
294 295
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
296 297 298 299 300 301 302 303 304
		return
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) ListAppVersionAudits(req *restful.Request, resp *restful.Response) {
	limit, offset := params.ParsePaging(req)
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.StatusTime)
H
hongming 已提交
305
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
306 307 308 309 310
	appId := req.PathParameter("app")
	versionId := req.PathParameter("version")
	conditions, err := params.ParseConditions(req)

	if err != nil {
H
hongming 已提交
311
		klog.V(4).Infoln(err)
Z
zryfish 已提交
312
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
313 314 315 316 317 318 319 320 321 322 323
		return
	}

	conditions.Match[openpitrix.AppId] = appId
	if versionId != "" {
		conditions.Match[openpitrix.VersionId] = versionId
	}

	result, err := h.openpitrix.ListAppVersionAudits(conditions, orderBy, reverse, limit, offset)

	if err != nil {
H
hongming 已提交
324 325
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
326 327 328 329 330 331 332 333 334
		return
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) ListReviews(req *restful.Request, resp *restful.Response) {
	limit, offset := params.ParsePaging(req)
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.StatusTime)
H
hongming 已提交
335
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
336 337 338
	conditions, err := params.ParseConditions(req)

	if err != nil {
H
hongming 已提交
339
		klog.V(4).Infoln(err)
Z
zryfish 已提交
340
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
341 342 343 344 345 346
		return
	}

	result, err := h.openpitrix.ListAppVersionReviews(conditions, orderBy, reverse, limit, offset)

	if err != nil {
H
hongming 已提交
347
		klog.Errorln(err)
Z
zryfish 已提交
348
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
349 350 351 352 353 354 355 356 357
		return
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) ListAppVersions(req *restful.Request, resp *restful.Response) {
	limit, offset := params.ParsePaging(req)
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
H
hongming 已提交
358
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
359 360 361 362 363
	appId := req.PathParameter("app")
	statistics := params.GetBoolValueWithDefault(req, "statistics", false)
	conditions, err := params.ParseConditions(req)

	if err != nil {
H
hongming 已提交
364
		klog.V(4).Infoln(err)
Z
zryfish 已提交
365
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
366 367 368 369 370 371 372
		return
	}
	conditions.Match[openpitrix.AppId] = appId

	result, err := h.openpitrix.ListAppVersions(conditions, orderBy, reverse, limit, offset)

	if err != nil {
H
hongming 已提交
373
		klog.Errorln(err)
Z
zryfish 已提交
374
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
375 376 377 378 379 380
		return
	}

	if statistics {
		for _, item := range result.Items {
			if version, ok := item.(*openpitrix.AppVersion); ok {
H
hongming 已提交
381
				statisticsResult, err := h.openpitrix.ListApplications(&params.Conditions{Match: map[string]string{openpitrix.AppId: version.AppId, openpitrix.VersionId: version.VersionId}}, 0, 0, "", false)
H
hongming 已提交
382
				if err != nil {
H
hongming 已提交
383
					klog.Errorln(err)
Z
zryfish 已提交
384
					api.HandleInternalError(resp, nil, err)
H
hongming 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397
					return
				}
				version.ClusterTotal = &statisticsResult.TotalCount
			}
		}
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) ListApps(req *restful.Request, resp *restful.Response) {
	limit, offset := params.ParsePaging(req)
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
H
hongming 已提交
398
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
399 400 401 402
	statistics := params.GetBoolValueWithDefault(req, "statistics", false)
	conditions, err := params.ParseConditions(req)

	if err != nil {
H
hongming 已提交
403
		klog.V(4).Infoln(err)
Z
zryfish 已提交
404
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
405 406 407
		return
	}

H
hongming 已提交
408 409 410 411 412 413 414 415
	if req.PathParameter("workspace") != "" {
		conditions.Match[openpitrix.ISV] = req.PathParameter("workspace")
	}

	if conditions.Match[openpitrix.ISV] == "" {
		conditions.Match[openpitrix.ISV] = req.QueryParameter("workspace")
	}

H
hongming 已提交
416 417 418
	result, err := h.openpitrix.ListApps(conditions, orderBy, reverse, limit, offset)

	if err != nil {
H
hongming 已提交
419 420
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
421 422 423 424 425 426 427 428 429
		return
	}

	if statistics {
		for _, item := range result.Items {
			if app, ok := item.(*openpitrix.App); ok {
				statuses := "active|used|enabled|stopped|pending|creating|upgrading|updating|rollbacking|stopping|starting|recovering|resizing|scaling|deleting"
				statisticsResult, err := h.openpitrix.ListApplications(&params.Conditions{Match: map[string]string{openpitrix.AppId: app.AppId, openpitrix.Status: statuses}}, 0, 0, "", false)
				if err != nil {
H
hongming 已提交
430 431
					klog.Errorln(err)
					handleOpenpitrixError(resp, err)
H
hongming 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
					return
				}
				app.ClusterTotal = &statisticsResult.TotalCount
			}
		}
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) ModifyApp(req *restful.Request, resp *restful.Response) {

	var patchAppRequest openpitrix.ModifyAppRequest
	err := req.ReadEntity(&patchAppRequest)
	appId := req.PathParameter("app")

	if err != nil {
H
hongming 已提交
449
		klog.V(4).Infoln(err)
Z
zryfish 已提交
450
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
451 452 453 454 455 456
		return
	}

	err = h.openpitrix.ModifyApp(appId, &patchAppRequest)

	if err != nil {
H
hongming 已提交
457 458
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
459 460 461 462 463 464 465 466 467 468 469 470
		return
	}

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) DescribeApp(req *restful.Request, resp *restful.Response) {
	appId := req.PathParameter("app")

	result, err := h.openpitrix.DescribeApp(appId)

	if err != nil {
H
hongming 已提交
471 472
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485
		return
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) DeleteApp(req *restful.Request, resp *restful.Response) {
	appId := req.PathParameter("app")

	err := h.openpitrix.DeleteApp(appId)

	if err != nil {
		if status.Code(err) == codes.NotFound {
Z
zryfish 已提交
486
			api.HandleNotFound(resp, nil, err)
H
hongming 已提交
487 488
			return
		}
Z
zryfish 已提交
489
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
490 491 492 493 494 495 496 497 498 499
		return
	}

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) CreateApp(req *restful.Request, resp *restful.Response) {
	createAppRequest := &openpitrix.CreateAppRequest{}
	err := req.ReadEntity(createAppRequest)
	if err != nil {
H
hongming 已提交
500
		klog.V(4).Infoln(err)
Z
zryfish 已提交
501
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
502 503 504 505 506
		return
	}

	createAppRequest.Username = req.HeaderParameter(constants.UserNameHeader)

Z
Zhengyi Lai 已提交
507 508 509 510
	if req.PathParameter("workspace") != "" {
		createAppRequest.Isv = req.PathParameter("workspace")
	}

H
hongming 已提交
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
	validate, _ := strconv.ParseBool(req.QueryParameter("validate"))

	var result interface{}

	if validate {
		validatePackageRequest := &openpitrix.ValidatePackageRequest{
			VersionPackage: createAppRequest.VersionPackage,
			VersionType:    createAppRequest.VersionType,
		}
		result, err = h.openpitrix.ValidatePackage(validatePackageRequest)
	} else {
		result, err = h.openpitrix.CreateApp(createAppRequest)
	}

	if err != nil {
		if status.Code(err) == codes.InvalidArgument {
Z
zryfish 已提交
527
			api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
528 529
			return
		}
Z
zryfish 已提交
530
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
531 532 533 534 535 536 537 538 539 540
		return
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) CreateAppVersion(req *restful.Request, resp *restful.Response) {
	var createAppVersionRequest openpitrix.CreateAppVersionRequest
	err := req.ReadEntity(&createAppVersionRequest)
	if err != nil {
H
hongming 已提交
541
		klog.V(4).Infoln(err)
Z
zryfish 已提交
542
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
		return
	}
	// override app id
	createAppVersionRequest.AppId = req.PathParameter("app")
	createAppVersionRequest.Username = req.HeaderParameter(constants.UserNameHeader)

	validate, _ := strconv.ParseBool(req.QueryParameter("validate"))

	var result interface{}

	if validate {
		validatePackageRequest := &openpitrix.ValidatePackageRequest{
			VersionPackage: createAppVersionRequest.Package,
			VersionType:    createAppVersionRequest.Type,
		}
		result, err = h.openpitrix.ValidatePackage(validatePackageRequest)
	} else {
		result, err = h.openpitrix.CreateAppVersion(&createAppVersionRequest)
	}

	if err != nil {
H
hongming 已提交
564 565
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578
		return
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) ModifyAppVersion(req *restful.Request, resp *restful.Response) {

	var patchAppVersionRequest openpitrix.ModifyAppVersionRequest
	err := req.ReadEntity(&patchAppVersionRequest)
	versionId := req.PathParameter("version")

	if err != nil {
H
hongming 已提交
579
		klog.V(4).Infoln(err)
Z
zryfish 已提交
580
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
581 582 583 584 585 586
		return
	}

	err = h.openpitrix.ModifyAppVersion(versionId, &patchAppVersionRequest)

	if err != nil {
H
hongming 已提交
587 588
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
589 590 591 592 593 594 595 596 597 598 599 600
		return
	}

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) DeleteAppVersion(req *restful.Request, resp *restful.Response) {
	versionId := req.PathParameter("version")

	err := h.openpitrix.DeleteAppVersion(versionId)

	if err != nil {
H
hongming 已提交
601 602
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
603 604 605 606 607 608 609 610 611 612 613 614
		return
	}

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) DescribeAppVersion(req *restful.Request, resp *restful.Response) {
	versionId := req.PathParameter("version")

	result, err := h.openpitrix.DescribeAppVersion(versionId)

	if err != nil {
H
hongming 已提交
615 616
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
617 618 619 620 621 622 623 624 625 626 627 628
		return
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) DescribeAttachment(req *restful.Request, resp *restful.Response) {
	attachmentId := req.PathParameter("attachment")
	fileName := req.QueryParameter("filename")
	result, err := h.openpitrix.DescribeAttachment(attachmentId)

	if err != nil {
H
hongming 已提交
629 630
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
		return
	}

	// file raw
	if fileName != "" {
		data := result.AttachmentContent[fileName]
		resp.Write(data)
		resp.Header().Set("Content-Type", "text/plain")
		return
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) CreateCategory(req *restful.Request, resp *restful.Response) {
	createCategoryRequest := &openpitrix.CreateCategoryRequest{}
	err := req.ReadEntity(createCategoryRequest)
	if err != nil {
H
hongming 已提交
649
		klog.V(4).Infoln(err)
Z
zryfish 已提交
650
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
651 652 653 654 655 656
		return
	}

	result, err := h.openpitrix.CreateCategory(createCategoryRequest)

	if err != nil {
H
hongming 已提交
657 658
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
659 660 661 662 663 664 665 666 667 668 669
		return
	}

	resp.WriteEntity(result)
}
func (h *openpitrixHandler) DeleteCategory(req *restful.Request, resp *restful.Response) {
	categoryId := req.PathParameter("category")

	err := h.openpitrix.DeleteCategory(categoryId)

	if err != nil {
H
hongming 已提交
670 671
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
672 673 674 675 676 677 678 679 680 681
		return
	}

	resp.WriteEntity(errors.None)
}
func (h *openpitrixHandler) ModifyCategory(req *restful.Request, resp *restful.Response) {
	var modifyCategoryRequest openpitrix.ModifyCategoryRequest
	categoryId := req.PathParameter("category")
	err := req.ReadEntity(&modifyCategoryRequest)
	if err != nil {
H
hongming 已提交
682
		klog.V(4).Infoln(err)
Z
zryfish 已提交
683
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
684 685 686 687 688 689
		return
	}

	err = h.openpitrix.ModifyCategory(categoryId, &modifyCategoryRequest)

	if err != nil {
H
hongming 已提交
690 691
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
692 693 694 695 696 697 698 699 700 701 702
		return
	}

	resp.WriteEntity(errors.None)
}
func (h *openpitrixHandler) DescribeCategory(req *restful.Request, resp *restful.Response) {
	categoryId := req.PathParameter("category")

	result, err := h.openpitrix.DescribeCategory(categoryId)

	if err != nil {
H
hongming 已提交
703 704
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
705 706 707 708 709 710 711 712
		return
	}

	resp.WriteEntity(result)
}
func (h *openpitrixHandler) ListCategories(req *restful.Request, resp *restful.Response) {
	limit, offset := params.ParsePaging(req)
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
H
hongming 已提交
713
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
714 715 716 717
	statistics := params.GetBoolValueWithDefault(req, "statistics", false)
	conditions, err := params.ParseConditions(req)

	if err != nil {
H
hongming 已提交
718
		klog.V(4).Infoln(err)
Z
zryfish 已提交
719
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
720 721 722 723 724 725
		return
	}

	result, err := h.openpitrix.ListCategories(conditions, orderBy, reverse, limit, offset)

	if err != nil {
H
hongming 已提交
726 727
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
728 729 730 731 732 733 734 735
		return
	}

	if statistics {
		for _, item := range result.Items {
			if category, ok := item.(*openpitrix.Category); ok {
				statisticsResult, err := h.openpitrix.ListApps(&params.Conditions{Match: map[string]string{"category_id": category.CategoryID, "status": openpitrix.StatusActive, "repo": openpitrix.BuiltinRepoId}}, "", false, 0, 0)
				if err != nil {
H
hongming 已提交
736 737
					klog.Errorln(err)
					handleOpenpitrixError(resp, err)
H
hongming 已提交
738 739 740 741 742 743 744 745 746 747 748 749 750 751
					return
				}
				category.AppTotal = &statisticsResult.TotalCount
			}
		}
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) CreateRepo(req *restful.Request, resp *restful.Response) {
	createRepoRequest := &openpitrix.CreateRepoRequest{}
	err := req.ReadEntity(createRepoRequest)
	if err != nil {
H
hongming 已提交
752
		klog.V(4).Infoln(err)
Z
zryfish 已提交
753
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
		return
	}
	validate, _ := strconv.ParseBool(req.QueryParameter("validate"))

	var result interface{}

	if validate {
		validateRepoRequest := &openpitrix.ValidateRepoRequest{
			Type:       createRepoRequest.Type,
			Url:        createRepoRequest.URL,
			Credential: createRepoRequest.Credential,
		}
		result, err = h.openpitrix.ValidateRepo(validateRepoRequest)
	} else {
		result, err = h.openpitrix.CreateRepo(createRepoRequest)
	}

	if err != nil {
H
hongming 已提交
772 773
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
774 775 776 777 778 779 780 781 782 783 784
		return
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) DoRepoAction(req *restful.Request, resp *restful.Response) {
	repoActionRequest := &openpitrix.RepoActionRequest{}
	repoId := req.PathParameter("repo")
	err := req.ReadEntity(repoActionRequest)
	if err != nil {
H
hongming 已提交
785
		klog.V(4).Infoln(err)
Z
zryfish 已提交
786
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
787 788 789 790 791 792
		return
	}

	err = h.openpitrix.DoRepoAction(repoId, repoActionRequest)

	if err != nil {
H
hongming 已提交
793 794
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
795 796 797 798 799 800 801 802 803 804 805 806
		return
	}

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) DeleteRepo(req *restful.Request, resp *restful.Response) {
	repoId := req.PathParameter("repo")

	err := h.openpitrix.DeleteRepo(repoId)

	if err != nil {
H
hongming 已提交
807 808
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
809 810 811 812 813 814 815 816 817 818 819
		return
	}

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) ModifyRepo(req *restful.Request, resp *restful.Response) {
	var updateRepoRequest openpitrix.ModifyRepoRequest
	repoId := req.PathParameter("repo")
	err := req.ReadEntity(&updateRepoRequest)
	if err != nil {
H
hongming 已提交
820
		klog.V(4).Infoln(err)
Z
zryfish 已提交
821
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
822 823 824 825 826 827
		return
	}

	err = h.openpitrix.ModifyRepo(repoId, &updateRepoRequest)

	if err != nil {
H
hongming 已提交
828 829
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
830 831 832 833 834 835 836 837 838 839 840 841
		return
	}

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) DescribeRepo(req *restful.Request, resp *restful.Response) {
	repoId := req.PathParameter("repo")

	result, err := h.openpitrix.DescribeRepo(repoId)

	if err != nil {
H
hongming 已提交
842 843
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
844 845 846 847 848
		return
	}

	resp.WriteEntity(result)
}
P
pengcong06 已提交
849

H
hongming 已提交
850 851 852
func (h *openpitrixHandler) ListRepos(req *restful.Request, resp *restful.Response) {
	limit, offset := params.ParsePaging(req)
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
H
hongming 已提交
853
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
854 855
	conditions, err := params.ParseConditions(req)

Z
Zhengyi Lai 已提交
856 857 858 859
	if req.PathParameter("workspace") != "" {
		conditions.Match[openpitrix.WorkspaceLabel] = req.PathParameter("workspace")
	}

H
hongming 已提交
860
	if err != nil {
H
hongming 已提交
861
		klog.V(4).Infoln(err)
Z
zryfish 已提交
862
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
863 864 865 866 867 868
		return
	}

	result, err := h.openpitrix.ListRepos(conditions, orderBy, reverse, limit, offset)

	if err != nil {
H
hongming 已提交
869 870
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
871 872 873 874 875 876 877 878 879 880 881 882
		return
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) ListRepoEvents(req *restful.Request, resp *restful.Response) {
	repoId := req.PathParameter("repo")
	limit, offset := params.ParsePaging(req)
	conditions, err := params.ParseConditions(req)

	if err != nil {
H
hongming 已提交
883
		klog.V(4).Infoln(err)
Z
zryfish 已提交
884
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
885 886 887 888 889 890
		return
	}

	result, err := h.openpitrix.ListRepoEvents(repoId, conditions, limit, offset)

	if err != nil {
H
hongming 已提交
891 892
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
893 894 895 896
		return
	}

	resp.WriteEntity(result)
Z
zryfish 已提交
897
}
H
hongming 已提交
898 899 900 901

func handleOpenpitrixError(resp *restful.Response, err error) {
	if status.Code(err) == codes.NotFound {
		klog.V(4).Infoln(err)
Z
zryfish 已提交
902
		api.HandleNotFound(resp, nil, err)
H
hongming 已提交
903 904 905 906
		return
	}
	if status.Code(err) == codes.InvalidArgument {
		klog.V(4).Infoln(err)
Z
zryfish 已提交
907
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
908 909 910
		return
	}
	klog.Errorln(err)
Z
zryfish 已提交
911
	api.HandleInternalError(resp, nil, err)
H
hongming 已提交
912
}