forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_restore.go
180 lines (156 loc) · 6.27 KB
/
app_restore.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package controller
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/api/handler"
"github.com/goodrain/rainbond/api/middleware"
"github.com/goodrain/rainbond/api/model"
httputil "github.com/goodrain/rainbond/util/http"
)
// AppRestoreController is an implementation of AppRestoreInterface
type AppRestoreController struct {
}
// RestoreEnvs restores environment variables. delete the existing environment
// variables first, then create the ones in the request body.
func (a *AppRestoreController) RestoreEnvs(w http.ResponseWriter, r *http.Request) {
var req model.RestoreEnvsReq
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
if !ok {
return
}
tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
err := handler.GetAppRestoreHandler().RestoreEnvs(tenantID, serviceID, &req)
if err != nil {
format := "Service ID: %s; failed to restore envs: %v"
logrus.Errorf(format, serviceID, err)
httputil.ReturnError(r, w, 500, fmt.Sprintf(format, serviceID, err))
return
}
httputil.ReturnSuccess(r, w, "ok")
return
}
// RestorePorts restores service ports. delete the existing ports first,
// then create the ones in the request body.
func (a *AppRestoreController) RestorePorts(w http.ResponseWriter, r *http.Request) {
var req model.RestorePortsReq
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil) {
return
}
tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
err := handler.GetAppRestoreHandler().RestorePorts(tenantID, serviceID, &req)
if err != nil {
format := "Service ID: %s; failed to restore ports: %v"
logrus.Errorf(format, serviceID, err)
httputil.ReturnError(r, w, 500, fmt.Sprintf(format, serviceID, err))
return
}
httputil.ReturnSuccess(r, w, "ok")
return
}
// RestoreVolumes restores service volumes. delete the existing volumes first,
// then create the ones in the request body.
func (a *AppRestoreController) RestoreVolumes(w http.ResponseWriter, r *http.Request) {
var req model.RestoreVolumesReq
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil) {
return
}
tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
err := handler.GetAppRestoreHandler().RestoreVolumes(tenantID, serviceID, &req)
if err != nil {
format := "Service ID: %s; failed to restore volumes: %v"
logrus.Errorf(format, serviceID, err)
httputil.ReturnError(r, w, 500, fmt.Sprintf(format, serviceID, err))
return
}
httputil.ReturnSuccess(r, w, "ok")
return
}
// RestoreProbe restores service probe. delete the existing probe first,
// then create the one in the request body.
func (a *AppRestoreController) RestoreProbe(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
format := "error reading request body: %v"
httputil.ReturnError(r, w, 500, fmt.Sprintf(format, err))
}
// set a new body, which will simulate the same data we read
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
var probeReq *model.ServiceProbe
if string(body) != "" {
var req model.ServiceProbe
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil) {
return
}
probeReq = &req
} else {
probeReq = nil
}
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
if err := handler.GetAppRestoreHandler().RestoreProbe(serviceID, probeReq); err != nil {
format := "Service ID: %s; failed to restore volumes: %v"
logrus.Errorf(format, serviceID, err)
httputil.ReturnError(r, w, 500, fmt.Sprintf(format, serviceID, err))
return
}
httputil.ReturnSuccess(r, w, "ok")
return
}
// RestoreDeps restores service dependencies. delete the existing dependencies first,
// then create the ones in the request body.
func (a *AppRestoreController) RestoreDeps(w http.ResponseWriter, r *http.Request) {
var req model.RestoreDepsReq
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil) {
return
}
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
err := handler.GetAppRestoreHandler().RestoreDeps(tenantID, serviceID, &req)
if err != nil {
format := "Service ID: %s; failed to restore service dependencies: %v"
logrus.Errorf(format, serviceID, err)
httputil.ReturnError(r, w, 500, fmt.Sprintf(format, serviceID, err))
return
}
httputil.ReturnSuccess(r, w, "ok")
return
}
// RestoreDepVols restores service dependent volumes. delete the existing
// dependent volumes first, then create the ones in the request body.
func (a *AppRestoreController) RestoreDepVols(w http.ResponseWriter, r *http.Request) {
var req model.RestoreDepVolsReq
if ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil); !ok {
return
}
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
err := handler.GetAppRestoreHandler().RestoreDepVols(tenantID, serviceID, &req)
if err != nil {
format := "Service ID: %s; failed to restore volume dependencies: %v"
logrus.Errorf(format, serviceID, err)
httputil.ReturnError(r, w, 500, fmt.Sprintf(format, serviceID, err))
return
}
httputil.ReturnSuccess(r, w, nil)
}
// RestorePlugins restores service plugins. delete the existing
// service plugins first, then create the ones in the request body.
func (a *AppRestoreController) RestorePlugins(w http.ResponseWriter, r *http.Request) {
var req model.RestorePluginsReq
if ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil); !ok {
return
}
tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
if err := handler.GetAppRestoreHandler().RestorePlugins(tenantID, serviceID, &req); err != nil {
format := "Service ID: %s; failed to restore plugins: %v"
logrus.Errorf(format, serviceID, err)
httputil.ReturnError(r, w, 500, fmt.Sprintf(format, serviceID, err))
}
httputil.ReturnSuccess(r, w, nil)
}