2022-09-16 18:53:45 +08:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2022-10-17 16:32:31 +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/1Panel-dev/1Panel/backend/global"
|
2022-09-16 18:53:45 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// Create backup account
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary Create backup account
|
|
|
|
// @Description 创建备份账号
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.BackupOperate true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /backups [post]
|
|
|
|
// @x-panel-log {"bodyKeys":["type"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"创建备份账号 [type]","formatEN":"create backup account [type]"}
|
2022-09-16 18:53:45 +08:00
|
|
|
func (b *BaseApi) CreateBackup(c *gin.Context) {
|
|
|
|
var req dto.BackupOperate
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := backupService.Create(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// List bucket
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary List bucket
|
|
|
|
// @Description 获取 bucket 列表
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.ForBuckets true "request"
|
|
|
|
// @Success 200 {anrry} string
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /backups/search [post]
|
2022-09-16 18:53:45 +08:00
|
|
|
func (b *BaseApi) ListBuckets(c *gin.Context) {
|
|
|
|
var req dto.ForBuckets
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buckets, err := backupService.GetBuckets(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, buckets)
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// Delete backup account
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary Delete backup account
|
|
|
|
// @Description 删除备份账号
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.BatchDeleteReq true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /backups/del [post]
|
|
|
|
// @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFuntions":[{"input_colume":"id","input_value":"ids","isList":true,"db":"backup_accounts","output_colume":"type","output_value":"types"}],"formatZH":"删除备份账号 [types]","formatEN":"delete backup account [types]"}
|
2022-09-16 18:53:45 +08:00
|
|
|
func (b *BaseApi) DeleteBackup(c *gin.Context) {
|
|
|
|
var req dto.BatchDeleteReq
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := backupService.BatchDelete(req.Ids); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// Page backup records
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary Search backup records with page
|
|
|
|
// @Description 获取备份记录列表分页
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.RecordSearch true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /backups/record/search [post]
|
2022-11-29 17:39:10 +08:00
|
|
|
func (b *BaseApi) SearchBackupRecords(c *gin.Context) {
|
|
|
|
var req dto.RecordSearch
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
total, list, err := backupService.SearchRecordsWithPage(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, dto.PageResult{
|
|
|
|
Items: list,
|
|
|
|
Total: total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// Download backup record
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary Download backup record
|
|
|
|
// @Description 下载备份记录
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.DownloadRecord true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /backups/record/download [post]
|
|
|
|
// @x-panel-log {"bodyKeys":["source","fileName"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"下载备份记录 [source][fileName]","formatEN":"download backup records [source][fileName]"}
|
2022-10-28 11:02:47 +08:00
|
|
|
func (b *BaseApi) DownloadRecord(c *gin.Context) {
|
|
|
|
var req dto.DownloadRecord
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath, err := backupService.DownloadRecord(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.File(filePath)
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// Delete backup record
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary Delete backup record
|
|
|
|
// @Description 删除备份记录
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.BatchDeleteReq true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /backups/record/del [post]
|
|
|
|
// @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFuntions":[{"input_colume":"id","input_value":"ids","isList":true,"db":"backup_records","output_colume":"file_name","output_value":"files"}],"formatZH":"删除备份记录 [files]","formatEN":"delete backup records [files]"}
|
2022-10-27 23:09:39 +08:00
|
|
|
func (b *BaseApi) DeleteBackupRecord(c *gin.Context) {
|
|
|
|
var req dto.BatchDeleteReq
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := backupService.BatchDeleteRecord(req.Ids); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// Update backup account
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary Update backup account
|
|
|
|
// @Description 更新备份账号信息
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.BackupOperate true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /backups/update [post]
|
|
|
|
// @x-panel-log {"bodyKeys":["type"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"更新备份账号 [types]","formatEN":"update backup account [types]"}
|
2022-09-16 18:53:45 +08:00
|
|
|
func (b *BaseApi) UpdateBackup(c *gin.Context) {
|
|
|
|
var req dto.BackupOperate
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
upMap := make(map[string]interface{})
|
|
|
|
upMap["bucket"] = req.Bucket
|
|
|
|
upMap["credential"] = req.Credential
|
|
|
|
upMap["vars"] = req.Vars
|
2022-12-13 18:54:28 +08:00
|
|
|
if err := backupService.Update(req.ID, upMap); err != nil {
|
2022-09-16 18:53:45 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// List backup account
|
|
|
|
// @Tags Backup Account
|
|
|
|
// @Summary Search backup account
|
|
|
|
// @Description 获取备份账号列表
|
|
|
|
// @Success 200 {anrry} dto.BackupInfo
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /backups/search [get]
|
2022-09-19 19:42:06 +08:00
|
|
|
func (b *BaseApi) ListBackup(c *gin.Context) {
|
|
|
|
data, err := backupService.List()
|
2022-09-16 18:53:45 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-19 19:42:06 +08:00
|
|
|
helper.SuccessWithData(c, data)
|
2022-09-16 18:53:45 +08:00
|
|
|
}
|