handler.go 25.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 (
L
LiHui 已提交
17
	"encoding/json"
H
hongming 已提交
18
	"fmt"
Z
zryfish 已提交
19 20 21 22 23
	"io/ioutil"
	"net/url"
	"strconv"
	"strings"

Z
zryfish 已提交
24
	"github.com/emicklei/go-restful"
H
hongming 已提交
25 26
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
27
	apierrors "k8s.io/apimachinery/pkg/api/errors"
L
LiHui 已提交
28
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
H
hongming 已提交
29
	"k8s.io/klog"
Z
zryfish 已提交
30

Z
zryfish 已提交
31
	"kubesphere.io/kubesphere/pkg/api"
L
LiHui 已提交
32 33 34
	"kubesphere.io/kubesphere/pkg/apis/application/v1alpha1"
	"kubesphere.io/kubesphere/pkg/apiserver/request"
	"kubesphere.io/kubesphere/pkg/client/clientset/versioned"
Z
zryfish 已提交
35 36
	"kubesphere.io/kubesphere/pkg/constants"
	"kubesphere.io/kubesphere/pkg/informers"
H
hongming 已提交
37 38
	"kubesphere.io/kubesphere/pkg/models/openpitrix"
	"kubesphere.io/kubesphere/pkg/server/errors"
Z
zryfish 已提交
39
	"kubesphere.io/kubesphere/pkg/server/params"
L
LiHui 已提交
40 41 42 43
	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"
Z
zryfish 已提交
44
)
Z
zryfish 已提交
45 46

type openpitrixHandler struct {
H
hongming 已提交
47
	openpitrix openpitrix.Interface
Z
zryfish 已提交
48 49
}

L
LiHui 已提交
50 51 52 53 54 55 56 57 58
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 已提交
59

Z
zryfish 已提交
60
	return &openpitrixHandler{
L
LiHui 已提交
61
		openpitrix.NewOpenpitrixOperator(ksInformers, ksClient, s3Client),
Z
zryfish 已提交
62
	}
Z
zryfish 已提交
63 64
}

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

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

L
LiHui 已提交
75 76
	createRepoRequest.Workspace = new(string)
	*createRepoRequest.Workspace = req.PathParameter("workspace")
Z
zryfish 已提交
77

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

L
LiHui 已提交
92 93 94 95 96 97 98 99 100 101 102 103
	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 已提交
104
			Url:         parsedUrl.String(),
L
LiHui 已提交
105 106 107 108 109 110
			SyncPeriod:  0,
			Description: stringutils.ShortenString(createRepoRequest.Description, 512),
		},
	}

	if strings.HasPrefix(createRepoRequest.URL, "https://") || strings.HasPrefix(createRepoRequest.URL, "http://") {
L
LiHui 已提交
111 112 113
		if userInfo != nil {
			repo.Spec.Credential.Username = userInfo.Username()
			repo.Spec.Credential.Password, _ = userInfo.Password()
L
LiHui 已提交
114 115 116 117 118 119 120 121 122 123
		}
	} 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 已提交
124

L
LiHui 已提交
125 126 127 128 129 130 131 132
	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 已提交
133

L
LiHui 已提交
134 135 136 137 138 139 140 141 142
	// 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 已提交
143 144

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

L
LiHui 已提交
150
	resp.WriteEntity(result)
H
hongming 已提交
151 152
}

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

L
LiHui 已提交
164
	err = h.openpitrix.DoRepoAction(repoId, repoActionRequest)
H
hongming 已提交
165 166

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

	resp.WriteEntity(errors.None)
}

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

L
LiHui 已提交
178
	err := h.openpitrix.DeleteRepo(repoId)
H
hongming 已提交
179 180

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

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

L
LiHui 已提交
201
	err = h.openpitrix.ModifyRepo(repoId, &updateRepoRequest)
H
hongming 已提交
202 203

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

	resp.WriteEntity(errors.None)
}

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

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

	if err != nil {
H
hongming 已提交
220
		klog.Errorln(err)
L
LiHui 已提交
221 222 223 224 225
		if apierrors.IsNotFound(err) {
			api.HandleNotFound(resp, nil, err)
			return
		}
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
226 227 228
		return
	}

