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

Z
zryfish 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
import (
	"github.com/emicklei/go-restful"
	v1 "k8s.io/api/core/v1"
	"k8s.io/klog"
	"kubesphere.io/kubesphere/pkg/api"
	"kubesphere.io/kubesphere/pkg/constants"
	"kubesphere.io/kubesphere/pkg/informers"
	"kubesphere.io/kubesphere/pkg/models"
	"kubesphere.io/kubesphere/pkg/models/openpitrix/application"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/resource"
	"kubesphere.io/kubesphere/pkg/server/params"
	"openpitrix.io/openpitrix/pkg/pb"
)
Z
zryfish 已提交
16 17

type openpitrixHandler struct {
Z
zryfish 已提交
18 19
	namespacesGetter *resource.NamespacedResourceGetter
	applicationOperator application.Interface
Z
zryfish 已提交
20 21
}

Z
zryfish 已提交
22 23 24 25 26
func newOpenpitrixHandler(factory informers.InformerFactory, client pb.ClusterManagerClient) *openpitrixHandler {
	return &openpitrixHandler{
		namespacesGetter: resource.New(factory),
		applicationOperator: application.NewApplicaitonOperator(factory.KubernetesSharedInformerFactory(), client),
	}
Z
zryfish 已提交
27 28 29
}

func (h *openpitrixHandler) handleListApplications(request *restful.Request, response *restful.Response) {
Z
zryfish 已提交
30 31 32 33 34
	limit, offset := params.ParsePaging(request.QueryParameter(params.PagingParam))
	namespaceName := request.PathParameter("namespace")
	conditions, err := params.ParseConditions(request.QueryParameter(params.ConditionsParam))
	orderBy := request.QueryParameter(params.OrderByParam)
	reverse := params.ParseReverse(request)
Z
zryfish 已提交
35

Z
zryfish 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	if orderBy == "" {
		orderBy = "create_time"
		reverse = true
	}

	if err != nil {
		api.HandleBadRequest(response, err)
		return
	}

	if namespaceName != "" {
		namespace, err := h.namespacesGetter.Get(api.ResourceKindNamespace, "", namespaceName)

		if err != nil {
			api.HandleInternalError(response, err)
			return
		}
		var runtimeId string

		if ns, ok := namespace.(*v1.Namespace); ok {
			runtimeId = ns.Annotations[constants.OpenPitrixRuntimeAnnotationKey]
		}

		if runtimeId == "" {
			response.WriteAsJson(models.PageableResponse{Items: []interface{}{}, TotalCount: 0})
			return
		} else {
			conditions.Match["runtime_id"] = runtimeId
		}
	}

	result, err := h.applicationOperator.List(conditions, limit, offset, orderBy, reverse)

	if err != nil {
		klog.Errorln(err)
		api.HandleInternalError(response, err)
		return
	}

	resp.WriteAsJson(result)
Z
zryfish 已提交
76
}