-
Notifications
You must be signed in to change notification settings - Fork 2
/
objects.go
159 lines (147 loc) · 5.1 KB
/
objects.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package actions
import (
"fmt"
"github.com/golang/glog"
"github.com/ottogroup/penelope/pkg/http/auth"
"github.com/ottogroup/penelope/pkg/http/auth/model"
"github.com/ottogroup/penelope/pkg/repository"
"github.com/ottogroup/penelope/pkg/requestobjects"
"net/http"
"reflect"
"time"
)
func formatTime(t time.Time) string {
if t.IsZero() {
return ""
}
return t.Format(time.RFC3339)
}
func mapBackupToResponse(backup *repository.Backup, jobs []*repository.Job) requestobjects.BackupResponse {
var jobResponse []requestobjects.JobResponse
for _, job := range jobs {
var foreignJobID string
if backup.Type == repository.BigQuery {
foreignJobID = string(job.ForeignJobID.BigQueryID)
}
if backup.Type == repository.CloudStorage {
foreignJobID = string(job.ForeignJobID.CloudStorageID)
}
jobResponse = append(jobResponse, requestobjects.JobResponse{
ID: job.ID,
BackupID: job.BackupID,
ForeignJobID: foreignJobID,
Status: job.Status.String(),
Source: job.Source,
CreatedTimestamp: formatTime(job.CreatedTimestamp),
UpdatedTimestamp: formatTime(job.UpdatedTimestamp),
DeletedTimestamp: formatTime(job.DeletedTimestamp),
})
}
status := backup.Status
if repository.Prepared == backup.Status {
status = "Running" //rewording prepared status for frontend
}
return requestobjects.BackupResponse{
ID: backup.ID,
Sink: backup.SinkOptions.Sink,
Status: status.String(),
SinkProject: backup.SinkOptions.TargetProject,
CreatedTimestamp: formatTime(backup.CreatedTimestamp),
UpdatedTimestamp: formatTime(backup.UpdatedTimestamp),
DeletedTimestamp: formatTime(backup.DeletedTimestamp),
CreateRequest: requestobjects.CreateRequest{
Type: backup.Type.String(),
Strategy: backup.Strategy.String(),
Project: backup.SourceProject,
TargetOptions: requestobjects.TargetOptions{
StorageClass: backup.StorageClass,
Region: backup.Region,
ArchiveTTM: backup.SinkOptions.ArchiveTTM,
},
SnapshotOptions: requestobjects.SnapshotOptions{
FrequencyInHours: backup.FrequencyInHours,
LifetimeInDays: backup.SnapshotOptions.LifetimeInDays,
LastScheduled: formatTime(backup.LastScheduledTime),
},
MirrorOptions: requestobjects.MirrorOptions{
LifetimeInDays: backup.MirrorOptions.LifetimeInDays,
},
BigQueryOptions: requestobjects.BigQueryOptions{
Dataset: backup.Dataset,
Table: backup.Table,
ExcludedTables: backup.ExcludedTables,
},
GCSOptions: requestobjects.GCSOptions{
Bucket: backup.Bucket,
ExcludePath: backup.ExcludePath,
IncludePath: backup.IncludePath,
},
},
Jobs: jobResponse,
}
}
func mapToRestoreResponse(backup *repository.Backup, jobs []*repository.Job) (restoreResponse requestobjects.RestoreResponse) {
restoreResponse.BackupID = backup.ID
for _, job := range jobs {
var action string
var backupType string
if backup.Type == repository.BigQuery {
backupType = "bq"
action += fmt.Sprintf(`bq --location=EU load --project_id "%s" --source_format=AVRO %s.%s "%s"`,
backup.SourceProject,
backup.BigQueryOptions.Dataset,
job.Source,
repository.BuildFullObjectStoragePath(backup.Sink, backup.BigQueryOptions.Dataset, job.Source, job.ID),
)
}
if backup.Type == repository.CloudStorage {
backupType = "gcs"
}
restoreResponse.RestoreActions = append(restoreResponse.RestoreActions, requestobjects.RestoreAction{
Type: backupType,
Action: action,
})
}
return restoreResponse
}
func checkRequestBodyIsValid(w http.ResponseWriter, err error) bool {
if err != nil {
logMsg := fmt.Sprintf("Error reading request body. Err: %s", err)
respMsg := "Could not read body of request"
prepareResponse(w, logMsg, respMsg, http.StatusUnprocessableEntity)
return false
}
return true
}
func getPrincipalOrElsePrepareFailedResponse(w http.ResponseWriter, r *http.Request) (*model.Principal, bool) {
principal, ok := r.Context().Value(auth.CtxPrincipalKey).(*model.Principal)
if !ok || principal == nil {
prepareResponse(w, "no principal found in context", "could not retrieve user-info", http.StatusInternalServerError)
return nil, false
}
return principal, true
}
func checkBackupIsFound(w http.ResponseWriter, backup *repository.Backup, backupID string) bool {
if backup == nil || reflect.ValueOf(backup).IsNil() {
logMsg := fmt.Sprintf("no backup with id %q found", backupID)
prepareResponse(w, logMsg, logMsg, http.StatusNotFound)
return false
}
return true
}
func checkParsingBodyIsValid(w http.ResponseWriter, err error, body string) bool {
if err != nil {
logMsg := fmt.Sprintf("Error parsing json request body. Err: %s\n body: %s", err, body)
respMsg := "Could not parsing json request body of request"
prepareResponse(w, logMsg, respMsg, http.StatusUnprocessableEntity)
return false
}
return true
}
func prepareResponse(w http.ResponseWriter, logMsg string, responseMsg string, responseCode int) {
glog.Info(logMsg)
w.WriteHeader(responseCode)
if _, err := fmt.Fprint(w, responseMsg); err != nil {
glog.Warningf("Error writing response: %s", err)
}
}