-
Notifications
You must be signed in to change notification settings - Fork 2
/
updating.go
97 lines (85 loc) · 3.39 KB
/
updating.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
package actions
import (
"encoding/json"
"fmt"
"github.com/golang/glog"
"github.com/ottogroup/penelope/pkg/builder"
"github.com/ottogroup/penelope/pkg/repository"
"github.com/ottogroup/penelope/pkg/requestobjects"
"go.opencensus.io/trace"
"io/ioutil"
"net/http"
)
type UpdateBackupHandler struct {
processorBuilder *builder.ProcessorBuilder
}
func NewUpdateBackupHandler(processorBuilder *builder.ProcessorBuilder) *UpdateBackupHandler {
return &UpdateBackupHandler{processorBuilder: processorBuilder}
}
// ServeHTTP will handle Updating operation
func (dl *UpdateBackupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(r.Context(), "UpdateBackupHandler.ServeHTTP")
defer span.End()
bodyBytes, err := ioutil.ReadAll(r.Body)
if !checkRequestBodyIsValid(w, err) {
return
}
var request requestobjects.UpdateRequest
err = json.Unmarshal(bodyBytes, &request)
if err != nil {
logMsg := fmt.Sprintf("Error creating new backup processor. Err: %s", err)
respMsg := "Could not unmarshal request body"
prepareResponse(w, logMsg, respMsg, http.StatusBadRequest)
return
}
principal, isValid := getPrincipalOrElsePrepareFailedResponse(w, r)
if !isValid {
return
}
// business logic
processor, err := dl.processorBuilder.ProcessorForRequestType(ctx, requestobjects.Updating)
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)
prepareResponse(w, logMsg, "could not handle request", http.StatusPreconditionFailed)
return
}
backup := result.GetBackup()
if !checkBackupIsFound(w, backup, request.BackupID) {
return
}
updateResponse := prepareUpdateResponse(backup)
responseBody, err := json.Marshal(&updateResponse)
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
}
msg := fmt.Sprintf("Backup with id %s successfully updated", backup.ID)
glog.Info(msg)
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)
}
}
func prepareUpdateResponse(backup *repository.Backup) requestobjects.UpdateResponse {
updateResponse := requestobjects.UpdateResponse{}
updateResponse.Status = backup.Status.String()
updateResponse.BackupID = backup.ID
updateResponse.CreatedTimestamp = formatTime(backup.CreatedTimestamp)
updateResponse.UpdatedTimestamp = formatTime(backup.UpdatedTimestamp)
updateResponse.DeletedTimestamp = formatTime(backup.DeletedTimestamp)
return updateResponse
}