handler.go 25.6 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 85
		return
	}

L
LiHui 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	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,
			Url:         createRepoRequest.URL,
			SyncPeriod:  0,
			Description: stringutils.ShortenString(createRepoRequest.Description, 512),
		},
	}

	if strings.HasPrefix(createRepoRequest.URL, "https://") || strings.HasPrefix(createRepoRequest.URL, "http://") {
		if parsedUrl.User != nil {
			repo.Spec.Credential.Username = parsedUrl.User.Username()
			repo.Spec.Credential.Password, _ = parsedUrl.User.Password()
		}
	} 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 已提交
118

L
LiHui 已提交
119 120 121 122 123 124 125 126
	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 已提交
127

L
LiHui 已提交
128 129 130 131 132 133 134 135 136
	// 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 已提交
137 138

	if err != nil {
H
hongming 已提交
139 140
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
141 142 143
		return
	}

L
LiHui 已提交
144
	resp.WriteEntity(result)
H
hongming 已提交
145 146
}

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

L
LiHui 已提交
158
	err = h.openpitrix.DoRepoAction(repoId, repoActionRequest)
H
hongming 已提交
159 160

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

	resp.WriteEntity(errors.None)
}

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

L
LiHui 已提交
172
	err := h.openpitrix.DeleteRepo(repoId)
H
hongming 已提交
173 174

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

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

L
LiHui 已提交
195
	err = h.openpitrix.ModifyRepo(repoId, &updateRepoRequest)
H
hongming 已提交
196 197

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

	resp.WriteEntity(errors.None)
}

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

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

	if err != nil {
H
hongming 已提交
214 215
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
216 217 218
		return
	}

L
LiHui 已提交
219 220 221 222 223 224 225 226 227 228
	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 已提交
229
		klog.V(4).Infoln(err)
L
LiHui 已提交
230
		api.HandleBadRequest(resp, nil, err)
P
pengcong06 已提交
231 232 233
		return
	}

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

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

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

L
LiHui 已提交
246
	resp.WriteEntity(result)
P
pengcong06 已提交
247 248
}

L
LiHui 已提交
249 250 251 252 253
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 已提交
254 255 256 257 258 259
	if err != nil {
		klog.V(4).Infoln(err)
		api.HandleBadRequest(resp, nil, err)
		return
	}

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

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

L
LiHui 已提交
268 269 270 271 272
	resp.WriteEntity(result)
}

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

L
LiHui 已提交
286 287 288
//=============
// helm application template handler
//=============
H
hongming 已提交
289 290 291 292 293

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

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

	appId := strings.TrimSuffix(req.PathParameter("app"), v1alpha1.HelmApplicationAppStoreSuffix)
H
hongming 已提交
305 306 307 308

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

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

	resp.WriteEntity(errors.None)
}

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

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

L
LiHui 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
	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 已提交
349 350

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

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

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

L
LiHui 已提交
372
	err = h.openpitrix.ModifyApp(appId, &patchAppRequest)
H
hongming 已提交
373 374

	if err != nil {
H
hongming 已提交
375 376
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
377 378 379
		return
	}

L
LiHui 已提交
380
	resp.WriteEntity(errors.None)
H
hongming 已提交
381 382
}

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

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

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

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

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

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

	resp.WriteEntity(result)
}

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

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

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

L
LiHui 已提交
425 426 427 428 429 430 431
	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 已提交
432 433

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

L
LiHui 已提交
442
	resp.WriteEntity(errors.None)
H
hongming 已提交
443 444
}

L
LiHui 已提交
445 446 447 448
// app version
func (h *openpitrixHandler) CreateAppVersion(req *restful.Request, resp *restful.Response) {
	var createAppVersionRequest openpitrix.CreateAppVersionRequest
	err := req.ReadEntity(&createAppVersionRequest)
H
hongming 已提交
449
	if err != nil {
H
hongming 已提交
450
		klog.V(4).Infoln(err)
Z
zryfish 已提交
451
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
452 453
		return
	}
L
LiHui 已提交
454 455 456 457 458 459 460
	// 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 已提交
461

L
LiHui 已提交
462 463 464 465 466 467 468 469 470 471 472
	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 已提交
473 474

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

L
LiHui 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
	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 已提交
506 507 508 509 510
	}

	resp.WriteEntity(result)
}

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

H
hongming 已提交
518 519 520
	conditions, err := params.ParseConditions(req)

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

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

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

L
LiHui 已提交
535
	result, err := h.openpitrix.ListAppVersions(conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
536 537

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

	resp.WriteEntity(result)
}

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

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

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

L
LiHui 已提交
558
	err = h.openpitrix.ModifyAppVersion(versionId, &patchAppVersionRequest)
H
hongming 已提交
559 560

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

	resp.WriteEntity(errors.None)
}

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

L
LiHui 已提交
573
	result, err := h.openpitrix.GetAppVersionPackage(appId, versionId)
H
hongming 已提交
574 575

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

L
LiHui 已提交
581
	resp.WriteEntity(result)
H
hongming 已提交
582 583
}

L
LiHui 已提交
584 585 586 587 588
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 已提交
589 590
	}

L
LiHui 已提交
591
	result, err := h.openpitrix.GetAppVersionFiles(versionId, getAppVersionFilesRequest)
