github.com/polarismesh/polaris@v1.17.8/apiserver/httpserver/discover/v2/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 v2 19 20 import ( 21 "io" 22 "strings" 23 24 "github.com/emicklei/go-restful/v3" 25 "github.com/golang/protobuf/proto" 26 apimodel "github.com/polarismesh/specification/source/go/api/v1/model" 27 apitraffic "github.com/polarismesh/specification/source/go/api/v1/traffic_manage" 28 29 v1 "github.com/polarismesh/polaris/apiserver/httpserver/discover/v1" 30 httpcommon "github.com/polarismesh/polaris/apiserver/httpserver/utils" 31 apiv1 "github.com/polarismesh/polaris/common/api/v1" 32 ) 33 34 const ( 35 deprecatedRoutingV2TypeUrl = "type.googleapis.com/v2." 36 newRoutingV2TypeUrl = "type.googleapis.com/v1." 37 ) 38 39 func (h *HTTPServerV2) replaceV2TypeUrl(req *restful.Request) (string, error) { 40 requestBytes, err := io.ReadAll(req.Request.Body) 41 if err != nil { 42 return "", err 43 } 44 requestText := strings.ReplaceAll(string(requestBytes), deprecatedRoutingV2TypeUrl, newRoutingV2TypeUrl) 45 return requestText, nil 46 } 47 48 // CreateRoutings 创建规则路由 49 func (h *HTTPServerV2) CreateRoutings(req *restful.Request, rsp *restful.Response) { 50 handler := &httpcommon.Handler{ 51 Request: req, 52 Response: rsp, 53 } 54 55 requestText, err := h.replaceV2TypeUrl(req) 56 if err != nil { 57 handler.WriteHeaderAndProtoV2(apiv1.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 58 return 59 } 60 var routings v1.RouterArr 61 ctx, err := handler.ParseArrayByText(func() proto.Message { 62 msg := &apitraffic.RouteRule{} 63 routings = append(routings, msg) 64 return msg 65 }, requestText) 66 if err != nil { 67 handler.WriteHeaderAndProtoV2(apiv1.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 68 return 69 } 70 71 ret := h.namingServer.CreateRoutingConfigsV2(ctx, routings) 72 handler.WriteHeaderAndProtoV2(ret) 73 } 74 75 // DeleteRoutings 删除规则路由 76 func (h *HTTPServerV2) DeleteRoutings(req *restful.Request, rsp *restful.Response) { 77 handler := &httpcommon.Handler{ 78 Request: req, 79 Response: rsp, 80 } 81 requestText, err := h.replaceV2TypeUrl(req) 82 if err != nil { 83 handler.WriteHeaderAndProtoV2(apiv1.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 84 return 85 } 86 var routings v1.RouterArr 87 ctx, err := handler.ParseArrayByText(func() proto.Message { 88 msg := &apitraffic.RouteRule{} 89 routings = append(routings, msg) 90 return msg 91 }, requestText) 92 if err != nil { 93 handler.WriteHeaderAndProtoV2(apiv1.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 94 return 95 } 96 97 ret := h.namingServer.DeleteRoutingConfigsV2(ctx, routings) 98 handler.WriteHeaderAndProtoV2(ret) 99 } 100 101 // UpdateRoutings 修改规则路由 102 func (h *HTTPServerV2) UpdateRoutings(req *restful.Request, rsp *restful.Response) { 103 handler := &httpcommon.Handler{ 104 Request: req, 105 Response: rsp, 106 } 107 requestText, err := h.replaceV2TypeUrl(req) 108 if err != nil { 109 handler.WriteHeaderAndProtoV2(apiv1.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 110 return 111 } 112 var routings v1.RouterArr 113 ctx, err := handler.ParseArrayByText(func() proto.Message { 114 msg := &apitraffic.RouteRule{} 115 routings = append(routings, msg) 116 return msg 117 }, requestText) 118 if err != nil { 119 handler.WriteHeaderAndProtoV2(apiv1.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 120 return 121 } 122 123 ret := h.namingServer.UpdateRoutingConfigsV2(ctx, routings) 124 handler.WriteHeaderAndProtoV2(ret) 125 } 126 127 // GetRoutings 查询规则路由 128 func (h *HTTPServerV2) GetRoutings(req *restful.Request, rsp *restful.Response) { 129 handler := &httpcommon.Handler{ 130 Request: req, 131 Response: rsp, 132 } 133 134 queryParams := httpcommon.ParseQueryParams(req) 135 ret := h.namingServer.QueryRoutingConfigsV2(handler.ParseHeaderContext(), queryParams) 136 handler.WriteHeaderAndProtoV2(ret) 137 } 138 139 // EnableRoutings 启用规则路由 140 func (h *HTTPServerV2) EnableRoutings(req *restful.Request, rsp *restful.Response) { 141 handler := &httpcommon.Handler{ 142 Request: req, 143 Response: rsp, 144 } 145 requestText, err := h.replaceV2TypeUrl(req) 146 if err != nil { 147 handler.WriteHeaderAndProtoV2(apiv1.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 148 return 149 } 150 var routings v1.RouterArr 151 ctx, err := handler.ParseArrayByText(func() proto.Message { 152 msg := &apitraffic.RouteRule{} 153 routings = append(routings, msg) 154 return msg 155 }, requestText) 156 if err != nil { 157 handler.WriteHeaderAndProtoV2(apiv1.NewBatchWriteResponseWithMsg(apimodel.Code_ParseException, err.Error())) 158 return 159 } 160 161 ret := h.namingServer.EnableRoutings(ctx, routings) 162 handler.WriteHeaderAndProtoV2(ret) 163 }