handler.go 25.7 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 (
L
LiHui 已提交
17
	"encoding/json"
H
hongming 已提交
18
	"fmt"
Z
zryfish 已提交
19
	"github.com/emicklei/go-restful"
H
hongming 已提交
20 21
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
L
LiHui 已提交
22 23
	"io/ioutil"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
H
hongming 已提交
24
	"k8s.io/klog"
Z
zryfish 已提交
25
	"kubesphere.io/kubesphere/pkg/api"
L
LiHui 已提交
26 27 28
	"kubesphere.io/kubesphere/pkg/apis/application/v1alpha1"
	"kubesphere.io/kubesphere/pkg/apiserver/request"
	"kubesphere.io/kubesphere/pkg/client/clientset/versioned"
Z
zryfish 已提交
29 30
	"kubesphere.io/kubesphere/pkg/constants"
	"kubesphere.io/kubesphere/pkg/informers"
H
hongming 已提交
31 32
	"kubesphere.io/kubesphere/pkg/models/openpitrix"
	"kubesphere.io/kubesphere/pkg/server/errors"
Z
zryfish 已提交
33
	"kubesphere.io/kubesphere/pkg/server/params"
L
LiHui 已提交
34 35 36 37 38
	openpitrixoptions "kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
	"kubesphere.io/kubesphere/pkg/simple/client/s3"
	"kubesphere.io/kubesphere/pkg/utils/idutils"
	"kubesphere.io/kubesphere/pkg/utils/stringutils"
	"net/url"
H
hongming 已提交
39 40
	"strconv"
	"strings"
Z
zryfish 已提交
41
)
Z
zryfish 已提交
42 43

type openpitrixHandler struct {
H
hongming 已提交
44
	openpitrix openpitrix.Interface
Z
zryfish 已提交
45 46
}

L
LiHui 已提交
47 48 49 50 51 52 53 54 55
func newOpenpitrixHandler(ksInformers informers.InformerFactory, ksClient versioned.Interface, option *openpitrixoptions.Options) *openpitrixHandler {
	var s3Client s3.Interface
	if option != nil && option.S3Options != nil && len(option.S3Options.Endpoint) != 0 {
		var err error
		s3Client, err = s3.NewS3Client(option.S3Options)
		if err != nil {
			klog.Errorf("failed to connect to storage, please check storage service status, error: %v", err)
		}
	}
H
hongming 已提交
56

Z
zryfish 已提交
57
	return &openpitrixHandler{
L
LiHui 已提交
58
		openpitrix.NewOpenpitrixOperator(ksInformers, ksClient, s3Client),
Z
zryfish 已提交
59
	}
Z
zryfish 已提交
60 61
}