H
hongming 已提交
592 593

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

	resp.WriteEntity(result)
}

L
LiHui 已提交
602 603 604 605 606 607 608 609 610
// 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 已提交
611
	if err != nil {
H
hongming 已提交
612
		klog.V(4).Infoln(err)
Z
zryfish 已提交
613
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
614 615 616
		return
	}

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

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

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

	resp.WriteEntity(result)
}

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

L
LiHui 已提交
643 644 645 646 647 648 649
	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 已提交
650 651

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

	resp.WriteEntity(errors.None)
}

L
LiHui 已提交
660
// application release
H
hongming 已提交
661

L
LiHui 已提交
662 663 664 665 666 667 668
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 已提交
669 670

	if err != nil {
H
hongming 已提交
671 672
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
673 674 675
		return
	}

L
LiHui 已提交
676 677
	resp.WriteEntity(app)
	return
H
hongming 已提交
678 679
}

L
LiHui 已提交
680 681 682 683 684
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 已提交
685

L
LiHui 已提交
686
	err := h.openpitrix.DeleteApplication(workspace, clusterName, namespace, applicationId)
H
hongming 已提交
687 688

	if err != nil {
H
hongming 已提交
689 690
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
691 692 693
		return
	}

L
LiHui 已提交
694
	resp.WriteEntity(errors.None)
H
hongming 已提交
695 696
}

L
LiHui 已提交
697 698 699 700 701 702 703 704
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 已提交
705

L
LiHui 已提交
706 707
	if offset < 0 {
		offset = 0
H
hongming 已提交
708 709
	}

L
LiHui 已提交
710 711
	if limit <= 0 {
		limit = 10
H
hongming 已提交
712 713 714
	}

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

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

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

L
LiHui 已提交
728
	resp.WriteAsJson(result)
H
hongming 已提交
729 730
}

L
LiHui 已提交
731 732 733 734 735
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 已提交
736
	if err != nil {
H
hongming 已提交
737
		klog.V(4).Infoln(err)
Z
zryfish 已提交
738
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
739 740 741
		return
	}

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

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

L
LiHui 已提交
756
	resp.WriteEntity(errors.None)
H
hongming 已提交
757 758
}

L
LiHui 已提交
759 760 761 762 763
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 已提交
764
	if err != nil {
H
hongming 已提交
765
		klog.V(4).Infoln(err)
Z
zryfish 已提交
766
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
767 768 769
		return
	}

L
LiHui 已提交
770 771 772 773
	modifyClusterAttributesRequest.Namespace = namespace
	modifyClusterAttributesRequest.ClusterID = applicationId

	err = h.openpitrix.ModifyApplication(modifyClusterAttributesRequest)
H
hongming 已提交
774 775

	if err != nil {
H
hongming 已提交
776 777
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
778 779 780
		return
	}

L
LiHui 已提交
781
	resp.WriteEntity(errors.None)
H
hongming 已提交
782 783
}

L
LiHui 已提交
784 785 786 787 788 789 790
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 已提交
791
	if err != nil {
H
hongming 已提交
792
		klog.V(4).Infoln(err)
Z
zryfish 已提交
793
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
794 795
		return
	}
L
LiHui 已提交
796 797 798
	user, _ := request.UserFrom(req.Request.Context())
	if user != nil {
		createClusterRequest.Username = user.GetName()
799 800
	}

L
LiHui 已提交
801
	err = h.openpitrix.CreateApplication(workspace, clusterName, namespace, createClusterRequest)
H
hongming 已提交
802 803

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

L
LiHui 已提交
809
	resp.WriteEntity(errors.None)
H
hongming 已提交
810 811
}

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

L
LiHui 已提交
821
	result, err := h.openpitrix.CreateCategory(createCategoryRequest)
H
hongming 已提交
822 823

	if err != nil {
H
hongming 已提交
824 825
		klog.Errorln(err)
		handleOpenpitrixError(resp, err)
H
hongming 已提交
826 827 828
		return
	}

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

L
LiHui 已提交
834
	err := h.openpitrix.DeleteCategory(categoryId)
H
hongming 已提交
835 836

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

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

L
LiHui 已提交
854
	err = h.openpitrix.ModifyCategory(categoryId, &modifyCategoryRequest)
H
hongming 已提交
855 856

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

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

L
LiHui 已提交
867
	result, err := h.openpitrix.DescribeCategory(categoryId)
H
hongming 已提交
868 869

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

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

883 884 885 886 887 888
	if err != nil {
		klog.V(4).Infoln(err)
		api.HandleBadRequest(resp, nil, err)
		return
	}

L
LiHui 已提交
889
	result, err := h.openpitrix.ListCategories(conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
890 891

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

	resp.WriteEntity(result)
}

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

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

L
LiHui 已提交
913
	result, err := h.openpitrix.ListAppVersionReviews(conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
914 915

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

	resp.WriteEntity(result)
Z
zryfish 已提交
922
}
H
hongming 已提交
923

L
LiHui 已提交
924 925 926 927 928 929 930 931
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 已提交
932 933
		return
	}
L
LiHui 已提交
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950

	// 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 已提交
951
		api.HandleBadRequest(resp, nil, err)
H
hongming 已提交
952 953
		return
	}
L
LiHui 已提交
954 955 956 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

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