bzp2010 commented on a change in pull request #2099:
URL: https://github.com/apache/apisix-dashboard/pull/2099#discussion_r727656885



##########
File path: api/internal/handler/proto/proto.go
##########
@@ -0,0 +1,291 @@
+/*
+ * 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 proto
+
+import (
+       "context"
+       "encoding/json"
+       "errors"
+       "fmt"
+       "net/http"
+       "reflect"
+       "strings"
+
+       "github.com/gin-gonic/gin"
+       "github.com/shiningrush/droplet"
+       "github.com/shiningrush/droplet/data"
+       "github.com/shiningrush/droplet/wrapper"
+       wgin "github.com/shiningrush/droplet/wrapper/gin"
+
+       "github.com/apisix/manager-api/internal/core/entity"
+       "github.com/apisix/manager-api/internal/core/store"
+       "github.com/apisix/manager-api/internal/handler"
+       "github.com/apisix/manager-api/internal/utils"
+)
+
+type Handler struct {
+       routeStore        store.Interface
+       serviceStore      store.Interface
+       consumerStore     store.Interface
+       pluginConfigStore store.Interface
+       globalRuleStore   store.Interface
+       protoStore        store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+       return &Handler{
+               routeStore:        store.GetStore(store.HubKeyRoute),
+               serviceStore:      store.GetStore(store.HubKeyService),
+               consumerStore:     store.GetStore(store.HubKeyConsumer),
+               pluginConfigStore: store.GetStore(store.HubKeyPluginConfig),
+               globalRuleStore:   store.GetStore(store.HubKeyGlobalRule),
+               protoStore:        store.GetStore(store.HubKeyProto),
+       }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+       r.GET("/apisix/admin/proto/:id", wgin.Wraps(h.Get,
+               wrapper.InputType(reflect.TypeOf(GetInput{}))))
+       r.GET("/apisix/admin/proto", wgin.Wraps(h.List,
+               wrapper.InputType(reflect.TypeOf(ListInput{}))))
+       r.POST("/apisix/admin/proto", wgin.Wraps(h.Create,
+               wrapper.InputType(reflect.TypeOf(entity.Proto{}))))
+       r.PUT("/apisix/admin/proto", wgin.Wraps(h.Update,
+               wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
+       r.PUT("/apisix/admin/proto/:id", wgin.Wraps(h.Update,
+               wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
+       r.PATCH("/apisix/admin/proto/:id", wgin.Wraps(h.Patch,
+               wrapper.InputType(reflect.TypeOf(PatchInput{}))))
+       r.PATCH("/apisix/admin/proto/:id/*path", wgin.Wraps(h.Patch,
+               wrapper.InputType(reflect.TypeOf(PatchInput{}))))
+       r.DELETE("/apisix/admin/proto/:ids", wgin.Wraps(h.BatchDelete,
+               wrapper.InputType(reflect.TypeOf(BatchDeleteInput{}))))
+}
+
+var plugins = []string{"grpc-transcode"}
+
+type GetInput struct {
+       ID string `auto_read:"id,path" validate:"required"`
+}
+
+func (h *Handler) Get(c droplet.Context) (interface{}, error) {
+       input := c.Input().(*GetInput)
+
+       r, err := h.protoStore.Get(c.Context(), input.ID)
+       if err != nil {
+               return handler.SpecCodeResponse(err), err
+       }
+
+       proto := r.(*entity.Proto)
+
+       return proto, nil
+}
+
+type ListInput struct {
+       Desc string `auto_read:"desc,query"`
+       store.Pagination
+}
+
+func (h *Handler) List(c droplet.Context) (interface{}, error) {
+       input := c.Input().(*ListInput)
+
+       ret, err := h.protoStore.List(c.Context(), store.ListInput{
+               Predicate: func(obj interface{}) bool {
+                       if input.Desc != "" {
+                               return 
strings.Contains(obj.(*entity.Proto).Desc, input.Desc)
+                       }
+                       return true
+               },
+               Format: func(obj interface{}) interface{} {
+                       upstream := obj.(*entity.Proto)
+                       return upstream
+               },
+               PageSize:   input.PageSize,
+               PageNumber: input.PageNumber,
+       })
+       if err != nil {
+               return nil, err
+       }
+
+       return ret, nil
+}
+
+func (h *Handler) Create(c droplet.Context) (interface{}, error) {
+       input := c.Input().(*entity.Proto)
+
+       // check proto id exist
+       if input.ID != nil {
+               protoID := utils.InterfaceToString(input.ID)
+               ret, err := h.protoStore.Get(c.Context(), protoID)
+               if err != nil && err != data.ErrNotFound {
+                       return handler.SpecCodeResponse(err), err
+               }
+               if ret != nil {
+                       return &data.SpecCodeResponse{StatusCode: 
http.StatusBadRequest}, errors.New("proto id exists")
+               }
+       }
+
+       // create
+       ret, err := h.protoStore.Create(c.Context(), input)
+       if err != nil {
+               return handler.SpecCodeResponse(err), err
+       }
+
+       return ret, nil
+}
+
+type UpdateInput struct {
+       ID string `auto_read:"id,path"`
+       entity.Proto
+}
+
+func (h *Handler) Update(c droplet.Context) (interface{}, error) {
+       input := c.Input().(*UpdateInput)
+
+       // check if ID in body is equal ID in path
+       if err := handler.IDCompare(input.ID, input.Proto.ID); err != nil {
+               return &data.SpecCodeResponse{StatusCode: 
http.StatusBadRequest}, err
+       }
+
+       if input.ID != "" {
+               input.Proto.ID = input.ID
+       }
+
+       // check proto id exist
+       protoID := utils.InterfaceToString(input.Proto.ID)
+       ret, err := h.protoStore.Get(c.Context(), protoID)
+       if ret == nil {
+               return &data.SpecCodeResponse{StatusCode: 
http.StatusBadRequest}, errors.New("proto id not exists")
+       }
+
+       res, err := h.protoStore.Update(c.Context(), &input.Proto, true)

Review comment:
       In fact, other APIs can also use put requests to create data.
   
   PUT `/apisix/admin/routes/1` `{xxxx}`
   

##########
File path: api/internal/handler/proto/proto_test.go
##########
@@ -0,0 +1,57 @@
+/*
+ * 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 proto
+
+import (
+       "encoding/json"
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+
+       "github.com/apisix/manager-api/internal/core/entity"
+)
+
+type testCase struct {

Review comment:
       yes, fixing

##########
File path: api/test/e2enew/proto/proto_test.go
##########
@@ -0,0 +1,274 @@
+/*
+ * 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 proto
+
+import (
+       "encoding/json"
+       "net/http"
+
+       "github.com/onsi/ginkgo"
+       "github.com/onsi/gomega"
+
+       "github.com/apisix/manager-api/test/e2enew/base"
+)
+
+var correctProtobuf = `syntax = "proto3";
+    package helloworld;
+    service Greeter {
+        rpc SayHello (HelloRequest) returns (HelloReply) {}
+    }
+    message HelloRequest {
+        string name = 1;
+    }
+    message HelloReply {
+        string message = 1;
+    }`
+
+var _ = ginkgo.Describe("Proto", func() {
+       ginkgo.It("create proto success", func() {
+               createProtoBody := make(map[string]interface{})
+               createProtoBody["id"] = 1
+               createProtoBody["desc"] = "test_proto1"
+               createProtoBody["content"] = correctProtobuf
+
+               _createProtoBody, err := json.Marshal(createProtoBody)
+               gomega.Expect(err).To(gomega.BeNil())
+
+               base.RunTestCase(base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodPost,
+                       Path:         "/apisix/admin/proto",
+                       Body:         string(_createProtoBody),
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+               })
+       })
+       ginkgo.It("create proto failed, id existed", func() {
+               createProtoBody := make(map[string]interface{})
+               createProtoBody["id"] = 1
+               createProtoBody["desc"] = "test_proto1"
+               createProtoBody["content"] = correctProtobuf
+
+               _createProtoBody, err := json.Marshal(createProtoBody)
+               gomega.Expect(err).To(gomega.BeNil())
+
+               base.RunTestCase(base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodPost,
+                       Path:         "/apisix/admin/proto",
+                       Body:         string(_createProtoBody),
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectBody:   "proto id exists",
+                       ExpectStatus: http.StatusBadRequest,
+               })
+       })
+       ginkgo.It("update proto success", func() {
+               updateProtoBody := make(map[string]interface{})
+               updateProtoBody["id"] = 1
+               updateProtoBody["desc"] = "test_proto1_modify"
+               updateProtoBody["content"] = correctProtobuf
+
+               _updateProtoBody, err := json.Marshal(updateProtoBody)
+               gomega.Expect(err).To(gomega.BeNil())
+
+               base.RunTestCase(base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodPut,
+                       Path:         "/apisix/admin/proto",
+                       Body:         string(_updateProtoBody),
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectBody:   "test_proto1_modify",
+                       ExpectStatus: http.StatusOK,
+               })
+       })
+       ginkgo.It("update proto failed, id not existed", func() {

Review comment:
       Yes, because of here 
   
[https://github.com/apache/apisix-dashboard/blob/f928b3b9a0ca71d0635b6e31ab0b509d76332f2e/api/internal/handler/proto/proto.go#L162-#L167](https://github.com/apache/apisix-dashboard/blob/f928b3b9a0ca71d0635b6e31ab0b509d76332f2e/api/internal/handler/proto/proto.go#L162-#L167)
 
   
   This is wrong, I will modify it.

##########
File path: api/internal/handler/proto/proto_test.go
##########
@@ -0,0 +1,57 @@
+/*
+ * 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 proto
+
+import (
+       "encoding/json"
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+
+       "github.com/apisix/manager-api/internal/core/entity"
+)
+
+type testCase struct {

Review comment:
       fixed

##########
File path: api/test/e2enew/proto/proto_test.go
##########
@@ -0,0 +1,274 @@
+/*
+ * 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 proto
+
+import (
+       "encoding/json"
+       "net/http"
+
+       "github.com/onsi/ginkgo"
+       "github.com/onsi/gomega"
+
+       "github.com/apisix/manager-api/test/e2enew/base"
+)
+
+var correctProtobuf = `syntax = "proto3";
+    package helloworld;
+    service Greeter {
+        rpc SayHello (HelloRequest) returns (HelloReply) {}
+    }
+    message HelloRequest {
+        string name = 1;
+    }
+    message HelloReply {
+        string message = 1;
+    }`
+
+var _ = ginkgo.Describe("Proto", func() {
+       ginkgo.It("create proto success", func() {
+               createProtoBody := make(map[string]interface{})
+               createProtoBody["id"] = 1
+               createProtoBody["desc"] = "test_proto1"
+               createProtoBody["content"] = correctProtobuf
+
+               _createProtoBody, err := json.Marshal(createProtoBody)
+               gomega.Expect(err).To(gomega.BeNil())
+
+               base.RunTestCase(base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodPost,
+                       Path:         "/apisix/admin/proto",
+                       Body:         string(_createProtoBody),
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+               })
+       })
+       ginkgo.It("create proto failed, id existed", func() {
+               createProtoBody := make(map[string]interface{})
+               createProtoBody["id"] = 1
+               createProtoBody["desc"] = "test_proto1"
+               createProtoBody["content"] = correctProtobuf
+
+               _createProtoBody, err := json.Marshal(createProtoBody)
+               gomega.Expect(err).To(gomega.BeNil())
+
+               base.RunTestCase(base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodPost,
+                       Path:         "/apisix/admin/proto",
+                       Body:         string(_createProtoBody),
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectBody:   "proto id exists",
+                       ExpectStatus: http.StatusBadRequest,
+               })
+       })
+       ginkgo.It("update proto success", func() {
+               updateProtoBody := make(map[string]interface{})
+               updateProtoBody["id"] = 1
+               updateProtoBody["desc"] = "test_proto1_modify"
+               updateProtoBody["content"] = correctProtobuf
+
+               _updateProtoBody, err := json.Marshal(updateProtoBody)
+               gomega.Expect(err).To(gomega.BeNil())
+
+               base.RunTestCase(base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodPut,
+                       Path:         "/apisix/admin/proto",
+                       Body:         string(_updateProtoBody),
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectBody:   "test_proto1_modify",
+                       ExpectStatus: http.StatusOK,
+               })
+       })
+       ginkgo.It("update proto failed, id not existed", func() {

Review comment:
       fixed




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to