2022-10-20 18:45:47 +08:00
package v1
import (
2022-12-21 15:54:34 +08:00
"context"
2023-03-15 15:58:26 +08:00
"encoding/base64"
2022-12-24 13:31:30 +08:00
2022-10-20 18:45:47 +08:00
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/gin-gonic/gin"
)
2023-01-04 22:31:51 +08:00
// @Tags Database Mysql
// @Summary Create mysql database
// @Description 创建 mysql 数据库
// @Accept json
// @Param request body dto.MysqlDBCreate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /databases [post]
2023-10-07 15:46:44 +08:00
// @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建 mysql 数据库 [name]","formatEN":"create mysql database [name]"}
2022-10-20 18:45:47 +08:00
func ( b * BaseApi ) CreateMysql ( c * gin . Context ) {
var req dto . MysqlDBCreate
2023-10-27 14:19:26 +08:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-10-20 18:45:47 +08:00
return
}
2023-10-27 14:19:26 +08:00
2023-03-15 15:58:26 +08:00
if len ( req . Password ) != 0 {
password , err := base64 . StdEncoding . DecodeString ( req . Password )
if err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrBadRequest , constant . ErrTypeInvalidParams , err )
return
}
req . Password = string ( password )
}
2022-12-21 15:54:34 +08:00
if _ , err := mysqlService . Create ( context . Background ( ) , req ) ; err != nil {
2022-10-20 18:45:47 +08:00
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , nil )
}
2023-12-25 17:08:09 +08:00
// @Tags Database Mysql
// @Summary Bind user of mysql database
// @Description 绑定 mysql 数据库用户
// @Accept json
// @Param request body dto.BindUser true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /databases/bind [post]
// @x-panel-log {"bodyKeys":["database", "username"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"绑定 mysql 数据库名 [database] [username]","formatEN":"bind mysql database [database] [username]"}
func ( b * BaseApi ) BindUser ( c * gin . Context ) {
var req dto . BindUser
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
return
}
if len ( req . Password ) != 0 {
password , err := base64 . StdEncoding . DecodeString ( req . Password )
if err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrBadRequest , constant . ErrTypeInvalidParams , err )
return
}
req . Password = string ( password )
}
if err := mysqlService . BindUser ( req ) ; err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , nil )
}
2023-01-04 22:31:51 +08:00
// @Tags Database Mysql
// @Summary Update mysql database description
// @Description 更新 mysql 数据库库描述信息
// @Accept json
2023-02-13 15:48:18 +08:00
// @Param request body dto.UpdateDescription true "request"
2023-01-04 22:31:51 +08:00
// @Success 200
// @Security ApiKeyAuth
// @Router /databases/description/update [post]
2023-10-07 15:46:44 +08:00
// @x-panel-log {"bodyKeys":["id","description"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"database_mysqls","output_column":"name","output_value":"name"}],"formatZH":"mysql 数据库 [name] 描述信息修改 [description]","formatEN":"The description of the mysql database [name] is modified => [description]"}
2022-12-24 13:31:30 +08:00
func ( b * BaseApi ) UpdateMysqlDescription ( c * gin . Context ) {
2023-02-13 15:48:18 +08:00
var req dto . UpdateDescription
2023-10-27 14:19:26 +08:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-12-24 13:31:30 +08:00
return
}
2023-10-27 14:19:26 +08:00
2022-12-24 13:31:30 +08:00
if err := mysqlService . UpdateDescription ( req ) ; err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , nil )
}
2023-01-04 22:31:51 +08:00
// @Tags Database Mysql
// @Summary Change mysql password
// @Description 修改 mysql 密码
// @Accept json
// @Param request body dto.ChangeDBInfo true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /databases/change/password [post]
2023-10-07 15:46:44 +08:00
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"database_mysqls","output_column":"name","output_value":"name"}],"formatZH":"更新数据库 [name] 密码","formatEN":"Update database [name] password"}
2022-12-08 19:07:32 +08:00
func ( b * BaseApi ) ChangeMysqlPassword ( c * gin . Context ) {
2022-10-24 18:46:19 +08:00
var req dto . ChangeDBInfo
2023-10-27 14:19:26 +08:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-10-24 18:46:19 +08:00
return
}
2023-10-27 14:19:26 +08:00
2023-03-15 15:58:26 +08:00
if len ( req . Value ) != 0 {
value , err := base64 . StdEncoding . DecodeString ( req . Value )
if err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrBadRequest , constant . ErrTypeInvalidParams , err )
return
}
req . Value = string ( value )
}
2022-12-08 19:07:32 +08:00
if err := mysqlService . ChangePassword ( req ) ; err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , nil )
}
2023-01-04 22:31:51 +08:00
// @Tags Database Mysql
// @Summary Change mysql access
// @Description 修改 mysql 访问权限
// @Accept json
// @Param request body dto.ChangeDBInfo true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /databases/change/access [post]
2023-10-07 15:46:44 +08:00
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"database_mysqls","output_column":"name","output_value":"name"}],"formatZH":"更新数据库 [name] 访问权限","formatEN":"Update database [name] access"}
2022-12-08 19:07:32 +08:00
func ( b * BaseApi ) ChangeMysqlAccess ( c * gin . Context ) {
var req dto . ChangeDBInfo
2023-10-27 14:19:26 +08:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-12-08 19:07:32 +08:00
return
}
2023-10-27 14:19:26 +08:00
2022-12-08 19:07:32 +08:00
if err := mysqlService . ChangeAccess ( req ) ; err != nil {
2022-10-24 18:46:19 +08:00
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , nil )
}
2023-01-04 22:31:51 +08:00
// @Tags Database Mysql
// @Summary Update mysql variables
// @Description mysql 性能调优
// @Accept json
// @Param request body dto.MysqlVariablesUpdate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /databases/variables/update [post]
2023-10-07 15:46:44 +08:00
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"调整 mysql 数据库性能参数","formatEN":"adjust mysql database performance parameters"}
2022-10-25 11:41:19 +08:00
func ( b * BaseApi ) UpdateMysqlVariables ( c * gin . Context ) {
2023-08-29 10:50:15 +08:00
var req dto . MysqlVariablesUpdate
2023-10-27 14:19:26 +08:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-10-25 11:41:19 +08:00
return
}
2022-11-04 19:02:15 +08:00
2022-11-18 16:14:23 +08:00
if err := mysqlService . UpdateVariables ( req ) ; err != nil {
2022-10-25 11:41:19 +08:00
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , nil )
}
2023-01-05 11:57:03 +08:00
// @Tags Database Mysql
// @Summary Page mysql databases
2023-01-04 22:31:51 +08:00
// @Description 获取 mysql 数据库列表分页
// @Accept json
2023-07-24 14:55:26 +08:00
// @Param request body dto.MysqlDBSearch true "request"
2023-01-04 22:31:51 +08:00
// @Success 200 {object} dto.PageResult
// @Security ApiKeyAuth
// @Router /databases/search [post]
2022-10-20 18:45:47 +08:00
func ( b * BaseApi ) SearchMysql ( c * gin . Context ) {
2023-07-24 14:55:26 +08:00
var req dto . MysqlDBSearch
2023-10-27 14:19:26 +08:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-10-20 18:45:47 +08:00
return
}
total , list , err := mysqlService . SearchWithPage ( req )
if err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , dto . PageResult {
Items : list ,
Total : total ,
} )
}
2023-01-05 11:57:03 +08:00
// @Tags Database Mysql
// @Summary List mysql database names
2023-01-04 22:31:51 +08:00
// @Description 获取 mysql 数据库列表
// @Accept json
// @Param request body dto.PageInfo true "request"
2023-08-03 16:19:30 +08:00
// @Success 200 {array} dto.MysqlOption
2023-01-04 22:31:51 +08:00
// @Security ApiKeyAuth
// @Router /databases/options [get]
2022-11-18 16:14:23 +08:00
func ( b * BaseApi ) ListDBName ( c * gin . Context ) {
2023-08-03 16:19:30 +08:00
list , err := mysqlService . ListDBOption ( )
2022-10-28 18:46:14 +08:00
if err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , list )
}
2023-07-27 16:07:27 +08:00
// @Tags Database Mysql
// @Summary Load mysql database from remote
// @Description 从服务器获取
2023-08-29 10:50:15 +08:00
// @Accept json
2023-09-16 13:16:15 +08:00
// @Param request body dto.MysqlLoadDB true "request"
2023-07-27 16:07:27 +08:00
// @Security ApiKeyAuth
2023-08-29 10:50:15 +08:00
// @Router /databases/load [post]
2023-07-27 16:07:27 +08:00
func ( b * BaseApi ) LoadDBFromRemote ( c * gin . Context ) {
2023-09-16 13:16:15 +08:00
var req dto . MysqlLoadDB
2023-10-27 14:19:26 +08:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2023-08-29 10:50:15 +08:00
return
}
2023-10-27 14:19:26 +08:00
2023-08-29 10:50:15 +08:00
if err := mysqlService . LoadFromRemote ( req ) ; err != nil {
2023-07-27 16:07:27 +08:00
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , nil )
}
2023-01-04 22:31:51 +08:00
// @Tags Database Mysql
// @Summary Check before delete mysql database
// @Description Mysql 数据库删除前检查
// @Accept json
2023-08-29 10:50:15 +08:00
// @Param request body dto.MysqlDBDeleteCheck true "request"
2023-07-06 18:04:22 +08:00
// @Success 200 {array} string
2023-01-04 22:31:51 +08:00
// @Security ApiKeyAuth
// @Router /databases/del/check [post]
2022-12-04 17:28:03 +08:00
func ( b * BaseApi ) DeleteCheckMysql ( c * gin . Context ) {
2023-08-29 10:50:15 +08:00
var req dto . MysqlDBDeleteCheck
2023-10-27 14:19:26 +08:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-10-20 18:45:47 +08:00
return
}
2022-12-04 17:28:03 +08:00
2023-08-29 10:50:15 +08:00
apps , err := mysqlService . DeleteCheck ( req )
2022-12-04 17:28:03 +08:00
if err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , apps )
}
2023-01-04 22:31:51 +08:00
// @Tags Database Mysql
// @Summary Delete mysql database
// @Description 删除 mysql 数据库
// @Accept json
// @Param request body dto.MysqlDBDelete true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /databases/del [post]
2023-10-07 15:46:44 +08:00
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"database_mysqls","output_column":"name","output_value":"name"}],"formatZH":"删除 mysql 数据库 [name]","formatEN":"delete mysql database [name]"}
2022-12-04 17:28:03 +08:00
func ( b * BaseApi ) DeleteMysql ( c * gin . Context ) {
2022-12-26 14:47:08 +08:00
var req dto . MysqlDBDelete
2023-10-27 14:19:26 +08:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2022-10-20 18:45:47 +08:00
return
}
2022-12-27 16:30:25 +08:00
tx , ctx := helper . GetTxAndContext ( )
if err := mysqlService . Delete ( ctx , req ) ; err != nil {
2022-10-20 18:45:47 +08:00
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
2022-12-27 16:30:25 +08:00
tx . Rollback ( )
2022-10-20 18:45:47 +08:00
return
}
2022-12-27 16:30:25 +08:00
tx . Commit ( )
2022-10-20 18:45:47 +08:00
helper . SuccessWithData ( c , nil )
}
2022-10-21 18:50:38 +08:00
2023-01-04 22:31:51 +08:00
// @Tags Database Mysql
// @Summary Load mysql remote access
// @Description 获取 mysql 远程访问权限
2023-08-29 10:50:15 +08:00
// @Accept json
2023-09-16 13:16:15 +08:00
// @Param request body dto.OperationWithNameAndType true "request"
2023-01-04 22:31:51 +08:00
// @Success 200 {boolean} isRemote
// @Security ApiKeyAuth
2023-08-29 10:50:15 +08:00
// @Router /databases/remote [post]
2022-12-09 14:07:18 +08:00
func ( b * BaseApi ) LoadRemoteAccess ( c * gin . Context ) {
2023-09-16 13:16:15 +08:00
var req dto . OperationWithNameAndType
2023-10-27 14:19:26 +08:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2023-08-29 10:50:15 +08:00
return
}
2023-12-28 16:29:18 +08:00
if req . Type == constant . AppPostgresql {
helper . SuccessWithData ( c , true )
return
}
2023-08-29 10:50:15 +08:00
isRemote , err := mysqlService . LoadRemoteAccess ( req )
2022-12-09 14:07:18 +08:00
if err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , isRemote )
}
2023-01-04 22:31:51 +08:00
// @Tags Database Mysql
// @Summary Load mysql status info
// @Description 获取 mysql 状态信息
2023-08-29 10:50:15 +08:00
// @Accept json
2023-09-16 13:16:15 +08:00
// @Param request body dto.OperationWithNameAndType true "request"
2023-01-04 22:31:51 +08:00
// @Success 200 {object} dto.MysqlStatus
// @Security ApiKeyAuth
2023-08-29 10:50:15 +08:00
// @Router /databases/status [post]
2022-10-21 18:50:38 +08:00
func ( b * BaseApi ) LoadStatus ( c * gin . Context ) {
2023-09-16 13:16:15 +08:00
var req dto . OperationWithNameAndType
2023-10-27 14:19:26 +08:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2023-08-29 10:50:15 +08:00
return
}
2023-10-27 14:19:26 +08:00
2023-08-29 10:50:15 +08:00
data , err := mysqlService . LoadStatus ( req )
2022-10-21 18:50:38 +08:00
if err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , data )
}
2023-01-04 22:31:51 +08:00
// @Tags Database Mysql
// @Summary Load mysql variables info
// @Description 获取 mysql 性能参数信息
2023-08-29 10:50:15 +08:00
// @Accept json
2023-09-16 13:16:15 +08:00
// @Param request body dto.OperationWithNameAndType true "request"
2023-01-04 22:31:51 +08:00
// @Success 200 {object} dto.MysqlVariables
// @Security ApiKeyAuth
2023-08-29 10:50:15 +08:00
// @Router /databases/variables [post]
2022-10-25 18:34:33 +08:00
func ( b * BaseApi ) LoadVariables ( c * gin . Context ) {
2023-09-16 13:16:15 +08:00
var req dto . OperationWithNameAndType
2023-10-27 14:19:26 +08:00
if err := helper . CheckBindAndValidate ( & req , c ) ; err != nil {
2023-08-29 10:50:15 +08:00
return
}
2023-10-27 14:19:26 +08:00
2023-08-29 10:50:15 +08:00
data , err := mysqlService . LoadVariables ( req )
2022-10-21 18:50:38 +08:00
if err != nil {
helper . ErrorWithDetail ( c , constant . CodeErrInternalServer , constant . ErrTypeInternalServer , err )
return
}
helper . SuccessWithData ( c , data )
}