handler.go 22.9 KB
Newer Older
Z
zryfish 已提交
1 2
package v1

Z
zryfish 已提交
3
import (
H
hongming 已提交
4
	"fmt"
Z
zryfish 已提交
5
	"github.com/emicklei/go-restful"
H
hongming 已提交
6 7 8
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	k8sinformers "k8s.io/client-go/informers"
H
hongming 已提交
9
	"k8s.io/klog"
Z
zryfish 已提交
10 11 12 13
	"kubesphere.io/kubesphere/pkg/api"
	"kubesphere.io/kubesphere/pkg/constants"
	"kubesphere.io/kubesphere/pkg/informers"
	"kubesphere.io/kubesphere/pkg/models"
H
hongming 已提交
14 15
	"kubesphere.io/kubesphere/pkg/models/openpitrix"
	"kubesphere.io/kubesphere/pkg/server/errors"
Z
zryfish 已提交
16
	"kubesphere.io/kubesphere/pkg/server/params"
H
hongming 已提交
17 18 19
	op "kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
	"strconv"
	"strings"
Z
zryfish 已提交
20
)
Z
zryfish 已提交
21 22

type openpitrixHandler struct {
H
hongming 已提交
23 24
	openpitrix openpitrix.Interface
	informers  k8sinformers.SharedInformerFactory
Z
zryfish 已提交
25 26
}

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

Z
zryfish 已提交
29
	return &openpitrixHandler{
H
hongming 已提交
30 31
		openpitrix: openpitrix.NewOpenpitrixOperator(factory.KubernetesSharedInformerFactory(), opClient),
		informers:  factory.KubernetesSharedInformerFactory(),
Z
zryfish 已提交
32
	}
Z
zryfish 已提交
33 34
}

H
hongming 已提交
35 36 37 38
func (h *openpitrixHandler) ListApplications(request *restful.Request, response *restful.Response) {
	limit, offset := params.ParsePaging(request)
	namespace := request.PathParameter("namespace")
	orderBy := params.GetStringValueWithDefault(request, params.OrderByParam, openpitrix.CreateTime)
H
hongming 已提交
39
	reverse := params.GetBoolValueWithDefault(request, params.ReverseParam, false)
H
hongming 已提交
40
	conditions, err := params.ParseConditions(request)
Z
zryfish 已提交
41 42

	if err != nil {
H
hongming 已提交
43
		klog.V(4).Infoln(err)
Z
zryfish 已提交
44
		api.HandleBadRequest(response, nil, err)
Z
zryfish 已提交
45 46 47
		return
	}

H
hongming 已提交
48 49 50
	// filter namespaced applications by runtime_id
	if namespace != "" {
		ns, err := h.informers.Core().V1().Namespaces().Lister().Get(namespace)
Z
zryfish 已提交
51 52

		if err != nil {
H
hongming 已提交
53
			klog.Errorln(err)
Z
zryfish 已提交
54
			api.HandleInternalError(response, nil, err)
Z
zryfish 已提交
55 56 57
			return
		}

H
hongming 已提交
58
		runtimeId := ns.Annotations[constants.OpenPitrixRuntimeAnnotationKey]
Z
zryfish 已提交
59 60

		if runtimeId == "" {
H
hongming 已提交
61
			// runtime id not exist,return empty response
Z
zryfish 已提交
62 63 64
			response.WriteAsJson(models.PageableResponse{Items: []interface{}{}, TotalCount: 0})
			return
		} else {
H
hongming 已提交
65 66
			// filter by runtime id
			conditions.Match[openpitrix.RuntimeId] = runtimeId
Z
zryfish 已提交
67 68 69
		}
	}

H
hongming 已提交
70
	result, err := h.openpitrix.ListApplications(conditions, limit, offset, orderBy, reverse)
Z
zryfish 已提交
71 72

	if err != nil {
H
hongming 已提交
73
		klog.Errorln(err)
Z
zryfish 已提交
74
		api.HandleInternalError(response, nil, err)
Z
zryfish 已提交
75 76 77
		return
	}

H
hongming 已提交
78 79 80 81 82 83 84 85 86 87
	response.WriteAsJson(result)
}

func (h *openpitrixHandler) DescribeApplication(req *restful.Request, resp *restful.Response) {
	clusterId := req.PathParameter("application")
	namespace := req.PathParameter("namespace")

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

	if err != nil {
H
hongming 已提交
88 89
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
90 91 92 93 94 95
		return
	}

	ns, err := h.informers.Core().V1().Namespaces().Lister().Get(namespace)

	if err != nil {
H
hongming 已提交
96
		klog.Errorln(err)
Z
zryfish 已提交
97
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
98 99 100 101 102 103 104
		return
	}

	runtimeId := ns.Annotations[constants.OpenPitrixRuntimeAnnotationKey]

	if runtimeId != app.Cluster.RuntimeId {
		err = fmt.Errorf("rumtime not match %s,%s", app.Cluster.RuntimeId, runtimeId)
H
hongming 已提交
105
		klog.V(4).Infoln(err)
Z
zryfish 已提交
106
		api.HandleForbidden(resp, nil, err)
H
hongming 已提交
107 108 109 110 111 112 113 114 115 116 117 118
		return
	}

	resp.WriteEntity(app)
	return
}

