github.com/polarismesh/polaris@v1.17.8/apiserver/httpserver/discover/v1/console_access.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 v1 19 20 import ( 21 "context" 22 "net/http" 23 24 "github.com/emicklei/go-restful/v3" 25 "github.com/golang/protobuf/proto" 26 apifault "github.com/polarismesh/specification/source/go/api/v1/fault_tolerance" 27 apimodel "github.com/polarismesh/specification/source/go/api/v1/model" 28 apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" 29 apitraffic "github.com/polarismesh/specification/source/go/api/v1/traffic_manage" 30 31 httpcommon "github.com/polarismesh/polaris/apiserver/httpserver/utils" 32 api "github.com/polarismesh/polaris/common/api/v1" 33 "github.com/polarismesh/polaris/common/utils" 34 ) 35 36 // CreateNamespaces 创建命名空间 37 func (h *HTTPServerV1) CreateNamespaces(req *restful.Request, rsp *restful.Response) { 38 handler := &httpcommon.Handler{ 39 Request: req, 40 Response: rsp, 41 } 42 43 var namespaces NamespaceArr 44 ctx, err := handler.ParseArray(func() proto.Message { 45 msg := &apimodel.Namespace{} 46 namespaces = append(namespaces, msg) 47 return msg 48 }) 49 if err != nil { 50 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 51 return 52 } 53 54 handler.WriteHeaderAndProto(h.namespaceServer.CreateNamespaces(ctx, namespaces)) 55 } 56 57 // DeleteNamespaces 删除命名空间 58 func (h *HTTPServerV1) DeleteNamespaces(req *restful.Request, rsp *restful.Response) { 59 handler := &httpcommon.Handler{ 60 Request: req, 61 Response: rsp, 62 } 63 64 var namespaces NamespaceArr 65 ctx, err := handler.ParseArray(func() proto.Message { 66 msg := &apimodel.Namespace{} 67 namespaces = append(namespaces, msg) 68 return msg 69 }) 70 if err != nil { 71 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 72 return 73 } 74 75 ret := h.namespaceServer.DeleteNamespaces(ctx, namespaces) 76 if code := api.CalcCode(ret); code != http.StatusOK { 77 handler.WriteHeaderAndProto(ret) 78 return 79 } 80 81 handler.WriteHeaderAndProto(ret) 82 } 83 84 // UpdateNamespaces 修改命名空间 85 func (h *HTTPServerV1) UpdateNamespaces(req *restful.Request, rsp *restful.Response) { 86 handler := &httpcommon.Handler{ 87 Request: req, 88 Response: rsp, 89 } 90 91 var namespaces NamespaceArr 92 ctx, err := handler.ParseArray(func() proto.Message { 93 msg := &apimodel.Namespace{} 94 namespaces = append(namespaces, msg) 95 return msg 96 }) 97 if err != nil { 98 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 99 return 100 } 101 102 ret := h.namespaceServer.UpdateNamespaces(ctx, namespaces) 103 if code := api.CalcCode(ret); code != http.StatusOK { 104 handler.WriteHeaderAndProto(ret) 105 return 106 } 107 108 handler.WriteHeaderAndProto(ret) 109 } 110 111 // GetNamespaces 查询命名空间 112 func (h *HTTPServerV1) GetNamespaces(req *restful.Request, rsp *restful.Response) { 113 handler := &httpcommon.Handler{ 114 Request: req, 115 Response: rsp, 116 } 117 118 ret := h.namespaceServer.GetNamespaces(handler.ParseHeaderContext(), req.Request.URL.Query()) 119 handler.WriteHeaderAndProto(ret) 120 } 121 122 // GetNamespaceToken 命名空间token的获取 123 func (h *HTTPServerV1) GetNamespaceToken(req *restful.Request, rsp *restful.Response) { 124 handler := &httpcommon.Handler{ 125 Request: req, 126 Response: rsp, 127 } 128 129 token := req.HeaderParameter("Polaris-Token") 130 ctx := context.WithValue(context.Background(), utils.StringContext("polaris-token"), token) 131 132 queryParams := httpcommon.ParseQueryParams(req) 133 namespace := &apimodel.Namespace{ 134 Name: utils.NewStringValue(queryParams["name"]), 135 Token: utils.NewStringValue(queryParams["token"]), 136 } 137 138 ret := h.namespaceServer.GetNamespaceToken(ctx, namespace) 139 handler.WriteHeaderAndProto(ret) 140 } 141 142 // UpdateNamespaceToken 更新命名空间的token 143 func (h *HTTPServerV1) UpdateNamespaceToken(req *restful.Request, rsp *restful.Response) { 144 handler := &httpcommon.Handler{ 145 Request: req, 146 Response: rsp, 147 } 148 149 var namespace apimodel.Namespace 150 ctx, err := handler.Parse(&namespace) 151 if err != nil { 152 handler.WriteHeaderAndProto(api.NewResponseWithMsg(apimodel.Code_ParseException, err.Error())) 153 return 154 } 155 156 ret := h.namespaceServer.UpdateNamespaceToken(ctx, &namespace) 157 handler.WriteHeaderAndProto(ret) 158 } 159 160 // CreateServices 创建服务 161 func (h *HTTPServerV1) CreateServices(req *restful.Request, rsp *restful.Response) { 162 handler := &httpcommon.Handler{ 163 Request: req, 164 Response: rsp, 165 } 166 167 var services ServiceArr 168 ctx, err := handler.ParseArray(func() proto.Message { 169 msg := &apiservice.Service{} 170 services = append(services, msg) 171 return msg 172 }) 173 if err != nil { 174 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 175 return 176 } 177 178 handler.WriteHeaderAndProto(h.namingServer.CreateServices(ctx, services)) 179 } 180 181 // DeleteServices 删除服务 182 func (h *HTTPServerV1) DeleteServices(req *restful.Request, rsp *restful.Response) { 183 handler := &httpcommon.Handler{ 184 Request: req, 185 Response: rsp, 186 } 187 188 var services ServiceArr 189 ctx, err := handler.ParseArray(func() proto.Message { 190 msg := &apiservice.Service{} 191 services = append(services, msg) 192 return msg 193 }) 194 if err != nil { 195 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 196 return 197 } 198 199 ret := h.namingServer.DeleteServices(ctx, services) 200 if code := api.CalcCode(ret); code != http.StatusOK { 201 handler.WriteHeaderAndProto(ret) 202 return 203 } 204 205 handler.WriteHeaderAndProto(ret) 206 } 207 208 // UpdateServices 修改服务 209 func (h *HTTPServerV1) UpdateServices(req *restful.Request, rsp *restful.Response) { 210 handler := &httpcommon.Handler{ 211 Request: req, 212 Response: rsp, 213 } 214 215 var services ServiceArr 216 ctx, err := handler.ParseArray(func() proto.Message { 217 msg := &apiservice.Service{} 218 services = append(services, msg) 219 return msg 220 }) 221 if err != nil { 222 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 223 return 224 } 225 226 ret := h.namingServer.UpdateServices(ctx, services) 227 if code := api.CalcCode(ret); code != http.StatusOK { 228 handler.WriteHeaderAndProto(ret) 229 return 230 } 231 handler.WriteHeaderAndProto(ret) 232 } 233 234 // GetAllServices 查询服务 235 func (h *HTTPServerV1) GetAllServices(req *restful.Request, rsp *restful.Response) { 236 handler := &httpcommon.Handler{ 237 Request: req, 238 Response: rsp, 239 } 240 241 queryParams := httpcommon.ParseQueryParams(req) 242 ctx := handler.ParseHeaderContext() 243 ret := h.namingServer.GetAllServices(ctx, queryParams) 244 handler.WriteHeaderAndProto(ret) 245 } 246 247 // GetServices 查询服务 248 func (h *HTTPServerV1) GetServices(req *restful.Request, rsp *restful.Response) { 249 handler := &httpcommon.Handler{ 250 Request: req, 251 Response: rsp, 252 } 253 254 queryParams := httpcommon.ParseQueryParams(req) 255 ctx := handler.ParseHeaderContext() 256 ret := h.namingServer.GetServices(ctx, queryParams) 257 handler.WriteHeaderAndProto(ret) 258 } 259 260 // GetServicesCount 查询服务总数 261 func (h *HTTPServerV1) GetServicesCount(req *restful.Request, rsp *restful.Response) { 262 handler := &httpcommon.Handler{ 263 Request: req, 264 Response: rsp, 265 } 266 ret := h.namingServer.GetServicesCount(handler.ParseHeaderContext()) 267 handler.WriteHeaderAndProto(ret) 268 } 269 270 // GetServiceToken 获取服务token 271 func (h *HTTPServerV1) GetServiceToken(req *restful.Request, rsp *restful.Response) { 272 handler := &httpcommon.Handler{ 273 Request: req, 274 Response: rsp, 275 } 276 token := req.HeaderParameter("Polaris-Token") 277 ctx := context.WithValue(context.Background(), utils.StringContext("polaris-token"), token) 278 279 queryParams := httpcommon.ParseQueryParams(req) 280 service := &apiservice.Service{ 281 Name: utils.NewStringValue(queryParams["name"]), 282 Namespace: utils.NewStringValue(queryParams["namespace"]), 283 Token: utils.NewStringValue(queryParams["token"]), 284 } 285 286 ret := h.namingServer.GetServiceToken(ctx, service) 287 handler.WriteHeaderAndProto(ret) 288 } 289 290 // UpdateServiceToken 更新服务token 291 func (h *HTTPServerV1) UpdateServiceToken(req *restful.Request, rsp *restful.Response) { 292 handler := &httpcommon.Handler{ 293 Request: req, 294 Response: rsp, 295 } 296 297 var service apiservice.Service 298 ctx, err := handler.Parse(&service) 299 if err != nil { 300 handler.WriteHeaderAndProto(api.NewResponseWithMsg(apimodel.Code_ParseException, err.Error())) 301 return 302 } 303 304 handler.WriteHeaderAndProto(h.namingServer.UpdateServiceToken(ctx, &service)) 305 } 306 307 // CreateServiceAlias service alias 308 func (h *HTTPServerV1) CreateServiceAlias(req *restful.Request, rsp *restful.Response) { 309 handler := &httpcommon.Handler{ 310 Request: req, 311 Response: rsp, 312 } 313 314 var alias apiservice.ServiceAlias 315 ctx, err := handler.Parse(&alias) 316 if err != nil { 317 handler.WriteHeaderAndProto(api.NewResponseWithMsg(apimodel.Code_ParseException, err.Error())) 318 return 319 } 320 321 handler.WriteHeaderAndProto(h.namingServer.CreateServiceAlias(ctx, &alias)) 322 } 323 324 // UpdateServiceAlias 修改服务别名 325 func (h *HTTPServerV1) UpdateServiceAlias(req *restful.Request, rsp *restful.Response) { 326 handler := &httpcommon.Handler{ 327 Request: req, 328 Response: rsp, 329 } 330 331 var alias apiservice.ServiceAlias 332 ctx, err := handler.Parse(&alias) 333 if err != nil { 334 handler.WriteHeaderAndProto(api.NewResponseWithMsg(apimodel.Code_ParseException, err.Error())) 335 return 336 } 337 338 ret := h.namingServer.UpdateServiceAlias(ctx, &alias) 339 if code := api.CalcCode(ret); code != http.StatusOK { 340 handler.WriteHeaderAndProto(ret) 341 return 342 } 343 344 handler.WriteHeaderAndProto(ret) 345 } 346 347 // DeleteServiceAliases 删除服务别名 348 func (h *HTTPServerV1) DeleteServiceAliases(req *restful.Request, rsp *restful.Response) { 349 handler := &httpcommon.Handler{ 350 Request: req, 351 Response: rsp, 352 } 353 354 var aliases ServiceAliasArr 355 ctx, err := handler.ParseArray(func() proto.Message { 356 msg := &apiservice.ServiceAlias{} 357 aliases = append(aliases, msg) 358 return msg 359 }) 360 if err != nil { 361 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 362 return 363 } 364 ret := h.namingServer.DeleteServiceAliases(ctx, aliases) 365 if code := api.CalcCode(ret); code != http.StatusOK { 366 handler.WriteHeaderAndProto(ret) 367 return 368 } 369 370 handler.WriteHeaderAndProto(ret) 371 } 372 373 // GetServiceAliases 根据源服务获取服务别名 374 func (h *HTTPServerV1) GetServiceAliases(req *restful.Request, rsp *restful.Response) { 375 handler := &httpcommon.Handler{ 376 Request: req, 377 Response: rsp, 378 } 379 380 queryParams := httpcommon.ParseQueryParams(req) 381 ret := h.namingServer.GetServiceAliases(handler.ParseHeaderContext(), queryParams) 382 handler.WriteHeaderAndProto(ret) 383 } 384 385 // CreateInstances 创建服务实例 386 func (h *HTTPServerV1) CreateInstances(req *restful.Request, rsp *restful.Response) { 387 handler := &httpcommon.Handler{ 388 Request: req, 389 Response: rsp, 390 } 391 392 var instances InstanceArr 393 ctx, err := handler.ParseArray(func() proto.Message { 394 msg := &apiservice.Instance{} 395 instances = append(instances, msg) 396 return msg 397 }) 398 if err != nil { 399 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 400 return 401 } 402 403 handler.WriteHeaderAndProto(h.namingServer.CreateInstances(ctx, instances)) 404 } 405 406 // DeleteInstances 删除服务实例 407 func (h *HTTPServerV1) DeleteInstances(req *restful.Request, rsp *restful.Response) { 408 handler := &httpcommon.Handler{ 409 Request: req, 410 Response: rsp, 411 } 412 413 var instances InstanceArr 414 ctx, err := handler.ParseArray(func() proto.Message { 415 msg := &apiservice.Instance{} 416 instances = append(instances, msg) 417 return msg 418 }) 419 if err != nil { 420 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 421 return 422 } 423 424 ret := h.namingServer.DeleteInstances(ctx, instances) 425 if code := api.CalcCode(ret); code != http.StatusOK { 426 handler.WriteHeaderAndProto(ret) 427 return 428 } 429 430 handler.WriteHeaderAndProto(ret) 431 } 432 433 // DeleteInstancesByHost 根据host删除服务实例 434 func (h *HTTPServerV1) DeleteInstancesByHost(req *restful.Request, rsp *restful.Response) { 435 handler := &httpcommon.Handler{ 436 Request: req, 437 Response: rsp, 438 } 439 440 var instances InstanceArr 441 ctx, err := handler.ParseArray(func() proto.Message { 442 msg := &apiservice.Instance{} 443 instances = append(instances, msg) 444 return msg 445 }) 446 if err != nil { 447 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 448 return 449 } 450 451 ret := h.namingServer.DeleteInstancesByHost(ctx, instances) 452 if code := api.CalcCode(ret); code != http.StatusOK { 453 handler.WriteHeaderAndProto(ret) 454 return 455 } 456 457 handler.WriteHeaderAndProto(ret) 458 } 459 460 // UpdateInstances 修改服务实例 461 func (h *HTTPServerV1) UpdateInstances(req *restful.Request, rsp *restful.Response) { 462 handler := &httpcommon.Handler{ 463 Request: req, 464 Response: rsp, 465 } 466 467 var instances InstanceArr 468 ctx, err := handler.ParseArray(func() proto.Message { 469 msg := &apiservice.Instance{} 470 instances = append(instances, msg) 471 return msg 472 }) 473 if err != nil { 474 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 475 return 476 } 477 478 ret := h.namingServer.UpdateInstances(ctx, instances) 479 if code := api.CalcCode(ret); code != http.StatusOK { 480 handler.WriteHeaderAndProto(ret) 481 return 482 } 483 484 handler.WriteHeaderAndProto(ret) 485 } 486 487 // UpdateInstancesIsolate 修改服务实例的隔离状态 488 func (h *HTTPServerV1) UpdateInstancesIsolate(req *restful.Request, rsp *restful.Response) { 489 handler := &httpcommon.Handler{ 490 Request: req, 491 Response: rsp, 492 } 493 494 var instances InstanceArr 495 ctx, err := handler.ParseArray(func() proto.Message { 496 msg := &apiservice.Instance{} 497 instances = append(instances, msg) 498 return msg 499 }) 500 if err != nil { 501 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 502 return 503 } 504 505 ret := h.namingServer.UpdateInstancesIsolate(ctx, instances) 506 if code := api.CalcCode(ret); code != http.StatusOK { 507 handler.WriteHeaderAndProto(ret) 508 return 509 } 510 511 handler.WriteHeaderAndProto(ret) 512 } 513 514 // GetInstances 查询服务实例 515 func (h *HTTPServerV1) GetInstances(req *restful.Request, rsp *restful.Response) { 516 handler := &httpcommon.Handler{ 517 Request: req, 518 Response: rsp, 519 } 520 521 queryParams := httpcommon.ParseQueryParams(req) 522 ret := h.namingServer.GetInstances(handler.ParseHeaderContext(), queryParams) 523 handler.WriteHeaderAndProto(ret) 524 } 525 526 // GetInstancesCount 查询服务实例 527 func (h *HTTPServerV1) GetInstancesCount(req *restful.Request, rsp *restful.Response) { 528 handler := &httpcommon.Handler{ 529 Request: req, 530 Response: rsp, 531 } 532 533 ret := h.namingServer.GetInstancesCount(handler.ParseHeaderContext()) 534 handler.WriteHeaderAndProto(ret) 535 } 536 537 // GetInstanceLabels 查询某个服务下所有实例的标签信息 538 func (h *HTTPServerV1) GetInstanceLabels(req *restful.Request, rsp *restful.Response) { 539 handler := &httpcommon.Handler{ 540 Request: req, 541 Response: rsp, 542 } 543 544 ret := h.namingServer.GetInstanceLabels(handler.ParseHeaderContext(), httpcommon.ParseQueryParams(req)) 545 handler.WriteHeaderAndProto(ret) 546 } 547 548 // CreateRoutings 创建规则路由 549 func (h *HTTPServerV1) CreateRoutings(req *restful.Request, rsp *restful.Response) { 550 handler := &httpcommon.Handler{ 551 Request: req, 552 Response: rsp, 553 } 554 555 var routings RoutingArr 556 ctx, err := handler.ParseArray(func() proto.Message { 557 msg := &apitraffic.Routing{} 558 routings = append(routings, msg) 559 return msg 560 }) 561 if err != nil { 562 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 563 return 564 } 565 566 ret := h.namingServer.CreateRoutingConfigs(ctx, routings) 567 handler.WriteHeaderAndProto(ret) 568 } 569 570 // DeleteRoutings 删除规则路由 571 func (h *HTTPServerV1) DeleteRoutings(req *restful.Request, rsp *restful.Response) { 572 handler := &httpcommon.Handler{ 573 Request: req, 574 Response: rsp, 575 } 576 577 var routings RoutingArr 578 ctx, err := handler.ParseArray(func() proto.Message { 579 msg := &apitraffic.Routing{} 580 routings = append(routings, msg) 581 return msg 582 }) 583 if err != nil { 584 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 585 return 586 } 587 588 ret := h.namingServer.DeleteRoutingConfigs(ctx, routings) 589 if code := api.CalcCode(ret); code != http.StatusOK { 590 handler.WriteHeaderAndProto(ret) 591 return 592 } 593 594 handler.WriteHeaderAndProto(ret) 595 } 596 597 // UpdateRoutings 修改规则路由 598 func (h *HTTPServerV1) UpdateRoutings(req *restful.Request, rsp *restful.Response) { 599 handler := &httpcommon.Handler{ 600 Request: req, 601 Response: rsp, 602 } 603 604 var routings RoutingArr 605 ctx, err := handler.ParseArray(func() proto.Message { 606 msg := &apitraffic.Routing{} 607 routings = append(routings, msg) 608 return msg 609 }) 610 if err != nil { 611 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 612 return 613 } 614 615 ret := h.namingServer.UpdateRoutingConfigs(ctx, routings) 616 if code := api.CalcCode(ret); code != http.StatusOK { 617 handler.WriteHeaderAndProto(ret) 618 return 619 } 620 621 handler.WriteHeaderAndProto(ret) 622 } 623 624 // GetRoutings 查询规则路由 625 func (h *HTTPServerV1) GetRoutings(req *restful.Request, rsp *restful.Response) { 626 handler := &httpcommon.Handler{ 627 Request: req, 628 Response: rsp, 629 } 630 631 queryParams := httpcommon.ParseQueryParams(req) 632 ret := h.namingServer.GetRoutingConfigs(handler.ParseHeaderContext(), queryParams) 633 handler.WriteHeaderAndProto(ret) 634 } 635 636 // CreateRateLimits 创建限流规则 637 func (h *HTTPServerV1) CreateRateLimits(req *restful.Request, rsp *restful.Response) { 638 handler := &httpcommon.Handler{ 639 Request: req, 640 Response: rsp, 641 } 642 643 var rateLimits RateLimitArr 644 ctx, err := handler.ParseArray(func() proto.Message { 645 msg := &apitraffic.Rule{} 646 rateLimits = append(rateLimits, msg) 647 return msg 648 }) 649 if err != nil { 650 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 651 return 652 } 653 654 handler.WriteHeaderAndProto(h.namingServer.CreateRateLimits(ctx, rateLimits)) 655 } 656 657 // DeleteRateLimits 删除限流规则 658 func (h *HTTPServerV1) DeleteRateLimits(req *restful.Request, rsp *restful.Response) { 659 handler := &httpcommon.Handler{ 660 Request: req, 661 Response: rsp, 662 } 663 664 var rateLimits RateLimitArr 665 ctx, err := handler.ParseArray(func() proto.Message { 666 msg := &apitraffic.Rule{} 667 rateLimits = append(rateLimits, msg) 668 return msg 669 }) 670 if err != nil { 671 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 672 return 673 } 674 675 ret := h.namingServer.DeleteRateLimits(ctx, rateLimits) 676 if code := api.CalcCode(ret); code != http.StatusOK { 677 handler.WriteHeaderAndProto(ret) 678 return 679 } 680 handler.WriteHeaderAndProto(ret) 681 } 682 683 // EnableRateLimits 激活限流规则 684 func (h *HTTPServerV1) EnableRateLimits(req *restful.Request, rsp *restful.Response) { 685 handler := &httpcommon.Handler{ 686 Request: req, 687 Response: rsp, 688 } 689 var rateLimits RateLimitArr 690 ctx, err := handler.ParseArray(func() proto.Message { 691 msg := &apitraffic.Rule{} 692 rateLimits = append(rateLimits, msg) 693 return msg 694 }) 695 if err != nil { 696 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 697 return 698 } 699 ret := h.namingServer.EnableRateLimits(ctx, rateLimits) 700 if code := api.CalcCode(ret); code != http.StatusOK { 701 handler.WriteHeaderAndProto(ret) 702 return 703 } 704 705 handler.WriteHeaderAndProto(ret) 706 } 707 708 // UpdateRateLimits 修改限流规则 709 func (h *HTTPServerV1) UpdateRateLimits(req *restful.Request, rsp *restful.Response) { 710 handler := &httpcommon.Handler{ 711 Request: req, 712 Response: rsp, 713 } 714 715 var rateLimits RateLimitArr 716 ctx, err := handler.ParseArray(func() proto.Message { 717 msg := &apitraffic.Rule{} 718 rateLimits = append(rateLimits, msg) 719 return msg 720 }) 721 if err != nil { 722 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 723 return 724 } 725 726 ret := h.namingServer.UpdateRateLimits(ctx, rateLimits) 727 if code := api.CalcCode(ret); code != http.StatusOK { 728 handler.WriteHeaderAndProto(ret) 729 return 730 } 731 732 handler.WriteHeaderAndProto(ret) 733 } 734 735 // GetRateLimits 查询限流规则 736 func (h *HTTPServerV1) GetRateLimits(req *restful.Request, rsp *restful.Response) { 737 handler := &httpcommon.Handler{ 738 Request: req, 739 Response: rsp, 740 } 741 742 queryParams := httpcommon.ParseQueryParams(req) 743 ret := h.namingServer.GetRateLimits(handler.ParseHeaderContext(), queryParams) 744 handler.WriteHeaderAndProto(ret) 745 } 746 747 // CreateCircuitBreakers 创建熔断规则 748 func (h *HTTPServerV1) CreateCircuitBreakers(req *restful.Request, rsp *restful.Response) { 749 handler := &httpcommon.Handler{ 750 Request: req, 751 Response: rsp, 752 } 753 754 var circuitBreakers CircuitBreakerArr 755 ctx, err := handler.ParseArray(func() proto.Message { 756 msg := &apifault.CircuitBreaker{} 757 circuitBreakers = append(circuitBreakers, msg) 758 return msg 759 }) 760 if err != nil { 761 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 762 return 763 } 764 765 ret := h.namingServer.CreateCircuitBreakers(ctx, circuitBreakers) 766 handler.WriteHeaderAndProto(ret) 767 } 768 769 // CreateCircuitBreakerVersions 创建熔断规则版本 770 func (h *HTTPServerV1) CreateCircuitBreakerVersions(req *restful.Request, rsp *restful.Response) { 771 handler := &httpcommon.Handler{ 772 Request: req, 773 Response: rsp, 774 } 775 776 var circuitBreakers CircuitBreakerArr 777 ctx, err := handler.ParseArray(func() proto.Message { 778 msg := &apifault.CircuitBreaker{} 779 circuitBreakers = append(circuitBreakers, msg) 780 return msg 781 }) 782 if err != nil { 783 handler.WriteHeaderAndProto(api.NewBatchQueryResponseWithMsg(apimodel.Code_ParseException, err.Error())) 784 return 785 } 786 787 handler.WriteHeaderAndProto(h.namingServer.CreateCircuitBreakerVersions(ctx, circuitBreakers)) 788 } 789 790 // DeleteCircuitBreakers 删除熔断规则 791 func (h *HTTPServerV1) DeleteCircuitBreakers(req *restful.Request, rsp *restful.Response) { 792 handler := &httpcommon.Handler{ 793 Request: req, 794 Response: rsp, 795 } 796 797 var circuitBreakers CircuitBreakerArr 798 ctx, err := handler.ParseArray(func() proto.Message { 799 msg := &apifault.CircuitBreaker{} 800 circuitBreakers = append(circuitBreakers, msg) 801 return msg 802 }) 803 if err != nil { 804 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 805 return 806 } 807 808 ret := h.namingServer.DeleteCircuitBreakers(ctx, circuitBreakers) 809 if code := api.CalcCode(ret); code != http.StatusOK { 810 handler.WriteHeaderAndProto(ret) 811 return 812 } 813 handler.WriteHeaderAndProto(ret) 814 } 815 816 // UpdateCircuitBreakers 修改熔断规则 817 func (h *HTTPServerV1) UpdateCircuitBreakers(req *restful.Request, rsp *restful.Response) { 818 handler := &httpcommon.Handler{ 819 Request: req, 820 Response: rsp, 821 } 822 823 var circuitBreakers CircuitBreakerArr 824 ctx, err := handler.ParseArray(func() proto.Message { 825 msg := &apifault.CircuitBreaker{} 826 circuitBreakers = append(circuitBreakers, msg) 827 return msg 828 }) 829 if err != nil { 830 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 831 return 832 } 833 834 ret := h.namingServer.UpdateCircuitBreakers(ctx, circuitBreakers) 835 if code := api.CalcCode(ret); code != http.StatusOK { 836 handler.WriteHeaderAndProto(ret) 837 return 838 } 839 handler.WriteHeaderAndProto(ret) 840 } 841 842 // ReleaseCircuitBreakers 发布熔断规则 843 func (h *HTTPServerV1) ReleaseCircuitBreakers(req *restful.Request, rsp *restful.Response) { 844 handler := &httpcommon.Handler{ 845 Request: req, 846 Response: rsp, 847 } 848 849 var configRelease ConfigReleaseArr 850 ctx, err := handler.ParseArray(func() proto.Message { 851 msg := &apiservice.ConfigRelease{} 852 configRelease = append(configRelease, msg) 853 return msg 854 }) 855 if err != nil { 856 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 857 return 858 } 859 860 ret := h.namingServer.ReleaseCircuitBreakers(ctx, configRelease) 861 if code := api.CalcCode(ret); code != http.StatusOK { 862 handler.WriteHeaderAndProto(ret) 863 return 864 } 865 handler.WriteHeaderAndProto(ret) 866 } 867 868 // UnBindCircuitBreakers 解绑熔断规则 869 func (h *HTTPServerV1) UnBindCircuitBreakers(req *restful.Request, rsp *restful.Response) { 870 handler := &httpcommon.Handler{ 871 Request: req, 872 Response: rsp, 873 } 874 875 var configRelease ConfigReleaseArr 876 ctx, err := handler.ParseArray(func() proto.Message { 877 msg := &apiservice.ConfigRelease{} 878 configRelease = append(configRelease, msg) 879 return msg 880 }) 881 if err != nil { 882 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 883 return 884 } 885 886 ret := h.namingServer.UnBindCircuitBreakers(ctx, configRelease) 887 if code := api.CalcCode(ret); code != http.StatusOK { 888 handler.WriteHeaderAndProto(ret) 889 return 890 } 891 handler.WriteHeaderAndProto(ret) 892 } 893 894 // GetCircuitBreaker 根据id和version获取熔断规则 895 func (h *HTTPServerV1) GetCircuitBreaker(req *restful.Request, rsp *restful.Response) { 896 handler := &httpcommon.Handler{ 897 Request: req, 898 Response: rsp, 899 } 900 901 queryParams := httpcommon.ParseQueryParams(req) 902 ret := h.namingServer.GetCircuitBreaker(handler.ParseHeaderContext(), queryParams) 903 handler.WriteHeaderAndProto(ret) 904 } 905 906 // GetCircuitBreakerVersions 查询熔断规则的所有版本 907 func (h *HTTPServerV1) GetCircuitBreakerVersions(req *restful.Request, rsp *restful.Response) { 908 handler := &httpcommon.Handler{ 909 Request: req, 910 Response: rsp, 911 } 912 913 queryParams := httpcommon.ParseQueryParams(req) 914 ret := h.namingServer.GetCircuitBreakerVersions(handler.ParseHeaderContext(), queryParams) 915 handler.WriteHeaderAndProto(ret) 916 } 917 918 // GetMasterCircuitBreakers 查询master熔断规则 919 func (h *HTTPServerV1) GetMasterCircuitBreakers(req *restful.Request, rsp *restful.Response) { 920 handler := &httpcommon.Handler{ 921 Request: req, 922 Response: rsp, 923 } 924 925 queryParams := httpcommon.ParseQueryParams(req) 926 ret := h.namingServer.GetMasterCircuitBreakers(handler.ParseHeaderContext(), queryParams) 927 handler.WriteHeaderAndProto(ret) 928 } 929 930 // GetReleaseCircuitBreakers 根据规则id查询已发布的熔断规则 931 func (h *HTTPServerV1) GetReleaseCircuitBreakers(req *restful.Request, rsp *restful.Response) { 932 handler := &httpcommon.Handler{ 933 Request: req, 934 Response: rsp, 935 } 936 937 queryParams := httpcommon.ParseQueryParams(req) 938 ret := h.namingServer.GetReleaseCircuitBreakers(handler.ParseHeaderContext(), queryParams) 939 handler.WriteHeaderAndProto(ret) 940 } 941 942 // GetCircuitBreakerByService 根据服务查询绑定熔断规则 943 func (h *HTTPServerV1) GetCircuitBreakerByService(req *restful.Request, rsp *restful.Response) { 944 handler := &httpcommon.Handler{ 945 Request: req, 946 Response: rsp, 947 } 948 949 queryParams := httpcommon.ParseQueryParams(req) 950 ret := h.namingServer.GetCircuitBreakerByService(handler.ParseHeaderContext(), queryParams) 951 handler.WriteHeaderAndProto(ret) 952 } 953 954 // GetServiceOwner 根据服务获取服务负责人 955 func (h *HTTPServerV1) GetServiceOwner(req *restful.Request, rsp *restful.Response) { 956 handler := &httpcommon.Handler{ 957 Request: req, 958 Response: rsp, 959 } 960 961 var services ServiceArr 962 ctx, err := handler.ParseArray(func() proto.Message { 963 msg := &apiservice.Service{} 964 services = append(services, msg) 965 return msg 966 }) 967 if err != nil { 968 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 969 return 970 } 971 972 handler.WriteHeaderAndProto(h.namingServer.GetServiceOwner(ctx, services)) 973 } 974 975 // GetCircuitBreakerToken 获取熔断规则token 976 func (h *HTTPServerV1) GetCircuitBreakerToken(req *restful.Request, rsp *restful.Response) { 977 handler := &httpcommon.Handler{ 978 Request: req, 979 Response: rsp, 980 } 981 token := req.HeaderParameter("Polaris-Token") 982 ctx := context.WithValue(context.Background(), utils.StringContext("polaris-token"), token) 983 984 queryParams := httpcommon.ParseQueryParams(req) 985 circuitBreaker := &apifault.CircuitBreaker{ 986 Id: utils.NewStringValue(queryParams["id"]), 987 Version: utils.NewStringValue("master"), 988 Token: utils.NewStringValue(queryParams["token"]), 989 } 990 ret := h.namingServer.GetCircuitBreakerToken(ctx, circuitBreaker) 991 handler.WriteHeaderAndProto(ret) 992 } 993 994 // CreateCircuitBreakerRules create the circuitbreaker rues 995 func (h *HTTPServerV1) CreateCircuitBreakerRules(req *restful.Request, rsp *restful.Response) { 996 handler := &httpcommon.Handler{ 997 Request: req, 998 Response: rsp, 999 } 1000 1001 var circuitBreakerRules CircuitBreakerRuleAttr 1002 ctx, err := handler.ParseArray(func() proto.Message { 1003 msg := &apifault.CircuitBreakerRule{} 1004 circuitBreakerRules = append(circuitBreakerRules, msg) 1005 return msg 1006 }) 1007 if err != nil { 1008 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 1009 return 1010 } 1011 1012 handler.WriteHeaderAndProto(h.namingServer.CreateCircuitBreakerRules(ctx, circuitBreakerRules)) 1013 } 1014 1015 // DeleteCircuitBreakerRules delete the circuitbreaker rues 1016 func (h *HTTPServerV1) DeleteCircuitBreakerRules(req *restful.Request, rsp *restful.Response) { 1017 handler := &httpcommon.Handler{ 1018 Request: req, 1019 Response: rsp, 1020 } 1021 1022 var circuitBreakerRules CircuitBreakerRuleAttr 1023 ctx, err := handler.ParseArray(func() proto.Message { 1024 msg := &apifault.CircuitBreakerRule{} 1025 circuitBreakerRules = append(circuitBreakerRules, msg) 1026 return msg 1027 }) 1028 if err != nil { 1029 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 1030 return 1031 } 1032 1033 ret := h.namingServer.DeleteCircuitBreakerRules(ctx, circuitBreakerRules) 1034 if code := api.CalcCode(ret); code != http.StatusOK { 1035 handler.WriteHeaderAndProto(ret) 1036 return 1037 } 1038 handler.WriteHeaderAndProto(ret) 1039 } 1040 1041 // EnableCircuitBreakerRules enable the circuitbreaker rues 1042 func (h *HTTPServerV1) EnableCircuitBreakerRules(req *restful.Request, rsp *restful.Response) { 1043 handler := &httpcommon.Handler{ 1044 Request: req, 1045 Response: rsp, 1046 } 1047 var circuitBreakerRules CircuitBreakerRuleAttr 1048 ctx, err := handler.ParseArray(func() proto.Message { 1049 msg := &apifault.CircuitBreakerRule{} 1050 circuitBreakerRules = append(circuitBreakerRules, msg) 1051 return msg 1052 }) 1053 if err != nil { 1054 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 1055 return 1056 } 1057 ret := h.namingServer.EnableCircuitBreakerRules(ctx, circuitBreakerRules) 1058 if code := api.CalcCode(ret); code != http.StatusOK { 1059 handler.WriteHeaderAndProto(ret) 1060 return 1061 } 1062 1063 handler.WriteHeaderAndProto(ret) 1064 } 1065 1066 // UpdateCircuitBreakerRules update the circuitbreaker rues 1067 func (h *HTTPServerV1) UpdateCircuitBreakerRules(req *restful.Request, rsp *restful.Response) { 1068 handler := &httpcommon.Handler{ 1069 Request: req, 1070 Response: rsp, 1071 } 1072 1073 var circuitBreakerRules CircuitBreakerRuleAttr 1074 ctx, err := handler.ParseArray(func() proto.Message { 1075 msg := &apifault.CircuitBreakerRule{} 1076 circuitBreakerRules = append(circuitBreakerRules, msg) 1077 return msg 1078 }) 1079 if err != nil { 1080 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 1081 return 1082 } 1083 1084 ret := h.namingServer.UpdateCircuitBreakerRules(ctx, circuitBreakerRules) 1085 if code := api.CalcCode(ret); code != http.StatusOK { 1086 handler.WriteHeaderAndProto(ret) 1087 return 1088 } 1089 1090 handler.WriteHeaderAndProto(ret) 1091 } 1092 1093 // GetCircuitBreakerRules query the circuitbreaker rues 1094 func (h *HTTPServerV1) GetCircuitBreakerRules(req *restful.Request, rsp *restful.Response) { 1095 handler := &httpcommon.Handler{ 1096 Request: req, 1097 Response: rsp, 1098 } 1099 1100 queryParams := httpcommon.ParseQueryParams(req) 1101 ret := h.namingServer.GetCircuitBreakerRules(handler.ParseHeaderContext(), queryParams) 1102 handler.WriteHeaderAndProto(ret) 1103 } 1104 1105 // CreateFaultDetectRules create the fault detect rues 1106 func (h *HTTPServerV1) CreateFaultDetectRules(req *restful.Request, rsp *restful.Response) { 1107 handler := &httpcommon.Handler{ 1108 Request: req, 1109 Response: rsp, 1110 } 1111 1112 var faultDetectRules FaultDetectRuleAttr 1113 ctx, err := handler.ParseArray(func() proto.Message { 1114 msg := &apifault.FaultDetectRule{} 1115 faultDetectRules = append(faultDetectRules, msg) 1116 return msg 1117 }) 1118 if err != nil { 1119 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 1120 return 1121 } 1122 1123 handler.WriteHeaderAndProto(h.namingServer.CreateFaultDetectRules(ctx, faultDetectRules)) 1124 } 1125 1126 // DeleteFaultDetectRules delete the fault detect rues 1127 func (h *HTTPServerV1) DeleteFaultDetectRules(req *restful.Request, rsp *restful.Response) { 1128 handler := &httpcommon.Handler{ 1129 Request: req, 1130 Response: rsp, 1131 } 1132 1133 var faultDetectRules FaultDetectRuleAttr 1134 ctx, err := handler.ParseArray(func() proto.Message { 1135 msg := &apifault.FaultDetectRule{} 1136 faultDetectRules = append(faultDetectRules, msg) 1137 return msg 1138 }) 1139 if err != nil { 1140 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 1141 return 1142 } 1143 1144 ret := h.namingServer.DeleteFaultDetectRules(ctx, faultDetectRules) 1145 if code := api.CalcCode(ret); code != http.StatusOK { 1146 handler.WriteHeaderAndProto(ret) 1147 return 1148 } 1149 handler.WriteHeaderAndProto(ret) 1150 } 1151 1152 // UpdateFaultDetectRules update the fault detect rues 1153 func (h *HTTPServerV1) UpdateFaultDetectRules(req *restful.Request, rsp *restful.Response) { 1154 handler := &httpcommon.Handler{ 1155 Request: req, 1156 Response: rsp, 1157 } 1158 1159 var faultDetectRules FaultDetectRuleAttr 1160 ctx, err := handler.ParseArray(func() proto.Message { 1161 msg := &apifault.FaultDetectRule{} 1162 faultDetectRules = append(faultDetectRules, msg) 1163 return msg 1164 }) 1165 if err != nil { 1166 handler.WriteHeaderAndProto(api.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 1167 return 1168 } 1169 1170 ret := h.namingServer.UpdateFaultDetectRules(ctx, faultDetectRules) 1171 if code := api.CalcCode(ret); code != http.StatusOK { 1172 handler.WriteHeaderAndProto(ret) 1173 return 1174 } 1175 1176 handler.WriteHeaderAndProto(ret) 1177 } 1178 1179 // GetFaultDetectRules query the fault detect rues 1180 func (h *HTTPServerV1) GetFaultDetectRules(req *restful.Request, rsp *restful.Response) { 1181 handler := &httpcommon.Handler{ 1182 Request: req, 1183 Response: rsp, 1184 } 1185 1186 queryParams := httpcommon.ParseQueryParams(req) 1187 ret := h.namingServer.GetFaultDetectRules(handler.ParseHeaderContext(), queryParams) 1188 handler.WriteHeaderAndProto(ret) 1189 }