L
LiHui 已提交
62
func (h *openpitrixHandler) CreateRepo(req *restful.Request, resp *restful.Response) {
Z
zryfish 已提交
63

L
LiHui 已提交
64 65
	createRepoRequest := &openpitrix.CreateRepoRequest{}
	err := req.ReadEntity(createRepoRequest)
Z
zryfish 已提交
66
	if err != nil {
H
hongming 已提交
67
		klog.V(4).Infoln(err)
Z
Zhengyi Lai 已提交
68
		api.HandleBadRequest(resp, nil, err)
Z
zryfish 已提交
69 70 71
		return
	}

L
LiHui 已提交
72 73
	createRepoRequest.Workspace = new(string)
	*createRepoRequest.Workspace = req.PathParameter("workspace")
Z
zryfish 已提交
74

L
LiHui 已提交
75 76 77 78 79 80
	user, _ := request.UserFrom(req.Request.Context())
	creator := ""
	if user != nil {
		creator = user.GetName()
	}
	parsedUrl, err := url.Parse(createRepoRequest.URL)
Z
zryfish 已提交
81
	if err != nil {
L
LiHui 已提交
82
		api.HandleBadRequest(resp, nil, err)
Z
zryfish 已提交
83 84
		return
	}
L
LiHui 已提交
85 86 87
	userInfo := parsedUrl.User
	// trim credential from url
	parsedUrl.User = nil
Z
zryfish 已提交
88

L
LiHui 已提交
89 90 91 92 93 94 95 96 97 98 99 100
	repo := v1alpha1.HelmRepo{
		ObjectMeta: metav1.ObjectMeta{
			Name: idutils.GetUuid36(v1alpha1.HelmRepoIdPrefix),
			Annotations: map[string]string{
				constants.CreatorAnnotationKey: creator,
			},
			Labels: map[string]string{
				constants.WorkspaceLabelKey: *createRepoRequest.Workspace,
			},
		},
		Spec: v1alpha1.HelmRepoSpec{
			Name:        createRepoRequest.Name,
L
LiHui 已提交
101
			Url:         parsedUrl.String(),
L
LiHui 已提交
102 103 104 105 106 107
			SyncPeriod:  0,
			Description: stringutils.ShortenString(createRepoRequest.Description, 512),
		},
	}

	if strings.HasPrefix(createRepoRequest.URL, "https://") || strings.HasPrefix(createRepoRequest.URL, "http://") {
L
LiHui 已提交
108 109 110
		if userInfo != nil {
			repo.Spec.Credential.Username = userInfo.Username()
			repo.Spec.Credential.Password, _ = userInfo.Password()
L
LiHui 已提交
111 112 113 114 115 116 117 118 119 120
		}
	} else if strings.HasPrefix(createRepoRequest.URL, "s3://") {
		cfg := v1alpha1.S3Config{}
		err := json.Unmarshal([]byte(createRepoRequest.Credential), &cfg)
		if err != nil {
			api.HandleBadRequest(resp, nil, err)
			return
		}
		repo.Spec.Credential.S3Config = cfg
	}
H
hongming 已提交
121

L
LiHui 已提交
122 123 124 125 126 127 128 129
	var result interface{}
	// 1. validate repo
	result, err = h.openpitrix.ValidateRepo(createRepoRequest.URL, &repo.Spec.Credential)
	if err != nil {
		klog.Errorf("validate repo failed, err: %s", err)
		api.HandleBadRequest(resp, nil, err)
		return
	}
H
hongming 已提交
130

L
LiHui 已提交
131 132 133 134 135 136 137 138 139
	// 2. create repo
	validate, _ := strconv.ParseBool(req.QueryParameter("validate"))
	if !validate {
		if repo.GetTrueName() == "" {
			api.HandleBadRequest(resp, nil, fmt.Errorf("repo name is empty"))
			return
		}
		result, err = h.openpitrix.CreateRepo(&repo)
	}
H
hongming 已提交
140 141

	if err != nil {
H
hongming 已提交
142 143
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
144 145 146
		return
	}

L
LiHui 已提交
147
	resp.WriteEntity(result)
H
hongming 已提交
148 149
}

L
LiHui 已提交
150 151 152 153
func (h *openpitrixHandler) DoRepoAction(req *restful.Request, resp *restful.Response) {
	repoActionRequest := &openpitrix.RepoActionRequest{}
	repoId := req.PathParameter("repo")
	err := req.ReadEntity(repoActionRequest)
H
hongming 已提交
154
	if err != nil {
H
hongming 已提交
155
		klog.V(4).Infoln(err)
Z
zryfish 已提交
156
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
157 158
		return
	}
L
LiHui 已提交
159
	repoActionRequest.Workspace = req.PathParameter("workspace")
H
hongming 已提交
160

L
LiHui 已提交
161
	err = h.openpitrix.DoRepoAction(repoId, repoActionRequest)
H
hongming 已提交
162 163

	if err != nil {
H
hongming 已提交
164
		klog.Errorln(err)
L
LiHui 已提交
165
		handleOpenpitrixError(resp, err)
H
hongming 已提交
166 167 168 169 170 171
		return
	}

	resp.WriteEntity(errors.None)
}

L
LiHui 已提交
172 173
func (h *openpitrixHandler) DeleteRepo(req *restful.Request, resp *restful.Response) {
	repoId := req.PathParameter("repo")
H
hongming 已提交
174

L
LiHui 已提交
175
	err := h.openpitrix.DeleteRepo(repoId)
H
hongming 已提交
176 177

	if err != nil {
H
hongming 已提交
178 179
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
180
		return
L
LiHui 已提交
181 182
	} else {
		klog.V(4).Info("delete repo: ", repoId)
H
hongming 已提交
183 184
	}

L
LiHui 已提交
185 186 187 188 189 190 191 192
	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 已提交
193
		klog.V(4).Infoln(err)
L
LiHui 已提交
194
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
195 196 197
		return
	}

L
LiHui 已提交
198
	err = h.openpitrix.ModifyRepo(repoId, &updateRepoRequest)
H
hongming 已提交
199 200

	if err != nil {
H
hongming 已提交
201 202
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
203
		return
L
LiHui 已提交
204 205
	} else {
		klog.V(4).Info("modify repo: ", repoId)
H
hongming 已提交
206 207 208 209 210
	}

	resp.WriteEntity(errors.None)
}