L
LiHui 已提交
229 230 231 232 233 234 235 236 237 238
	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 已提交
239
		klog.V(4).Infoln(err)
L
LiHui 已提交
240
		api.HandleBadRequest(resp, nil, err)
P
pengcong06 已提交
241 242 243
		return
	}

L
LiHui 已提交
244 245 246 247 248
	if req.PathParameter("workspace") != "" {
		conditions.Match[openpitrix.WorkspaceLabel] = req.PathParameter("workspace")
	}

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

	if err != nil {
H
hongming 已提交
251
		klog.Errorln(err)
P
pengcong06 已提交
252
		handleOpenpitrixError(resp, err)
H
hongming 已提交
253 254 255
		return
	}

L
LiHui 已提交
256
	resp.WriteEntity(result)
P
pengcong06 已提交
257 258
}

L
LiHui 已提交
259 260 261 262 263
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 已提交
264 265 266 267 268 269
	if err != nil {
		klog.V(4).Infoln(err)
		api.HandleBadRequest(resp, nil, err)
		return
	}

L
LiHui 已提交
270
	result, err := h.openpitrix.ListRepoEvents(repoId, conditions, limit, offset)
P
pengcong06 已提交
271 272 273 274 275 276

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

L
LiHui 已提交
278 279 280 281 282
	resp.WriteEntity(result)
}

func handleOpenpitrixError(resp *restful.Response, err error) {
	if status.Code(err) == codes.NotFound {
H
hongming 已提交
283
		klog.V(4).Infoln(err)
L
LiHui 已提交
284
		api.HandleNotFound(resp, nil, err)
H
hongming 已提交
285 286
		return
	}
L
LiHui 已提交
287 288 289
	if status.Code(err) == codes.InvalidArgument || status.Code(err) == codes.FailedPrecondition {
		klog.V(4).Infoln(err)
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
290 291
		return
	}
L
LiHui 已提交
292 293
	klog.Errorln(err)
	api.HandleInternalError(resp, nil, err)
H
hongming 已提交
294 295
}

L
LiHui 已提交
296 297 298
//=============
// helm application template handler
//=============
H
hongming 已提交
299 300 301 302 303

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

L
LiHui 已提交
309 310 311 312 313 314
	user, _ := request.UserFrom(req.Request.Context())
	if user != nil {
		doActionRequest.Username = user.GetName()
	}

	appId := strings.TrimSuffix(req.PathParameter("app"), v1alpha1.HelmApplicationAppStoreSuffix)
H
hongming 已提交
315 316 317 318

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

	if err != nil {
H
hongming 已提交
319 320
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
321 322 323 324 325 326
		return
	}

	resp.WriteEntity(errors.None)
}

L
LiHui 已提交
327 328 329
func (h *openpitrixHandler) CreateApp(req *restful.Request, resp *restful.Response) {
	createAppRequest := &openpitrix.CreateAppRequest{}
	err := req.ReadEntity(createAppRequest)
H
hongming 已提交
330
	if err != nil {
H
hongming 已提交
331
		klog.V(4).Infoln(err)
Z
zryfish 已提交
332
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
333 334 335
		return
	}

L
LiHui 已提交
336 337 338 339
	user, _ := request.UserFrom(req.Request.Context())
	if user != nil {
		createAppRequest.Username = user.GetName()
	}
H
hongming 已提交
340

L
LiHui 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	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 已提交
359 360

	if err != nil {
L
LiHui 已提交
361 362 363 364 365
		if status.Code(err) == codes.InvalidArgument {
			api.HandleBadRequest(resp, nil, err)
			return
		}
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
366 367 368
		return
	}

L
LiHui 已提交
369
	resp.WriteEntity(result)
H
hongming 已提交
370
}
L
LiHui 已提交
371 372 373 374
func (h *openpitrixHandler) ModifyApp(req *restful.Request, resp *restful.Response) {
	var patchAppRequest openpitrix.ModifyAppRequest
	err := req.ReadEntity(&patchAppRequest)
	appId := req.PathParameter("app")
H
hongming 已提交
375

L
LiHui 已提交
376 377 378 379
	if err != nil {
		klog.V(4).Infoln(err)
		api.HandleBadRequest(resp, nil, err)
		return
H
hongming 已提交
380 381
	}

L
LiHui 已提交
382
	err = h.openpitrix.ModifyApp(appId, &patchAppRequest)
H
hongming 已提交
383 384

	if err != nil {
H
hongming 已提交
385 386
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
387 388 389
		return
	}

L
LiHui 已提交
390
	resp.WriteEntity(errors.None)
H
hongming 已提交
391 392
}

