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
}

H
hongming 已提交
47 48
func (h *openpitrixHandler) ListApplications(request *restful.Request, response *restful.Response) {
	limit, offset := params.ParsePaging(request)
P
pengcong06 已提交
49
	runtimeId := request.PathParameter("runtime")
H
hongming 已提交
50 51
	namespace := request.PathParameter("namespace")
	orderBy := params.GetStringValueWithDefault(request, params.OrderByParam, openpitrix.CreateTime)
H
hongming 已提交
52
	reverse := params.GetBoolValueWithDefault(request, params.ReverseParam, false)
H
hongming 已提交
53
	conditions, err := params.ParseConditions(request)
Z
zryfish 已提交
54 55

	if err != nil {
H
hongming 已提交
56
		klog.V(4).Infoln(err)
Z
zryfish 已提交
57
		api.HandleBadRequest(response, nil, err)
Z
zryfish 已提交
58 59
		return
	}
P
pengcong06 已提交
60 61
	conditions.Match[openpitrix.Zone] = namespace
	conditions.Match[openpitrix.RuntimeId] = runtimeId
Z
zryfish 已提交
62

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

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

H
hongming 已提交
71 72 73 74
	response.WriteAsJson(result)
}

func (h *openpitrixHandler) DescribeApplication(req *restful.Request, resp *restful.Response) {
P
pengcong06 已提交
75
	runtimeId := req.PathParameter("runtime")
H
hongming 已提交
76
	namespace := req.PathParameter("namespace")
P
pengcong06 已提交
77
	clusterId := req.PathParameter("application")
H
hongming 已提交
78

P
pengcong06 已提交
79
	app, err := h.openpitrix.DescribeApplication(namespace, clusterId, runtimeId)
H
hongming 已提交
80 81

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

	resp.WriteEntity(app)
	return
}

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

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

P
pengcong06 已提交
104
	err = h.openpitrix.CreateApplication(runtimeId, namespace, createClusterRequest)
H
hongming 已提交
105 106

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

	resp.WriteEntity(errors.None)
}

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

P
pengcong06 已提交
127
	app, err := h.openpitrix.DescribeApplication(namespace, clusterId, runtimeId)