L
LiHui 已提交
211 212 213 214
func (h *openpitrixHandler) DescribeRepo(req *restful.Request, resp *restful.Response) {
	repoId := req.PathParameter("repo")

	result, err := h.openpitrix.DescribeRepo(repoId)
H
hongming 已提交
215 216

	if err != nil {
H
hongming 已提交
217 218
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
219 220 221
		return
	}

L
LiHui 已提交
222 223 224 225 226 227 228 229 230 231
	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)
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
	conditions, err := params.ParseConditions(req)

	if err != nil {
P
pengcong06 已提交
232
		klog.V(4).Infoln(err)
L
LiHui 已提交
233
		api.HandleBadRequest(resp, nil, err)
P
pengcong06 已提交
234 235 236
		return
	}

L
LiHui 已提交
237 238 239 240 241
	if req.PathParameter("workspace") != "" {
		conditions.Match[openpitrix.WorkspaceLabel] = req.PathParameter("workspace")
	}

	result, err := h.openpitrix.ListRepos(conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
242 243

	if err != nil {
H
hongming 已提交
244
		klog.Errorln(err)
P
pengcong06 已提交
245
		handleOpenpitrixError(resp, err)
H
hongming 已提交
246 247 248
		return
	}

L
LiHui 已提交
249
	resp.WriteEntity(result)
P
pengcong06 已提交
250 251
}

L
LiHui 已提交
252 253 254 255 256
func (h *openpitrixHandler) ListRepoEvents(req *restful.Request, resp *restful.Response) {
	repoId := req.PathParameter("repo")
	limit, offset := params.ParsePaging(req)
	conditions, err := params.ParseConditions(req)

P
pengcong06 已提交
257 258 259 260 261 262
	if err != nil {
		klog.V(4).Infoln(err)
		api.HandleBadRequest(resp, nil, err)
		return
	}

L
LiHui 已提交
263
	result, err := h.openpitrix.ListRepoEvents(repoId, conditions, limit, offset)
P
pengcong06 已提交
264 265 266 267 268 269

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

L
LiHui 已提交
271 272 273 274 275
	resp.WriteEntity(result)
}

func handleOpenpitrixError(resp *restful.Response, err error) {
	if status.Code(err) == codes.NotFound {
H
hongming 已提交
276
		klog.V(4).Infoln(err)
L
LiHui 已提交
277
		api.HandleNotFound(resp, nil, err)
H
hongming 已提交
278 279
		return
	}
L
LiHui 已提交
280 281 282
	if status.Code(err) == codes.InvalidArgument || status.Code(err) == codes.FailedPrecondition {
		klog.V(4).Infoln(err)
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
283 284
		return
	}
L
LiHui 已提交
285 286
	klog.Errorln(err)
	api.HandleInternalError(resp, nil, err)
H
hongming 已提交
287 288
}

L
LiHui 已提交
289 290 291
//=============
// helm application template handler
//=============
H
hongming 已提交
292 293 294 295 296

func (h *openpitrixHandler) DoAppAction(req *restful.Request, resp *restful.Response) {
	var doActionRequest openpitrix.ActionRequest
	err := req.ReadEntity(&doActionRequest)
	if err != nil {
H
hongming 已提交
297
		klog.V(4).Infoln(err)
Z
zryfish 已提交
298
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
299 300 301
		return
	}

L
LiHui 已提交
302 303 304 305 306 307
	user, _ := request.UserFrom(req.Request.Context())
	if user != nil {
		doActionRequest.Username = user.GetName()
	}

	appId := strings.TrimSuffix(req.PathParameter("app"), v1alpha1.HelmApplicationAppStoreSuffix)
H
hongming 已提交
308 309 310 311

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

	if err != nil {
H
hongming 已提交
312 313
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
314 315 316 317 318 319
		return
	}

	resp.WriteEntity(errors.None)
}

