-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
overrides.go
258 lines (240 loc) · 10.2 KB
/
overrides.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package handlers
import (
"net/http"
"github.com/apache/dubbo-admin/pkg/admin/config"
"github.com/apache/dubbo-admin/pkg/logger"
"github.com/apache/dubbo-admin/pkg/admin/model"
"github.com/apache/dubbo-admin/pkg/admin/services"
"github.com/apache/dubbo-admin/pkg/admin/util"
"github.com/gin-gonic/gin"
)
var overrideServiceImpl services.OverrideService = &services.OverrideServiceImpl{}
// CreateOverride create a new override rule
// @Summary Create a new override rule
// @Description Create a new override rule
// @Tags OverrideRule
// @Accept json
// @Produce json
// @Param env path string false "environment" default(dev)
// @Param dynamicConfig body model.DynamicConfig true "Override Rule Input"
// @Success 201 {boolean} true
// @Failure 400 {object} model.HTTPError
// @Failure 500 {object} model.HTTPError
// @Router /api/{env}/rules/override [post]
func CreateOverride(c *gin.Context) {
var dynamicConfig *model.DynamicConfig
if err := c.ShouldBindJSON(&dynamicConfig); err != nil {
logger.Errorf("Error parsing override rule input when trying to create override rule, err msg is %s.", err.Error())
c.JSON(http.StatusBadRequest, model.HTTPError{Error: err.Error()})
return
}
serviceName := dynamicConfig.Service
application := dynamicConfig.Application
if serviceName == "" && application == "" {
c.JSON(http.StatusBadRequest, model.HTTPError{Error: "service or application must not be empty"})
return
}
// TODO: providerService.findVersionInApplication(application).equals("2.6")
// if application != "" && providerService.findVersionInApplication(application).equals("2.6") {
// c.JSON(http.StatusBadRequest, errors.New("dubbo 2.6 does not support application scope dynamic config"))
// return
// }
err := overrideServiceImpl.SaveOverride(dynamicConfig)
if err != nil {
if _, ok := err.(*config.RuleExists); ok {
logger.Infof("Override rule already exists!")
} else {
logger.Infof("Override rule create failed, err msg is %s.", err.Error())
c.JSON(http.StatusInternalServerError, model.HTTPError{Error: err.Error()})
}
return
}
c.JSON(http.StatusCreated, true)
}
// UpdateOverride update override rule
// @Summary Update override rule
// @Description Update override rule
// @Tags OverrideRule
// @Accept json
// @Produce json
// @Param env path string false "environment" default(dev)
// @Param id path string true "Override Rule Id"
// @Param dynamicConfig body model.DynamicConfig true "Override Rule Input"
// @Success 200 {boolean} true
// @Failure 400 {object} model.HTTPError
// @Failure 500 {object} model.HTTPError
// @Router /api/{env}/rules/override/{id} [post]
func UpdateOverride(c *gin.Context) {
id := c.Param("id")
// env := c.Param("env")
var dynamicConfig model.DynamicConfig
if err := c.ShouldBindJSON(&dynamicConfig); err != nil {
c.JSON(http.StatusBadRequest, model.HTTPError{Error: err.Error()})
return
}
old, err := overrideServiceImpl.FindOverride(id)
if err != nil {
logger.Errorf("Check failed before trying to update override rule for service %s , err msg is: %s", dynamicConfig.Service, err.Error())
c.JSON(http.StatusBadRequest, model.HTTPError{Error: err.Error()})
return
}
if old == nil {
c.JSON(http.StatusBadRequest, model.HTTPError{Error: "override not found"})
return
}
if err := overrideServiceImpl.UpdateOverride(&dynamicConfig); err != nil {
logger.Errorf("Update tag rule for service %s failed, err msg is: %s", dynamicConfig.Service, err.Error())
c.JSON(http.StatusInternalServerError, model.HTTPError{Error: err.Error()})
return
}
c.JSON(http.StatusOK, true)
}
// SearchOverride search override rule with key word
// @Summary Search override rule
// @Description Search override rule
// @Tags OverrideRule
// @Accept json
// @Produce json
// @Param env path string false "environment" default(dev)
// @Param application query string false "application and service must not left empty at the same time"
// @Param service query string false "application and service must not left empty at the same time"
// @Param serviceVersion query string false "version of dubbo service"
// @Param serviceGroup query string false "group of dubbo service"
// @Success 200 {object} []model.DynamicConfig
// @Failure 400 {object} model.HTTPError
// @Failure 500 {object} model.HTTPError
// @Router /api/{env}/rules/override [get]
func SearchOverride(c *gin.Context) {
service := c.DefaultQuery("service", "")
application := c.DefaultQuery("application", "")
serviceVersion := c.DefaultQuery("serviceVersion", "")
serviceGroup := c.DefaultQuery("serviceGroup", "")
var override *model.DynamicConfig
result := make([]*model.DynamicConfig, 0)
var err error
if service != "" {
id := util.BuildServiceKey("", service, serviceGroup, serviceVersion)
override, err = overrideServiceImpl.FindOverride(id)
} else if application != "" {
override, err = overrideServiceImpl.FindOverride(application)
} else {
c.JSON(http.StatusBadRequest, model.HTTPError{Error: "Either Service or application is required!"})
return
}
if err != nil {
if _, ok := err.(*config.RuleNotFound); ok {
logger.Infof("No override rule found for query parameters: application %s, service %, group %, version %", application, service, serviceGroup, serviceVersion)
c.JSON(http.StatusOK, []*model.DynamicConfig{})
} else {
logger.Errorf("Check override rule failed, err msg is: %s", err.Error())
c.JSON(http.StatusInternalServerError, model.HTTPError{Error: err.Error()})
}
}
if override != nil {
result = append(result, override)
}
c.JSON(http.StatusOK, result)
}
// DetailOverride show the detail of one specified rule
// @Summary Show the detail of one specified rule
// @Description Show the detail of one specified rule
// @Tags OverrideRule
// @Accept json
// @Produce json
// @Param env path string false "environment" default(dev)
// @Param id path string true "rule id"
// @Success 200 {object} model.DynamicConfig
// @Failure 400 {object} model.HTTPError
// @Failure 500 {object} model.HTTPError
// @Router /api/{env}/rules/override/{id} [get]
func DetailOverride(c *gin.Context) {
id := c.Param("id")
override, err := overrideServiceImpl.FindOverride(id)
if err != nil {
logger.Errorf("Check override rule detail with id %s failed, err msg is: %s", id, err.Error())
c.JSON(http.StatusInternalServerError, model.HTTPError{Error: err.Error()})
return
}
if override == nil {
c.JSON(http.StatusBadRequest, model.HTTPError{Error: "Unknown ID!"})
return
}
c.JSON(http.StatusOK, override)
}
// EnableOverride Enable the specified rule
// @Summary Enable the specified rule
// @Description Enable the specified rule
// @Tags OverrideRule
// @Accept json
// @Produce json
// @Param env path string false "environment" default(dev)
// @Param id path string true "rule id"
// @Success 200 {boolean} true
// @Failure 500 {object} model.HTTPError
// @Router /api/{env}/rules/override/enable/{id} [put]
func EnableOverride(c *gin.Context) {
id := c.Param("id")
err := overrideServiceImpl.EnableOverride(id)
if err != nil {
logger.Errorf("Enable override rule with id %s failed, err msg is: %s", id, err.Error())
c.JSON(http.StatusBadRequest, model.HTTPError{Error: err.Error()})
return
}
c.JSON(http.StatusOK, true)
}
// DeleteOverride delete the specified rule
// @Summary Delete the specified rule
// @Description Delete the specified rule
// @Tags OverrideRule
// @Accept json
// @Produce json
// @Param env path string false "environment" default(dev)
// @Param id path string true "rule id"
// @Success 200 {boolean} true
// @Failure 500 {object} model.HTTPError
// @Router /api/{env}/rules/override/{id} [delete]
func DeleteOverride(c *gin.Context) {
id := c.Param("id")
err := overrideServiceImpl.DeleteOverride(id)
if err != nil {
logger.Errorf("Delete override rule with id %s failed, err msg is: %s", id, err.Error())
c.JSON(http.StatusInternalServerError, model.HTTPError{Error: err.Error()})
return
}
c.JSON(http.StatusOK, true)
}
// DisableOverride Disable the specified rule
// @Summary Disable the specified rule
// @Description Disable the specified rule
// @Tags OverrideRule
// @Accept json
// @Produce json
// @Param env path string false "environment" default(dev)
// @Param id path string true "rule id"
// @Success 200 {boolean} true
// @Failure 500 {object} model.HTTPError
// @Router /api/{env}/rules/override/disable/{id} [put]
func DisableOverride(c *gin.Context) {
id := c.Param("id")
err := overrideServiceImpl.DisableOverride(id)
if err != nil {
logger.Errorf("Disable override rule with id %s failed, err msg is: %s", id, err.Error())
c.JSON(http.StatusInternalServerError, model.HTTPError{Error: err.Error()})
return
}
c.JSON(http.StatusOK, true)
}