register.go 9.4 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*

 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 v1alpha2

import (
	"github.com/emicklei/go-restful"
	"github.com/emicklei/go-restful-openapi"
H
hongming 已提交
23
	"k8s.io/api/core/v1"
H
hongming 已提交
24
	"k8s.io/apimachinery/pkg/runtime/schema"
H
hongming 已提交
25
	"kubesphere.io/kubesphere/pkg/apis/tenant/v1alpha1"
H
hongming 已提交
26 27
	"kubesphere.io/kubesphere/pkg/apiserver/runtime"
	"kubesphere.io/kubesphere/pkg/apiserver/tenant"
R
runzexia 已提交
28 29
	"kubesphere.io/kubesphere/pkg/models/devops"
	"kubesphere.io/kubesphere/pkg/params"
H
hongming 已提交
30
	"kubesphere.io/kubesphere/pkg/simple/client/elasticsearch"
31

H
hongming 已提交
32 33
	"kubesphere.io/kubesphere/pkg/errors"
	"kubesphere.io/kubesphere/pkg/models"
34

R
runzexia 已提交
35
	"net/http"
H
hongming 已提交
36 37
)

R
runzexia 已提交
38 39 40 41
const (
	GroupName = "tenant.kubesphere.io"
	RespOK    = "ok"
)
H
hongming 已提交
42 43 44 45 46 47 48 49 50 51

var GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1alpha2"}

var (
	WebServiceBuilder = runtime.NewContainerBuilder(addWebService)
	AddToContainer    = WebServiceBuilder.AddToContainer
)