L
LiHui 已提交
320 321 322
func (h *openpitrixHandler) CreateApp(req *restful.Request, resp *restful.Response) {
	createAppRequest := &openpitrix.CreateAppRequest{}
	err := req.ReadEntity(createAppRequest)
H
hongming 已提交
323
	if err != nil {
H
hongming 已提交
324
		klog.V(4).Infoln(err)
Z
zryfish 已提交
325
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
326 327 328
		return
	}

L
LiHui 已提交
329 330 331 332
	user, _ := request.UserFrom(req.Request.Context())
	if user != nil {
		createAppRequest.Username = user.GetName()
	}
H
hongming 已提交
333

L
LiHui 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
	if req.PathParameter("workspace") != "" {
		createAppRequest.Isv = req.PathParameter("workspace")
	}

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

	var result interface{}

	if validate {
		validatePackageRequest := &openpitrix.ValidatePackageRequest{
			VersionPackage: createAppRequest.VersionPackage,
			VersionType:    createAppRequest.VersionType,
		}
		_ = validatePackageRequest
		result, err = h.openpitrix.ValidatePackage(validatePackageRequest)
	} else {
		result, err = h.openpitrix.CreateApp(createAppRequest)
	}
H
hongming 已提交
352 353

	if err != nil {
L
LiHui 已提交
354 355 356 357 358
		if status.Code(err) == codes.InvalidArgument {
			api.HandleBadRequest(resp, nil, err)
			return
		}
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
359 360 361
		return
	}

L
LiHui 已提交
362
	resp.WriteEntity(result)
H
hongming 已提交
363
}
L
LiHui 已提交
364 365 366 367
func (h *openpitrixHandler) ModifyApp(req *restful.Request, resp *restful.Response) {
	var patchAppRequest openpitrix.ModifyAppRequest
	err := req.ReadEntity(&patchAppRequest)
	appId := req.PathParameter("app")
H
hongming 已提交
368

L
LiHui 已提交
369 370 371 372
	if err != nil {
		klog.V(4).Infoln(err)
		api.HandleBadRequest(resp, nil, err)
		return
H
hongming 已提交
373 374
	}

L
LiHui 已提交
375
	err = h.openpitrix.ModifyApp(appId, &patchAppRequest)
H
hongming 已提交
376 377

	if err != nil {
H
hongming 已提交
378 379
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
380 381 382
		return
	}

L
LiHui 已提交
383
	resp.WriteEntity(errors.None)
H
hongming 已提交
384 385
}

L
LiHui 已提交
386
func (h *openpitrixHandler) ListApps(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
387
	limit, offset := params.ParsePaging(req)
L
LiHui 已提交
388
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
H
hongming 已提交
389
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
390 391 392
	conditions, err := params.ParseConditions(req)

	if err != nil {
H
hongming 已提交
393
		klog.V(4).Infoln(err)
Z
zryfish 已提交
394
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
395 396 397
		return
	}

L
LiHui 已提交
398 399
	if req.PathParameter("workspace") != "" {
		conditions.Match[openpitrix.WorkspaceLabel] = req.PathParameter("workspace")
H
hongming 已提交
400 401
	}

L
LiHui 已提交
402 403 404 405 406
	if conditions.Match[openpitrix.WorkspaceLabel] == "" {
		conditions.Match[openpitrix.WorkspaceLabel] = req.QueryParameter("workspace")
	}

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

	if err != nil {
H
hongming 已提交
409 410
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
411 412 413 414 415 416
		return
	}

	resp.WriteEntity(result)
}

L
LiHui 已提交
417 418 419 420
func (h *openpitrixHandler) DescribeApp(req *restful.Request, resp *restful.Response) {
	appId := req.PathParameter("app")

	result, err := h.openpitrix.DescribeApp(appId)
H
hongming 已提交
421 422

	if err != nil {
L
LiHui 已提交
423 424
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
425 426 427
		return
	}

L
LiHui 已提交
428 429 430 431 432 433 434
	resp.WriteEntity(result)
}

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

	err := h.openpitrix.DeleteApp(appId)
H
hongming 已提交
435 436

	if err != nil {
L
LiHui 已提交
437 438 439 440
		if status.Code(err) == codes.NotFound {
			api.HandleNotFound(resp, nil, err)
			return
		}
Z
zryfish 已提交
441
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
442 443 444
		return
	}

L
LiHui 已提交
445
	resp.WriteEntity(errors.None)
H
hongming 已提交
446 447
}

L
LiHui 已提交
448 449 450 451
// app version
func (h *openpitrixHandler) CreateAppVersion(req *restful.Request, resp *restful.Response) {
	var createAppVersionRequest openpitrix.CreateAppVersionRequest
	err := req.ReadEntity(&createAppVersionRequest)
H
hongming 已提交
452
	if err != nil {
H
hongming 已提交
453
		klog.V(4).Infoln(err)
Z
zryfish 已提交
454
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
455 456
		return
	}
L
LiHui 已提交
457 458 459 460 461 462 463
	// override app id
	createAppVersionRequest.AppId = req.PathParameter("app")
	user, _ := request.UserFrom(req.Request.Context())
	if user != nil {
		createAppVersionRequest.Username = user.GetName()
	}
	validate, _ := strconv.ParseBool(req.QueryParameter("validate"))
H
hongming 已提交
464

L
LiHui 已提交
465 466 467 468 469 470 471 472 473 474 475
	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)
	}
