diff --git a/cmd/server/docs/docs.go b/cmd/server/docs/docs.go new file mode 100644 index 00000000..695318e9 --- /dev/null +++ b/cmd/server/docs/docs.go @@ -0,0 +1,380 @@ +// Package docs GENERATED BY SWAG; DO NOT EDIT +// This file was generated by swaggo/swag +package docs + +import "github.com/swaggo/swag" + +const docTemplate = `{ + "schemes": {{ marshal .Schemes }}, + "swagger": "2.0", + "info": { + "description": "{{escape .Description}}", + "title": "{{.Title}}", + "termsOfService": "http://kubeoperator.io", + "contact": { + "name": "Fit2cloud Support", + "url": "https://www.fit2cloud.com", + "email": "support@fit2cloud.com" + }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + }, + "version": "{{.Version}}" + }, + "host": "{{.Host}}", + "basePath": "{{.BasePath}}", + "paths": { + "/users": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "List all users", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "List all users", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/user.User" + } + } + } + } + }, + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Create user", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Create user", + "parameters": [ + { + "description": "request", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/docs.UserCreate" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/user.User" + } + } + } + } + }, + "/users/search": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Search users by Condition", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Search users", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/api.Page" + } + } + } + } + }, + "/users/{name}": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Get user by name", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Get user by name", + "parameters": [ + { + "type": "string", + "description": "用户名称", + "name": "name", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/user.User" + } + } + } + }, + "put": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Update user by name", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Update user by name", + "parameters": [ + { + "type": "string", + "description": "用户名称", + "name": "name", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/user.User" + } + } + } + }, + "delete": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Delete user by name", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Delete user by name", + "parameters": [ + { + "type": "string", + "description": "用户名称", + "name": "name", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/user.User" + } + } + } + } + } + }, + "definitions": { + "api.Page": { + "type": "object", + "properties": { + "items": {}, + "total": { + "type": "integer" + } + } + }, + "docs.Authenticate": { + "type": "object", + "properties": { + "password": { + "type": "string" + } + } + }, + "docs.Mfa": { + "type": "object", + "properties": { + "enable": { + "type": "boolean" + } + } + }, + "docs.UserCreate": { + "type": "object", + "properties": { + "authenticate": { + "$ref": "#/definitions/docs.Authenticate" + }, + "email": { + "type": "string" + }, + "isAdmin": { + "type": "boolean" + }, + "mfa": { + "$ref": "#/definitions/docs.Mfa" + }, + "name": { + "type": "string" + }, + "nickName": { + "type": "string" + } + } + }, + "user.Authenticate": { + "type": "object", + "properties": { + "password": { + "type": "string" + }, + "token": { + "type": "string" + } + } + }, + "user.Mfa": { + "type": "object", + "properties": { + "enable": { + "type": "boolean" + }, + "secret": { + "type": "string" + } + } + }, + "user.User": { + "type": "object", + "properties": { + "apiVersion": { + "type": "string" + }, + "authenticate": { + "$ref": "#/definitions/user.Authenticate" + }, + "builtIn": { + "type": "boolean" + }, + "createAt": { + "type": "string" + }, + "createdBy": { + "type": "string" + }, + "description": { + "type": "string" + }, + "email": { + "type": "string" + }, + "isAdmin": { + "type": "boolean" + }, + "kind": { + "type": "string" + }, + "language": { + "type": "string" + }, + "mfa": { + "$ref": "#/definitions/user.Mfa" + }, + "name": { + "type": "string" + }, + "nickName": { + "type": "string" + }, + "type": { + "type": "string" + }, + "updateAt": { + "type": "string" + }, + "uuid": { + "type": "string" + } + } + } + }, + "securityDefinitions": { + "ApiKeyAuth": { + "type": "apiKey", + "name": "Authorization", + "in": "header" + } + } +}` + +// SwaggerInfo holds exported Swagger Info so clients can modify it +var SwaggerInfo = &swag.Spec{ + Version: "", + Host: "", + BasePath: "/kubepi/api/v1", + Schemes: []string{}, + Title: "KubePi Restful API", + Description: "", + InfoInstanceName: "swagger", + SwaggerTemplate: docTemplate, +} + +func init() { + swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) +} diff --git a/cmd/server/docs/swagger.json b/cmd/server/docs/swagger.json new file mode 100644 index 00000000..d8c19ecf --- /dev/null +++ b/cmd/server/docs/swagger.json @@ -0,0 +1,354 @@ +{ + "swagger": "2.0", + "info": { + "title": "KubePi Restful API", + "termsOfService": "http://kubeoperator.io", + "contact": { + "name": "Fit2cloud Support", + "url": "https://www.fit2cloud.com", + "email": "support@fit2cloud.com" + }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + } + }, + "basePath": "/kubepi/api/v1", + "paths": { + "/users": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "List all users", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "List all users", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/user.User" + } + } + } + } + }, + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Create user", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Create user", + "parameters": [ + { + "description": "request", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/docs.UserCreate" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/user.User" + } + } + } + } + }, + "/users/search": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Search users by Condition", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Search users", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/api.Page" + } + } + } + } + }, + "/users/{name}": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Get user by name", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Get user by name", + "parameters": [ + { + "type": "string", + "description": "用户名称", + "name": "name", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/user.User" + } + } + } + }, + "put": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Update user by name", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Update user by name", + "parameters": [ + { + "type": "string", + "description": "用户名称", + "name": "name", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/user.User" + } + } + } + }, + "delete": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Delete user by name", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Delete user by name", + "parameters": [ + { + "type": "string", + "description": "用户名称", + "name": "name", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/user.User" + } + } + } + } + } + }, + "definitions": { + "api.Page": { + "type": "object", + "properties": { + "items": {}, + "total": { + "type": "integer" + } + } + }, + "docs.Authenticate": { + "type": "object", + "properties": { + "password": { + "type": "string" + } + } + }, + "docs.Mfa": { + "type": "object", + "properties": { + "enable": { + "type": "boolean" + } + } + }, + "docs.UserCreate": { + "type": "object", + "properties": { + "authenticate": { + "$ref": "#/definitions/docs.Authenticate" + }, + "email": { + "type": "string" + }, + "isAdmin": { + "type": "boolean" + }, + "mfa": { + "$ref": "#/definitions/docs.Mfa" + }, + "name": { + "type": "string" + }, + "nickName": { + "type": "string" + } + } + }, + "user.Authenticate": { + "type": "object", + "properties": { + "password": { + "type": "string" + }, + "token": { + "type": "string" + } + } + }, + "user.Mfa": { + "type": "object", + "properties": { + "enable": { + "type": "boolean" + }, + "secret": { + "type": "string" + } + } + }, + "user.User": { + "type": "object", + "properties": { + "apiVersion": { + "type": "string" + }, + "authenticate": { + "$ref": "#/definitions/user.Authenticate" + }, + "builtIn": { + "type": "boolean" + }, + "createAt": { + "type": "string" + }, + "createdBy": { + "type": "string" + }, + "description": { + "type": "string" + }, + "email": { + "type": "string" + }, + "isAdmin": { + "type": "boolean" + }, + "kind": { + "type": "string" + }, + "language": { + "type": "string" + }, + "mfa": { + "$ref": "#/definitions/user.Mfa" + }, + "name": { + "type": "string" + }, + "nickName": { + "type": "string" + }, + "type": { + "type": "string" + }, + "updateAt": { + "type": "string" + }, + "uuid": { + "type": "string" + } + } + } + }, + "securityDefinitions": { + "ApiKeyAuth": { + "type": "apiKey", + "name": "Authorization", + "in": "header" + } + } +} \ No newline at end of file diff --git a/cmd/server/docs/swagger.yaml b/cmd/server/docs/swagger.yaml new file mode 100644 index 00000000..b52f75af --- /dev/null +++ b/cmd/server/docs/swagger.yaml @@ -0,0 +1,225 @@ +basePath: /kubepi/api/v1 +definitions: + api.Page: + properties: + items: {} + total: + type: integer + type: object + docs.Authenticate: + properties: + password: + type: string + type: object + docs.Mfa: + properties: + enable: + type: boolean + type: object + docs.UserCreate: + properties: + authenticate: + $ref: '#/definitions/docs.Authenticate' + email: + type: string + isAdmin: + type: boolean + mfa: + $ref: '#/definitions/docs.Mfa' + name: + type: string + nickName: + type: string + type: object + user.Authenticate: + properties: + password: + type: string + token: + type: string + type: object + user.Mfa: + properties: + enable: + type: boolean + secret: + type: string + type: object + user.User: + properties: + apiVersion: + type: string + authenticate: + $ref: '#/definitions/user.Authenticate' + builtIn: + type: boolean + createAt: + type: string + createdBy: + type: string + description: + type: string + email: + type: string + isAdmin: + type: boolean + kind: + type: string + language: + type: string + mfa: + $ref: '#/definitions/user.Mfa' + name: + type: string + nickName: + type: string + type: + type: string + updateAt: + type: string + uuid: + type: string + type: object +info: + contact: + email: support@fit2cloud.com + name: Fit2cloud Support + url: https://www.fit2cloud.com + license: + name: Apache 2.0 + url: http://www.apache.org/licenses/LICENSE-2.0.html + termsOfService: http://kubeoperator.io + title: KubePi Restful API +paths: + /users: + get: + consumes: + - application/json + description: List all users + produces: + - application/json + responses: + "200": + description: OK + schema: + items: + $ref: '#/definitions/user.User' + type: array + security: + - ApiKeyAuth: [] + summary: List all users + tags: + - users + post: + consumes: + - application/json + description: Create user + parameters: + - description: request + in: body + name: request + required: true + schema: + $ref: '#/definitions/docs.UserCreate' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/user.User' + security: + - ApiKeyAuth: [] + summary: Create user + tags: + - users + /users/{name}: + delete: + consumes: + - application/json + description: Delete user by name + parameters: + - description: 用户名称 + in: path + name: name + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/user.User' + security: + - ApiKeyAuth: [] + summary: Delete user by name + tags: + - users + get: + consumes: + - application/json + description: Get user by name + parameters: + - description: 用户名称 + in: path + name: name + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/user.User' + security: + - ApiKeyAuth: [] + summary: Get user by name + tags: + - users + put: + consumes: + - application/json + description: Update user by name + parameters: + - description: 用户名称 + in: path + name: name + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/user.User' + security: + - ApiKeyAuth: [] + summary: Update user by name + tags: + - users + /users/search: + post: + consumes: + - application/json + description: Search users by Condition + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/api.Page' + security: + - ApiKeyAuth: [] + summary: Search users + tags: + - users +securityDefinitions: + ApiKeyAuth: + in: header + name: Authorization + type: apiKey +swagger: "2.0" diff --git a/cmd/server/main.go b/cmd/server/main.go index a7b22ce6..eec27d17 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -5,6 +5,8 @@ import ( "runtime" _ "github.com/KubeOperator/kubepi/cmd/server/docs" + _ "github.com/KubeOperator/kubepi/internal/model/v1/docs" + _ "github.com/KubeOperator/kubepi/internal/model/v1/user" "github.com/KubeOperator/kubepi/internal/route" "github.com/KubeOperator/kubepi/internal/server" "github.com/KubeOperator/kubepi/pkg/network/ip" @@ -13,7 +15,7 @@ import ( //go:generate swag init -//swag init -g "cmd/server/main.go" -o "cmd/server/docs" +//swag init -g "cmd/server/main.go" -o "cmd/server/docs" --parseDependency --parseInternal --parseDepth 2 var ( configPath string diff --git a/internal/api/v1/user/user.go b/internal/api/v1/user/user.go index 9d9e4f7c..b742f2f4 100644 --- a/internal/api/v1/user/user.go +++ b/internal/api/v1/user/user.go @@ -36,16 +36,15 @@ func NewHandler() *Handler { // List User // @Tags users -// @Summary Show all users -// @Description Show clusters +// @Summary Search users +// @Description Search users by Condition // @Accept json // @Produce json // @Success 200 {object} api.Page // @Security ApiKeyAuth -// @Router /users/search [get] +// @Router /users/search [post] func (h *Handler) SearchUsers() iris.Handler { return func(ctx *context.Context) { - pageNum, _ := ctx.Values().GetInt(pkgV1.PageNum) pageSize, _ := ctx.Values().GetInt(pkgV1.PageSize) @@ -85,6 +84,16 @@ func (h *Handler) SearchUsers() iris.Handler { } } +// Create User +// @Tags users +// @Summary Create user +// @Description Create user +// @Accept json +// @Produce json +// @Param request body docs.UserCreate true "request" +// @Success 200 {object} v1User.User +// @Security ApiKeyAuth +// @Router /users [post] func (h *Handler) CreateUser() iris.Handler { return func(ctx *context.Context) { var req User @@ -158,6 +167,16 @@ func (h *Handler) CreateUser() iris.Handler { } } +// Delete User +// @Tags users +// @Summary Delete user by name +// @Description Delete user by name +// @Accept json +// @Produce json +// @Param name path string true "用户名称" +// @Success 200 {object} v1User.User +// @Security ApiKeyAuth +// @Router /users/{name} [delete] func (h *Handler) DeleteUser() iris.Handler { return func(ctx *context.Context) { userName := ctx.Params().GetString("name") @@ -217,6 +236,16 @@ func (h *Handler) DeleteUser() iris.Handler { } } +// Get User +// @Tags users +// @Summary Get user by name +// @Description Get user by name +// @Accept json +// @Produce json +// @Param name path string true "用户名称" +// @Success 200 {object} v1User.User +// @Security ApiKeyAuth +// @Router /users/{name} [get] func (h *Handler) GetUser() iris.Handler { return func(ctx *context.Context) { userName := ctx.Params().GetString("name") @@ -240,6 +269,15 @@ func (h *Handler) GetUser() iris.Handler { } } +// List User +// @Tags users +// @Summary List all users +// @Description List all users +// @Accept json +// @Produce json +// @Success 200 {object} []v1User.User +// @Security ApiKeyAuth +// @Router /users [get] func (h *Handler) GetUsers() iris.Handler { return func(ctx *context.Context) { us, err := h.userService.List(common.DBOptions{}) @@ -252,6 +290,16 @@ func (h *Handler) GetUsers() iris.Handler { } } +// Update User +// @Tags users +// @Summary Update user by name +// @Description Update user by name +// @Accept json +// @Produce json +// @Param name path string true "用户名称" +// @Success 200 {object} v1User.User +// @Security ApiKeyAuth +// @Router /users/{name} [put] func (h *Handler) UpdateUser() iris.Handler { return func(ctx *context.Context) { userName := ctx.Params().GetString("name") diff --git a/internal/model/v1/docs/user.go b/internal/model/v1/docs/user.go new file mode 100644 index 00000000..91c47216 --- /dev/null +++ b/internal/model/v1/docs/user.go @@ -0,0 +1,18 @@ +package docs + +type UserCreate struct { + Name string `json:"name"` + NickName string `json:"nickName" storm:"index"` + Email string `json:"email" storm:"unique"` + IsAdmin bool `json:"isAdmin"` + Authenticate Authenticate `json:"authenticate"` + Mfa Mfa `json:"mfa"` +} + +type Authenticate struct { + Password string `json:"password"` +} + +type Mfa struct { + Enable bool `json:"enable"` +}