github.com/polarismesh/polaris@v1.17.8/test/integrate/http/config_center.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package http 19 20 import ( 21 "bytes" 22 "errors" 23 "fmt" 24 "io" 25 "mime/multipart" 26 "net/http" 27 "os" 28 29 "github.com/golang/protobuf/jsonpb" 30 apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage" 31 32 api "github.com/polarismesh/polaris/common/api/v1" 33 ) 34 35 /** 36 * @brief 实例数组转JSON 37 */ 38 func JSONFromConfigGroup(group *apiconfig.ConfigFileGroup) (*bytes.Buffer, error) { 39 m := jsonpb.Marshaler{Indent: " "} 40 41 buffer := bytes.NewBuffer([]byte{}) 42 43 err := m.Marshal(buffer, group) 44 if err != nil { 45 return nil, err 46 } 47 return buffer, nil 48 } 49 50 func JSONFromConfigFile(file *apiconfig.ConfigFile) (*bytes.Buffer, error) { 51 m := jsonpb.Marshaler{Indent: " "} 52 53 buffer := bytes.NewBuffer([]byte{}) 54 55 err := m.Marshal(buffer, file) 56 if err != nil { 57 return nil, err 58 } 59 return buffer, nil 60 } 61 62 func JSONFromConfigFileRelease(file *apiconfig.ConfigFileRelease) (*bytes.Buffer, error) { 63 m := jsonpb.Marshaler{Indent: " "} 64 65 buffer := bytes.NewBuffer([]byte{}) 66 67 err := m.Marshal(buffer, file) 68 if err != nil { 69 return nil, err 70 } 71 return buffer, nil 72 } 73 74 func (c *Client) CreateConfigGroup(group *apiconfig.ConfigFileGroup) (*apiconfig.ConfigResponse, error) { 75 fmt.Printf("\ncreate config_file_groups\n") 76 77 url := fmt.Sprintf("http://%v/config/%v/configfilegroups", c.Address, c.Version) 78 79 body, err := JSONFromConfigGroup(group) 80 if err != nil { 81 fmt.Printf("%v\n", err) 82 return nil, err 83 } 84 85 response, err := c.SendRequest("POST", url, body) 86 if err != nil { 87 fmt.Printf("%v\n", err) 88 return nil, err 89 } 90 91 ret, err := GetConfigResponse(response) 92 if err != nil { 93 fmt.Printf("%v\n", err) 94 return nil, err 95 } 96 97 return checkCreateConfigResponse(ret) 98 } 99 100 func (c *Client) UpdateConfigGroup(group *apiconfig.ConfigFileGroup) (*apiconfig.ConfigResponse, error) { 101 fmt.Printf("\nupdate config_file_groups\n") 102 103 url := fmt.Sprintf("http://%v/config/%v/configfilegroups", c.Address, c.Version) 104 105 body, err := JSONFromConfigGroup(group) 106 if err != nil { 107 fmt.Printf("%v\n", err) 108 return nil, err 109 } 110 111 response, err := c.SendRequest("PUT", url, body) 112 if err != nil { 113 fmt.Printf("%v\n", err) 114 return nil, err 115 } 116 117 ret, err := GetConfigResponse(response) 118 if err != nil { 119 fmt.Printf("%v\n", err) 120 return nil, err 121 } 122 123 return checkCreateConfigResponse(ret) 124 } 125 126 func (c *Client) QueryConfigGroup(group *apiconfig.ConfigFileGroup, 127 offset, limit int64) (*apiconfig.ConfigBatchQueryResponse, error) { 128 fmt.Printf("\nquery config_file_groups\n") 129 130 url := fmt.Sprintf("http://%v/config/%v/configfilegroups?namespace=%s&group=%s&offset=%d&limit=%d", 131 c.Address, c.Version, group.Namespace.GetValue(), group.Name.GetValue(), offset, limit) 132 133 body, err := JSONFromConfigGroup(group) 134 if err != nil { 135 fmt.Printf("%v\n", err) 136 return nil, err 137 } 138 139 response, err := c.SendRequest("GET", url, body) 140 if err != nil { 141 fmt.Printf("%v\n", err) 142 return nil, err 143 } 144 145 ret, err := GetConfigQueryResponse(response) 146 if err != nil { 147 fmt.Printf("%v\n", err) 148 return nil, err 149 } 150 151 return checkQueryConfigResponse(ret) 152 } 153 154 func (c *Client) DeleteConfigGroup(group *apiconfig.ConfigFileGroup) (*apiconfig.ConfigResponse, error) { 155 fmt.Printf("\ndelete config_file_groups\n") 156 157 url := fmt.Sprintf("http://%v/config/%v/configfilegroups?namespace=%s&group=%s", 158 c.Address, c.Version, group.Namespace.GetValue(), group.Name.GetValue()) 159 160 body, err := JSONFromConfigGroup(group) 161 if err != nil { 162 fmt.Printf("%v\n", err) 163 return nil, err 164 } 165 166 response, err := c.SendRequest("DELETE", url, body) 167 if err != nil { 168 fmt.Printf("%v\n", err) 169 return nil, err 170 } 171 172 ret, err := GetConfigResponse(response) 173 if err != nil { 174 fmt.Printf("%v\n", err) 175 return nil, err 176 } 177 178 return checkCreateConfigResponse(ret) 179 } 180 181 func (c *Client) CreateConfigFile(file *apiconfig.ConfigFile) (*apiconfig.ConfigResponse, error) { 182 fmt.Printf("\ncreate config_file\n") 183 184 url := fmt.Sprintf("http://%v/config/%v/configfiles", c.Address, c.Version) 185 186 body, err := JSONFromConfigFile(file) 187 if err != nil { 188 fmt.Printf("%v\n", err) 189 return nil, err 190 } 191 192 response, err := c.SendRequest("POST", url, body) 193 if err != nil { 194 fmt.Printf("%v\n", err) 195 return nil, err 196 } 197 198 ret, err := GetConfigResponse(response) 199 if err != nil { 200 fmt.Printf("%v\n", err) 201 return nil, err 202 } 203 204 return checkCreateConfigResponse(ret) 205 } 206 207 func (c *Client) UpdateConfigFile(file *apiconfig.ConfigFile) (*apiconfig.ConfigResponse, error) { 208 fmt.Printf("\nupdate config_file\n") 209 210 url := fmt.Sprintf("http://%v/config/%v/configfiles", c.Address, c.Version) 211 212 body, err := JSONFromConfigFile(file) 213 if err != nil { 214 fmt.Printf("%v\n", err) 215 return nil, err 216 } 217 218 response, err := c.SendRequest("PUT", url, body) 219 if err != nil { 220 fmt.Printf("%v\n", err) 221 return nil, err 222 } 223 224 ret, err := GetConfigResponse(response) 225 if err != nil { 226 fmt.Printf("%v\n", err) 227 return nil, err 228 } 229 230 return checkCreateConfigResponse(ret) 231 } 232 233 func (c *Client) DeleteConfigFile(file *apiconfig.ConfigFile) (*apiconfig.ConfigResponse, error) { 234 fmt.Printf("\ndelete config_file\n") 235 236 url := fmt.Sprintf("http://%v/config/%v/configfiles?namespace=%s&group=%s&name=%s", c.Address, c.Version, 237 file.Namespace.GetValue(), file.Group.GetValue(), file.Name.GetValue()) 238 239 body, err := JSONFromConfigFile(file) 240 if err != nil { 241 fmt.Printf("%v\n", err) 242 return nil, err 243 } 244 245 response, err := c.SendRequest("DELETE", url, body) 246 if err != nil { 247 fmt.Printf("%v\n", err) 248 return nil, err 249 } 250 251 ret, err := GetConfigResponse(response) 252 if err != nil { 253 fmt.Printf("%v\n", err) 254 return nil, err 255 } 256 257 return checkCreateConfigResponse(ret) 258 } 259 260 func (c *Client) ExportConfigFile(req *apiconfig.ConfigFileExportRequest) error { 261 fmt.Printf("\nexport config_file\n") 262 263 url := fmt.Sprintf("http://%v/config/%v/configfiles/export", c.Address, c.Version) 264 265 m := jsonpb.Marshaler{Indent: " "} 266 body := bytes.NewBuffer([]byte{}) 267 err := m.Marshal(body, req) 268 if err != nil { 269 return err 270 } 271 272 response, err := c.SendRequest("POST", url, body) 273 if err != nil { 274 fmt.Printf("%v\n", err) 275 return err 276 } 277 if response.StatusCode != http.StatusOK { 278 return errors.New("invalid http code") 279 } 280 if response.Header.Get("Content-Type") != "application/zip" { 281 return errors.New("invalid content type") 282 } 283 284 defer response.Body.Close() 285 out, err := os.Create("export.zip") 286 if err != nil { 287 fmt.Printf("%v\n", err) 288 return err 289 } 290 defer out.Close() 291 if _, err := io.Copy(out, response.Body); err != nil { 292 fmt.Printf("%v\n", err) 293 return err 294 } 295 return nil 296 } 297 298 func (c *Client) ImportConfigFile(namespace, group, conflictHandling string) (*apiconfig.ConfigImportResponse, error) { 299 fmt.Printf("\nimport config_file\n") 300 301 var buf bytes.Buffer 302 mw := multipart.NewWriter(&buf) 303 304 if err := mw.WriteField("namespace", namespace); err != nil { 305 fmt.Printf("%v\n", err) 306 return nil, err 307 } 308 if err := mw.WriteField("group", group); err != nil { 309 fmt.Printf("%v\n", err) 310 return nil, err 311 } 312 if err := mw.WriteField("conflict_handling", conflictHandling); err != nil { 313 fmt.Printf("%v\n", err) 314 return nil, err 315 } 316 317 filename := "export.zip" 318 f, err := os.Open(filename) 319 if err != nil { 320 fmt.Printf("%v\n", err) 321 return nil, err 322 } 323 defer f.Close() 324 fw, _ := mw.CreateFormFile("config", filename) 325 if _, err := io.Copy(fw, f); err != nil { 326 fmt.Printf("%v\n", err) 327 return nil, err 328 } 329 mw.Close() 330 331 url := fmt.Sprintf("http://%v/config/%v/configfiles/import?namespace=%s&group=%s&conflict_handling=%s", c.Address, 332 c.Version, namespace, group, conflictHandling) 333 334 request, err := http.NewRequest(http.MethodPost, url, &buf) 335 if err != nil { 336 fmt.Printf("%v\n", err) 337 return nil, err 338 } 339 request.Header.Add("Content-Type", mw.FormDataContentType()) 340 request.Header.Add("Request-Id", "test") 341 request.Header.Add("X-Polaris-Token", 342 "nu/0WRA4EqSR1FagrjRj0fZwPXuGlMpX+zCuWu4uMqy8xr1vRjisSbA25aAC3mtU8MeeRsKhQiDAynUR09I=") 343 344 response, err := c.Worker.Do(request) 345 if err != nil { 346 fmt.Printf("%v\n", err) 347 return nil, err 348 } 349 350 ret, err := GetConfigImportResponse(response) 351 if err != nil { 352 fmt.Printf("%v\n", err) 353 return nil, err 354 } 355 if ret.GetCode().GetValue() != api.ExecuteSuccess { 356 return nil, errors.New(ret.GetInfo().GetValue()) 357 } 358 return ret, nil 359 } 360 361 func (c *Client) CreateConfigFileRelease(file *apiconfig.ConfigFileRelease) (*apiconfig.ConfigResponse, error) { 362 fmt.Printf("\ncreate config_file_release\n") 363 364 url := fmt.Sprintf("http://%v/config/%v/configfiles/release", c.Address, c.Version) 365 366 body, err := JSONFromConfigFileRelease(file) 367 if err != nil { 368 fmt.Printf("%v\n", err) 369 return nil, err 370 } 371 372 response, err := c.SendRequest("POST", url, body) 373 if err != nil { 374 fmt.Printf("%v\n", err) 375 return nil, err 376 } 377 378 ret, err := GetConfigResponse(response) 379 if err != nil { 380 fmt.Printf("%v\n", err) 381 return nil, err 382 } 383 384 return checkCreateConfigResponse(ret) 385 } 386 387 func (c *Client) GetAllConfigEncryptAlgorithms() (*apiconfig.ConfigEncryptAlgorithmResponse, error) { 388 fmt.Printf("\nquery config encrypt algorithm\n") 389 url := fmt.Sprintf("http://%v/config/%v/configfiles/encryptalgorithm", c.Address, c.Version) 390 response, err := c.SendRequest("GET", url, nil) 391 if err != nil { 392 fmt.Printf("%v\n", err) 393 return nil, err 394 } 395 ret, err := GetConfigEncryptAlgorithmResponse(response) 396 if err != nil { 397 fmt.Printf("%v\n", err) 398 return nil, err 399 } 400 if ret.GetCode().GetValue() != api.ExecuteSuccess { 401 return nil, errors.New(ret.GetInfo().GetValue()) 402 } 403 return ret, nil 404 } 405 406 func checkCreateConfigResponse(ret *apiconfig.ConfigResponse) ( 407 *apiconfig.ConfigResponse, error) { 408 409 switch { 410 case ret.GetCode().GetValue() != api.ExecuteSuccess: 411 return nil, errors.New(ret.GetInfo().GetValue()) 412 } 413 414 return ret, nil 415 } 416 417 func checkQueryConfigResponse(ret *apiconfig.ConfigBatchQueryResponse) ( 418 *apiconfig.ConfigBatchQueryResponse, error) { 419 420 switch { 421 case ret.GetCode().GetValue() != api.ExecuteSuccess: 422 return nil, errors.New(ret.GetInfo().GetValue()) 423 } 424 425 return ret, nil 426 }