H
hongming 已提交
476 477

	if err != nil {
H
hongming 已提交
478
		klog.Errorln(err)
L
LiHui 已提交
479
		handleOpenpitrixError(resp, err)
H
hongming 已提交
480 481 482
		return
	}

L
LiHui 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
	resp.WriteEntity(result)
}

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

	err := h.openpitrix.DeleteAppVersion(versionId)

	if err != nil {
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
		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 {
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
		return
H
hongming 已提交
509 510 511 512 513
	}

	resp.WriteEntity(result)
}

L
LiHui 已提交
514
func (h *openpitrixHandler) ListAppVersions(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
515 516
	limit, offset := params.ParsePaging(req)
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
H
hongming 已提交
517
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
L
LiHui 已提交
518 519 520
	appId := req.PathParameter("app")
	appId = strings.TrimSuffix(appId, v1alpha1.HelmApplicationAppStoreSuffix)

H
hongming 已提交
521 522 523
	conditions, err := params.ParseConditions(req)

	if err != nil {
H
hongming 已提交
524
		klog.V(4).Infoln(err)
Z
zryfish 已提交
525
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
526 527
		return
	}
L
LiHui 已提交
528
	conditions.Match[openpitrix.AppId] = appId
H
hongming 已提交
529

H
hongming 已提交
530
	if req.PathParameter("workspace") != "" {
L
LiHui 已提交
531
		conditions.Match[openpitrix.WorkspaceLabel] = req.PathParameter("workspace")
H
hongming 已提交
532 533
	}

L
LiHui 已提交
534 535
	if conditions.Match[openpitrix.WorkspaceLabel] == "" {
		conditions.Match[openpitrix.WorkspaceLabel] = req.QueryParameter("workspace")
H
hongming 已提交
536 537
	}

L
LiHui 已提交
538
	result, err := h.openpitrix.ListAppVersions(conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
539 540

	if err != nil {
H
hongming 已提交
541
		klog.Errorln(err)
L
LiHui 已提交
542
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
543 544 545 546 547 548
		return
	}

	resp.WriteEntity(result)
}

L
LiHui 已提交
549
func (h *openpitrixHandler) ModifyAppVersion(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
550

L
LiHui 已提交
551 552 553
	var patchAppVersionRequest openpitrix.ModifyAppVersionRequest
	err := req.ReadEntity(&patchAppVersionRequest)
	versionId := req.PathParameter("version")
H
hongming 已提交
554 555

	if err != nil {
H
hongming 已提交
556
		klog.V(4).Infoln(err)
Z
zryfish 已提交
557
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
558 559 560
		return
	}

L
LiHui 已提交
561
	err = h.openpitrix.ModifyAppVersion(versionId, &patchAppVersionRequest)
H
hongming 已提交
562 563

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

	resp.WriteEntity(errors.None)
}

L
LiHui 已提交
572
func (h *openpitrixHandler) GetAppVersionPackage(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
573
	appId := req.PathParameter("app")
L
LiHui 已提交
574
	versionId := req.PathParameter("version")
H
hongming 已提交
575

L
LiHui 已提交
576
	result, err := h.openpitrix.GetAppVersionPackage(appId, versionId)
H
hongming 已提交
577 578

	if err != nil {
H
hongming 已提交
579
		klog.Errorln(err)
Z
zryfish 已提交
580
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
581 582 583
		return
	}

L
LiHui 已提交
584
	resp.WriteEntity(result)
H
hongming 已提交
585 586
}

L
LiHui 已提交
587 588 589 590 591
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, ",")
Z
Zhengyi Lai 已提交
592 593
	}

L
LiHui 已提交
594
	result, err := h.openpitrix.GetAppVersionFiles(versionId, getAppVersionFilesRequest)
H
hongming 已提交
595 596

	if err != nil {
L
LiHui 已提交
597 598
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
599 600 601 602 603 604
		return
	}

	resp.WriteEntity(result)
}

L
LiHui 已提交
605 606 607 608 609 610 611 612 613
// app version audit
func (h *openpitrixHandler) ListAppVersionAudits(req *restful.Request, resp *restful.Response) {
	limit, offset := params.ParsePaging(req)
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.StatusTime)
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
	appId := req.PathParameter("app")
	versionId := req.PathParameter("version")
	conditions, err := params.ParseConditions(req)

