167 lines
5.1 KiB
Go
167 lines
5.1 KiB
Go
/*
|
|
*
|
|
* Copyright 2019 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.
|
|
* /
|
|
*/
|
|
|
|
package openpitrix
|
|
|
|
import (
|
|
"github.com/emicklei/go-restful"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
"k8s.io/klog"
|
|
"kubesphere.io/kubesphere/pkg/models/openpitrix"
|
|
"kubesphere.io/kubesphere/pkg/server/errors"
|
|
"kubesphere.io/kubesphere/pkg/server/params"
|
|
"kubesphere.io/kubesphere/pkg/simple/client"
|
|
"net/http"
|
|
)
|
|
|
|
func CreateCategory(req *restful.Request, resp *restful.Response) {
|
|
createCategoryRequest := &openpitrix.CreateCategoryRequest{}
|
|
err := req.ReadEntity(createCategoryRequest)
|
|
if err != nil {
|
|
resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
|
|
return
|
|
}
|
|
|
|
result, err := openpitrix.CreateCategory(createCategoryRequest)
|
|
|
|
if _, notEnabled := err.(client.ClientSetNotEnabledError); notEnabled {
|
|
resp.WriteHeaderAndEntity(http.StatusNotImplemented, errors.Wrap(err))
|
|
return
|
|
}
|
|
if status.Code(err) == codes.InvalidArgument {
|
|
resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
|
|
return
|
|
}
|
|
if err != nil {
|
|
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
|
return
|
|
}
|
|
|
|
resp.WriteEntity(result)
|
|
}
|
|
func DeleteCategory(req *restful.Request, resp *restful.Response) {
|
|
categoryId := req.PathParameter("category")
|
|
|
|
err := openpitrix.DeleteCategory(categoryId)
|
|
|
|
if _, notEnabled := err.(client.ClientSetNotEnabledError); notEnabled {
|
|
resp.WriteHeaderAndEntity(http.StatusNotImplemented, errors.Wrap(err))
|
|
return
|
|
}
|
|
if status.Code(err) == codes.NotFound {
|
|
resp.WriteHeaderAndEntity(http.StatusNotFound, errors.Wrap(err))
|
|
return
|
|
}
|
|
if err != nil {
|
|
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
|
return
|
|
}
|
|
|
|
resp.WriteEntity(errors.None)
|
|
}
|
|
func ModifyCategory(req *restful.Request, resp *restful.Response) {
|
|
var modifyCategoryRequest openpitrix.ModifyCategoryRequest
|
|
categoryId := req.PathParameter("category")
|
|
err := req.ReadEntity(&modifyCategoryRequest)
|
|
if err != nil {
|
|
resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
|
|
return
|
|
}
|
|
|
|
err = openpitrix.PatchCategory(categoryId, &modifyCategoryRequest)
|
|
|
|
if _, notEnabled := err.(client.ClientSetNotEnabledError); notEnabled {
|
|
resp.WriteHeaderAndEntity(http.StatusNotImplemented, errors.Wrap(err))
|
|
return
|
|
}
|
|
if status.Code(err) == codes.InvalidArgument {
|
|
resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
|
|
return
|
|
}
|
|
if err != nil {
|
|
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
|
return
|
|
}
|
|
|
|
resp.WriteEntity(errors.None)
|
|
}
|
|
func DescribeCategory(req *restful.Request, resp *restful.Response) {
|
|
categoryId := req.PathParameter("category")
|
|
|
|
result, err := openpitrix.DescribeCategory(categoryId)
|
|
|
|
if _, notEnabled := err.(client.ClientSetNotEnabledError); notEnabled {
|
|
resp.WriteHeaderAndEntity(http.StatusNotImplemented, errors.Wrap(err))
|
|
return
|
|
}
|
|
|
|
if status.Code(err) == codes.NotFound {
|
|
resp.WriteHeaderAndEntity(http.StatusNotFound, errors.Wrap(err))
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
|
return
|
|
}
|
|
|
|
resp.WriteEntity(result)
|
|
}
|
|
func ListCategories(req *restful.Request, resp *restful.Response) {
|
|
orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
|
|
limit, offset := params.ParsePaging(req)
|
|
reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
|
|
statistics := params.GetBoolValueWithDefault(req, "statistics", false)
|
|
conditions, err := params.ParseConditions(req)
|
|
|
|
if err != nil {
|
|
resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
|
|
return
|
|
}
|
|
|
|
result, err := openpitrix.ListCategories(conditions, orderBy, reverse, limit, offset)
|
|
|
|
if _, notEnabled := err.(client.ClientSetNotEnabledError); notEnabled {
|
|
resp.WriteHeaderAndEntity(http.StatusNotImplemented, errors.Wrap(err))
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
klog.Errorln(err)
|
|
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
|
return
|
|
}
|
|
|
|
if statistics {
|
|
for _, item := range result.Items {
|
|
if category, ok := item.(*openpitrix.Category); ok {
|
|
statisticsResult, err := openpitrix.ListApps(¶ms.Conditions{Match: map[string]string{openpitrix.CategoryId: category.CategoryID, openpitrix.Status: openpitrix.StatusActive, openpitrix.RepoId: openpitrix.BuiltinRepoId}}, "", false, 0, 0)
|
|
if err != nil {
|
|
klog.Errorln(err)
|
|
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
|
return
|
|
}
|
|
category.AppTotal = &statisticsResult.TotalCount
|
|
}
|
|
}
|
|
}
|
|
|
|
resp.WriteEntity(result)
|
|
}
|