H
hongming 已提交
128 129

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

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

	err = h.openpitrix.ModifyApplication(modifyClusterAttributesRequest)

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

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) DeleteApplication(req *restful.Request, resp *restful.Response) {
P
pengcong06 已提交
154
	runtimeId := req.PathParameter("runtime")
H
hongming 已提交
155 156
	clusterId := req.PathParameter("application")
	namespace := req.PathParameter("namespace")
P
pengcong06 已提交
157
	app, err := h.openpitrix.DescribeApplication(namespace, clusterId, runtimeId)
H
hongming 已提交
158 159

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

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

	err = h.openpitrix.DeleteApplication(clusterId)
H
hongming 已提交
173 174

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

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

func (h *openpitrixHandler) UpgradeApplication(req *restful.Request, resp *restful.Response) {
P
pengcong06 已提交
184
	runtimeId := req.PathParameter("runtime")
P
pengcong06 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	namespace := req.PathParameter("namespace")
	clusterId := req.PathParameter("application")
	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)

	app, err := h.openpitrix.DescribeApplication(namespace, clusterId, runtimeId)

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

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

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

	if err != nil {
H
hongming 已提交
215
		klog.Errorln(err)
P
pengcong06 已提交
216
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229
		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 已提交
230
		klog.Errorln(err)
Z
zryfish 已提交
231
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
232 233 234 235 236 237 238 239 240 241
		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 已提交
242
		klog.V(4).Infoln(err)
Z
zryfish 已提交
243
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
244 245 246 247 248 249 250 251
		return
	}

	appId := req.PathParameter("app")

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

	if err != nil {
H
hongming 已提交
252 253
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
254 255 256 257 258 259 260 261 262 263
		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 已提交
264
		klog.V(4).Infoln(err)
Z
zryfish 已提交
265
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
266 267 268 269 270 271 272 273 274
		return
	}
	doActionRequest.Username = req.HeaderParameter(constants.UserNameHeader)

	versionId := req.PathParameter("version")

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

	if err != nil {
H
hongming 已提交
275 276
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
		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 已提交
293 294
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
295 296 297 298 299 300 301 302 303
		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 已提交
304
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
305 306 307 308 309
	appId := req.PathParameter("app")
	versionId := req.PathParameter("version")
	conditions, err := params.ParseConditions(req)

	if err != nil {
H
hongming 已提交
310
		klog.V(4).Infoln(err)
Z
zryfish 已提交
311
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
312 313 314 315 316 317 318 319 320 321 322
		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 已提交
323 324
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
325 326 327 328 329 330 331 332 333
		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 已提交
334
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
335 336 337
	conditions, err := params.ParseConditions(req)

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

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

	if err != nil {
H
hongming 已提交
346
		klog.Errorln(err)
Z
zryfish 已提交
347
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
348 349 350 351 352 353 354 355 356
		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 已提交
357
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
358 359 360 361 362
	appId := req.PathParameter("app")
	statistics := params.GetBoolValueWithDefault(req, "statistics", false)
	conditions, err := params.ParseConditions(req)

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

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

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

	if statistics {
		for _, item := range result.Items {
			if version, ok := item.(*openpitrix.AppVersion); ok {
				statisticsResult, err := h.openpitrix.ListApplications(&params.Conditions{Match: map[string]string{"app_id": version.AppId, "version_id": version.VersionId}}, 0, 0, "", false)
				if err != nil {
H
hongming 已提交
382
					klog.Errorln(err)
Z
zryfish 已提交
383
					api.HandleInternalError(resp, nil, err)
H
hongming 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396
					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 已提交
397
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
398 399 400 401
	statistics := params.GetBoolValueWithDefault(req, "statistics", false)
	conditions, err := params.ParseConditions(req)

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

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

	if err != nil {
H
hongming 已提交
410 411
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
412 413 414 415 416 417 418 419 420
		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 已提交
421 422
					klog.Errorln(err)
					handleOpenpitrixError(resp, err)
H
hongming 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
					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 已提交
440
		klog.V(4).Infoln(err)
Z
zryfish 已提交
441
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
442 443 444 445 446 447
		return
	}

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

	if err != nil {
H
hongming 已提交
448 449
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
450 451 452 453 454 455 456 457 458 459 460 461
		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 已提交
462 463
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476
		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 已提交
477
			api.HandleNotFound(resp, nil, err)
H
hongming 已提交
478 479
			return
		}
Z
zryfish 已提交
480
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
481 482 483 484 485 486 487 488 489 490
		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 已提交
491
		klog.V(4).Infoln(err)
Z
zryfish 已提交
492
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
		return
	}

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

	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 已提交
514
			api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
515 516
			return
		}
Z
zryfish 已提交
517
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
518 519 520 521 522 523 524 525 526 527
		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 已提交
528
		klog.V(4).Infoln(err)
Z
zryfish 已提交
529
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
		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 已提交
551 552
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565
		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 已提交
566
		klog.V(4).Infoln(err)
Z
zryfish 已提交
567
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
568 569 570 571 572 573
		return
	}

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

	if err != nil {
H
hongming 已提交
574 575
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
576 577 578 579 580 581 582 583 584 585 586 587
		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 已提交
588 589
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
590 591 592 593 594 595 596 597 598 599 600 601
		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 已提交
602 603
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
604 605 606 607 608 609 610 611 612 613 614 615
		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 已提交
616 617
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
		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 已提交
636
		klog.V(4).Infoln(err)
Z
zryfish 已提交
637
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
638 639 640 641 642 643
		return
	}

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

	if err != nil {
H
hongming 已提交
644 645
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
646 647 648 649 650 651 652 653 654 655 656
		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 已提交
657 658
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
659 660 661 662 663 664 665 666 667 668
		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 已提交
669
		klog.V(4).Infoln(err)
Z
zryfish 已提交
670
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
671 672 673 674 675 676
		return
	}

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

	if err != nil {
H
hongming 已提交
677 678
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
679 680 681 682 683 684 685 686 687 688 689
		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 已提交
690 691
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
692 693 694 695 696 697 698 699
		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 已提交
700
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
701 702 703 704
	statistics := params.GetBoolValueWithDefault(req, "statistics", false)
	conditions, err := params.ParseConditions(req)

	if err != nil {
H
hongming 已提交
705
		klog.V(4).Infoln(err)
Z
zryfish 已提交
706
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
707 708 709 710 711 712
		return
	}

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

	if err != nil {
H
hongming 已提交
713 714
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
715 716 717 718 719 720 721 722
		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 已提交
723 724
					klog.Errorln(err)
					handleOpenpitrixError(resp, err)
H
hongming 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738
					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 已提交
739
		klog.V(4).Infoln(err)
Z
zryfish 已提交
740
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
		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 已提交
759 760
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
761 762 763 764 765 766 767 768 769 770 771
		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 已提交
772
		klog.V(4).Infoln(err)
Z
zryfish 已提交
773
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
774 775 776 777 778 779
		return
	}

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

	if err != nil {
H
hongming 已提交
780 781
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
782 783 784 785 786 787 788 789 790 791 792 793
		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 已提交
794 795
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
796 797 798 799 800 801 802 803 804 805 806
		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 已提交
807
		klog.V(4).Infoln(err)
Z
zryfish 已提交
808
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
809 810 811 812 813 814
		return
	}

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

	if err != nil {
H
hongming 已提交
815 816
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
817 818 819 820 821 822 823 824 825 826 827 828
		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 已提交
829 830
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
831 832 833 834 835
		return
	}

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

H
hongming 已提交
837 838 839
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 已提交
840
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
841 842 843
	conditions, err := params.ParseConditions(req)

	if err != nil {
H
hongming 已提交
844
		klog.V(4).Infoln(err)
Z
zryfish 已提交
845
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
846 847 848 849
		return
	}

	result, err := h.openpitrix.ListRepos(conditions, orderBy, reverse, limit, offset)
P
pengcong06 已提交
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872

	if err != nil {
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
		return
	}

	resp.WriteEntity(result)
}

func (h *openpitrixHandler) ListEvents(req *restful.Request, resp *restful.Response) {
	limit, offset := params.ParsePaging(req)
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
	conditions, err := params.ParseConditions(req)

	if err != nil {
		klog.V(4).Infoln(err)
		api.HandleBadRequest(resp, nil, err)
		return
	}

	result, err := h.openpitrix.ListEvents(conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
873 874

	if err != nil {
H
hongming 已提交
875 876
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
877 878 879 880 881 882 883 884 885 886 887 888
		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 已提交
889
		klog.V(4).Infoln(err)
Z
zryfish 已提交
890
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
891 892 893 894 895 896
		return
	}

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

	if err != nil {
H
hongming 已提交
897 898
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
899 900 901 902
		return
	}

	resp.WriteEntity(result)
Z
zryfish 已提交
903
}
H
hongming 已提交
904 905 906 907

func handleOpenpitrixError(resp *restful.Response, err error) {
	if status.Code(err) == codes.NotFound {
		klog.V(4).Infoln(err)
Z
zryfish 已提交
908
		api.HandleNotFound(resp, nil, err)
H
hongming 已提交
909 910 911 912
		return
	}
	if status.Code(err) == codes.InvalidArgument {
		klog.V(4).Infoln(err)
Z
zryfish 已提交
913
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
914 915 916
		return
	}
	klog.Errorln(err)
Z
zryfish 已提交
917
	api.HandleInternalError(resp, nil, err)
H
hongming 已提交
918
}