H
hongming 已提交
614
	if err != nil {
H
hongming 已提交
615
		klog.V(4).Infoln(err)
Z
zryfish 已提交
616
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
617 618 619
		return
	}

L
LiHui 已提交
620 621 622
	conditions.Match[openpitrix.AppId] = strings.TrimSuffix(appId, v1alpha1.HelmApplicationAppStoreSuffix)
	if versionId != "" {
		conditions.Match[openpitrix.VersionId] = versionId
H
hongming 已提交
623 624
	}

L
LiHui 已提交
625 626
	result, err := h.openpitrix.ListAppVersionAudits(conditions, orderBy, reverse, limit, offset)

H
hongming 已提交
627
	if err != nil {
H
hongming 已提交
628 629
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
630 631 632 633 634 635
		return
	}

	resp.WriteEntity(result)
}

L
LiHui 已提交
636 637 638
func (h *openpitrixHandler) DoAppVersionAction(req *restful.Request, resp *restful.Response) {
	var doActionRequest openpitrix.ActionRequest
	err := req.ReadEntity(&doActionRequest)
H
hongming 已提交
639
	if err != nil {
H
hongming 已提交
640
		klog.V(4).Infoln(err)
Z
zryfish 已提交
641
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
642 643
		return
	}
L
LiHui 已提交
644
	doActionRequest.Username = req.HeaderParameter(constants.UserNameHeader)
H
hongming 已提交
645

L
LiHui 已提交
646 647 648 649 650 651 652
	user, _ := request.UserFrom(req.Request.Context())
	if user != nil {
		doActionRequest.Username = user.GetName()
	}
	versionId := req.PathParameter("version")

	err = h.openpitrix.DoAppVersionAction(versionId, &doActionRequest)
H
hongming 已提交
653 654

	if err != nil {
H
hongming 已提交
655 656
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
657 658 659 660 661 662
		return
	}

	resp.WriteEntity(errors.None)
}

L
LiHui 已提交
663
// application release
H
hongming 已提交
664

L
LiHui 已提交
665 666 667 668 669 670 671
func (h *openpitrixHandler) DescribeApplication(req *restful.Request, resp *restful.Response) {
	clusterName := req.PathParameter("cluster")
	workspace := req.PathParameter("workspace")
	applicationId := req.PathParameter("application")
	namespace := req.PathParameter("namespace")

	app, err := h.openpitrix.DescribeApplication(workspace, clusterName, namespace, applicationId)
H
hongming 已提交
672 673

	if err != nil {
H
hongming 已提交
674 675
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
676 677 678
		return
	}

L
LiHui 已提交
679 680
	resp.WriteEntity(app)
	return
H
hongming 已提交
681 682
}

L
LiHui 已提交
683 684 685 686 687
func (h *openpitrixHandler) DeleteApplication(req *restful.Request, resp *restful.Response) {
	clusterName := req.PathParameter("cluster")
	workspace := req.PathParameter("workspace")
	applicationId := req.PathParameter("application")
	namespace := req.PathParameter("namespace")
H
hongming 已提交
688

L
LiHui 已提交
689
	err := h.openpitrix.DeleteApplication(workspace, clusterName, namespace, applicationId)
H
hongming 已提交
690 691

	if err != nil {
H
hongming 已提交
692 693
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
694 695 696
		return
	}

L
LiHui 已提交
697
	resp.WriteEntity(errors.None)
H
hongming 已提交
698 699
}

L
LiHui 已提交
700 701 702 703 704 705 706 707
func (h *openpitrixHandler) ListApplications(req *restful.Request, resp *restful.Response) {
	limit, offset := params.ParsePaging(req)
	clusterName := req.PathParameter("cluster")
	namespace := req.PathParameter("namespace")
	workspace := req.PathParameter("workspace")
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
	conditions, err := params.ParseConditions(req)
H
hongming 已提交
708

L
LiHui 已提交
709 710
	if offset < 0 {
		offset = 0
H
hongming 已提交
711 712
	}

L
LiHui 已提交
713 714
	if limit <= 0 {
		limit = 10
H
hongming 已提交
715 716 717
	}

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

L
LiHui 已提交
723
	result, err := h.openpitrix.ListApplications(workspace, clusterName, namespace, conditions, limit, offset, orderBy, reverse)
H
hongming 已提交
724 725

	if err != nil {
H
hongming 已提交
726
		klog.Errorln(err)
L
LiHui 已提交
727
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
728 729 730
		return
	}

L
LiHui 已提交
731
	resp.WriteAsJson(result)
H
hongming 已提交
732 733
}