func addWebService(c *restful.Container) error {
	tags := []string{"Tenant"}
H
hongming 已提交
52
	ok := "ok"
H
hongming 已提交
53 54 55 56
	ws := runtime.NewWebService(GroupVersion)

	ws.Route(ws.GET("/workspaces").
		To(tenant.ListWorkspaces).
H
hongming 已提交
57
		Returns(http.StatusOK, ok, models.PageableResponse{}).
H
hongming 已提交
58 59
		Doc("List workspace by user").
		Metadata(restfulspec.KeyOpenAPITags, tags))
H
hongming 已提交
60 61
	ws.Route(ws.GET("/workspaces/{workspace}").
		To(tenant.DescribeWorkspace).
H
hongming 已提交
62 63
		Doc("Describe workspace").
		Param(ws.PathParameter("workspace", "workspace name")).
H
hongming 已提交
64
		Returns(http.StatusOK, ok, v1alpha1.Workspace{}).
H
hongming 已提交
65
		Metadata(restfulspec.KeyOpenAPITags, tags))
H
hongming 已提交
66 67 68 69
	ws.Route(ws.GET("/workspaces/{workspace}/rules").
		To(tenant.ListWorkspaceRules).
		Param(ws.PathParameter("workspace", "workspace name")).
		Doc("List the rules for the current user").
H
hongming 已提交
70
		Returns(http.StatusOK, ok, models.SimpleRule{}).
H
hongming 已提交
71 72 73 74 75
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/namespaces/{namespace}/rules").
		To(tenant.ListNamespaceRules).
		Param(ws.PathParameter("namespace", "namespace")).
		Doc("List the rules for the current user").
H
hongming 已提交
76
		Returns(http.StatusOK, ok, models.SimpleRule{}).
H
hongming 已提交
77 78 79 80 81
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/devops/{devops}/rules").
		To(tenant.ListDevopsRules).
		Param(ws.PathParameter("devops", "devops project id")).
		Doc("List the rules for the current user").
H
hongming 已提交
82
		Returns(http.StatusOK, ok, models.SimpleRule{}).
H
hongming 已提交
83 84 85 86 87
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.GET("/workspaces/{workspace}/namespaces").
		To(tenant.ListNamespaces).
		Param(ws.PathParameter("workspace", "workspace name")).
		Doc("List the namespaces for the current user").
H
hongming 已提交
88
		Returns(http.StatusOK, ok, []v1.Namespace{}).
H
hongming 已提交
89
		Metadata(restfulspec.KeyOpenAPITags, tags))
H
hongming 已提交
90
	ws.Route(ws.GET("/workspaces/{workspace}/members/{member}/namespaces").
H
hongming 已提交
91
		To(tenant.ListNamespacesByUsername).
H
hongming 已提交
92
		Param(ws.PathParameter("workspace", "workspace name")).
H
hongming 已提交
93
		Param(ws.PathParameter("member", "workspace member's username")).
H
hongming 已提交
94
		Doc("List the namespaces for the workspace member").
H
hongming 已提交
95
		Returns(http.StatusOK, ok, []v1.Namespace{}).
H
hongming 已提交
96 97 98 99 100
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.POST("/workspaces/{workspace}/namespaces").
		To(tenant.CreateNamespace).
		Param(ws.PathParameter("workspace", "workspace name")).
		Doc("Create namespace").
H
hongming 已提交
101
		Returns(http.StatusOK, ok, []v1.Namespace{}).
H
hongming 已提交
102 103 104 105 106 107
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.DELETE("/workspaces/{workspace}/namespaces/{namespace}").
		To(tenant.DeleteNamespace).
		Param(ws.PathParameter("workspace", "workspace name")).
		Param(ws.PathParameter("namespace", "namespace")).
		Doc("Delete namespace").
H
hongming 已提交
108
		Returns(http.StatusOK, ok, errors.Error{}).
H
hongming 已提交
109 110 111 112 113
		Metadata(restfulspec.KeyOpenAPITags, tags))

	ws.Route(ws.GET("/workspaces/{workspace}/devops").
		To(tenant.ListDevopsProjects).
		Param(ws.PathParameter("workspace", "workspace name")).
R
runzexia 已提交
114 115 116 117 118 119 120
		Param(ws.QueryParameter(params.PagingParam, "page").
			Required(false).
			DataFormat("limit=%d,page=%d").
			DefaultValue("limit=10,page=1")).
		Param(ws.QueryParameter(params.ConditionsParam, "query conditions").
			Required(false).
			DataFormat("key=%s,key~%s")).
H
hongming 已提交
121 122
		Doc("List devops projects for the current user").
		Metadata(restfulspec.KeyOpenAPITags, tags))
H
hongming 已提交
123
	ws.Route(ws.GET("/workspaces/{workspace}/members/{member}/devops").
H
hongming 已提交
124
		To(tenant.ListDevopsProjectsByUsername).
H
hongming 已提交
125
		Param(ws.PathParameter("workspace", "workspace name")).
H
hongming 已提交
126
		Param(ws.PathParameter("member", "workspace member's username")).
R
runzexia 已提交
127 128 129 130 131 132 133
		Param(ws.QueryParameter(params.PagingParam, "page").
			Required(false).
			DataFormat("limit=%d,page=%d").
			DefaultValue("limit=10,page=1")).
		Param(ws.QueryParameter(params.ConditionsParam, "query conditions").
			Required(false).
			DataFormat("key=%s,key~%s")).
H
hongming 已提交
134
		Returns(http.StatusOK, ok, models.PageableResponse{}).
H
hongming 已提交
135 136 137 138 139 140
		Doc("List the devops projects for the workspace member").
		Metadata(restfulspec.KeyOpenAPITags, tags))
	ws.Route(ws.POST("/workspaces/{workspace}/devops").
		To(tenant.CreateDevopsProject).
		Param(ws.PathParameter("workspace", "workspace name")).
		Doc("Create devops project").
R
runzexia 已提交
141 142
		Reads(devops.DevOpsProject{}).
		Returns(http.StatusOK, RespOK, devops.DevOpsProject{}).
H
hongming 已提交
143
		Metadata(restfulspec.KeyOpenAPITags, tags))
H
hongming 已提交
144
	ws.Route(ws.DELETE("/workspaces/{workspace}/devops/{devops}").
H
hongming 已提交
145 146
		To(tenant.DeleteDevopsProject).
		Param(ws.PathParameter("workspace", "workspace name")).
H
hongming 已提交
147
		Param(ws.PathParameter("devops", "devops project id")).
H
hongming 已提交
148
		Doc("Delete devops project").
R
runzexia 已提交
149
		Returns(http.StatusOK, RespOK, devops.DevOpsProject{}).
H
hongming 已提交
150
		Metadata(restfulspec.KeyOpenAPITags, tags))
H
hongming 已提交
151
	ws.Route(ws.GET("/logs").
152 153
		To(tenant.LogQuery).
		Doc("Query cluster-level logs in a multi-tenants environment").
154
		Param(ws.QueryParameter("operation", "Query operation type. One of query, statistics, histogram.").DataType("string").Required(true)).
R
Ray Zhou 已提交
155
		Param(ws.QueryParameter("workspaces", "List of workspaces separated by comma the query will perform against.").DataType("string").Required(false)).
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
		Param(ws.QueryParameter("workspace_query", "List of keywords for filtering workspaces. Workspaces whose name contains at least one keyword will be matched for query. Non case-sensitive matching. eg. one,two.").DataType("string").Required(false)).
		Param(ws.QueryParameter("namespaces", "List of namespaces the query will perform against, eg. ns-one,ns-two").DataType("string").Required(false)).
		Param(ws.QueryParameter("namespace_query", "List of keywords for filtering namespaces. Namespaces whose name contains at least one keyword will be matched for query. Non case-sensitive matching. eg. one,two.").DataType("string").Required(false)).
		Param(ws.QueryParameter("workloads", "List of workloads the query will perform against, eg. wl-one,wl-two").DataType("string").Required(false)).
		Param(ws.QueryParameter("workload_query", "List of keywords for filtering workloads. Workloads whose name contains at least one keyword will be matched for query. Non case-sensitive matching. eg. one,two.").DataType("string").Required(false)).
		Param(ws.QueryParameter("pods", "List of pods the query will perform against, eg. pod-one,pod-two").DataType("string").Required(false)).
		Param(ws.QueryParameter("pod_query", "List of keywords for filtering pods. Pods whose name contains at least one keyword will be matched for query. Non case-sensitive matching. eg. one,two.").DataType("string").Required(false)).
		Param(ws.QueryParameter("containers", "List of containers the query will perform against, eg. container-one,container-two").DataType("string").Required(false)).
		Param(ws.QueryParameter("container_query", "List of keywords for filtering containers. Containers whose name contains at least one keyword will be matched for query. Non case-sensitive matching. eg. one,two.").DataType("string").Required(false)).
		Param(ws.QueryParameter("log_query", "List of keywords  for filtering logs. The query returns log containing at least one keyword. Non case-sensitive matching. eg. err,INFO.").DataType("string").Required(false)).
		Param(ws.QueryParameter("interval", "Count logs at intervals. Valid only if operation is histogram. The unit can be ms(milliseconds), s(seconds), m(minutes), h(hours), d(days), w(weeks), M(months), q(quarters), y(years). eg. 30m.").DataType("string").Required(false)).
		Param(ws.QueryParameter("start_time", "Start time of query range, eg. 1559664000000.").DataType("string").Required(false)).
		Param(ws.QueryParameter("end_time", "End time of query range, eg. 1559664000000.").DataType("string").Required(false)).
		Param(ws.QueryParameter("sort", "Sort log by time. One of acs, desc.").DataType("string").DefaultValue("desc").Required(false)).
		Param(ws.QueryParameter("from", "Beginning index of result to return. Use this option together with size.").DataType("integer").DefaultValue("0").Required(false)).
		Param(ws.QueryParameter("size", "Size of result to return.").DataType("integer").DefaultValue("10").Required(false)).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Writes(esclient.Response{}).
		Returns(http.StatusOK, RespOK, esclient.Response{})).
		Consumes(restful.MIME_JSON, restful.MIME_XML).
		Produces(restful.MIME_JSON)
H
hongming 已提交
177 178 179 180

	c.Add(ws)
	return nil
}