L
LiHui 已提交
393
func (h *openpitrixHandler) ListApps(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
394
	limit, offset := params.ParsePaging(req)
L
LiHui 已提交
395
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
H
hongming 已提交
396
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
397 398 399
	conditions, err := params.ParseConditions(req)

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

L
LiHui 已提交
405 406
	if req.PathParameter("workspace") != "" {
		conditions.Match[openpitrix.WorkspaceLabel] = req.PathParameter("workspace")
H
hongming 已提交
407 408
	}

L
LiHui 已提交
409 410 411 412 413
	if conditions.Match[openpitrix.WorkspaceLabel] == "" {
		conditions.Match[openpitrix.WorkspaceLabel] = req.QueryParameter("workspace")
	}

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

	if err != nil {
H
hongming 已提交
416 417
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
418 419 420 421 422 423
		return
	}

	resp.WriteEntity(result)
}

L
LiHui 已提交
424 425 426 427
func (h *openpitrixHandler) DescribeApp(req *restful.Request, resp *restful.Response) {
	appId := req.PathParameter("app")

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

	if err != nil {
L
LiHui 已提交
430 431
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
432 433 434
		return
	}

L
LiHui 已提交
435 436 437 438 439 440 441
	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 已提交
442 443

	if err != nil {
L
LiHui 已提交
444 445 446 447
		if status.Code(err) == codes.NotFound {
			api.HandleNotFound(resp, nil, err)
			return
		}
Z
zryfish 已提交
448
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
449 450 451
		return
	}

L
LiHui 已提交
452
	resp.WriteEntity(errors.None)
H
hongming 已提交
453 454
}

L
LiHui 已提交
455 456 457 458
// app version
func (h *openpitrixHandler) CreateAppVersion(req *restful.Request, resp *restful.Response) {
	var createAppVersionRequest openpitrix.CreateAppVersionRequest
	err := req.ReadEntity(&createAppVersionRequest)
H
hongming 已提交
459
	if err != nil {
H
hongming 已提交
460
		klog.V(4).Infoln(err)
Z
zryfish 已提交
461
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
462 463
		return
	}
L
LiHui 已提交
464 465 466 467 468 469 470
	// 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 已提交
471

L
LiHui 已提交
472 473 474 475 476 477 478 479 480 481 482
	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 已提交
483 484

	if err != nil {
H
hongming 已提交
485
		klog.Errorln(err)
L
LiHui 已提交
486
		handleOpenpitrixError(resp, err)
H
hongming 已提交
487 488 489
		return
	}

L
LiHui 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
	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 已提交
516 517 518 519 520
	}

	resp.WriteEntity(result)
}

