-
Notifications
You must be signed in to change notification settings - Fork 2
/
listing.go
74 lines (63 loc) · 2.39 KB
/
listing.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
package actions
import (
"encoding/json"
"fmt"
"github.com/ottogroup/penelope/pkg/builder"
"github.com/ottogroup/penelope/pkg/repository"
"github.com/ottogroup/penelope/pkg/requestobjects"
"go.opencensus.io/trace"
"net/http"
)
type ListingBackupHandler struct {
processorBuilder *builder.ProcessorBuilder
}
func NewListingBackupHandler(processorBuilder *builder.ProcessorBuilder) *ListingBackupHandler {
return &ListingBackupHandler{processorBuilder: processorBuilder}
}
// ServeHTTP will handle Listing operation
func (dl *ListingBackupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(r.Context(), "ListingBackupHandler.ServeHTTP")
defer span.End()
request := requestobjects.ListRequest{Project: r.URL.Query().Get("project")}
principal, isValid := getPrincipalOrElsePrepareFailedResponse(w, r)
if !isValid {
return
}
// business logic
processor, err := dl.processorBuilder.ProcessorForRequestType(ctx, requestobjects.Listing)
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
}
listResponse := requestobjects.ListingResponse{}
listResponse.Backups = []requestobjects.BackupResponse{}
for _, b := range result.GetBackups() {
listResponse.Backups = append(listResponse.Backups, mapBackupToResponse(b, []*repository.Job{}))
}
responseBody, err := json.Marshal(&listResponse)
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
}
}