2024-01-20 23:46:43 +01:00
|
|
|
package projects
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/ansible-semaphore/semaphore/api/helpers"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
projectService "github.com/ansible-semaphore/semaphore/services/project"
|
|
|
|
"github.com/gorilla/context"
|
2024-04-03 00:05:07 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2024-01-20 23:46:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetBackup(w http.ResponseWriter, r *http.Request) {
|
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
|
|
|
|
store := helpers.Store(r)
|
|
|
|
|
|
|
|
backup, err := projectService.GetBackup(project.ID, store)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helpers.WriteJSON(w, http.StatusOK, backup)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Restore(w http.ResponseWriter, r *http.Request) {
|
2024-04-03 00:05:07 +02:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
|
|
|
|
2024-01-20 23:46:43 +01:00
|
|
|
var backup projectService.BackupFormat
|
2024-02-04 13:46:27 +01:00
|
|
|
var p *db.Project
|
|
|
|
var err error
|
|
|
|
|
2024-01-20 23:46:43 +01:00
|
|
|
if !helpers.Bind(w, r, &backup) {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, backup)
|
|
|
|
return
|
|
|
|
}
|
2024-02-04 13:46:27 +01:00
|
|
|
store := helpers.Store(r)
|
|
|
|
if err = backup.Verify(); err != nil {
|
|
|
|
log.Error(err)
|
2024-04-03 00:05:07 +02:00
|
|
|
helpers.WriteError(w, err)
|
2024-02-04 13:46:27 +01:00
|
|
|
return
|
|
|
|
}
|
2024-04-03 00:05:07 +02:00
|
|
|
if p, err = backup.Restore(*user, store); err != nil {
|
2024-02-04 13:46:27 +01:00
|
|
|
log.Error(err)
|
2024-04-03 00:05:07 +02:00
|
|
|
helpers.WriteError(w, err)
|
2024-01-20 23:46:43 +01:00
|
|
|
return
|
|
|
|
}
|
2024-04-03 00:05:07 +02:00
|
|
|
|
2024-02-04 13:46:27 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, p)
|
2024-01-20 23:46:43 +01:00
|
|
|
}
|