-
Notifications
You must be signed in to change notification settings - Fork 2
/
restoring.go
82 lines (71 loc) · 2.77 KB
/
restoring.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package actions
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/ottogroup/penelope/pkg/builder"
"github.com/ottogroup/penelope/pkg/requestobjects"
"go.opencensus.io/trace"
"net/http"
)
type RestoringBackupHandler struct {
processorBuilder *builder.ProcessorBuilder
}
func NewRestoringBackupHandler(processorBuilder *builder.ProcessorBuilder) *RestoringBackupHandler {
return &RestoringBackupHandler{processorBuilder: processorBuilder}
}
// ServeHTTP will handle Updating Restoring
func (rb *RestoringBackupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(r.Context(), "RestoringBackupHandler.ServeHTTP")
defer span.End()
backupID, exist := mux.Vars(r)["backup_id"]
if !exist {
msg := "Bad request missing parameter: backup_id"
prepareResponse(w, msg, msg, http.StatusBadRequest)
return
}
var request requestobjects.RestoreRequest
request.BackupID = backupID
request.JobIDForTimestamp = r.URL.Query().Get("jobIDForTimestamp")
principal, isValid := getPrincipalOrElsePrepareFailedResponse(w, r)
if !isValid {
return
}
// business logic
processor, err := rb.processorBuilder.ProcessorForRequestType(ctx, requestobjects.Restoring)
if err != nil {
logMsg := fmt.Sprintf("Error creating new backup processor. Err: %s", err)
respMsg := "Could not handle request"
prepareResponse(w, logMsg, respMsg, http.StatusInternalServerError)
return
}
processorArguments := rb.processorBuilder.ProcessorArgumentsForRequest(&request, principal)
result, err := processor.Process(ctx, &processorArguments)
if err != nil {
logMsg := fmt.Sprintf("Error creating processing backup entity. Err: %s", err)
errMsg := fmt.Sprintf("could not handle request because of: %s", err)
prepareResponse(w, logMsg, errMsg, http.StatusPreconditionFailed)
return
}
backup := result.GetBackup()
if !checkBackupIsFound(w, backup, backupID) {
return
}
restoreResponse := mapToRestoreResponse(backup, result.GetJobs())
responseBody, err := json.Marshal(&restoreResponse)
if err != nil {
logMsg := fmt.Sprintf("Error restoring response body. Err: %s", err)
respMsg := "Could not handle request"
prepareResponse(w, logMsg, respMsg, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = w.Write(responseBody)
if err != nil {
logMsg := fmt.Sprintf("Error restoring response body. Err: %s", err)
respMsg := "Could not handle request"
prepareResponse(w, logMsg, respMsg, http.StatusInternalServerError)
return
}
}