-
Notifications
You must be signed in to change notification settings - Fork 2
/
bucket_listing.go
75 lines (65 loc) · 2.33 KB
/
bucket_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
75
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 BucketListingHandler struct {
processorBuilder *builder.ProcessorBuilder
}
func NewBucketListingHandler(processorBuilder *builder.ProcessorBuilder) *BucketListingHandler {
return &BucketListingHandler{processorBuilder: processorBuilder}
}
// ServeHTTP will handle BucketListing operation
func (bl *BucketListingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(r.Context(), "BucketListingHandler.ServeHTTP")
defer span.End()
projectID, exist := mux.Vars(r)["project_id"]
if !exist {
msg := "Bad request missing parameter: project_id"
prepareResponse(w, msg, msg, http.StatusBadRequest)
return
}
var request requestobjects.BucketListRequest
request.Project = projectID
principal, isValid := getPrincipalOrElsePrepareFailedResponse(w, r)
if !isValid {
return
}
// business logic
processor, err := bl.processorBuilder.ProcessorForRequestType(ctx, requestobjects.BucketListing)
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 := bl.processorBuilder.ProcessorArgumentsForRequest(&request, principal)
result, err := processor.Process(ctx, &processorArguments)
if err != nil {
logMsg := fmt.Sprintf("Error dataset listing processing backup entity. Err: %s", err)
errMsg := fmt.Sprintf("could not handle request because of: %s", err)
prepareResponse(w, logMsg, errMsg, http.StatusPreconditionFailed)
return
}
responseBody, err := json.Marshal(result.BucketListResponse)
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
}
}