forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 2
/
application_config_group.go
141 lines (124 loc) · 4.3 KB
/
application_config_group.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
package controller
import (
"net/http"
"strconv"
dbmodel "github.com/wutong-paas/wutong/db/model"
"github.com/go-chi/chi"
"github.com/sirupsen/logrus"
"github.com/wutong-paas/wutong/api/handler"
"github.com/wutong-paas/wutong/api/model"
ctxutil "github.com/wutong-paas/wutong/api/util/ctx"
"github.com/wutong-paas/wutong/db"
httputil "github.com/wutong-paas/wutong/util/http"
)
// AddConfigGroup -
func (a *ApplicationController) AddConfigGroup(w http.ResponseWriter, r *http.Request) {
var configReq model.ApplicationConfigGroup
appID := r.Context().Value(ctxutil.ContextKey("app_id")).(string)
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &configReq, nil) {
return
}
checkServiceExist(appID, configReq.ServiceIDs)
// create app ConfigGroups
resp, err := handler.GetApplicationHandler().AddConfigGroup(appID, &configReq)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
httputil.ReturnSuccess(r, w, resp)
}
// UpdateConfigGroup -
func (a *ApplicationController) UpdateConfigGroup(w http.ResponseWriter, r *http.Request) {
var updateReq model.UpdateAppConfigGroupReq
configGroupname := chi.URLParam(r, "config_group_name")
appID := r.Context().Value(ctxutil.ContextKey("app_id")).(string)
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &updateReq, nil) {
return
}
checkServiceExist(appID, updateReq.ServiceIDs)
// update app ConfigGroups
app, err := handler.GetApplicationHandler().UpdateConfigGroup(appID, configGroupname, &updateReq)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
httputil.ReturnSuccess(r, w, app)
}
// checkServiceExist -
func checkServiceExist(appID string, serviceIDs []string) {
// Get the application bound serviceIDs
availableServices := db.GetManager().TenantEnvServiceDao().GetServiceIDsByAppID(appID)
// Judge whether the requested service ID is correct
set := make(map[string]struct{})
for _, s := range availableServices {
set[s.ServiceID] = struct{}{}
}
for _, sid := range serviceIDs {
_, ok := set[sid]
if !ok {
logrus.Warningf("The serviceID [%s] is not under the application or does not exist", sid)
}
}
}
// DeleteConfigGroup -
func (a *ApplicationController) DeleteConfigGroup(w http.ResponseWriter, r *http.Request) {
configGroupname := chi.URLParam(r, "config_group_name")
appID := r.Context().Value(ctxutil.ContextKey("app_id")).(string)
// delete app ConfigGroups
err := handler.GetApplicationHandler().DeleteConfigGroup(appID, configGroupname)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
httputil.ReturnSuccess(r, w, nil)
}
// ListConfigGroups -
func (a *ApplicationController) ListConfigGroups(w http.ResponseWriter, r *http.Request) {
appID := r.Context().Value(ctxutil.ContextKey("app_id")).(string)
query := r.URL.Query()
pageQuery := query.Get("page")
pageSizeQuery := query.Get("pageSize")
page, _ := strconv.Atoi(pageQuery)
if page == 0 {
page = 1
}
pageSize, _ := strconv.Atoi(pageSizeQuery)
if pageSize == 0 {
pageSize = 10
}
// list app ConfigGroups
resp, err := handler.GetApplicationHandler().ListConfigGroups(appID, page, pageSize)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
httputil.ReturnSuccess(r, w, resp)
}
// SyncComponents -
func (a *ApplicationController) SyncComponents(w http.ResponseWriter, r *http.Request) {
var syncComponentReq model.SyncComponentReq
app := r.Context().Value(ctxutil.ContextKey("application")).(*dbmodel.Application)
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &syncComponentReq, nil) {
return
}
err := handler.GetApplicationHandler().SyncComponents(app, syncComponentReq.Components, syncComponentReq.DeleteComponentIDs)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
httputil.ReturnSuccess(r, w, nil)
}
// SyncAppConfigGroups -
func (a *ApplicationController) SyncAppConfigGroups(w http.ResponseWriter, r *http.Request) {
var syncAppConfigGroupReq model.SyncAppConfigGroup
app := r.Context().Value(ctxutil.ContextKey("application")).(*dbmodel.Application)
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &syncAppConfigGroupReq, nil) {
return
}
err := handler.GetApplicationHandler().SyncAppConfigGroups(app, syncAppConfigGroupReq.AppConfigGroups)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
httputil.ReturnSuccess(r, w, nil)
}