L
LiHui 已提交
521
func (h *openpitrixHandler) ListAppVersions(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
522 523
	limit, offset := params.ParsePaging(req)
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
H
hongming 已提交
524
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
L
LiHui 已提交
525 526 527
	appId := req.PathParameter("app")
	appId = strings.TrimSuffix(appId, v1alpha1.HelmApplicationAppStoreSuffix)

H
hongming 已提交
528 529 530
	conditions, err := params.ParseConditions(req)

	if err != nil {
H
hongming 已提交
531
		klog.V(4).Infoln(err)
Z
zryfish 已提交
532
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
533 534
		return
	}
L
LiHui 已提交
535
	conditions.Match[openpitrix.AppId] = appId
H
hongming 已提交
536

H
hongming 已提交
537
	if req.PathParameter("workspace") != "" {
L
LiHui 已提交
538
		conditions.Match[openpitrix.WorkspaceLabel] = req.PathParameter("workspace")
H
hongming 已提交
539 540
	}

L
LiHui 已提交
541 542
	if conditions.Match[openpitrix.WorkspaceLabel] == "" {
		conditions.Match[openpitrix.WorkspaceLabel] = req.QueryParameter("workspace")
H
hongming 已提交
543 544
	}

L
LiHui 已提交
545
	result, err := h.openpitrix.ListAppVersions(conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
546 547

	if err != nil {
H
hongming 已提交
548
		klog.Errorln(err)
L
LiHui 已提交
549
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
550 551 552 553 554 555
		return
	}

	resp.WriteEntity(result)
}

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

L
LiHui 已提交
558 559 560
	var patchAppVersionRequest openpitrix.ModifyAppVersionRequest
	err := req.ReadEntity(&patchAppVersionRequest)
	versionId := req.PathParameter("version")
H
hongming 已提交
561 562

	if err != nil {
H
hongming 已提交
563
		klog.V(4).Infoln(err)
Z
zryfish 已提交
564
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
565 566 567
		return
	}

L
LiHui 已提交
568
	err = h.openpitrix.ModifyAppVersion(versionId, &patchAppVersionRequest)
H
hongming 已提交
569 570

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

	resp.WriteEntity(errors.None)
}

L
LiHui 已提交
579
func (h *openpitrixHandler) GetAppVersionPackage(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
580
	appId := req.PathParameter("app")
L
LiHui 已提交
581
	versionId := req.PathParameter("version")
H
hongming 已提交
582

L
LiHui 已提交
583
	result, err := h.openpitrix.GetAppVersionPackage(appId, versionId)
H
hongming 已提交
584 585

	if err != nil {
H
hongming 已提交
586
		klog.Errorln(err)
Z
zryfish 已提交
587
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
588 589 590
		return
	}

L
LiHui 已提交
591
	resp.WriteEntity(result)
H
hongming 已提交
592 593
}

L
LiHui 已提交
594 595 596 597 598
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 已提交
599 600
	}

L
LiHui 已提交
601
	result, err := h.openpitrix.GetAppVersionFiles(versionId, getAppVersionFilesRequest)
H
hongming 已提交
602 603

	if err != nil {
L
LiHui 已提交
604
		klog.Errorln(err)
605 606 607 608 609
		if apierrors.IsNotFound(err) {
			api.HandleNotFound(resp, nil, err)
		} else {
			api.HandleBadRequest(resp, nil, err)
		}
H
hongming 已提交
610 611 612 613 614 615
		return
	}

	resp.WriteEntity(result)
}

L
LiHui 已提交
616 617 618 619 620 621 622 623 624
// 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 已提交
625
	if err != nil {
H
hongming 已提交
626
		klog.V(4).Infoln(err)
Z
zryfish 已提交
627
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
628 629 630
		return
	}

L
LiHui 已提交
631 632 633
	conditions.Match[openpitrix.AppId] = strings.TrimSuffix(appId, v1alpha1.HelmApplicationAppStoreSuffix)
	if versionId != "" {
		conditions.Match[openpitrix.VersionId] = versionId
H
hongming 已提交
634 635
	}

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

H
hongming 已提交
638
	if err != nil {
H
hongming 已提交
639 640
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
641 642 643 644 645 646
		return
	}

	resp.WriteEntity(result)
}

L
LiHui 已提交
647 648 649
func (h *openpitrixHandler) DoAppVersionAction(req *restful.Request, resp *restful.Response) {
	var doActionRequest openpitrix.ActionRequest
	err := req.ReadEntity(&doActionRequest)
H
hongming 已提交
650
	if err != nil {
H
hongming 已提交
651
		klog.V(4).Infoln(err)
Z
zryfish 已提交
652
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
653 654
		return
	}
L
LiHui 已提交
655
	doActionRequest.Username = req.HeaderParameter(constants.UserNameHeader)
H
hongming 已提交
656

L
LiHui 已提交
657 658 659 660 661 662 663
	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 已提交
664 665

	if err != nil {
H
hongming 已提交
666 667
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
668 669 670 671 672 673
		return
	}

	resp.WriteEntity(errors.None)
}