func (h *openpitrixHandler) CreateApplication(req *restful.Request, resp *restful.Response) {
	namespace := req.PathParameter("namespace")
	var createClusterRequest openpitrix.CreateClusterRequest
	err := req.ReadEntity(&createClusterRequest)
	if err != nil {
H
hongming 已提交
119
		klog.V(4).Infoln(err)
Z
zryfish 已提交
120
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
121 122 123 124 125 126 127 128
		return
	}

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

	err = h.openpitrix.CreateApplication(namespace, createClusterRequest)

	if err != nil {
H
hongming 已提交
129
		klog.Errorln(err)
Z
zryfish 已提交
130
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
131 132 133 134 135 136 137 138 139 140 141 142
		return
	}

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) ModifyApplication(req *restful.Request, resp *restful.Response) {
	var modifyClusterAttributesRequest openpitrix.ModifyClusterAttributesRequest
	clusterId := req.PathParameter("application")
	namespace := req.PathParameter("namespace")
	err := req.ReadEntity(&modifyClusterAttributesRequest)
	if err != nil {
H
hongming 已提交
143
		klog.V(4).Infoln(err)
Z
zryfish 已提交
144
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
145 146 147 148 149 150
		return
	}

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

	if err != nil {
H
hongming 已提交
151 152
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
153 154 155 156 157 158
		return
	}

	ns, err := h.informers.Core().V1().Namespaces().Lister().Get(namespace)

	if err != nil {
H
hongming 已提交
159
		klog.Errorln(err)
Z
zryfish 已提交
160
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
161 162 163 164 165 166 167
		return
	}

	runtimeId := ns.Annotations[constants.OpenPitrixRuntimeAnnotationKey]

	if runtimeId != app.Cluster.RuntimeId {
		err = fmt.Errorf("rumtime not match %s,%s", app.Cluster.RuntimeId, runtimeId)
H
hongming 已提交
168
		klog.V(4).Infoln(err)
Z
zryfish 已提交
169
		api.HandleForbidden(resp, nil, err)
H
hongming 已提交
170 171 172 173 174 175
		return
	}

	err = h.openpitrix.ModifyApplication(modifyClusterAttributesRequest)

	if err != nil {
H
hongming 已提交
176 177
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
178 179 180 181 182 183 184 185 186 187 188 189
		return
	}

	resp.WriteEntity(errors.None)
}

func (h *openpitrixHandler) DeleteApplication(req *restful.Request, resp *restful.Response) {
	clusterId := req.PathParameter("application")
	namespace := req.PathParameter("namespace")
	app, err := h.openpitrix.DescribeApplication(namespace, clusterId)

	if err != nil {
H
hongming 已提交
190 191
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
192 193 194 195 196 197
		return
	}

	ns, err := h.informers.Core().V1().Namespaces().Lister().Get(namespace)

	if err != nil {
H
hongming 已提交
198
		klog.Errorln(err)
Z
zryfish 已提交
199
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
200 201 202 203 204 205 206
		return
	}

	runtimeId := ns.Annotations[constants.OpenPitrixRuntimeAnnotationKey]

	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 212 213 214
		return
	}

	err = h.openpitrix.DeleteApplication(clusterId)

	if err != nil {
H
hongming 已提交
215 216
		klog.Errorln(err)
		handleOpenpitrixError(resp, 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 836 837 838
		return
	}

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

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

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

	if err != nil {
H
hongming 已提交
851 852
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
853 854 855 856 857 858 859 860 861 862 863 864
		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 已提交
865
		klog.V(4).Infoln(err)
Z
zryfish 已提交
866
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
867 868 869 870 871 872
		return
	}

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

	if err != nil {
H
hongming 已提交
873 874
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
875 876 877 878
		return
	}

	resp.WriteEntity(result)
Z
zryfish 已提交
879
}
H
hongming 已提交
880 881 882 883

func handleOpenpitrixError(resp *restful.Response, err error) {
	if status.Code(err) == codes.NotFound {
		klog.V(4).Infoln(err)
Z
zryfish 已提交
884
		api.HandleNotFound(resp, nil, err)
H
hongming 已提交
885 886 887 888
		return
	}
	if status.Code(err) == codes.InvalidArgument {
		klog.V(4).Infoln(err)
Z
zryfish 已提交
889
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
890 891 892
		return
	}
	klog.Errorln(err)
Z
zryfish 已提交
893
	api.HandleInternalError(resp, nil, err)
H
hongming 已提交
894
}