L
LiHui 已提交
734 735 736 737 738
func (h *openpitrixHandler) UpgradeApplication(req *restful.Request, resp *restful.Response) {
	namespace := req.PathParameter("namespace")
	applicationId := req.PathParameter("application")
	var upgradeClusterRequest openpitrix.UpgradeClusterRequest
	err := req.ReadEntity(&upgradeClusterRequest)
H
hongming 已提交
739
	if err != nil {
H
hongming 已提交
740
		klog.V(4).Infoln(err)
Z
zryfish 已提交
741
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
742 743 744
		return
	}

L
LiHui 已提交
745 746 747 748 749
	upgradeClusterRequest.Namespace = namespace
	upgradeClusterRequest.ClusterId = applicationId
	user, _ := request.UserFrom(req.Request.Context())
	if user != nil {
		upgradeClusterRequest.Username = user.GetName()
H
hongming 已提交
750 751
	}

L
LiHui 已提交
752
	err = h.openpitrix.UpgradeApplication(upgradeClusterRequest)
H
hongming 已提交
753
	if err != nil {
H
hongming 已提交
754
		klog.Errorln(err)
L
LiHui 已提交
755
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
756 757 758
		return
	}

L
LiHui 已提交
759
	resp.WriteEntity(errors.None)
H
hongming 已提交
760 761
}

L
LiHui 已提交
762 763 764 765 766
func (h *openpitrixHandler) ModifyApplication(req *restful.Request, resp *restful.Response) {
	var modifyClusterAttributesRequest openpitrix.ModifyClusterAttributesRequest
	applicationId := req.PathParameter("application")
	namespace := req.PathParameter("namespace")
	err := req.ReadEntity(&modifyClusterAttributesRequest)
H
hongming 已提交
767
	if err != nil {
H
hongming 已提交
768
		klog.V(4).Infoln(err)
Z
zryfish 已提交
769
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
770 771 772
		return
	}

L
LiHui 已提交
773 774 775 776
	modifyClusterAttributesRequest.Namespace = namespace
	modifyClusterAttributesRequest.ClusterID = applicationId

	err = h.openpitrix.ModifyApplication(modifyClusterAttributesRequest)
H
hongming 已提交
777 778

	if err != nil {
H
hongming 已提交
779 780
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
781 782 783
		return
	}

L
LiHui 已提交
784
	resp.WriteEntity(errors.None)
H
hongming 已提交
785 786
}

L
LiHui 已提交
787 788 789 790 791 792 793
func (h *openpitrixHandler) CreateApplication(req *restful.Request, resp *restful.Response) {
	clusterName := req.PathParameter("cluster")
	namespace := req.PathParameter("namespace")
	workspace := req.PathParameter("workspace")
	var createClusterRequest openpitrix.CreateClusterRequest
	err := req.ReadEntity(&createClusterRequest)
	createClusterRequest.Workspace = workspace
H
hongming 已提交
794
	if err != nil {
H
hongming 已提交
795
		klog.V(4).Infoln(err)
Z
zryfish 已提交
796
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
797 798
		return
	}
L
LiHui 已提交
799 800 801
	user, _ := request.UserFrom(req.Request.Context())
	if user != nil {
		createClusterRequest.Username = user.GetName()
802 803
	}

L
LiHui 已提交
804
	err = h.openpitrix.CreateApplication(workspace, clusterName, namespace, createClusterRequest)
H
hongming 已提交
805 806

	if err != nil {
H
hongming 已提交
807
		klog.Errorln(err)
L
LiHui 已提交
808
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
809 810 811
		return
	}

L
LiHui 已提交
812
	resp.WriteEntity(errors.None)
H
hongming 已提交
813 814
}

