-
Notifications
You must be signed in to change notification settings - Fork 2
/
getting.go
108 lines (96 loc) · 3.16 KB
/
getting.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package actions
import (
"encoding/json"
"fmt"
"github.com/golang/glog"
"github.com/gorilla/mux"
"github.com/ottogroup/penelope/pkg/builder"
"github.com/ottogroup/penelope/pkg/requestobjects"
"go.opencensus.io/trace"
"net/http"
"strconv"
)
type GettingBackupHandler struct {
processorBuilder *builder.ProcessorBuilder
}
func NewGettingBackupHandler(processorBuilder *builder.ProcessorBuilder) *GettingBackupHandler {
return &GettingBackupHandler{processorBuilder: processorBuilder}
}
// ServeHTTP will handle Getting operation
func (dl *GettingBackupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(r.Context(), "GettingBackupHandler.ServeHTTP")
defer span.End()
backupID, ok := mux.Vars(r)["backup_id"]
if !ok {
msg := "Bad request missing parameter: backup_id"
prepareResponse(w, msg, msg, http.StatusBadRequest)
return
}
request := requestobjects.GetRequest{}
request.BackupID = backupID
q := r.URL.Query()
if q.Get("size") != "" {
i, err := strconv.Atoi(q.Get("size"))
if err != nil {
BadRequestResponse(w, r)
return
}
request.Page.Size = i
}
if q.Get("page") != "" {
i, err := strconv.Atoi(q.Get("page"))
if err != nil {
BadRequestResponse(w, r)
return
}
request.Page.Number = i
}
principal, isValid := getPrincipalOrElsePrepareFailedResponse(w, r)
if !isValid {
return
}
// business logic
processor, err := dl.processorBuilder.ProcessorForRequestType(ctx, requestobjects.Getting)
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 := dl.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, request.BackupID) {
return
}
backupResponse := mapBackupToResponse(backup, result.GetJobs())
backupResponse.JobsTotal = result.JobsTotal
responseBody, err := json.Marshal(&backupResponse)
if err != nil {
logMsg := fmt.Sprintf("Error creating 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 creating response body. Err: %s", err)
respMsg := "Could not handle request"
prepareResponse(w, logMsg, respMsg, http.StatusInternalServerError)
return
}
}
func BadRequestResponse(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
if _, err := fmt.Fprintf(w, "Unkown api endpoint %s", r.URL.Path); err != nil {
glog.Warningf("Error writing response for %s: %s", r.URL.Path, err)
}
}