L
LiHui 已提交
674
// application release
H
hongming 已提交
675

L
LiHui 已提交
676 677 678 679 680 681 682
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 已提交
683 684

	if err != nil {
H
hongming 已提交
685 686
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
687 688 689
		return
	}

L
LiHui 已提交
690 691
	resp.WriteEntity(app)
	return
H
hongming 已提交
692 693
}

L
LiHui 已提交
694 695 696 697 698
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 已提交
699

L
LiHui 已提交
700
	err := h.openpitrix.DeleteApplication(workspace, clusterName, namespace, applicationId)
H
hongming 已提交
701 702

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

L
LiHui 已提交
708
	resp.WriteEntity(errors.None)
H
hongming 已提交
709 710
}

L
LiHui 已提交
711 712 713 714 715 716 717 718
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 已提交
719

L
LiHui 已提交
720 721
	if offset < 0 {
		offset = 0
H
hongming 已提交
722 723
	}

L
LiHui 已提交
724 725
	if limit <= 0 {
		limit = 10
H
hongming 已提交
726 727 728
	}

	if err != nil {
H
hongming 已提交
729
		klog.V(4).Infoln(err)
Z
zryfish 已提交
730
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
731 732 733
		return
	}

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

	if err != nil {
H
hongming 已提交
737
		klog.Errorln(err)
L
LiHui 已提交
738
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
739 740 741
		return
	}

L
LiHui 已提交
742
	resp.WriteAsJson(result)
H
hongming 已提交
743 744
}

L
LiHui 已提交
745 746 747 748 749
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 已提交
750
	if err != nil {
H
hongming 已提交
751
		klog.V(4).Infoln(err)
Z
zryfish 已提交
752
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
753 754 755
		return
	}

L
LiHui 已提交
756 757 758 759 760
	upgradeClusterRequest.Namespace = namespace
	upgradeClusterRequest.ClusterId = applicationId
	user, _ := request.UserFrom(req.Request.Context())
	if user != nil {
		upgradeClusterRequest.Username = user.GetName()
H
hongming 已提交
761 762
	}

L
LiHui 已提交
763
	err = h.openpitrix.UpgradeApplication(upgradeClusterRequest)
H
hongming 已提交
764
	if err != nil {
H
hongming 已提交
765
		klog.Errorln(err)
L
LiHui 已提交
766
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
767 768 769
		return
	}

L
LiHui 已提交
770
	resp.WriteEntity(errors.None)
H
hongming 已提交
771 772
}

L
LiHui 已提交
773 774 775 776 777
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 已提交
778
	if err != nil {
H
hongming 已提交
779
		klog.V(4).Infoln(err)
Z
zryfish 已提交
780
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
781 782 783
		return
	}

L
LiHui 已提交
784 785 786 787
	modifyClusterAttributesRequest.Namespace = namespace
	modifyClusterAttributesRequest.ClusterID = applicationId

	err = h.openpitrix.ModifyApplication(modifyClusterAttributesRequest)
H
hongming 已提交
788 789

	if err != nil {
H
hongming 已提交
790 791
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
792 793 794
		return
	}

L
LiHui 已提交
795
	resp.WriteEntity(errors.None)
H
hongming 已提交
796 797
}

L
LiHui 已提交
798 799 800 801 802 803 804
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 已提交
805
	if err != nil {
H
hongming 已提交
806
		klog.V(4).Infoln(err)
Z
zryfish 已提交
807
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
808 809
		return
	}
L
LiHui 已提交
810 811 812
	user, _ := request.UserFrom(req.Request.Context())
	if user != nil {
		createClusterRequest.Username = user.GetName()
813 814
	}

L
LiHui 已提交
815
	err = h.openpitrix.CreateApplication(workspace, clusterName, namespace, createClusterRequest)
H
hongming 已提交
816 817

	if err != nil {
H
hongming 已提交
818
		klog.Errorln(err)
L
LiHui 已提交
819
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
820 821 822
		return
	}