L
LiHui 已提交
815 816 817
func (h *openpitrixHandler) CreateCategory(req *restful.Request, resp *restful.Response) {
	createCategoryRequest := &openpitrix.CreateCategoryRequest{}
	err := req.ReadEntity(createCategoryRequest)
H
hongming 已提交
818
	if err != nil {
H
hongming 已提交
819
		klog.V(4).Infoln(err)
Z
zryfish 已提交
820
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
821 822 823
		return
	}

L
LiHui 已提交
824
	result, err := h.openpitrix.CreateCategory(createCategoryRequest)
H
hongming 已提交
825 826

	if err != nil {
H
hongming 已提交
827 828
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
829 830 831
		return
	}

L
LiHui 已提交
832
	resp.WriteEntity(result)
H
hongming 已提交
833
}
L
LiHui 已提交
834 835
func (h *openpitrixHandler) DeleteCategory(req *restful.Request, resp *restful.Response) {
	categoryId := req.PathParameter("category")
H
hongming 已提交
836

L
LiHui 已提交
837
	err := h.openpitrix.DeleteCategory(categoryId)
H
hongming 已提交
838 839

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

	resp.WriteEntity(errors.None)
}
L
LiHui 已提交
847 848 849 850
func (h *openpitrixHandler) ModifyCategory(req *restful.Request, resp *restful.Response) {
	var modifyCategoryRequest openpitrix.ModifyCategoryRequest
	categoryId := req.PathParameter("category")
	err := req.ReadEntity(&modifyCategoryRequest)
H
hongming 已提交
851
	if err != nil {
H
hongming 已提交
852
		klog.V(4).Infoln(err)
Z
zryfish 已提交
853
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
854 855 856
		return
	}

L
LiHui 已提交
857
	err = h.openpitrix.ModifyCategory(categoryId, &modifyCategoryRequest)
H
hongming 已提交
858 859

	if err != nil {
H
hongming 已提交
860 861
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
862 863 864 865 866
		return
	}

	resp.WriteEntity(errors.None)
}
L
LiHui 已提交
867 868
func (h *openpitrixHandler) DescribeCategory(req *restful.Request, resp *restful.Response) {
	categoryId := req.PathParameter("category")
H
hongming 已提交
869

L
LiHui 已提交
870
	result, err := h.openpitrix.DescribeCategory(categoryId)
H
hongming 已提交
871 872

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

	resp.WriteEntity(result)
}
L
LiHui 已提交
880
func (h *openpitrixHandler) ListCategories(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
881 882
	limit, offset := params.ParsePaging(req)
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
H
hongming 已提交
883
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
884 885
	conditions, err := params.ParseConditions(req)

886 887 888 889 890 891
	if err != nil {
		klog.V(4).Infoln(err)
		api.HandleBadRequest(resp, nil, err)
		return
	}

L
LiHui 已提交
892
	result, err := h.openpitrix.ListCategories(conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
893 894

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

	resp.WriteEntity(result)
}

L
LiHui 已提交
903 904
// review
func (h *openpitrixHandler) ListReviews(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
905
	limit, offset := params.ParsePaging(req)
L
LiHui 已提交
906 907
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.StatusTime)
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
908 909 910
	conditions, err := params.ParseConditions(req)

	if err != nil {
H
hongming 已提交
911
		klog.V(4).Infoln(err)
Z
zryfish 已提交
912
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
913 914 915
		return
	}

L
LiHui 已提交
916
	result, err := h.openpitrix.ListAppVersionReviews(conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
917 918

	if err != nil {
H
hongming 已提交
919
		klog.Errorln(err)
L
LiHui 已提交
920
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
921 922 923 924
		return
	}

	resp.WriteEntity(result)
Z
zryfish 已提交
925
}
H
hongming 已提交
926

L
LiHui 已提交
927 928 929 930 931 932 933 934
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 {
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
935 936
		return
	}
L
LiHui 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953

	// 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) CreateAttachment(req *restful.Request, resp *restful.Response) {

	r := req.Request
	err := r.ParseMultipartForm(10 << 20)
	if err != nil {
Z
zryfish 已提交
954
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
955 956
		return
	}
L
LiHui 已提交
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987

	var att *openpitrix.Attachment
	// just save one attachment
	for fName := range r.MultipartForm.File {
		f, _, err := r.FormFile(fName)
		if err != nil {
			api.HandleBadRequest(resp, nil, err)
			return
		}
		data, err := ioutil.ReadAll(f)
		f.Close()

		att, err = h.openpitrix.CreateAttachment(data)
		if err != nil {
			api.HandleInternalError(resp, nil, err)
			return
		}
		break
	}

	resp.WriteEntity(att)
}

func (h *openpitrixHandler) DeleteAttachments(req *restful.Request, resp *restful.Response) {
	attachmentId := req.PathParameter("attachment")

	ids := strings.Split(attachmentId, ",")
	err := h.openpitrix.DeleteAttachments(ids)
	if err != nil {
		api.HandleInternalError(resp, nil, err)
	}
H
hongming 已提交
988
}