register.go 17.3 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 23 24 25 26 27
/*
 *
 * 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 v1

import (
	"github.com/emicklei/go-restful"
	"github.com/emicklei/go-restful-openapi"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"kubesphere.io/kubesphere/pkg/apiserver/openpitrix"
	"kubesphere.io/kubesphere/pkg/apiserver/runtime"
	"kubesphere.io/kubesphere/pkg/constants"
	"kubesphere.io/kubesphere/pkg/models"
Z
zryfish 已提交
28 29
	"kubesphere.io/kubesphere/pkg/models/openpitrix/application"
	"kubesphere.io/kubesphere/pkg/models/openpitrix/type"
H
hongming 已提交
30 31 32 33 34 35 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 76 77 78 79 80 81
	"kubesphere.io/kubesphere/pkg/server/errors"
	"kubesphere.io/kubesphere/pkg/server/params"
	"net/http"
)

const GroupName = "openpitrix.io"

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

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

func addWebService(c *restful.Container) error {

	ok := "ok"
	mimePatch := []string{restful.MIME_JSON, runtime.MimeMergePatchJson, runtime.MimeJsonPatchJson}
	webservice := runtime.NewWebService(GroupVersion)

	webservice.Route(webservice.GET("/applications").
		To(openpitrix.ListApplications).
		Returns(http.StatusOK, ok, models.PageableResponse{}).
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
		Doc("List all applications").
		Param(webservice.QueryParameter(params.ConditionsParam, "query conditions, connect multiple conditions with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").
			Required(false).
			DataFormat("key=value,key~value").
			DefaultValue("")).
		Param(webservice.PathParameter("namespace", "the name of the project")).
		Param(webservice.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
			Required(false).
			DataFormat("limit=%d,page=%d").
			DefaultValue("limit=10,page=1")))

	webservice.Route(webservice.GET("/namespaces/{namespace}/applications").
		To(openpitrix.ListApplications).
		Returns(http.StatusOK, ok, models.PageableResponse{}).
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
		Doc("List all applications within the specified namespace").
		Param(webservice.QueryParameter(params.ConditionsParam, "query conditions, connect multiple conditions with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").
			Required(false).
			DataFormat("key=value,key~value").
			DefaultValue("")).
		Param(webservice.PathParameter("namespace", "the name of the project")).
		Param(webservice.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
			Required(false).
			DataFormat("limit=%d,page=%d").
			DefaultValue("limit=10,page=1")))

	webservice.Route(webservice.GET("/namespaces/{namespace}/applications/{application}").
		To(openpitrix.DescribeApplication).
Z
zryfish 已提交
82
		Returns(http.StatusOK, ok, application.Application{}).
H
hongming 已提交
83 84 85 86 87 88 89 90 91
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
		Doc("Describe the specified application of the namespace").
		Param(webservice.PathParameter("namespace", "the name of the project")).
		Param(webservice.PathParameter("application", "application ID")))

	webservice.Route(webservice.POST("/namespaces/{namespace}/applications").
		To(openpitrix.CreateApplication).
		Doc("Deploy a new application").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Z
zryfish 已提交
92
		Reads(types.CreateClusterRequest{}).
H
hongming 已提交
93 94 95 96 97 98 99 100
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("namespace", "the name of the project")))

	webservice.Route(webservice.PATCH("/namespaces/{namespace}/applications/{application}").
		Consumes(mimePatch...).
		To(openpitrix.ModifyApplication).
		Doc("Modify application").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Z
zryfish 已提交
101
		Reads(types.ModifyClusterAttributesRequest{}).
H
hongming 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("namespace", "the name of the project")).
		Param(webservice.PathParameter("application", "the id of the application cluster")))

	webservice.Route(webservice.DELETE("/namespaces/{namespace}/applications/{application}").
		To(openpitrix.DeleteApplication).
		Doc("Delete the specified application").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("namespace", "the name of the project")).
		Param(webservice.PathParameter("application", "the id of the application cluster")))

	webservice.Route(webservice.POST("/apps/{app}/versions").
		To(openpitrix.CreateAppVersion).
		Doc("Create a new app template version").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
Z
zryfish 已提交
118
		Reads(types.CreateAppVersionRequest{}).
H
hongming 已提交
119
		Param(webservice.QueryParameter("validate", "Validate format of package(pack by op tool)")).
Z
zryfish 已提交
120
		Returns(http.StatusOK, ok, types.CreateAppVersionResponse{}).
H
hongming 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133
		Param(webservice.PathParameter("app", "app template id")))
	webservice.Route(webservice.DELETE("/apps/{app}/versions/{version}").
		To(openpitrix.DeleteAppVersion).
		Doc("Delete the specified app template version").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("version", "app template version id")).
		Param(webservice.PathParameter("app", "app template id")))
	webservice.Route(webservice.PATCH("/apps/{app}/versions/{version}").
		Consumes(mimePatch...).
		To(openpitrix.ModifyAppVersion).
		Doc("Patch the specified app template version").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
Z
zryfish 已提交
134
		Reads(types.ModifyAppVersionRequest{}).
H
hongming 已提交
135 136 137 138 139 140 141
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("version", "app template version id")).
		Param(webservice.PathParameter("app", "app template id")))
	webservice.Route(webservice.GET("/apps/{app}/versions/{version}").
		To(openpitrix.DescribeAppVersion).
		Doc("Describe the specified app template version").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
Z
zryfish 已提交
142
		Returns(http.StatusOK, ok, types.AppVersion{}).
H
hongming 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
		Param(webservice.PathParameter("version", "app template version id")).
		Param(webservice.PathParameter("app", "app template id")))
	webservice.Route(webservice.GET("/apps/{app}/versions").
		To(openpitrix.ListAppVersions).
		Doc("Get active versions of app, can filter with these fields(version_id, app_id, name, owner, description, package_name, status, type), default return all active app versions").
		Param(webservice.QueryParameter(params.ConditionsParam, "query conditions,connect multiple conditions with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").
			Required(false).
			DataFormat("key=%s,key~%s")).
		Param(webservice.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
			Required(false).
			DataFormat("limit=%d,page=%d").
			DefaultValue("limit=10,page=1")).
		Param(webservice.PathParameter("app", "app template id")).
		Param(webservice.QueryParameter(params.ReverseParam, "sort parameters, e.g. reverse=true")).
		Param(webservice.QueryParameter(params.OrderByParam, "sort parameters, e.g. orderBy=createTime")).
		Returns(http.StatusOK, ok, models.PageableResponse{}))
	webservice.Route(webservice.GET("/apps/{app}/versions/{version}/audits").
		To(openpitrix.ListAppVersionAudits).
		Doc("List audits information of version-specific app template").
Z
zryfish 已提交
162
		Returns(http.StatusOK, ok, types.AppVersionAudit{}).
H
hongming 已提交
163 164 165 166 167
		Param(webservice.PathParameter("version", "app template version id")).
		Param(webservice.PathParameter("app", "app template id")))
	webservice.Route(webservice.GET("/apps/{app}/versions/{version}/package").
		To(openpitrix.GetAppVersionPackage).
		Doc("Get packages of version-specific app").
Z
zryfish 已提交
168
		Returns(http.StatusOK, ok, types.GetAppVersionPackageResponse{}).
H
hongming 已提交
169 170 171 172 173 174 175 176 177 178 179
		Param(webservice.PathParameter("version", "app template version id")).
		Param(webservice.PathParameter("app", "app template id")))
	webservice.Route(webservice.POST("/apps/{app}/versions/{version}/action").
		To(openpitrix.DoAppVersionAction).
		Doc("Perform submit or other operations on app").
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("version", "app template version id")).
		Param(webservice.PathParameter("app", "app template id")))
	webservice.Route(webservice.GET("/apps/{app}/versions/{version}/files").
		To(openpitrix.GetAppVersionFiles).
		Doc("Get app template package files").
Z
zryfish 已提交
180
		Returns(http.StatusOK, ok, types.GetAppVersionPackageFilesResponse{}).
H
hongming 已提交
181 182 183 184 185 186 187 188 189 190 191 192
		Param(webservice.PathParameter("version", "app template version id")).
		Param(webservice.PathParameter("app", "app template id")))
	webservice.Route(webservice.GET("/reviews").
		To(openpitrix.ListReviews).
		Doc("Get reviews of version-specific app").
		Param(webservice.QueryParameter(params.ConditionsParam, "query conditions,connect multiple conditions with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").
			Required(false).
			DataFormat("key=%s,key~%s")).
		Param(webservice.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
			Required(false).
			DataFormat("limit=%d,page=%d").
			DefaultValue("limit=10,page=1")).
Z
zryfish 已提交
193
		Returns(http.StatusOK, ok, types.AppVersionReview{}))
H
hongming 已提交
194 195 196 197
	webservice.Route(webservice.GET("/apps/{app}/audits").
		To(openpitrix.ListAppVersionAudits).
		Doc("List audits information of the specific app template").
		Param(webservice.PathParameter("app", "app template id")).
Z
zryfish 已提交
198
		Returns(http.StatusOK, ok, types.AppVersionAudit{}))
H
hongming 已提交
199 200 201 202
	webservice.Route(webservice.POST("/apps").
		To(openpitrix.CreateApp).
		Doc("Create a new app template").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
Z
zryfish 已提交
203 204
		Returns(http.StatusOK, ok, types.CreateAppResponse{}).
		Reads(types.CreateAppRequest{}).
H
hongming 已提交
205 206 207 208 209 210 211 212 213 214 215 216
		Param(webservice.PathParameter("app", "app template id")))
	webservice.Route(webservice.DELETE("/apps/{app}").
		To(openpitrix.DeleteApp).
		Doc("Delete the specified app template").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("app", "app template id")))
	webservice.Route(webservice.PATCH("/apps/{app}").
		Consumes(mimePatch...).
		To(openpitrix.ModifyApp).
		Doc("Patch the specified app template").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
Z
zryfish 已提交
217
		Reads(types.ModifyAppVersionRequest{}).
H
hongming 已提交
218 219 220 221 222 223
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("app", "app template id")))
	webservice.Route(webservice.GET("/apps/{app}").
		To(openpitrix.DescribeApp).
		Doc("Describe the specified app template").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
Z
zryfish 已提交
224
		Returns(http.StatusOK, ok, types.AppVersion{}).
H
hongming 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
		Param(webservice.PathParameter("app", "app template id")))
	webservice.Route(webservice.POST("/apps/{app}/action").
		To(openpitrix.DoAppAction).
		Doc("Perform recover or suspend operation on app").
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("version", "app template version id")).
		Param(webservice.PathParameter("app", "app template id")))
	webservice.Route(webservice.GET("/apps").
		To(openpitrix.ListApps).
		Doc("List app templates").
		Param(webservice.QueryParameter(params.ConditionsParam, "query conditions,connect multiple conditions with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").
			Required(false).
			DataFormat("key=%s,key~%s")).
		Param(webservice.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
			Required(false).
			DataFormat("limit=%d,page=%d").
			DefaultValue("limit=10,page=1")).
		Param(webservice.QueryParameter(params.ReverseParam, "sort parameters, e.g. reverse=true")).
		Param(webservice.QueryParameter(params.OrderByParam, "sort parameters, e.g. orderBy=createTime")).
		Returns(http.StatusOK, ok, models.PageableResponse{}))
	webservice.Route(webservice.POST("/categories").
		To(openpitrix.CreateCategory).
		Doc("Create app template category").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
Z
zryfish 已提交
249 250
		Reads(types.CreateCategoryRequest{}).
		Returns(http.StatusOK, ok, types.CreateCategoryResponse{}).
H
hongming 已提交
251 252 253 254 255 256 257 258 259 260 261 262
		Param(webservice.PathParameter("app", "app template id")))
	webservice.Route(webservice.DELETE("/categories/{category}").
		To(openpitrix.DeleteCategory).
		Doc("Delete the specified category").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("category", "category id")))
	webservice.Route(webservice.PATCH("/categories/{category}").
		Consumes(mimePatch...).
		To(openpitrix.ModifyCategory).
		Doc("Patch the specified category").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
Z
zryfish 已提交
263
		Reads(types.ModifyCategoryRequest{}).
H
hongming 已提交
264 265 266 267 268 269
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("category", "category id")))
	webservice.Route(webservice.GET("/categories/{category}").
		To(openpitrix.DescribeCategory).
		Doc("Describe the specified category").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
Z
zryfish 已提交
270
		Returns(http.StatusOK, ok, types.Category{}).
H
hongming 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
		Param(webservice.PathParameter("category", "category id")))
	webservice.Route(webservice.GET("/categories").
		To(openpitrix.ListCategories).
		Doc("List categories").
		Param(webservice.QueryParameter(params.ConditionsParam, "query conditions,connect multiple conditions with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").
			Required(false).
			DataFormat("key=%s,key~%s")).
		Param(webservice.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
			Required(false).
			DataFormat("limit=%d,page=%d").
			DefaultValue("limit=10,page=1")).
		Param(webservice.QueryParameter(params.ReverseParam, "sort parameters, e.g. reverse=true")).
		Param(webservice.QueryParameter(params.OrderByParam, "sort parameters, e.g. orderBy=createTime")).
		Returns(http.StatusOK, ok, models.PageableResponse{}))

	webservice.Route(webservice.GET("/attachments/{attachment}").
		To(openpitrix.DescribeAttachment).
		Doc("Get attachment by attachment id").
		Param(webservice.PathParameter("attachment", "attachment id")).
Z
zryfish 已提交
290
		Returns(http.StatusOK, ok, types.Attachment{}))
H
hongming 已提交
291 292 293 294 295 296

	webservice.Route(webservice.POST("/repos").
		To(openpitrix.CreateRepo).
		Doc("Create repository, repository used to store package of app").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
		Param(webservice.QueryParameter("validate", "Validate repository")).
Z
zryfish 已提交
297 298
		Returns(http.StatusOK, ok, types.CreateRepoResponse{}).
		Reads(types.CreateRepoRequest{}))
H
hongming 已提交
299 300 301 302 303 304 305 306 307 308 309
	webservice.Route(webservice.DELETE("/repos/{repo}").
		To(openpitrix.DeleteRepo).
		Doc("Delete the specified repository").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("repo", "repo id")))
	webservice.Route(webservice.PATCH("/repos/{repo}").
		Consumes(mimePatch...).
		To(openpitrix.ModifyRepo).
		Doc("Patch the specified repository").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
Z
zryfish 已提交
310
		Reads(types.ModifyRepoRequest{}).
H
hongming 已提交
311 312 313 314 315 316
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("repo", "repo id")))
	webservice.Route(webservice.GET("/repos/{repo}").
		To(openpitrix.DescribeRepo).
		Doc("Describe the specified repository").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.OpenpitrixTag}).
Z
zryfish 已提交
317
		Returns(http.StatusOK, ok, types.Repo{}).
H
hongming 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
		Param(webservice.PathParameter("repo", "repo id")))
	webservice.Route(webservice.GET("/repos").
		To(openpitrix.ListRepos).
		Doc("List repositories").
		Param(webservice.QueryParameter(params.ConditionsParam, "query conditions,connect multiple conditions with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").
			Required(false).
			DataFormat("key=%s,key~%s")).
		Param(webservice.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
			Required(false).
			DataFormat("limit=%d,page=%d").
			DefaultValue("limit=10,page=1")).
		Param(webservice.QueryParameter(params.ReverseParam, "sort parameters, e.g. reverse=true")).
		Param(webservice.QueryParameter(params.OrderByParam, "sort parameters, e.g. orderBy=createTime")).
		Returns(http.StatusOK, ok, models.PageableResponse{}))
	webservice.Route(webservice.POST("/repos/{repo}/action").
		To(openpitrix.DoRepoAction).
		Doc("Start index repository event").
Z
zryfish 已提交
335
		Reads(types.RepoActionRequest{}).
H
hongming 已提交
336 337 338 339 340 341 342 343 344 345 346 347
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("repo", "repo id")))
	webservice.Route(webservice.GET("/repos/{repo}/events").
		To(openpitrix.ListRepoEvents).
		Doc("Get repository events").
		Returns(http.StatusOK, ok, models.PageableResponse{}).
		Param(webservice.PathParameter("repo", "repo id")))

	c.Add(webservice)

	return nil
}