|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + |
| 7 | + "github.com/1Panel-dev/1Panel/agent/app/api/v2/helper" |
| 8 | + "github.com/1Panel-dev/1Panel/agent/app/dto" |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | +) |
| 11 | + |
| 12 | +// @Tags Database Mongodb |
| 13 | +// @Summary Create mongodb database |
| 14 | +// @Accept json |
| 15 | +// @Param request body dto.MongodbDBCreate true "request" |
| 16 | +// @Success 200 |
| 17 | +// @Security ApiKeyAuth |
| 18 | +// @Security Timestamp |
| 19 | +// @Router /databases/mongodb [post] |
| 20 | +// @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建 mongodb 数据库 [name]","formatEN":"create mongodb database [name]"} |
| 21 | +func (b *BaseApi) CreateMongodb(c *gin.Context) { |
| 22 | + var req dto.MongodbDBCreate |
| 23 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + if len(req.Password) != 0 { |
| 28 | + password, err := base64.StdEncoding.DecodeString(req.Password) |
| 29 | + if err != nil { |
| 30 | + helper.BadRequest(c, err) |
| 31 | + return |
| 32 | + } |
| 33 | + req.Password = string(password) |
| 34 | + } |
| 35 | + |
| 36 | + if _, err := mongodbService.Create(context.Background(), req); err != nil { |
| 37 | + helper.InternalServer(c, err) |
| 38 | + return |
| 39 | + } |
| 40 | + helper.Success(c) |
| 41 | +} |
| 42 | + |
| 43 | +// @Tags Database Mongodb |
| 44 | +// @Summary Page mongodb databases |
| 45 | +// @Accept json |
| 46 | +// @Param request body dto.MongodbDBSearch true "request" |
| 47 | +// @Success 200 {object} dto.PageResult |
| 48 | +// @Security ApiKeyAuth |
| 49 | +// @Security Timestamp |
| 50 | +// @Router /databases/mongodb/search [post] |
| 51 | +func (b *BaseApi) SearchMongodb(c *gin.Context) { |
| 52 | + var req dto.MongodbDBSearch |
| 53 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + total, list, err := mongodbService.SearchWithPage(req) |
| 58 | + if err != nil { |
| 59 | + helper.InternalServer(c, err) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + helper.SuccessWithData(c, dto.PageResult{ |
| 64 | + Items: list, |
| 65 | + Total: total, |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +// @Tags Database Mongodb |
| 70 | +// @Summary Update mongodb database description |
| 71 | +// @Accept json |
| 72 | +// @Param request body dto.UpdateDescription true "request" |
| 73 | +// @Success 200 |
| 74 | +// @Security ApiKeyAuth |
| 75 | +// @Security Timestamp |
| 76 | +// @Router /databases/mongodb/description [post] |
| 77 | +// @x-panel-log {"bodyKeys":["id","description"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"database_mongodbs","output_column":"name","output_value":"name"}],"formatZH":"mongodb 数据库 [name] 描述信息修改 [description]","formatEN":"The description of the mongodb database [name] is modified => [description]"} |
| 78 | +func (b *BaseApi) UpdateMongodbDescription(c *gin.Context) { |
| 79 | + var req dto.UpdateDescription |
| 80 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + if err := mongodbService.UpdateDescription(req); err != nil { |
| 85 | + helper.InternalServer(c, err) |
| 86 | + return |
| 87 | + } |
| 88 | + helper.Success(c) |
| 89 | +} |
| 90 | + |
| 91 | +// @Tags Database Mongodb |
| 92 | +// @Summary Load mongodb database from remote |
| 93 | +// @Accept json |
| 94 | +// @Param request body dto.MongodbLoadDB true "request" |
| 95 | +// @Success 200 |
| 96 | +// @Security ApiKeyAuth |
| 97 | +// @Security Timestamp |
| 98 | +// @Router /databases/mongodb/load [post] |
| 99 | +func (b *BaseApi) LoadMongodbFromRemote(c *gin.Context) { |
| 100 | + var req dto.MongodbLoadDB |
| 101 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + if err := mongodbService.LoadFromRemote(req); err != nil { |
| 106 | + helper.InternalServer(c, err) |
| 107 | + return |
| 108 | + } |
| 109 | + helper.Success(c) |
| 110 | +} |
| 111 | + |
| 112 | +// @Tags Database Mongodb |
| 113 | +// @Summary Bind mongodb database user info |
| 114 | +// @Accept json |
| 115 | +// @Param request body dto.MongodbBind true "request" |
| 116 | +// @Success 200 |
| 117 | +// @Security ApiKeyAuth |
| 118 | +// @Security Timestamp |
| 119 | +// @Router /databases/mongodb/bind [post] |
| 120 | +// @x-panel-log {"bodyKeys":["database", "name", "username"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"绑定 mongodb 数据库 [database] [name] 用户 [username]","formatEN":"bind mongodb database [database] [name] user [username]"} |
| 121 | +func (b *BaseApi) BindMongodbUser(c *gin.Context) { |
| 122 | + var req dto.MongodbBind |
| 123 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + if len(req.Password) != 0 { |
| 128 | + password, err := base64.StdEncoding.DecodeString(req.Password) |
| 129 | + if err != nil { |
| 130 | + helper.BadRequest(c, err) |
| 131 | + return |
| 132 | + } |
| 133 | + req.Password = string(password) |
| 134 | + } |
| 135 | + |
| 136 | + if err := mongodbService.BindUser(req); err != nil { |
| 137 | + helper.InternalServer(c, err) |
| 138 | + return |
| 139 | + } |
| 140 | + helper.Success(c) |
| 141 | +} |
| 142 | + |
| 143 | +// @Tags Database Mongodb |
| 144 | +// @Summary Change mongodb database password |
| 145 | +// @Accept json |
| 146 | +// @Param request body dto.MongodbPassword true "request" |
| 147 | +// @Success 200 |
| 148 | +// @Security ApiKeyAuth |
| 149 | +// @Security Timestamp |
| 150 | +// @Router /databases/mongodb/password [post] |
| 151 | +// @x-panel-log {"bodyKeys":["database", "name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新 mongodb 数据库 [database] [name] 密码","formatEN":"update mongodb database [database] [name] password"} |
| 152 | +func (b *BaseApi) ChangeMongodbPassword(c *gin.Context) { |
| 153 | + var req dto.MongodbPassword |
| 154 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 155 | + return |
| 156 | + } |
| 157 | + |
| 158 | + if len(req.Password) != 0 { |
| 159 | + password, err := base64.StdEncoding.DecodeString(req.Password) |
| 160 | + if err != nil { |
| 161 | + helper.BadRequest(c, err) |
| 162 | + return |
| 163 | + } |
| 164 | + req.Password = string(password) |
| 165 | + } |
| 166 | + |
| 167 | + if err := mongodbService.ChangePassword(req); err != nil { |
| 168 | + helper.InternalServer(c, err) |
| 169 | + return |
| 170 | + } |
| 171 | + helper.Success(c) |
| 172 | +} |
| 173 | + |
| 174 | +// @Tags Database Mongodb |
| 175 | +// @Summary Load mongodb privileges |
| 176 | +// @Accept json |
| 177 | +// @Param request body dto.MongodbPrivilegesLoad true "request" |
| 178 | +// @Success 200 {string} string |
| 179 | +// @Security ApiKeyAuth |
| 180 | +// @Security Timestamp |
| 181 | +// @Router /databases/mongodb/privileges [post] |
| 182 | +func (b *BaseApi) LoadMongodbPrivileges(c *gin.Context) { |
| 183 | + var req dto.MongodbPrivilegesLoad |
| 184 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 185 | + return |
| 186 | + } |
| 187 | + |
| 188 | + permission, err := mongodbService.LoadPrivileges(req) |
| 189 | + if err != nil { |
| 190 | + helper.InternalServer(c, err) |
| 191 | + return |
| 192 | + } |
| 193 | + helper.SuccessWithData(c, permission) |
| 194 | +} |
| 195 | + |
| 196 | +// @Tags Database Mongodb |
| 197 | +// @Summary Change mongodb privileges |
| 198 | +// @Accept json |
| 199 | +// @Param request body dto.MongodbPrivileges true "request" |
| 200 | +// @Success 200 |
| 201 | +// @Security ApiKeyAuth |
| 202 | +// @Security Timestamp |
| 203 | +// @Router /databases/mongodb/privileges/change [post] |
| 204 | +// @x-panel-log {"bodyKeys":["database", "username"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新 mongodb 数据库 [database] 用户 [username] 权限","formatEN":"update mongodb database [database] user [username] privileges"} |
| 205 | +func (b *BaseApi) ChangeMongodbPrivileges(c *gin.Context) { |
| 206 | + var req dto.MongodbPrivileges |
| 207 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 208 | + return |
| 209 | + } |
| 210 | + |
| 211 | + if err := mongodbService.ChangePrivileges(req); err != nil { |
| 212 | + helper.InternalServer(c, err) |
| 213 | + return |
| 214 | + } |
| 215 | + helper.Success(c) |
| 216 | +} |
| 217 | + |
| 218 | +// @Tags Database Mongodb |
| 219 | +// @Summary Check before delete mongodb database |
| 220 | +// @Accept json |
| 221 | +// @Param request body dto.MongodbDBDeleteCheck true "request" |
| 222 | +// @Success 200 {array} string |
| 223 | +// @Security ApiKeyAuth |
| 224 | +// @Security Timestamp |
| 225 | +// @Router /databases/mongodb/del/check [post] |
| 226 | +func (b *BaseApi) DeleteCheckMongodb(c *gin.Context) { |
| 227 | + var req dto.MongodbDBDeleteCheck |
| 228 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 229 | + return |
| 230 | + } |
| 231 | + |
| 232 | + apps, err := mongodbService.DeleteCheck(req) |
| 233 | + if err != nil { |
| 234 | + helper.InternalServer(c, err) |
| 235 | + return |
| 236 | + } |
| 237 | + helper.SuccessWithData(c, apps) |
| 238 | +} |
| 239 | + |
| 240 | +// @Tags Database Mongodb |
| 241 | +// @Summary Delete mongodb database |
| 242 | +// @Accept json |
| 243 | +// @Param request body dto.MongodbDBDelete true "request" |
| 244 | +// @Success 200 |
| 245 | +// @Security ApiKeyAuth |
| 246 | +// @Security Timestamp |
| 247 | +// @Router /databases/mongodb/del [post] |
| 248 | +// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"database_mongodbs","output_column":"name","output_value":"name"}],"formatZH":"删除 mongodb 数据库 [name]","formatEN":"delete mongodb database [name]"} |
| 249 | +func (b *BaseApi) DeleteMongodb(c *gin.Context) { |
| 250 | + var req dto.MongodbDBDelete |
| 251 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 252 | + return |
| 253 | + } |
| 254 | + |
| 255 | + tx, ctx := helper.GetTxAndContext() |
| 256 | + if err := mongodbService.Delete(ctx, req); err != nil { |
| 257 | + helper.InternalServer(c, err) |
| 258 | + tx.Rollback() |
| 259 | + return |
| 260 | + } |
| 261 | + tx.Commit() |
| 262 | + helper.Success(c) |
| 263 | +} |
0 commit comments