L
LiHui 已提交
823
	resp.WriteEntity(errors.None)
H
hongming 已提交
824 825
}

L
LiHui 已提交
826 827 828
func (h *openpitrixHandler) CreateCategory(req *restful.Request, resp *restful.Response) {
	createCategoryRequest := &openpitrix.CreateCategoryRequest{}
	err := req.ReadEntity(createCategoryRequest)
H
hongming 已提交
829
	if err != nil {
H
hongming 已提交
830
		klog.V(4).Infoln(err)
Z
zryfish 已提交
831
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
832 833 834
		return
	}

L
LiHui 已提交
835
	result, err := h.openpitrix.CreateCategory(createCategoryRequest)
H
hongming 已提交
836 837

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

L
LiHui 已提交
843
	resp.WriteEntity(result)
H
hongming 已提交
844
}
L
LiHui 已提交
845 846
func (h *openpitrixHandler) DeleteCategory(req *restful.Request, resp *restful.Response) {
	categoryId := req.PathParameter("category")
H
hongming 已提交
847

L
LiHui 已提交
848
	err := h.openpitrix.DeleteCategory(categoryId)
H
hongming 已提交
849 850

	if err != nil {
H
hongming 已提交
851 852
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
853 854 855 856 857
		return
	}

	resp.WriteEntity(errors.None)
}
L
LiHui 已提交
858 859 860 861
func (h *openpitrixHandler) ModifyCategory(req *restful.Request, resp *restful.Response) {
	var modifyCategoryRequest openpitrix.ModifyCategoryRequest
	categoryId := req.PathParameter("category")
	err := req.ReadEntity(&modifyCategoryRequest)
H
hongming 已提交
862
	if err != nil {
H
hongming 已提交
863
		klog.V(4).Infoln(err)
Z
zryfish 已提交
864
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
865 866 867
		return
	}

L
LiHui 已提交
868
	err = h.openpitrix.ModifyCategory(categoryId, &modifyCategoryRequest)
H
hongming 已提交
869 870

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

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

L
LiHui 已提交
881
	result, err := h.openpitrix.DescribeCategory(categoryId)
H
hongming 已提交
882 883

	if err != nil {
H
hongming 已提交
884 885
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
886 887 888 889 890
		return
	}

	resp.WriteEntity(result)
}
L
LiHui 已提交
891
func (h *openpitrixHandler) ListCategories(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
892 893
	limit, offset := params.ParsePaging(req)
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
H
hongming 已提交
894
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
895 896
	conditions, err := params.ParseConditions(req)

897 898 899 900 901 902
	if err != nil {
		klog.V(4).Infoln(err)
		api.HandleBadRequest(resp, nil, err)
		return
	}

L
LiHui 已提交
903
	result, err := h.openpitrix.ListCategories(conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
904 905

	if err != nil {
H
hongming 已提交
906 907
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
908 909 910 911 912 913
		return
	}

	resp.WriteEntity(result)
}

L
LiHui 已提交
914 915
// review
func (h *openpitrixHandler) ListReviews(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
916
	limit, offset := params.ParsePaging(req)
L
LiHui 已提交
917 918
	orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.StatusTime)
	reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
H
hongming 已提交
919 920 921
	conditions, err := params.ParseConditions(req)

	if err != nil {
H
hongming 已提交
922
		klog.V(4).Infoln(err)
Z
zryfish 已提交
923
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
924 925 926
		return
	}

L
LiHui 已提交
927
	result, err := h.openpitrix.ListAppVersionReviews(conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
928 929

	if err != nil {
H
hongming 已提交
930
		klog.Errorln(err)
L
LiHui 已提交
931
		api.HandleInternalError(resp, nil, err)
H
hongming 已提交
932 933 934 935
		return
	}

	resp.WriteEntity(result)
Z
zryfish 已提交
936
}
H
hongming 已提交
937

L
LiHui 已提交
938 939 940 941 942 943 944 945
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 已提交
946 947
		return
	}
L
LiHui 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964

	// 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 已提交
965
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
966 967
		return
	}
L
LiHui 已提交
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998

	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 已提交
999
}