-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathclient_check.go
219 lines (187 loc) · 8.14 KB
/
client_check.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
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 paramcheck
import (
"context"
apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
api "github.com/polarismesh/polaris/common/api/v1"
"github.com/polarismesh/polaris/common/model"
"github.com/polarismesh/polaris/common/utils"
"github.com/polarismesh/polaris/config"
)
// UpsertAndReleaseConfigFileFromClient 创建/更新配置文件并发布
func (s *Server) UpsertAndReleaseConfigFileFromClient(ctx context.Context,
req *apiconfig.ConfigFilePublishInfo) *apiconfig.ConfigResponse {
if err := utils.CheckResourceName(req.GetNamespace()); err != nil {
return api.NewConfigResponseWithInfo(apimodel.Code_BadRequest, "invalid config namespace")
}
if err := utils.CheckResourceName(req.GetGroup()); err != nil {
return api.NewConfigResponseWithInfo(apimodel.Code_BadRequest, "invalid config group")
}
if err := CheckFileName(req.GetFileName()); err != nil {
return api.NewConfigResponseWithInfo(apimodel.Code_BadRequest, "invalid config file_name")
}
return s.nextServer.UpsertAndReleaseConfigFileFromClient(ctx, req)
}
// CreateConfigFileFromClient 调用config_file的方法创建配置文件
func (s *Server) CreateConfigFileFromClient(ctx context.Context,
req *apiconfig.ConfigFile) *apiconfig.ConfigClientResponse {
if checkRsp := s.checkConfigFileParams(req); checkRsp != nil {
return api.NewConfigClientResponseFromConfigResponse(checkRsp)
}
return s.nextServer.CreateConfigFileFromClient(ctx, req)
}
// UpdateConfigFileFromClient 调用config_file的方法更新配置文件
func (s *Server) UpdateConfigFileFromClient(ctx context.Context,
req *apiconfig.ConfigFile) *apiconfig.ConfigClientResponse {
if checkRsp := s.checkConfigFileParams(req); checkRsp != nil {
return api.NewConfigClientResponseFromConfigResponse(checkRsp)
}
return s.nextServer.UpdateConfigFileFromClient(ctx, req)
}
// DeleteConfigFileFromClient 删除配置文件,删除配置文件同时会通知客户端 Not_Found
func (s *Server) DeleteConfigFileFromClient(ctx context.Context,
req *apiconfig.ConfigFile) *apiconfig.ConfigResponse {
if req.GetNamespace().GetValue() == "" {
return api.NewConfigResponseWithInfo(
apimodel.Code_BadRequest, "namespace is empty")
}
if req.GetGroup().GetValue() == "" {
return api.NewConfigResponseWithInfo(
apimodel.Code_BadRequest, "file group is empty")
}
if req.GetName().GetValue() == "" {
return api.NewConfigResponseWithInfo(
apimodel.Code_BadRequest, "filename is empty")
}
return s.nextServer.DeleteConfigFileFromClient(ctx, req)
}
// PublishConfigFileFromClient 调用config_file_release的方法发布配置文件
func (s *Server) PublishConfigFileFromClient(ctx context.Context,
req *apiconfig.ConfigFileRelease) *apiconfig.ConfigClientResponse {
if err := CheckFileName(req.GetFileName()); err != nil {
ret := api.NewConfigResponse(apimodel.Code_InvalidConfigFileName)
return api.NewConfigClientResponseFromConfigResponse(ret)
}
if err := utils.CheckResourceName(req.GetNamespace()); err != nil {
ret := api.NewConfigResponse(apimodel.Code_InvalidNamespaceName)
return api.NewConfigClientResponseFromConfigResponse(ret)
}
if err := utils.CheckResourceName(req.GetGroup()); err != nil {
ret := api.NewConfigResponse(apimodel.Code_InvalidConfigFileGroupName)
return api.NewConfigClientResponseFromConfigResponse(ret)
}
if !s.checkNamespaceExisted(req.GetNamespace().GetValue()) {
ret := api.NewConfigResponse(apimodel.Code_NotFoundNamespace)
return api.NewConfigClientResponseFromConfigResponse(ret)
}
if req.GetReleaseType().GetValue() == model.ReleaseTypeGray && len(req.GetBetaLabels()) == 0 {
ret := api.NewConfigResponse(apimodel.Code_InvalidMatchRule)
return api.NewConfigClientResponseFromConfigResponse(ret)
}
return s.nextServer.PublishConfigFileFromClient(ctx, req)
}
// GetConfigFileWithCache 从缓存中获取配置文件,如果客户端的版本号大于服务端,则服务端重新加载缓存
func (s *Server) GetConfigFileWithCache(ctx context.Context,
req *apiconfig.ClientConfigFileInfo) *apiconfig.ConfigClientResponse {
if req.GetNamespace().GetValue() == "" {
return api.NewConfigClientResponseWithInfo(
apimodel.Code_BadRequest, "namespace is empty")
}
if req.GetGroup().GetValue() == "" {
return api.NewConfigClientResponseWithInfo(
apimodel.Code_BadRequest, "file group is empty")
}
if req.GetFileName().GetValue() == "" {
return api.NewConfigClientResponseWithInfo(
apimodel.Code_BadRequest, "filename is empty")
}
return s.nextServer.GetConfigFileWithCache(ctx, req)
}
// WatchConfigFiles 监听配置文件变化
func (s *Server) LongPullWatchFile(ctx context.Context,
request *apiconfig.ClientWatchConfigFileRequest) (config.WatchCallback, error) {
watchFiles := request.WatchFiles
if len(watchFiles) == 0 {
return func() *apiconfig.ConfigClientResponse {
return api.NewConfigClientResponse0(apimodel.Code_InvalidWatchConfigFileFormat)
}, nil
}
for _, configFile := range watchFiles {
namespace := configFile.GetNamespace().GetValue()
group := configFile.GetGroup().GetValue()
fileName := configFile.GetFileName().GetValue()
if namespace == "" {
return func() *apiconfig.ConfigClientResponse {
return api.NewConfigClientResponseWithInfo(
apimodel.Code_BadRequest, "namespace is empty")
}, nil
}
if group == "" {
return func() *apiconfig.ConfigClientResponse {
return api.NewConfigClientResponseWithInfo(
apimodel.Code_BadRequest, "file group is empty")
}, nil
}
if fileName == "" {
return func() *apiconfig.ConfigClientResponse {
return api.NewConfigClientResponseWithInfo(
apimodel.Code_BadRequest, "filename is empty")
}, nil
}
}
return s.nextServer.LongPullWatchFile(ctx, request)
}
// GetConfigFileNamesWithCache 获取某个配置分组下的配置文件
func (s *Server) GetConfigFileNamesWithCache(ctx context.Context,
req *apiconfig.ConfigFileGroupRequest) *apiconfig.ConfigClientListResponse {
if req.GetConfigFileGroup().GetNamespace().GetValue() == "" {
return api.NewConfigClientListResponseWithInfo(
apimodel.Code_BadRequest, "namespace is empty")
}
if req.GetConfigFileGroup().GetName().GetValue() == "" {
return api.NewConfigClientListResponseWithInfo(
apimodel.Code_BadRequest, "file group is empty")
}
return s.nextServer.GetConfigFileNamesWithCache(ctx, req)
}
func (s *Server) GetConfigGroupsWithCache(ctx context.Context,
req *apiconfig.ClientConfigFileInfo) *apiconfig.ConfigDiscoverResponse {
namespace := req.GetNamespace().GetValue()
out := api.NewConfigDiscoverResponse(apimodel.Code_ExecuteSuccess)
if namespace == "" {
out.Code = uint32(apimodel.Code_BadRequest)
out.Info = "invalid namespace"
return out
}
return s.nextServer.GetConfigGroupsWithCache(ctx, req)
}
// CasUpsertAndReleaseConfigFileFromClient 创建/更新配置文件并发布
func (s *Server) CasUpsertAndReleaseConfigFileFromClient(ctx context.Context,
req *apiconfig.ConfigFilePublishInfo) *apiconfig.ConfigResponse {
if err := CheckFileName(req.GetFileName()); err != nil {
return api.NewConfigResponse(apimodel.Code_InvalidConfigFileName)
}
if err := utils.CheckResourceName(req.GetNamespace()); err != nil {
return api.NewConfigResponse(apimodel.Code_InvalidNamespaceName)
}
if err := utils.CheckResourceName(req.GetGroup()); err != nil {
return api.NewConfigResponse(apimodel.Code_InvalidConfigFileGroupName)
}
return s.nextServer.CasUpsertAndReleaseConfigFileFromClient(ctx, req)
}