You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/01/08 08:47:42 UTC
[GitHub] [apisix-dashboard] Jaycean opened a new pull request #1245: Export route from OpenAPI Specification3.0
Jaycean opened a new pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245
Please answer these questions before submitting a pull request
- Why submit this pull request?
- [ ] Bugfix
- [x] New feature provided
- [ ] Improve performance
- [ ] Backport patches
- Related issues
#1102
___
### New feature or improvement
- Export route from OpenAPI Specification3.0
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765308727
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (7c1b83e) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/8852e630f0c2890d6607ab56925f3c0aaf6144d8?el=desc) (8852e63) will **decrease** coverage by `16.26%`.
> The diff coverage is `65.36%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
===========================================
- Coverage 62.75% 46.48% -16.27%
===========================================
Files 41 34 -7
Lines 2360 2233 -127
===========================================
- Hits 1481 1038 -443
- Misses 703 1060 +357
+ Partials 176 135 -41
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (-62.50%)` | :arrow_down: |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `65.36% <65.36%> (ø)` | |
| [api/internal/filter/request\_id.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9yZXF1ZXN0X2lkLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `0.00% <0.00%> (-70.41%)` | :arrow_down: |
| [api/internal/filter/cors.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9jb3JzLmdv) | `0.00% <0.00%> (-66.67%)` | :arrow_down: |
| [api/internal/filter/schema.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9zY2hlbWEuZ28=) | `0.00% <0.00%> (-55.47%)` | :arrow_down: |
| [api/internal/handler/server\_info/server\_info.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc2VydmVyX2luZm8vc2VydmVyX2luZm8uZ28=) | `57.14% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/utils/closer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2Nsb3Nlci5nbw==) | `0.00% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/handler/consumer/consumer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvY29uc3VtZXIvY29uc3VtZXIuZ28=) | `63.82% <0.00%> (-27.66%)` | :arrow_down: |
| ... and [26 more](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [8852e63...7c1b83e](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (7668938) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/b2908468d4c8df96bde32c088710a2ee7fae67a9?el=desc) (b290846) will **decrease** coverage by `0.05%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
==========================================
- Coverage 42.44% 42.39% -0.06%
==========================================
Files 31 31
Lines 1932 1932
==========================================
- Hits 820 819 -1
- Misses 1001 1002 +1
Partials 111 111
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (ø)` | |
| [api/internal/core/store/store.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmUuZ28=) | `79.37% <0.00%> (-0.63%)` | :arrow_down: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [b290846...7668938](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765308727
checklist:
- [x] export route with host **(test2 coverd)**
- [x] export route with hosts **(test1 coverd)**
- [x] export route with uri **(test2 coverd)**
- [x] export route with uris **(test1 coverd)**
- [x] export route with upstream
- [x] export route with upstream_id **(test8 coverd)**
- [x] export route with service_id(service create with upstream) **(test4 coverd)**
- [x] export route with upstream and service_id **(test6 coverd)**
- [x] export route with service_id(service create with upstream_id) **(test6 covered)**
- [ ] export route with labels and service_id(service create with labels)
- [ ] export route without labels and service_id(service create with labels)
- [x] export route with plugins and service_id(service create with plugins which is not the same as route's plugins) **(test5 coverd)**
- [x] export route with basic-auth plugin
- [x] export route with jwt-auth plugin
- [x] export route with key-auth plugin
- [x] export route with request-validation (body_schema and header_schema) plugin
- [x] export two routes **(test3 coverd)**
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] starsz commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
starsz commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r556541528
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,365 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-serviceProtocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+ plugins := make(map[string]interface{})
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for _, va := range route.Vars.([]interface{}) {
+ compile := regexp.MustCompile("^http_.*")
+ match := compile.Match([]byte(va.([]interface{})[0].(string)))
+ if match {
+ m[va.([]interface{})[2].(string)] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ }
+ }
+ continue
+ }
+ // analysis security plugins
+ securityEnv := &openapi3.SecurityRequirement{}
+ switch key {
+ case string(KeyAuth):
+ secSchemas["api_key"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewCSRFSecurityScheme()}
+ securityEnv.Authenticate("api_key", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(BasicAuth):
+ secSchemas["basicAuth"] = &openapi3.SecuritySchemeRef{Value: &openapi3.SecurityScheme{
+ Type: "basicAuth",
+ Name: "basicAuth",
+ In: "header",
+ }}
+ securityEnv.Authenticate("basicAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(JWTAuth):
+ secSchemas["bearerAuth"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewJWTSecurityScheme()}
+ securityEnv.Authenticate("bearerAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ }
+ plugins[key] = value
+ }
+ path.Security = secReq
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enableWebsocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case "GET":
+ //pathItem.Get = path
+ pathItem.Get = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Get",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+
+ case "POST":
+ //pathItem.Post = path
+ pathItem.Post = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Post",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "PUT":
+ //pathItem.Put = path
+ pathItem.Put = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Put",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "DELETE":
+ //pathItem.Delete = path
+ pathItem.Delete = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Delete",
+ Parameters: path.Parameters,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "PATCH":
+ //pathItem.Patch = path
+ pathItem.Patch = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Patch",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "HEAD":
+ //pathItem.Head = path
+ pathItem.Head = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Head",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ }
+ }
+ }
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger
+}
Review comment:
It's still too long.IMO, we can deal with `route.plugin` in another function.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] starsz commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
starsz commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765262758
> > > > > Hi, @Jaycean. Need unit test.
> > > >
> > > >
> > > > At present, the function interface needs to export the existing route data and ensure the correctness of the exported data, so I think E2E should be written more perfectly. I think if the simple unit test only tests whether the interface can export data, it does not represent whether the function of the interface is correct, and if the relevant route data is not created, the interface will not work normally use.
> > >
> > >
> > > Not agree with you.
> > > First of all, unit testing is very important. It helps find bugs easily and quickly if someone changes the code.And unit testing can cover more code than e2e test.
> > > For example, in this case.
> > > We need to test these situations at least:
> > >
> > > 1. route can't found
> > > 2. Uris contain "*"
> > > 3. plugins with `request-validation`
> > > 4. different route method
> > > ...
> > >
> > > > and if the relevant route data is not created, the interface will not work normally use.
> > >
> > >
> > > We can mock data in etcd.
> > > See https://github.com/apache/apisix-dashboard/blob/master/api/internal/handler/server_info/server_info_test.go for more details.
> >
> >
> > @starsz Now I have encountered a problem in unit testing, exporting the data that needs to create route, upstream and service. After reading the example you gave me, I still don't know how to write it. Can you give me some advice. Thks
OK. Sorry, we don't have a doc to introduce the test contribute doc yet.
First, you should know the mock technology.
You can reference the doc: https://github.com/stretchr/testify#mock-package.
Mock provides a way to check input and give the return value that we want.
In your code, we should mock those function:
```
routeStore.Get
routeStore.List
upstreamStore.Get
serviceStore.Get
```
Second, IMO, you should write two testcases
One test case for `ExportRoutes`, like:
`TestDataLoader_ExportRoutes`
And another test case for `ExportAllRoutes`.like
`TestDataLoader_ExportAllRoutes`
Third, in those test case function.We should mock the store like:
```
routeMockStore := &store.MockInterface{}
routeMockStore.On("Get", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
id := args.Get(0).(string)
// here we can do some check on the id.
}).Return(tc.giveRet, tc.giveErr)
```
Mock.Anything stands for the args when you called routeMockStore.Get.
tc.giveRet and tc.giveErr means what you want to get when called this line:
https://github.com/apache/apisix-dashboard/pull/1245/files#diff-195642dee38f4cdcc68921704b4b59dce5afc67d06074c1bc53004eb9b5dfc5eR70
The same as other mock functions.
Any feedback is welcome.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r563551156
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
Review comment:
done.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (0276df5) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/0f322e1d4a03e3416da1ae989fcfa49232035648?el=desc) (0f322e1) will **not change** coverage.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
=======================================
Coverage 42.44% 42.44%
=======================================
Files 31 31
Lines 1932 1932
=======================================
Hits 820 820
Misses 1001 1001
Partials 111 111
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (ø)` | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [0f322e1...0276df5](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-767936462
@starsz @nic-chen PTAL. Thks.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (0d191cb) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/b2908468d4c8df96bde32c088710a2ee7fae67a9?el=desc) (b290846) will **not change** coverage.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
=======================================
Coverage 42.44% 42.44%
=======================================
Files 31 31
Lines 1932 1932
=======================================
Hits 820 820
Misses 1001 1001
Partials 111 111
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (ø)` | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [b290846...0d191cb](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757598022
> Hi, @Jaycean. Need unit test.
At present, the function interface needs to export the existing route data and ensure the correctness of the exported data, so I think E2E should be written more perfectly. I think if the simple unit test only tests whether the interface can export data, it does not represent whether the function of the interface is correct, and if the relevant route data is not created, the interface will not work normally use.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765308727
checklist:
- [x] export route with host **(test2 coverd)**
- [x] export route with hosts **(test1 coverd)**
- [x] export route with uri **(test2 coverd)**
- [x] export route with uris **(test1 coverd)**
- [x] export route with upstream
- [x] export route with upstream_id **(test8 coverd)**
- [x] export route with service_id(service create with upstream) **(test4 coverd)**
- [x] export route with upstream and service_id **(test6 coverd)**
- [ ] export route with service_id(service create with upstream_id)
- [ ] export route with labels and service_id(service create with labels)
- [ ] export route without labels and service_id(service create with labels)
- [x] export route with plugins and service_id(service create with plugins which is not the same as route's plugins) **(test5 coverd)**
- [x] export route with basic-auth plugin
- [x] export route with jwt-auth plugin
- [x] export route with key-auth plugin
- [x] export route with request-validation (body_schema and header_schema) plugin
- [x] export two routes **(test3 coverd)**
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (5e86e17) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/1385fc200b15eff4ffee8fd4165e7ba414dcaba0?el=desc) (1385fc2) will **not change** coverage.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
=======================================
Coverage 42.44% 42.44%
=======================================
Files 31 31
Lines 1932 1932
=======================================
Hits 820 820
Misses 1001 1001
Partials 111 111
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (ø)` | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [1385fc2...5e86e17](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] nic-chen commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
nic-chen commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766603524
I vote for option A, at least it can be imported and exported normally on APISIX platform.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r561522755
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR1 + "}}",
+ },
+ }
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ exportStrR2 := `
+ "/hello2": {
+ "get": {
+ "operationId": "aaaa2Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaa2Post",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR2 = replaceStr(exportStrR2)
+
+ tests2 := []HttpTestCase{
+ {
+ Desc: "hit route2 that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route2",
Review comment:
```suggestion
Desc: "create route2 with uri and hosts to test whether the uri parsing is correct ",
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r564542685
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+//ExportRoutes Export data by passing route ID, such as "R1" or multiple route parameters, such as "R1,R2"
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(c.Context(), id)
+ if err != nil {
+ return nil, err
Review comment:
done.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+//ExportRoutes Export data by passing route ID, such as "R1" or multiple route parameters, such as "R1,R2"
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(c.Context(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+//ExportAllRoutes All routes can be directly exported without passing parameters
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+//RouteToOpenAPI3 Pass in route list parameter: []*entity.Route, convert route data to openapi3 and export processing function
+func (h *Handler) RouteToOpenAPI3(c droplet.Context, routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
Review comment:
done.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+//ExportRoutes Export data by passing route ID, such as "R1" or multiple route parameters, such as "R1,R2"
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(c.Context(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+//ExportAllRoutes All routes can be directly exported without passing parameters
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+//RouteToOpenAPI3 Pass in route list parameter: []*entity.Route, convert route data to openapi3 and export processing function
+func (h *Handler) RouteToOpenAPI3(c droplet.Context, routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(c.Context(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ //Parse upstream
+ _upstream, err := h.ParseRouteUpstream(c, route)
+
+ if err != nil {
+ log.Errorf("ParseRouteUpstream err: ", err)
+ return nil, err
+ } else if err == nil && _upstream != nil {
+ extensions["x-apisix-upstream"] = _upstream
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = ParseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // Parse Route URIs
+ paths, paramsRefs = ParseRouteUris(route, paths, paramsRefs, pathItem)
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = ParseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ pathItem.Get = ParsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ pathItem.Post = ParsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ pathItem.Put = ParsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ pathItem.Delete = ParsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ pathItem.Patch = ParsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ pathItem.Head = ParsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+//ParseLabels When service and route have labels at the same time, use route's label.
+//When route has no label, service sometimes uses service's label. This function is used to process this logic
+func ParseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
Review comment:
done
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+//ExportRoutes Export data by passing route ID, such as "R1" or multiple route parameters, such as "R1,R2"
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(c.Context(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+//ExportAllRoutes All routes can be directly exported without passing parameters
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+//RouteToOpenAPI3 Pass in route list parameter: []*entity.Route, convert route data to openapi3 and export processing function
+func (h *Handler) RouteToOpenAPI3(c droplet.Context, routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(c.Context(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ //Parse upstream
+ _upstream, err := h.ParseRouteUpstream(c, route)
+
+ if err != nil {
+ log.Errorf("ParseRouteUpstream err: ", err)
+ return nil, err
+ } else if err == nil && _upstream != nil {
+ extensions["x-apisix-upstream"] = _upstream
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = ParseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // Parse Route URIs
+ paths, paramsRefs = ParseRouteUris(route, paths, paramsRefs, pathItem)
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = ParseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ pathItem.Get = ParsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ pathItem.Post = ParsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ pathItem.Put = ParsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ pathItem.Delete = ParsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ pathItem.Patch = ParsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ pathItem.Head = ParsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+//ParseLabels When service and route have labels at the same time, use route's label.
+//When route has no label, service sometimes uses service's label. This function is used to process this logic
+func ParseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
+ if route.Labels != nil {
+ return route.Labels, nil
+ } else if route.Labels == nil && route.ServiceID != nil {
+ if serviceLabels != nil {
+ return serviceLabels, nil
+ } else if serviceLabels == nil {
+ return nil, nil
+ }
+ }
+ return nil, nil
+}
+
+//ParsePathItem Convert data in route to openapi3
+func ParsePathItem(path openapi3.Operation, routeMethod string) *openapi3.Operation {
+ _path := &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + routeMethod,
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ return _path
+}
+
+// ParseRoutePlugins Merge service with plugin in route
+func ParseRoutePlugins(route *entity.Route, paramsRefs []*openapi3.ParameterRef, plugins map[string]interface{}, path openapi3.Operation, servicePlugins map[string]interface{}, secSchemas openapi3.SecuritySchemes, requestBody *openapi3.RequestBody) (openapi3.Operation, openapi3.SecuritySchemes, []*openapi3.ParameterRef, map[string]interface{}, error) {
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+
+ m["*/*"] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
Review comment:
done
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+//ExportRoutes Export data by passing route ID, such as "R1" or multiple route parameters, such as "R1,R2"
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(c.Context(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+//ExportAllRoutes All routes can be directly exported without passing parameters
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+//RouteToOpenAPI3 Pass in route list parameter: []*entity.Route, convert route data to openapi3 and export processing function
+func (h *Handler) RouteToOpenAPI3(c droplet.Context, routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(c.Context(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ //Parse upstream
+ _upstream, err := h.ParseRouteUpstream(c, route)
+
+ if err != nil {
+ log.Errorf("ParseRouteUpstream err: ", err)
+ return nil, err
+ } else if err == nil && _upstream != nil {
+ extensions["x-apisix-upstream"] = _upstream
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = ParseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // Parse Route URIs
+ paths, paramsRefs = ParseRouteUris(route, paths, paramsRefs, pathItem)
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = ParseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ pathItem.Get = ParsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ pathItem.Post = ParsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ pathItem.Put = ParsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ pathItem.Delete = ParsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ pathItem.Patch = ParsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ pathItem.Head = ParsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+//ParseLabels When service and route have labels at the same time, use route's label.
+//When route has no label, service sometimes uses service's label. This function is used to process this logic
+func ParseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
+ if route.Labels != nil {
+ return route.Labels, nil
+ } else if route.Labels == nil && route.ServiceID != nil {
+ if serviceLabels != nil {
+ return serviceLabels, nil
+ } else if serviceLabels == nil {
+ return nil, nil
+ }
+ }
+ return nil, nil
+}
+
+//ParsePathItem Convert data in route to openapi3
+func ParsePathItem(path openapi3.Operation, routeMethod string) *openapi3.Operation {
+ _path := &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + routeMethod,
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ return _path
+}
+
+// ParseRoutePlugins Merge service with plugin in route
+func ParseRoutePlugins(route *entity.Route, paramsRefs []*openapi3.ParameterRef, plugins map[string]interface{}, path openapi3.Operation, servicePlugins map[string]interface{}, secSchemas openapi3.SecuritySchemes, requestBody *openapi3.RequestBody) (openapi3.Operation, openapi3.SecuritySchemes, []*openapi3.ParameterRef, map[string]interface{}, error) {
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
Review comment:
done
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (2cb620e) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/8852e630f0c2890d6607ab56925f3c0aaf6144d8?el=desc) (8852e63) will **decrease** coverage by `16.30%`.
> The diff coverage is `64.75%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
===========================================
- Coverage 62.75% 46.44% -16.31%
===========================================
Files 41 34 -7
Lines 2360 2237 -123
===========================================
- Hits 1481 1039 -442
- Misses 703 1063 +360
+ Partials 176 135 -41
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (-62.50%)` | :arrow_down: |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `64.75% <64.75%> (ø)` | |
| [api/internal/filter/request\_id.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9yZXF1ZXN0X2lkLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `0.00% <0.00%> (-70.41%)` | :arrow_down: |
| [api/internal/filter/cors.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9jb3JzLmdv) | `0.00% <0.00%> (-66.67%)` | :arrow_down: |
| [api/internal/filter/schema.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9zY2hlbWEuZ28=) | `0.00% <0.00%> (-55.47%)` | :arrow_down: |
| [api/internal/handler/server\_info/server\_info.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc2VydmVyX2luZm8vc2VydmVyX2luZm8uZ28=) | `57.14% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/utils/closer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2Nsb3Nlci5nbw==) | `0.00% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/handler/consumer/consumer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvY29uc3VtZXIvY29uc3VtZXIuZ28=) | `63.82% <0.00%> (-27.66%)` | :arrow_down: |
| ... and [26 more](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [8852e63...fbf404a](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r561519973
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
Review comment:
it would be better to change the test case desc
```suggestion
Desc: "create route with uris and hosts",
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766539166
@liuxiran @nic-chen
The unit test and test cases have been supplemented. In the process of testing, I found a problem:
- At present, the path data structure of OpenAPI 3 is used to store URIs, and the data structure is map [string]. If the URI is repeated, the data will be covered, if the repeated URIs are directly covered, only one URI can be exported, which will lead to the loss of user data.
- My idea is to add a uniform suffix after the same URI when exporting, so that the import can recognize the URI
For example:
```
{
"components": {},
"info": {
"title": "RoutesExport",
"version": "3.0.0"
},
"openapi": "3.0.0",
"paths": {
"/hello": {
"get": {}
}
"/hello-repeaturi1": {
"get": {}
}
"/hello-repeaturi2": {
"get": {}
}
}
}
```
- My method also has the risk of repeating the user named URI
- Do you have any better suggestions? We can discuss them. Thks.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766645128
> not sure how other platforms would resolve those issues, we should have a search.
I tried Aliyun API Gateway and eolinker( a API management platform):
- In Aliyun API Gateway, when we export multiple APIs, it will split each API into a single file(It's also a good choice)
- In eolinker, it does not support export OpenAPI.
As most people choose option A, let's solve the problem with A first, after this feature megerd and released, let's wait for more users feedback ^_^
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r556553464
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,365 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-serviceProtocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+ plugins := make(map[string]interface{})
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for _, va := range route.Vars.([]interface{}) {
+ compile := regexp.MustCompile("^http_.*")
+ match := compile.Match([]byte(va.([]interface{})[0].(string)))
+ if match {
+ m[va.([]interface{})[2].(string)] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ }
+ }
+ continue
+ }
+ // analysis security plugins
+ securityEnv := &openapi3.SecurityRequirement{}
+ switch key {
+ case string(KeyAuth):
+ secSchemas["api_key"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewCSRFSecurityScheme()}
+ securityEnv.Authenticate("api_key", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(BasicAuth):
+ secSchemas["basicAuth"] = &openapi3.SecuritySchemeRef{Value: &openapi3.SecurityScheme{
+ Type: "basicAuth",
+ Name: "basicAuth",
+ In: "header",
+ }}
+ securityEnv.Authenticate("basicAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(JWTAuth):
+ secSchemas["bearerAuth"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewJWTSecurityScheme()}
+ securityEnv.Authenticate("bearerAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ }
+ plugins[key] = value
+ }
+ path.Security = secReq
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enableWebsocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case "GET":
+ //pathItem.Get = path
+ pathItem.Get = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Get",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+
+ case "POST":
+ //pathItem.Post = path
+ pathItem.Post = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Post",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "PUT":
+ //pathItem.Put = path
+ pathItem.Put = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Put",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "DELETE":
+ //pathItem.Delete = path
+ pathItem.Delete = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Delete",
+ Parameters: path.Parameters,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "PATCH":
+ //pathItem.Patch = path
+ pathItem.Patch = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Patch",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "HEAD":
+ //pathItem.Head = path
+ pathItem.Head = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Head",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ }
+ }
+ }
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger
+}
Review comment:
Tomorrow I will split it into several methods
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r556544370
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,365 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-serviceProtocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+ plugins := make(map[string]interface{})
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for _, va := range route.Vars.([]interface{}) {
+ compile := regexp.MustCompile("^http_.*")
+ match := compile.Match([]byte(va.([]interface{})[0].(string)))
+ if match {
+ m[va.([]interface{})[2].(string)] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ }
+ }
+ continue
+ }
+ // analysis security plugins
+ securityEnv := &openapi3.SecurityRequirement{}
+ switch key {
+ case string(KeyAuth):
+ secSchemas["api_key"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewCSRFSecurityScheme()}
+ securityEnv.Authenticate("api_key", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(BasicAuth):
+ secSchemas["basicAuth"] = &openapi3.SecuritySchemeRef{Value: &openapi3.SecurityScheme{
+ Type: "basicAuth",
+ Name: "basicAuth",
+ In: "header",
+ }}
+ securityEnv.Authenticate("basicAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(JWTAuth):
+ secSchemas["bearerAuth"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewJWTSecurityScheme()}
+ securityEnv.Authenticate("bearerAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ }
+ plugins[key] = value
+ }
+ path.Security = secReq
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enableWebsocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case "GET":
+ //pathItem.Get = path
+ pathItem.Get = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Get",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+
+ case "POST":
+ //pathItem.Post = path
+ pathItem.Post = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Post",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "PUT":
+ //pathItem.Put = path
+ pathItem.Put = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Put",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "DELETE":
+ //pathItem.Delete = path
+ pathItem.Delete = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Delete",
+ Parameters: path.Parameters,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "PATCH":
+ //pathItem.Patch = path
+ pathItem.Patch = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Patch",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "HEAD":
+ //pathItem.Head = path
+ pathItem.Head = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Head",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ }
+ }
+ }
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger
+}
Review comment:
Yes, new parsing content has been added today, and there is no time to separate functions
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766587292
> @liuxiran @nic-chen
> The unit test and test cases have been supplemented. In the process of testing, I found a problem:
>
> * At present, the path data structure of OpenAPI 3 is used to store URIs, and the data structure is map [string]. If the URI is repeated, the data will be covered, if the repeated URIs are directly covered, only one URI can be exported, which will lead to the loss of user data.
> * My idea is to add a uniform suffix after the same URI when exporting, so that the import can recognize the URI
> For example:
>
> ```
> {
> "components": {},
> "info": {
> "title": "RoutesExport",
> "version": "3.0.0"
> },
> "openapi": "3.0.0",
> "paths": {
> "/hello": {
> "get": {}
> }
> "/hello-repeaturi1": {
> "get": {}
> }
> "/hello-repeaturi2": {
> "get": {}
> }
> }
> }
> ```
>
> * My method also has the risk of repeating the user named URI
> * Do you have any better suggestions? We can discuss them. Thks.
>OpenAPI defines a unique operation as a combination of a path and an HTTP method. This means that two GET or two POST methods for the same path are not allowed – even if they have different parameters (parameters have no effect on uniqueness)
refer from https://swagger.io/docs/specification/paths-and-operations/
After communicating with @Jaycean and @nic-chen there are three alternative solutions:
- A: add a uniform suffix after the same URI when exporting, and add a description to the user guide.
This solution can meet the platform-wide data is not lost, the disadvantage is that when users need to use the swagger file to generate documents or sdk, they need to modified manually.
- B: return error when exporting routes with same URI
In this way exported OAS3.0 file can be used directly, with the disadvantage that there may be data loss for APISIX. The remedy is that we provide additional import and export of platform-wide data, in accordance with the apisix data format.
Neither scheme is perfect though, which one do you perfer? any suggestions are welcome, thanks a lot
@Jaycean @nic-chen @membphis @starsz @juzhiyuan @imjoey
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766587292
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765308727
checklist:
- [x] export route with host **(test2 coverd)**
- [x] export route with hosts **(test1 coverd)**
- [x] export route with uri **(test2 coverd)**
- [x] export route with uris **(test1 coverd)**
- [x] export route with upstream
- [x] export route with upstream_id **(test8 coverd)**
- [x] export route with service_id(service create with upstream) **(test4 coverd)**
- [x] export route with upstream and service_id **(test6 coverd)**
- [x] export route with service_id(service create with upstream_id)
- [ ] export route with labels and service_id(service create with labels)
- [ ] export route without labels and service_id(service create with labels)
- [x] export route with plugins and service_id(service create with plugins which is not the same as route's plugins) **(test5 coverd)**
- [x] export route with basic-auth plugin
- [x] export route with jwt-auth plugin
- [x] export route with key-auth plugin
- [x] export route with request-validation (body_schema and header_schema) plugin
- [x] export two routes **(test3 coverd)**
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r564543770
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+//ExportRoutes Export data by passing route ID, such as "R1" or multiple route parameters, such as "R1,R2"
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(c.Context(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+//ExportAllRoutes All routes can be directly exported without passing parameters
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+//RouteToOpenAPI3 Pass in route list parameter: []*entity.Route, convert route data to openapi3 and export processing function
+func (h *Handler) RouteToOpenAPI3(c droplet.Context, routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(c.Context(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ //Parse upstream
+ _upstream, err := h.ParseRouteUpstream(c, route)
+
+ if err != nil {
+ log.Errorf("ParseRouteUpstream err: ", err)
+ return nil, err
+ } else if err == nil && _upstream != nil {
+ extensions["x-apisix-upstream"] = _upstream
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = ParseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // Parse Route URIs
+ paths, paramsRefs = ParseRouteUris(route, paths, paramsRefs, pathItem)
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = ParseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ pathItem.Get = ParsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ pathItem.Post = ParsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ pathItem.Put = ParsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ pathItem.Delete = ParsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ pathItem.Patch = ParsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ pathItem.Head = ParsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+//ParseLabels When service and route have labels at the same time, use route's label.
+//When route has no label, service sometimes uses service's label. This function is used to process this logic
+func ParseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
+ if route.Labels != nil {
+ return route.Labels, nil
+ } else if route.Labels == nil && route.ServiceID != nil {
+ if serviceLabels != nil {
+ return serviceLabels, nil
+ } else if serviceLabels == nil {
+ return nil, nil
+ }
+ }
+ return nil, nil
+}
+
+//ParsePathItem Convert data in route to openapi3
+func ParsePathItem(path openapi3.Operation, routeMethod string) *openapi3.Operation {
+ _path := &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + routeMethod,
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ return _path
+}
+
+// ParseRoutePlugins Merge service with plugin in route
+func ParseRoutePlugins(route *entity.Route, paramsRefs []*openapi3.ParameterRef, plugins map[string]interface{}, path openapi3.Operation, servicePlugins map[string]interface{}, secSchemas openapi3.SecuritySchemes, requestBody *openapi3.RequestBody) (openapi3.Operation, openapi3.SecuritySchemes, []*openapi3.ParameterRef, map[string]interface{}, error) {
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+
+ m["*/*"] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ continue
+ }
+ // analysis security plugins
+ securityEnv := &openapi3.SecurityRequirement{}
+ switch key {
+ case string(KeyAuth):
+ secSchemas["api_key"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewCSRFSecurityScheme()}
+ securityEnv.Authenticate("api_key", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(BasicAuth):
+ secSchemas["basicAuth"] = &openapi3.SecuritySchemeRef{Value: &openapi3.SecurityScheme{
+ Type: "basicAuth",
+ Name: "basicAuth",
+ In: "header",
+ }}
+ securityEnv.Authenticate("basicAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(JWTAuth):
+ secSchemas["bearerAuth"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewJWTSecurityScheme()}
+ securityEnv.Authenticate("bearerAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ }
+ plugins[key] = value
+ }
+ path.Security = secReq
+
+ if route.ServiceID != nil && servicePlugins != nil {
+ _servicePlugins, err := json.Marshal(servicePlugins)
+ if err != nil {
+ log.Errorf("MapToJson err: ", err)
+ return path, nil, nil, nil, err
+ }
+ _plugins, err := json.Marshal(plugins)
+ if err != nil {
+ log.Errorf("MapToJson err: ", err)
+ return path, nil, nil, nil, err
+ }
+ bytePlugins, err := utils.MergeJson(_servicePlugins, _plugins)
+ if err != nil {
+ log.Errorf("Plugins MergeJson err: ", err)
+ return path, nil, nil, nil, err
+ }
+ err = json.Unmarshal([]byte(bytePlugins), &plugins)
+ if err != nil {
+ log.Errorf("JsonToMapDemo err: ", err)
+ return path, nil, nil, nil, err
+ }
+ }
+ } else if route.Plugins == nil && route.ServiceID != nil {
+ plugins = servicePlugins
+ }
+ return path, secSchemas, paramsRefs, plugins, nil
+}
+
+// ParseRouteUris The URI and URIs of route are converted to paths URI in openapi3
+func ParseRouteUris(route *entity.Route, paths openapi3.Paths, paramsRefs []*openapi3.ParameterRef, pathItem *openapi3.PathItem) (openapi3.Paths, []*openapi3.ParameterRef) {
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ _time := time.Now().UnixNano() / 1e6
+ if strings.Contains(uri, "*") {
+ if _, ok := paths[strings.Split(uri, "*")[0]+"{params}"]; !ok {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ } else {
+ paths[strings.Split(uri, "*")[0]+"{params}"+"-APISIX-REPEAT-URI-"+strconv.FormatInt(_time, 10)] = pathItem
+ }
Review comment:
Here I add a global variable, which is initialized to 0 every time I call the interface. In the loop, if it meets the conditions, it will automatically increase to 1. I don't know if this is OK
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (3dbb918) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/bb0a0575f9207fc57aacf79ab5f9defb4cb06166?el=desc) (bb0a057) will **decrease** coverage by `16.92%`.
> The diff coverage is `55.83%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
===========================================
- Coverage 62.50% 45.57% -16.93%
===========================================
Files 41 33 -8
Lines 2360 2201 -159
===========================================
- Hits 1475 1003 -472
- Misses 711 1066 +355
+ Partials 174 132 -42
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (-62.50%)` | :arrow_down: |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `55.83% <55.83%> (ø)` | |
| [api/internal/filter/request\_id.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9yZXF1ZXN0X2lkLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `0.00% <0.00%> (-70.41%)` | :arrow_down: |
| [api/internal/filter/cors.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9jb3JzLmdv) | `0.00% <0.00%> (-66.67%)` | :arrow_down: |
| [api/internal/filter/schema.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9zY2hlbWEuZ28=) | `0.00% <0.00%> (-55.47%)` | :arrow_down: |
| [api/internal/handler/server\_info/server\_info.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc2VydmVyX2luZm8vc2VydmVyX2luZm8uZ28=) | `57.14% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/utils/closer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2Nsb3Nlci5nbw==) | `0.00% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/handler/consumer/consumer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvY29uc3VtZXIvY29uc3VtZXIuZ28=) | `63.82% <0.00%> (-27.66%)` | :arrow_down: |
| ... and [25 more](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [bb0a057...3dbb918](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (dff4ef0) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/cdab902f7bd710feb1fc8e7cae3910482007e5d3?el=desc) (cdab902) will **not change** coverage.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
=======================================
Coverage 42.44% 42.44%
=======================================
Files 31 31
Lines 1932 1932
=======================================
Hits 820 820
Misses 1001 1001
Partials 111 111
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (ø)` | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [cdab902...dff4ef0](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (217105a) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/ab5b1d94582fab0267d134ef342d19fde6114554?el=desc) (ab5b1d9) will **increase** coverage by `0.36%`.
> The diff coverage is `69.42%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
==========================================
+ Coverage 63.05% 63.41% +0.36%
==========================================
Files 41 42 +1
Lines 2360 2602 +242
==========================================
+ Hits 1488 1650 +162
- Misses 697 753 +56
- Partials 175 199 +24
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `62.50% <ø> (ø)` | |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `69.29% <69.29%> (ø)` | |
| [api/internal/route.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3JvdXRlLmdv) | `84.37% <100.00%> (+0.50%)` | :arrow_up: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `70.40% <0.00%> (-4.09%)` | :arrow_down: |
| [api/internal/core/storage/etcd.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmFnZS9ldGNkLmdv) | `50.50% <0.00%> (-2.03%)` | :arrow_down: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [ab5b1d9...217105a](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] membphis commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
membphis commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-768037066
wow, Congratulations this PR was merged
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-764207165
it would be better to add three more test cases:
- export a route created with upstream id
- export a route created with service which is created with upstream id
- export a route enabled `basic-auth` plugin
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757631777
> > > Hi, @Jaycean. Need unit test.
> >
> >
> > At present, the function interface needs to export the existing route data and ensure the correctness of the exported data, so I think E2E should be written more perfectly. I think if the simple unit test only tests whether the interface can export data, it does not represent whether the function of the interface is correct, and if the relevant route data is not created, the interface will not work normally use.
>
> Not agree with you.
> First of all, unit testing is very important. It helps find bugs easily and quickly if someone changes the code.And unit testing can cover more code than e2e test.
> For example, in this case.
> We need to test these situations at least:
>
> 1. route can't found
> 2. Uris contain "*"
> 3. plugins with `request-validation`
> 4. different route method
> ...
>
> > and if the relevant route data is not created, the interface will not work normally use.
>
> We can mock data in etcd.
> See https://github.com/apache/apisix-dashboard/blob/master/api/internal/handler/server_info/server_info_test.go for more details.
Thank you for your advice. I also think unit testing is really necessary. I just thought that this interface test is almost the same as E2E, but unit testing should cover all situations as much as possible
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-764447946
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (c9f8292) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/b659dfc6aff50ffbe7553824575a61aa69cbc409?el=desc) (b659dfc) will **decrease** coverage by `0.52%`.
> The diff coverage is `55.37%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
==========================================
- Coverage 62.67% 62.14% -0.53%
==========================================
Files 41 42 +1
Lines 2352 2594 +242
==========================================
+ Hits 1474 1612 +138
- Misses 705 790 +85
- Partials 173 192 +19
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `62.50% <ø> (ø)` | |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `55.18% <55.18%> (ø)` | |
| [api/internal/route.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3JvdXRlLmdv) | `84.37% <100.00%> (+0.50%)` | :arrow_up: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `74.48% <0.00%> (+4.08%)` | :arrow_up: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [b659dfc...be327f1](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (0ee473d) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/1385fc200b15eff4ffee8fd4165e7ba414dcaba0?el=desc) (1385fc2) will **increase** coverage by `1.75%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
==========================================
+ Coverage 42.44% 44.19% +1.75%
==========================================
Files 31 31
Lines 1932 1948 +16
==========================================
+ Hits 820 861 +41
+ Misses 1001 975 -26
- Partials 111 112 +1
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (ø)` | |
| [api/internal/core/store/store.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmUuZ28=) | `79.50% <0.00%> (-0.50%)` | :arrow_down: |
| [api/internal/core/store/store\_mock.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVfbW9jay5nbw==) | `0.00% <0.00%> (ø)` | |
| [api/internal/handler/ssl/ssl.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc3NsL3NzbC5nbw==) | `29.60% <0.00%> (+0.39%)` | :arrow_up: |
| [api/internal/handler/upstream/upstream.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvdXBzdHJlYW0vdXBzdHJlYW0uZ28=) | `20.16% <0.00%> (+0.67%)` | :arrow_up: |
| [api/internal/handler/service/service.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc2VydmljZS9zZXJ2aWNlLmdv) | `27.83% <0.00%> (+0.75%)` | :arrow_up: |
| [api/internal/utils/utils.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL3V0aWxzLmdv) | `59.09% <0.00%> (+1.27%)` | :arrow_up: |
| [api/internal/handler/route/route.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvcm91dGUvcm91dGUuZ28=) | `46.49% <0.00%> (+3.05%)` | :arrow_up: |
| [api/internal/filter/authentication.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9hdXRoZW50aWNhdGlvbi5nbw==) | `76.47% <0.00%> (+76.47%)` | :arrow_up: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [1385fc2...0ee473d](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] starsz commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
starsz commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757592760
Hi, @Jaycean. Need unit test.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r563550845
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
Review comment:
done.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-764207165
it would be better to add two more test cases:
- export a route created with upstream id
- export a route created with service which is created with upstream id
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] juzhiyuan commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766602482
because it's using `URI` as paths's `Key`, then data gets covered would happen. I would prefer adding a global prefix to avoid this happen.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-764207165
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean removed a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean removed a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765076940
> > > Hi, @Jaycean. Need unit test.
> >
> >
> > At present, the function interface needs to export the existing route data and ensure the correctness of the exported data, so I think E2E should be written more perfectly. I think if the simple unit test only tests whether the interface can export data, it does not represent whether the function of the interface is correct, and if the relevant route data is not created, the interface will not work normally use.
>
> Not agree with you.
> First of all, unit testing is very important. It helps find bugs easily and quickly if someone changes the code.And unit testing can cover more code than e2e test.
> For example, in this case.
> We need to test these situations at least:
>
> 1. route can't found
> 2. Uris contain "*"
> 3. plugins with `request-validation`
> 4. different route method
> ...
>
> > and if the relevant route data is not created, the interface will not work normally use.
>
> We can mock data in etcd.
> See https://github.com/apache/apisix-dashboard/blob/master/api/internal/handler/server_info/server_info_test.go for more details.
@starsz Now I have encountered a problem in unit testing, exporting the data that needs to create route, upstream and service. After reading the example you gave me, I still don't know how to write it. Can you give me some advice.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765308727
checklist:
- [x] export route with host **(test2 coverd)**
- [x] export route with hosts **(test1 coverd)**
- [x] export route with uri **(test2 coverd)**
- [x] export route with uris **(test1 coverd)**
- [x] export route with upstream
- [x] export route with upstream_id **(test8 coverd)**
- [x] export route with service_id(service create with upstream) **(test4 coverd)**
- [x] export route with upstream and service_id **(test6 coverd)**
- [ ] export route with service_id(service create with upstream_id)
- [ ] export route with labels and service_id(service create with labels)
- [ ] export route without labels and service_id(service create with labels)
- [x] export route with plugins and service_id(service create with plugins which is not the same as route's plugins) **(test5 coverd)**
- [x] export route with basic-auth plugin
- [x] export route with jwt-auth plugin
- [x] export route with key-auth plugin
- [ ] export route with request-validation (body_schema and header_schema) plugin
- [x] export two routes **(test3 coverd)**
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765308727
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r561660169
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR1 + "}}",
+ },
+ }
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ exportStrR2 := `
+ "/hello2": {
+ "get": {
+ "operationId": "aaaa2Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaa2Post",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR2 = replaceStr(exportStrR2)
+
+ tests2 := []HttpTestCase{
+ {
+ Desc: "hit route2 that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r2",
+ Body: `{
+ "name": "aaaa2",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uri": "/hello2",
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "}}",
+ },
+ }
+ for _, tc := range tests2 {
+ testCaseCheck(tc, t)
+ }
+
+ tests3 := []HttpTestCase{
+ {
+ Desc: "export route route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1,r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "," + exportStrR1 + "}}",
+ },
+ {
+ Desc: "delete the route just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "bar.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "delete the route2 just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route2 just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "bar.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ }
+ for _, tc := range tests3 {
+ testCaseCheck(tc, t)
+ }
+
+ serviceStrS1 := `
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "enable_websocket": true
+ `
+ serviceStrS1 = replaceStr(serviceStrS1)
+
+ exportStrR3 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-serviceID": "s1",
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ exportStrR3 = replaceStr(exportStrR3)
+
+ // The test case tests the creation of a complete service.
+ // And according to the service_ ID to create a route.
+ // Test whether the plugin upstream label in the service is integrated with route and exported.
+ tests4 := []HttpTestCase{
+ {
+ Desc: "create service with all options",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ Body: `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "create_time":1602883670,
+ "update_time":1602893670,
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`,
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "get the service s1",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectCode: http.StatusOK,
+ ExpectBody: serviceStrS1,
+ },
+ {
+ Desc: "create route3 using the service just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r3",
+ Body: `{
+ "methods": ["GET"],
+ "uri": "/hello",
+ "service_id": "s1"
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "export route3",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r3",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"code\":0,\"message\":\"\",\"data\":" + exportStrR3,
+ },
+ {
+ Desc: "delete the route3 just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r3",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route3 just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello",
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "delete the service1",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ Sleep: sleepTime,
+ },
+ }
+ for _, tc := range tests4 {
+ testCaseCheck(tc, t)
+ }
+
+ serviceStrS2 := `
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "enable_websocket": true`
+ serviceStrS2 = replaceStr(serviceStrS2)
+
+ exportStrR4 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ },
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-serviceID": "s2",
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ exportStrR4 = replaceStr(exportStrR4)
+ // This test case tests the creation of a complete service.
+ // And create a complete route, export rules as route upstream data for high priority, direct use.
+ // However, if the data in the service (label, plugins) does not exist in the route, it will be fused and exported.
+ // If it exists, the data in route will be used first
+ tests5 := []HttpTestCase{
+ {
+ Desc: "create service with all options",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s2",
+ Headers: map[string]string{"Authorization": token},
+ Body: `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "create_time":1602883670,
+ "update_time":1602893670,
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`,
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "get the service s2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectCode: http.StatusOK,
+ ExpectBody: serviceStrS2,
+ },
+ {
+ Desc: "create route4 using the service just created",
Review comment:
done.
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR1 + "}}",
+ },
+ }
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ exportStrR2 := `
+ "/hello2": {
+ "get": {
+ "operationId": "aaaa2Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaa2Post",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR2 = replaceStr(exportStrR2)
+
+ tests2 := []HttpTestCase{
+ {
+ Desc: "hit route2 that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route2",
Review comment:
done.
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
Review comment:
done.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] starsz commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
starsz commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r555525580
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,365 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
Review comment:
style: redundant line.
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,365 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
Review comment:
style: redundant line.
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,365 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-serviceProtocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+ plugins := make(map[string]interface{})
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for _, va := range route.Vars.([]interface{}) {
+ compile := regexp.MustCompile("^http_.*")
+ match := compile.Match([]byte(va.([]interface{})[0].(string)))
+ if match {
+ m[va.([]interface{})[2].(string)] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ }
+ }
+ continue
+ }
+ // analysis security plugins
+ securityEnv := &openapi3.SecurityRequirement{}
+ switch key {
+ case string(KeyAuth):
+ secSchemas["api_key"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewCSRFSecurityScheme()}
+ securityEnv.Authenticate("api_key", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(BasicAuth):
+ secSchemas["basicAuth"] = &openapi3.SecuritySchemeRef{Value: &openapi3.SecurityScheme{
+ Type: "basicAuth",
+ Name: "basicAuth",
+ In: "header",
+ }}
+ securityEnv.Authenticate("basicAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(JWTAuth):
+ secSchemas["bearerAuth"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewJWTSecurityScheme()}
+ securityEnv.Authenticate("bearerAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ }
+ plugins[key] = value
+ }
+ path.Security = secReq
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enableWebsocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case "GET":
+ //pathItem.Get = path
+ pathItem.Get = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Get",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+
+ case "POST":
+ //pathItem.Post = path
+ pathItem.Post = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Post",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "PUT":
+ //pathItem.Put = path
+ pathItem.Put = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Put",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "DELETE":
+ //pathItem.Delete = path
+ pathItem.Delete = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Delete",
+ Parameters: path.Parameters,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "PATCH":
+ //pathItem.Patch = path
+ pathItem.Patch = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Patch",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "HEAD":
+ //pathItem.Head = path
+ pathItem.Head = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Head",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ }
+ }
+ }
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger
+}
Review comment:
Besides, I think this function is too long. It's better to split it into some small functions.
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,365 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-serviceProtocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+ plugins := make(map[string]interface{})
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for _, va := range route.Vars.([]interface{}) {
+ compile := regexp.MustCompile("^http_.*")
+ match := compile.Match([]byte(va.([]interface{})[0].(string)))
+ if match {
+ m[va.([]interface{})[2].(string)] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ }
+ }
+ continue
+ }
+ // analysis security plugins
+ securityEnv := &openapi3.SecurityRequirement{}
+ switch key {
+ case string(KeyAuth):
+ secSchemas["api_key"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewCSRFSecurityScheme()}
+ securityEnv.Authenticate("api_key", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(BasicAuth):
+ secSchemas["basicAuth"] = &openapi3.SecuritySchemeRef{Value: &openapi3.SecurityScheme{
+ Type: "basicAuth",
+ Name: "basicAuth",
+ In: "header",
+ }}
+ securityEnv.Authenticate("basicAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(JWTAuth):
+ secSchemas["bearerAuth"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewJWTSecurityScheme()}
+ securityEnv.Authenticate("bearerAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ }
+ plugins[key] = value
+ }
+ path.Security = secReq
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enableWebsocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
Review comment:
Style: this format looks like strange
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,192 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "\"data\":{\"components\":{},\"info\":{\"title\":\"Routes Export\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{\"/hello_\":{\"get\":{\"operationId\":\"aaaaGet\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}},\"post\":{\"operationId\":\"aaaaPost\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\"
:{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}}}}}",
+ },
+ }
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ tests2 := []HttpTestCase{
+ {
+ Desc: "hit route2 that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r2",
+ Body: `{
+ "name": "aaaa2",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uri": "/hello2",
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "\"data\":{\"components\":{},\"info\":{\"title\":\"Routes Export\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{\"/hello2\":{\"get\":{\"operationId\":\"aaaa2Get\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}},\"post\":{\"operationId\":\"aaaa2Post\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins
\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}}}}}",
+ },
+ }
+ for _, tc := range tests2 {
+ testCaseCheck(tc, t)
+ }
+
+ tests3 := []HttpTestCase{
+
+ {
+ Desc: "export route route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1,r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "\"data\":{\"components\":{},\"info\":{\"title\":\"Routes Export\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{\"/hello2\":{\"get\":{\"operationId\":\"aaaa2Get\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}},\"post\":{\"operationId\":\"aaaa2Post\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins
\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}}},\"/hello_\":{\"get\":{\"operationId\":\"aaaa2Get\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}},\"post\":{\"operationId\":\"aaaa2Post\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-ap
isix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}}}}}",
+ },
Review comment:
This line is too long.
I think we can you JSON.Marshal to generate the expectBody.
Like this
```
ecpectR1, _ := json.Marshal("....“)
expectR2, _:= json.Marshal("....")
...
ExpectBody: []string{string(expectR1), string(expectR2)},
```
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,365 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-serviceProtocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+ plugins := make(map[string]interface{})
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for _, va := range route.Vars.([]interface{}) {
+ compile := regexp.MustCompile("^http_.*")
+ match := compile.Match([]byte(va.([]interface{})[0].(string)))
+ if match {
+ m[va.([]interface{})[2].(string)] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ }
+ }
+ continue
+ }
+ // analysis security plugins
+ securityEnv := &openapi3.SecurityRequirement{}
+ switch key {
+ case string(KeyAuth):
+ secSchemas["api_key"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewCSRFSecurityScheme()}
+ securityEnv.Authenticate("api_key", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(BasicAuth):
+ secSchemas["basicAuth"] = &openapi3.SecuritySchemeRef{Value: &openapi3.SecurityScheme{
+ Type: "basicAuth",
+ Name: "basicAuth",
+ In: "header",
+ }}
+ securityEnv.Authenticate("basicAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(JWTAuth):
+ secSchemas["bearerAuth"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewJWTSecurityScheme()}
+ securityEnv.Authenticate("bearerAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ }
+ plugins[key] = value
+ }
+ path.Security = secReq
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enableWebsocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case "GET":
+ //pathItem.Get = path
+ pathItem.Get = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Get",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+
+ case "POST":
+ //pathItem.Post = path
+ pathItem.Post = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Post",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "PUT":
+ //pathItem.Put = path
+ pathItem.Put = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Put",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "DELETE":
+ //pathItem.Delete = path
+ pathItem.Delete = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Delete",
+ Parameters: path.Parameters,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "PATCH":
+ //pathItem.Patch = path
+ pathItem.Patch = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Patch",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "HEAD":
+ //pathItem.Head = path
+ pathItem.Head = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Head",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
Review comment:
Those codes are the same. Why not provide a function to do this?
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,192 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "\"data\":{\"components\":{},\"info\":{\"title\":\"Routes Export\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{\"/hello_\":{\"get\":{\"operationId\":\"aaaaGet\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}},\"post\":{\"operationId\":\"aaaaPost\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\"
:{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}}}}}",
+ },
+ }
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ tests2 := []HttpTestCase{
+ {
+ Desc: "hit route2 that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r2",
+ Body: `{
+ "name": "aaaa2",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uri": "/hello2",
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "\"data\":{\"components\":{},\"info\":{\"title\":\"Routes Export\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{\"/hello2\":{\"get\":{\"operationId\":\"aaaa2Get\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}},\"post\":{\"operationId\":\"aaaa2Post\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins
\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}}}}}",
+ },
+ }
+ for _, tc := range tests2 {
+ testCaseCheck(tc, t)
+ }
+
+ tests3 := []HttpTestCase{
+
Review comment:
style: redundant line.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r563493790
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
Review comment:
no problem.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (fbf404a) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/8852e630f0c2890d6607ab56925f3c0aaf6144d8?el=desc) (8852e63) will **decrease** coverage by `16.33%`.
> The diff coverage is `64.61%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
===========================================
- Coverage 62.75% 46.42% -16.34%
===========================================
Files 41 34 -7
Lines 2360 2236 -124
===========================================
- Hits 1481 1038 -443
- Misses 703 1063 +360
+ Partials 176 135 -41
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (-62.50%)` | :arrow_down: |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `64.61% <64.61%> (ø)` | |
| [api/internal/filter/request\_id.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9yZXF1ZXN0X2lkLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `0.00% <0.00%> (-70.41%)` | :arrow_down: |
| [api/internal/filter/cors.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9jb3JzLmdv) | `0.00% <0.00%> (-66.67%)` | :arrow_down: |
| [api/internal/filter/schema.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9zY2hlbWEuZ28=) | `0.00% <0.00%> (-55.47%)` | :arrow_down: |
| [api/internal/handler/server\_info/server\_info.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc2VydmVyX2luZm8vc2VydmVyX2luZm8uZ28=) | `57.14% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/utils/closer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2Nsb3Nlci5nbw==) | `0.00% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/handler/consumer/consumer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvY29uc3VtZXIvY29uc3VtZXIuZ28=) | `63.82% <0.00%> (-27.66%)` | :arrow_down: |
| ... and [26 more](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [8852e63...fbf404a](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765474916
> > > > > > Hi, @Jaycean. Need unit test.
> > > > >
> > > > >
> > > > > At present, the function interface needs to export the existing route data and ensure the correctness of the exported data, so I think E2E should be written more perfectly. I think if the simple unit test only tests whether the interface can export data, it does not represent whether the function of the interface is correct, and if the relevant route data is not created, the interface will not work normally use.
> > > >
> > > >
> > > > Not agree with you.
> > > > First of all, unit testing is very important. It helps find bugs easily and quickly if someone changes the code.And unit testing can cover more code than e2e test.
> > > > For example, in this case.
> > > > We need to test these situations at least:
> > > >
> > > > 1. route can't found
> > > > 2. Uris contain "*"
> > > > 3. plugins with `request-validation`
> > > > 4. different route method
> > > > ...
> > > >
> > > > > and if the relevant route data is not created, the interface will not work normally use.
> > > >
> > > >
> > > > We can mock data in etcd.
> > > > See https://github.com/apache/apisix-dashboard/blob/master/api/internal/handler/server_info/server_info_test.go for more details.
> > >
> > >
> > > @starsz Now I have encountered a problem in unit testing, exporting the data that needs to create route, upstream and service. After reading the example you gave me, I still don't know how to write it. Can you give me some advice. Thks
>
> OK. Sorry, we don't have a doc to introduce the test contribute doc yet.
>
> First, you should know the mock technology.
> You can reference the doc: https://github.com/stretchr/testify#mock-package.
> Mock provides a way to check input and give the return value that we want.
>
> In your code, we should mock those function:
>
> ```
> routeStore.Get
> routeStore.List
> upstreamStore.Get
> serviceStore.Get
> ```
>
> Second, IMO, you should write two testcases
> One test case for `ExportRoutes`, like:
> `TestDataLoader_ExportRoutes`
> And another test case for `ExportAllRoutes`.like
> `TestDataLoader_ExportAllRoutes`
>
> Third, in those test case function.We should mock the store like:
>
> ```
> routeMockStore := &store.MockInterface{}
> routeMockStore.On("Get", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
> id := args.Get(0).(string)
> // here we can do some check on the id.
> }).Return(tc.giveRet, tc.giveErr)
> ```
>
> Mock.Anything stands for the args when you called routeMockStore.Get.
> tc.giveRet and tc.giveErr means what you want to get when called this line:
> https://github.com/apache/apisix-dashboard/pull/1245/files#diff-195642dee38f4cdcc68921704b4b59dce5afc67d06074c1bc53004eb9b5dfc5eR70
> The same as other mock functions.
>
> Any feedback is welcome.
Thank you very much. I'll try to write it.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765077174
> > > > Hi, @Jaycean. Need unit test.
> > >
> > >
> > > At present, the function interface needs to export the existing route data and ensure the correctness of the exported data, so I think E2E should be written more perfectly. I think if the simple unit test only tests whether the interface can export data, it does not represent whether the function of the interface is correct, and if the relevant route data is not created, the interface will not work normally use.
> >
> >
> > Not agree with you.
> > First of all, unit testing is very important. It helps find bugs easily and quickly if someone changes the code.And unit testing can cover more code than e2e test.
> > For example, in this case.
> > We need to test these situations at least:
> >
> > 1. route can't found
> > 2. Uris contain "*"
> > 3. plugins with `request-validation`
> > 4. different route method
> > ...
> >
> > > and if the relevant route data is not created, the interface will not work normally use.
> >
> >
> > We can mock data in etcd.
> > See https://github.com/apache/apisix-dashboard/blob/master/api/internal/handler/server_info/server_info_test.go for more details.
>
> @starsz Now I have encountered a problem in unit testing, exporting the data that needs to create route, upstream and service. After reading the example you gave me, I still don't know how to write it. Can you give me some advice. Thks
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r563494403
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = parseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ //pathItem.Get = path
+ pathItem.Get = parsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ //pathItem.Post = path
+ pathItem.Post = parsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ //pathItem.Put = path
+ pathItem.Put = parsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ //pathItem.Delete = path
+ pathItem.Delete = parsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ //pathItem.Patch = path
+ pathItem.Patch = parsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ //pathItem.Head = path
+ pathItem.Head = parsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+func parseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
Review comment:
When service and route have labels at the same time, use route's label. When route has no label, service sometimes uses service's label. This function is used to process this logic
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765308727
checklist:
- [x] export route with host **(test2 coverd)**
- [x] export route with hosts **(test1 coverd)**
- [x] export route with uri **(test2 coverd)**
- [x] export route with uris **(test1 coverd)**
- [x] export route with upstream
- [x] export route with upstream_id **(test8 coverd)**
- [x] export route with service_id(service create with upstream) **(test4 coverd)**
- [x] export route with upstream and service_id **(test6 coverd)**
- [x] export route with service_id(service create with upstream_id) **(test6 covered)**
- [x] export route with labels and service_id(service create with labels)
- [ ] export route without labels and service_id(service create with labels)
- [x] export route with plugins and service_id(service create with plugins which is not the same as route's plugins) **(test5 coverd)**
- [x] export route with basic-auth plugin
- [x] export route with jwt-auth plugin
- [x] export route with key-auth plugin
- [x] export route with request-validation (body_schema and header_schema) plugin
- [x] export two routes **(test3 coverd)**
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-764447946
> it would be better to add three more test cases:
>
> * export a route created with upstream id
> * export a route created with service which is created with upstream id
> * export a route created with `basic-auth` plugin enabled
I have added test cases for these three kinds of exports. Auth authentication has added the route export test cases opened by JWT plugin, and the following basic auth and API key will also be added.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r554659754
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,357 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.URI != "" {
+ extensions["x-apisix-uri"] = route.URI
+ }
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-serviceProtocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+ // analysis route.URIs
+ for _, uri := range route.Uris {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+ plugins := make(map[string]interface{})
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ for k, v := range value.(map[string]interface{}) {
+ if k == "header_schema" {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&v)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+ if k == "body_schema" {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&v)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for _, va := range route.Vars.([]interface{}) {
+ compile := regexp.MustCompile("^http_.*")
+ match := compile.Match([]byte(va.([]interface{})[0].(string)))
+ if match {
+ m[va.([]interface{})[2].(string)] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ }
+ }
+ continue
+ }
Review comment:
Thks, I'll check and change the code right away.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean removed a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean removed a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765076940
> > > Hi, @Jaycean. Need unit test.
> >
> >
> > At present, the function interface needs to export the existing route data and ensure the correctness of the exported data, so I think E2E should be written more perfectly. I think if the simple unit test only tests whether the interface can export data, it does not represent whether the function of the interface is correct, and if the relevant route data is not created, the interface will not work normally use.
>
> Not agree with you.
> First of all, unit testing is very important. It helps find bugs easily and quickly if someone changes the code.And unit testing can cover more code than e2e test.
> For example, in this case.
> We need to test these situations at least:
>
> 1. route can't found
> 2. Uris contain "*"
> 3. plugins with `request-validation`
> 4. different route method
> ...
>
> > and if the relevant route data is not created, the interface will not work normally use.
>
> We can mock data in etcd.
> See https://github.com/apache/apisix-dashboard/blob/master/api/internal/handler/server_info/server_info_test.go for more details.
@starsz Now I have encountered a problem in unit testing, exporting the data that needs to create route, upstream and service. After reading the example you gave me, I still don't know how to write it. Can you give me some advice.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (b0b3c36) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/1385fc200b15eff4ffee8fd4165e7ba414dcaba0?el=desc) (1385fc2) will **not change** coverage.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
=======================================
Coverage 42.44% 42.44%
=======================================
Files 31 31
Lines 1932 1932
=======================================
Hits 820 820
Misses 1001 1001
Partials 111 111
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (ø)` | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [1385fc2...b0b3c36](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r555623183
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,365 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-serviceProtocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+ plugins := make(map[string]interface{})
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for _, va := range route.Vars.([]interface{}) {
+ compile := regexp.MustCompile("^http_.*")
+ match := compile.Match([]byte(va.([]interface{})[0].(string)))
+ if match {
+ m[va.([]interface{})[2].(string)] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ }
+ }
+ continue
+ }
+ // analysis security plugins
+ securityEnv := &openapi3.SecurityRequirement{}
+ switch key {
+ case string(KeyAuth):
+ secSchemas["api_key"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewCSRFSecurityScheme()}
+ securityEnv.Authenticate("api_key", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(BasicAuth):
+ secSchemas["basicAuth"] = &openapi3.SecuritySchemeRef{Value: &openapi3.SecurityScheme{
+ Type: "basicAuth",
+ Name: "basicAuth",
+ In: "header",
+ }}
+ securityEnv.Authenticate("basicAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(JWTAuth):
+ secSchemas["bearerAuth"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewJWTSecurityScheme()}
+ securityEnv.Authenticate("bearerAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ }
+ plugins[key] = value
+ }
+ path.Security = secReq
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enableWebsocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
Review comment:
The unified tab indentation used here may be that GitHub web pages look a lot indented
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r561537853
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR1 + "}}",
+ },
+ }
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ exportStrR2 := `
+ "/hello2": {
+ "get": {
+ "operationId": "aaaa2Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaa2Post",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR2 = replaceStr(exportStrR2)
+
+ tests2 := []HttpTestCase{
+ {
+ Desc: "hit route2 that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r2",
+ Body: `{
+ "name": "aaaa2",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uri": "/hello2",
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "}}",
+ },
+ }
+ for _, tc := range tests2 {
+ testCaseCheck(tc, t)
+ }
+
+ tests3 := []HttpTestCase{
+ {
+ Desc: "export route route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1,r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "," + exportStrR1 + "}}",
+ },
+ {
+ Desc: "delete the route just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "bar.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "delete the route2 just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route2 just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "bar.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ }
+ for _, tc := range tests3 {
+ testCaseCheck(tc, t)
+ }
+
+ serviceStrS1 := `
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "enable_websocket": true
+ `
+ serviceStrS1 = replaceStr(serviceStrS1)
+
+ exportStrR3 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-serviceID": "s1",
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ exportStrR3 = replaceStr(exportStrR3)
+
+ // The test case tests the creation of a complete service.
+ // And according to the service_ ID to create a route.
+ // Test whether the plugin upstream label in the service is integrated with route and exported.
+ tests4 := []HttpTestCase{
+ {
+ Desc: "create service with all options",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ Body: `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "create_time":1602883670,
+ "update_time":1602893670,
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`,
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "get the service s1",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectCode: http.StatusOK,
+ ExpectBody: serviceStrS1,
+ },
+ {
+ Desc: "create route3 using the service just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r3",
+ Body: `{
+ "methods": ["GET"],
+ "uri": "/hello",
+ "service_id": "s1"
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "export route3",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r3",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"code\":0,\"message\":\"\",\"data\":" + exportStrR3,
+ },
+ {
+ Desc: "delete the route3 just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r3",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route3 just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello",
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "delete the service1",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ Sleep: sleepTime,
+ },
+ }
+ for _, tc := range tests4 {
+ testCaseCheck(tc, t)
+ }
+
+ serviceStrS2 := `
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "enable_websocket": true`
+ serviceStrS2 = replaceStr(serviceStrS2)
+
+ exportStrR4 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ },
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-serviceID": "s2",
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ exportStrR4 = replaceStr(exportStrR4)
+ // This test case tests the creation of a complete service.
+ // And create a complete route, export rules as route upstream data for high priority, direct use.
+ // However, if the data in the service (label, plugins) does not exist in the route, it will be fused and exported.
+ // If it exists, the data in route will be used first
+ tests5 := []HttpTestCase{
+ {
+ Desc: "create service with all options",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s2",
+ Headers: map[string]string{"Authorization": token},
+ Body: `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "create_time":1602883670,
+ "update_time":1602893670,
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`,
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "get the service s2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectCode: http.StatusOK,
+ ExpectBody: serviceStrS2,
+ },
+ {
+ Desc: "create route4 using the service just created",
Review comment:
you'd better update the desc too
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r555624491
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,192 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "\"data\":{\"components\":{},\"info\":{\"title\":\"Routes Export\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{\"/hello_\":{\"get\":{\"operationId\":\"aaaaGet\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}},\"post\":{\"operationId\":\"aaaaPost\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\"
:{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}}}}}",
+ },
+ }
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ tests2 := []HttpTestCase{
+ {
+ Desc: "hit route2 that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r2",
+ Body: `{
+ "name": "aaaa2",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uri": "/hello2",
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "\"data\":{\"components\":{},\"info\":{\"title\":\"Routes Export\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{\"/hello2\":{\"get\":{\"operationId\":\"aaaa2Get\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}},\"post\":{\"operationId\":\"aaaa2Post\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins
\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}}}}}",
+ },
+ }
+ for _, tc := range tests2 {
+ testCaseCheck(tc, t)
+ }
+
+ tests3 := []HttpTestCase{
+
+ {
+ Desc: "export route route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1,r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "\"data\":{\"components\":{},\"info\":{\"title\":\"Routes Export\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{\"/hello2\":{\"get\":{\"operationId\":\"aaaa2Get\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}},\"post\":{\"operationId\":\"aaaa2Post\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins
\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}}},\"/hello_\":{\"get\":{\"operationId\":\"aaaa2Get\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}},\"post\":{\"operationId\":\"aaaa2Post\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-ap
isix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}}}}}",
+ },
Review comment:
In this case, because it is to verify the exported data, in order to make the data cover as wide as possible, the resulting data is very large. I'll take a look at splitting them, so the readability is really better
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] starsz commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
starsz commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r564293578
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
Review comment:
Question:
1. Why the first API's method is `POST`
2. "/apisix/admin/exportall/routes" is not very good. cc @liuxiran
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766539166
@liuxiran @nic-chen
The unit test and test cases have been supplemented. In the process of testing, I found a problem:
- At present, the path data structure of OpenAPI 3 is used to store URIs, and the data structure is map [string]. If the URI is repeated, the data will be covered, if the repeated URIs are directly covered, only one URI can be exported, which will lead to the loss of user data.
- My idea is to add a uniform suffix after the same URI when exporting, so that the import can recognize the URI
For example:
```
{
"components": {},
"info": {
"title": "RoutesExport",
"version": "3.0.0"
},
"openapi": "3.0.0",
"paths": {
"/hello": {
"get": {}
}
"/hello-repeaturi2": {
"get": {}
}
"/hello-repeaturi2": {
"get": {}
}
}
}
```
- Do you have any better suggestions? We can discuss them. Thks.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] nic-chen commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
nic-chen commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766603524
I vote for option A, at least it can be imported and exported normally on APISIX platform.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r554653528
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,349 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "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/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+ "reflect"
+ "regexp"
+ "strings"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.URI != "" {
+ extensions["x-apisix-uri"] = route.URI
+ }
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-serviceProtocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+ // analysis route.URIs
+ for _, uri := range route.Uris {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+ plugins := make(map[string]interface{})
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ for k, v := range value.(map[string]interface{}) {
+ if k == "header_schema" {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&v)
+ err := json.Unmarshal(reqBytes, requestValidation)
Review comment:
Yes, I just added it
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r564543770
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+//ExportRoutes Export data by passing route ID, such as "R1" or multiple route parameters, such as "R1,R2"
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(c.Context(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+//ExportAllRoutes All routes can be directly exported without passing parameters
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+//RouteToOpenAPI3 Pass in route list parameter: []*entity.Route, convert route data to openapi3 and export processing function
+func (h *Handler) RouteToOpenAPI3(c droplet.Context, routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(c.Context(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ //Parse upstream
+ _upstream, err := h.ParseRouteUpstream(c, route)
+
+ if err != nil {
+ log.Errorf("ParseRouteUpstream err: ", err)
+ return nil, err
+ } else if err == nil && _upstream != nil {
+ extensions["x-apisix-upstream"] = _upstream
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = ParseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // Parse Route URIs
+ paths, paramsRefs = ParseRouteUris(route, paths, paramsRefs, pathItem)
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = ParseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ pathItem.Get = ParsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ pathItem.Post = ParsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ pathItem.Put = ParsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ pathItem.Delete = ParsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ pathItem.Patch = ParsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ pathItem.Head = ParsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+//ParseLabels When service and route have labels at the same time, use route's label.
+//When route has no label, service sometimes uses service's label. This function is used to process this logic
+func ParseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
+ if route.Labels != nil {
+ return route.Labels, nil
+ } else if route.Labels == nil && route.ServiceID != nil {
+ if serviceLabels != nil {
+ return serviceLabels, nil
+ } else if serviceLabels == nil {
+ return nil, nil
+ }
+ }
+ return nil, nil
+}
+
+//ParsePathItem Convert data in route to openapi3
+func ParsePathItem(path openapi3.Operation, routeMethod string) *openapi3.Operation {
+ _path := &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + routeMethod,
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ return _path
+}
+
+// ParseRoutePlugins Merge service with plugin in route
+func ParseRoutePlugins(route *entity.Route, paramsRefs []*openapi3.ParameterRef, plugins map[string]interface{}, path openapi3.Operation, servicePlugins map[string]interface{}, secSchemas openapi3.SecuritySchemes, requestBody *openapi3.RequestBody) (openapi3.Operation, openapi3.SecuritySchemes, []*openapi3.ParameterRef, map[string]interface{}, error) {
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+
+ m["*/*"] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ continue
+ }
+ // analysis security plugins
+ securityEnv := &openapi3.SecurityRequirement{}
+ switch key {
+ case string(KeyAuth):
+ secSchemas["api_key"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewCSRFSecurityScheme()}
+ securityEnv.Authenticate("api_key", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(BasicAuth):
+ secSchemas["basicAuth"] = &openapi3.SecuritySchemeRef{Value: &openapi3.SecurityScheme{
+ Type: "basicAuth",
+ Name: "basicAuth",
+ In: "header",
+ }}
+ securityEnv.Authenticate("basicAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(JWTAuth):
+ secSchemas["bearerAuth"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewJWTSecurityScheme()}
+ securityEnv.Authenticate("bearerAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ }
+ plugins[key] = value
+ }
+ path.Security = secReq
+
+ if route.ServiceID != nil && servicePlugins != nil {
+ _servicePlugins, err := json.Marshal(servicePlugins)
+ if err != nil {
+ log.Errorf("MapToJson err: ", err)
+ return path, nil, nil, nil, err
+ }
+ _plugins, err := json.Marshal(plugins)
+ if err != nil {
+ log.Errorf("MapToJson err: ", err)
+ return path, nil, nil, nil, err
+ }
+ bytePlugins, err := utils.MergeJson(_servicePlugins, _plugins)
+ if err != nil {
+ log.Errorf("Plugins MergeJson err: ", err)
+ return path, nil, nil, nil, err
+ }
+ err = json.Unmarshal([]byte(bytePlugins), &plugins)
+ if err != nil {
+ log.Errorf("JsonToMapDemo err: ", err)
+ return path, nil, nil, nil, err
+ }
+ }
+ } else if route.Plugins == nil && route.ServiceID != nil {
+ plugins = servicePlugins
+ }
+ return path, secSchemas, paramsRefs, plugins, nil
+}
+
+// ParseRouteUris The URI and URIs of route are converted to paths URI in openapi3
+func ParseRouteUris(route *entity.Route, paths openapi3.Paths, paramsRefs []*openapi3.ParameterRef, pathItem *openapi3.PathItem) (openapi3.Paths, []*openapi3.ParameterRef) {
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ _time := time.Now().UnixNano() / 1e6
+ if strings.Contains(uri, "*") {
+ if _, ok := paths[strings.Split(uri, "*")[0]+"{params}"]; !ok {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ } else {
+ paths[strings.Split(uri, "*")[0]+"{params}"+"-APISIX-REPEAT-URI-"+strconv.FormatInt(_time, 10)] = pathItem
+ }
Review comment:
Here I add a global variable, which is initialized to 0 every time I call the interface. In the loop, if it meets the conditions, it will automatically increase to 1. I don't know if this is OK
##########
File path: api/internal/handler/data_loader/route_export_test.go
##########
@@ -0,0 +1,1864 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "strings"
+ "testing"
+
+ "github.com/apisix/manager-api/internal/core/entity"
+ "github.com/apisix/manager-api/internal/core/store"
+ "github.com/shiningrush/droplet"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/mock"
+)
+
+// 1.Export data as the route of URIs Hosts
+func TestExportRoutes1(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ //*entity.Route
+ r1 := `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`
+
+ exportR1 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enable_websocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPOST",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enable_websocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ err := json.Unmarshal([]byte(r1), &route)
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ h := Handler{routeStore: mStore}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ ret1, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR1), string(ret1))
+ assert.NotNil(t, ret1)
+}
+
+// 2.Export data as the route of URI host
+func TestExportRoutes2(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ //*entity.Route
+ r2 := `{
+ "name": "aaaa2",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uri": "/hello2",
+ "host": "*.bar.com",
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`
+
+ exportR2 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello2": {
+ "get": {
+ "operationId": "aaaa2GET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enable_websocket": false,
+ "x-apisix-host": "*.bar.com",
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaa2POST",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enable_websocket": false,
+ "x-apisix-host": "*.bar.com",
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ err := json.Unmarshal([]byte(r2), &route)
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ h := Handler{routeStore: mStore}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ ret1, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR2), string(ret1))
+ assert.NotNil(t, ret1)
+}
+
+// 3.Create a service that contains complete data and use the service_id create route
+func TestExportRoutesCreateByServiceId(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ //*entity.Route
+ r := `{
+ "methods": ["GET"],
+ "uri": "/hello",
+ "service_id": "s1"
+ }`
+
+ s := `{
+ "id": "s1",
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "GET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 0,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ h := Handler{routeStore: mStore, serviceStore: mStoreService}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 4.Create a service containing plugin and a route containing plugin to test the fusion of exported data
+func TestExportRoutesCreateByServiceId2(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ //*entity.Route
+ r := `{
+ "methods": ["GET"],
+ "uri": "/hello",
+ "service_id": "s1",
+ "enable_websocket":false,
+ "plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`
+
+ s := `{
+ "id": "s1",
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "GET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ },
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 0,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ h := Handler{routeStore: mStore, serviceStore: mStoreService}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 5.Create a service according to the upstream ID and a route according to the service ID
+func TestExportRoutesCreateByServiceId3(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ us := `{
+ "id": "u1",
+ "nodes": [
+ {
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }
+ ],
+ "type": "roundrobin"
+ }`
+
+ s := `{
+ "id": "s1",
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream_id": "u1"
+ }`
+
+ r := `{
+ "methods": ["GET"],
+ "uri": "/hello",
+ "service_id": "s1",
+ "enable_websocket":false,
+ "plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1981,
+ "weight": 1
+ }]
+ }
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "GET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ },
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 0,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1981,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ var upstream *entity.Upstream
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+ err = json.Unmarshal([]byte(us), &upstream)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ mStoreUpstream := &store.MockInterface{}
+ mStoreUpstream.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(upstream, nil)
+
+ h := Handler{routeStore: mStore, serviceStore: mStoreService, upstreamStore: mStoreUpstream}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 6.Create and export route according to upstream ID
+func TestExportRoutesCreateByUpstreamId(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ us := `{
+ "id": "u1",
+ "nodes": [
+ {
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }
+ ],
+ "type": "roundrobin"
+ }`
+
+ r := `{
+ "methods": ["GET"],
+ "uri": "/hello",
+ "enable_websocket":false,
+ "plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "upstream_id": "u1"
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "GET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enable_websocket": false,
+ "x-apisix-plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 0,
+ "x-apisix-upstream": {
+ "id": "u1",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var upstream *entity.Upstream
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(us), &upstream)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreUpstream := &store.MockInterface{}
+ mStoreUpstream.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(upstream, nil)
+
+ h := Handler{routeStore: mStore, upstreamStore: mStoreUpstream}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 7.Create route according to upstream ID and service ID
+func TestExportRoutesCreateByUpstreamIdandServiceId(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ us := `{
+ "id": "u1",
+ "nodes": [
+ {
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }
+ ],
+ "type": "roundrobin"
+ }`
+
+ s := `{
+ "id": "s1",
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream_id": "u1"
+ }`
+
+ r := `{
+ "name": "route_all",
+ "desc": "所有",
+ "status": 1,
+ "methods": ["GET"],
+ "priority": 0,
+ "service_id": "s1",
+ "labels": {
+ "test": "1",
+ "API_VERSION": "v1"
+ },
+ "vars": [
+ ["arg_name", "==", "test"]
+ ],
+ "uri": "/hello",
+ "enable_websocket":false,
+ "plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "upstream_id": "u1"
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "route_allGET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "summary": "所有",
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "API_VERSION": "v1",
+ "test": "1"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ },
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "id": "u1",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "x-apisix-vars": [
+ ["arg_name", "==", "test"]
+ ]
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ var upstream *entity.Upstream
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+ err = json.Unmarshal([]byte(us), &upstream)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreUpstream := &store.MockInterface{}
+ mStoreUpstream.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(upstream, nil)
+
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ h := Handler{routeStore: mStore, upstreamStore: mStoreUpstream, serviceStore: mStoreService}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 8.Creating route using service ID does not contain upstream data
+func TestExportRoutesCreateByServiceIdNoUpstream(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ us := `{
+ "id": "u1",
+ "nodes": [
+ {
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }
+ ],
+ "type": "roundrobin"
+ }`
+
+ s := `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "enable_websocket":true,
+ "upstream_id": "6"
+ }`
+
+ r := `{
+ "name": "route_all",
+ "desc": "所有",
+ "status": 1,
+ "methods": ["GET"],
+ "priority": 0,
+ "service_id": "s5",
+ "labels": {
+ "test": "1",
+ "API_VERSION": "v1"
+ },
+ "vars": [
+ ["arg_name", "==", "test"]
+ ],
+ "uri": "/hello",
+ "enable_websocket":false,
+ "plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ }
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "route_allGET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "summary": "所有",
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "API_VERSION": "v1",
+ "test": "1"
+ },
+ "x-apisix-plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "id": "u1",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "x-apisix-vars": [
+ ["arg_name", "==", "test"]
+ ]
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ var upstream *entity.Upstream
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+ err = json.Unmarshal([]byte(us), &upstream)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreUpstream := &store.MockInterface{}
+ mStoreUpstream.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(upstream, nil)
+
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ h := Handler{routeStore: mStore, upstreamStore: mStoreUpstream, serviceStore: mStoreService}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 9.Create a service with label data and a route with label data, and export the route.
+// Label is the original data of the route
+func TestExportRoutesCreateByLabel(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ s := `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "enable_websocket":true,
+ "labels": {
+ "build": "10"
+ }
+ }`
+
+ r := `{
+ "name": "route_all",
+ "desc": "所有",
+ "status": 1,
+ "methods": ["GET"],
+ "service_id": "s1",
+ "labels": {
+ "test": "1",
+ "API_VERSION": "v1"
+ },
+ "uri": "/hello",
+ "enable_websocket":false,
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "route_allGET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "summary": "所有",
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "API_VERSION": "v1",
+ "test": "1"
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ h := Handler{routeStore: mStore, serviceStore: mStoreService}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 10.Create a service with label data and a route without label data, and export the route.
+// Label is the data of the service
+func TestExportRoutesCreateByLabel2(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ s := `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "enable_websocket":true,
+ "labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ }
+ }`
+
+ r := `{
+ "name": "route_all",
+ "desc": "所有",
+ "status": 1,
+ "methods": ["GET"],
+ "service_id": "s2",
+ "vars": [
+ ["arg_name", "==", "test"]
+ ],
+ "uri": "/hello"
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "route_allGET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "summary": "所有",
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-vars": [
+ ["arg_name", "==", "test"]
+ ]
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ h := Handler{routeStore: mStore, serviceStore: mStoreService}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 11.Test export route request_ validation data correctness
+func TestExportRoutesCreateByRequestValidation(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ r := `{
+ "uris": ["/test-test"],
+ "name": "route_all",
+ "desc": "所有",
+ "methods": ["GET"],
+ "hosts": ["test.com"],
+ "plugins": {
+ "request-validation": {
+ "body_schema": {
+ "properties": {
+ "boolean_payload": {
+ "type": "boolean"
+ },
+ "required_payload": {
+ "type": "string"
+ }
+ },
+ "required": ["required_payload"],
+ "type": "object"
+ },
+ "disable": false,
+ "header_schema": {
+ "properties": {
+ "test": {
+ "enum": "test-enum",
+ "type": "string"
+ }
+ },
+ "type": "string"
+ }
+ }
+ },
+ "status": 1
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/test-test": {
+ "get": {
+ "operationId": "route_allGET",
+ "parameters": [{
+ "in": "header",
+ "name": "test",
+ "schema": {
+ "type": "string"
+ }
+ }],
+ "requestBody": {
+ "content": {
+ "*/*": {
+ "schema": {
+ "properties": {
+ "boolean_payload": {
+ "type": "boolean"
+ },
+ "required_payload": {
+ "type": "string"
+ }
+ },
+ "required": ["required_payload"],
+ "type": "object"
+ }
+ }
+ }
+ },
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "summary": "所有",
+ "x-apisix-enable_websocket": false,
+ "x-apisix-hosts": ["test.com"],
+ "x-apisix-plugins": {},
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ err := json.Unmarshal([]byte(r), &route)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ h := Handler{routeStore: mStore}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 12.Export route create by jwt-auth plugin
+func TestExportRoutesCreateByJWTAuth(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ r := `{
+ "uri": "/hello",
+ "plugins": {
+ "jwt-auth": {}
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`
+
+ c := `{
+ "username": "jack",
+ "plugins": {
+ "jwt-auth": {
+ "key": "user-key",
+ "secret": "my-secret-key",
+ "algorithm": "HS256"
+ }
+ },
+ "desc": "test description"
+ }`
+
+ exportR := `{
+ "components": {
+ "securitySchemes": {
+ "bearerAuth": {
+ "bearerFormat": "JWT",
+ "scheme": "bearer",
+ "type": "http"
+ }
+ }
+ },
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {}
+ }
+ }`
+
+ var route *entity.Route
+ var consumer *entity.Consumer
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(c), &consumer)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreConsumer := &store.MockInterface{}
+ mStoreConsumer.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(consumer, nil)
+
+ h := Handler{routeStore: mStore, consumerStore: mStoreConsumer}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 13.Export route create by apikey-auth plugin basic-auth plugin
+func TestExportRoutesCreateByKeyAuthAndBasicAuth(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ r := `{
+ "uri": "/hello",
+ "plugins": {
+ "key-auth": {},
+ "basic-auth": {}
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`
+
+ c := `{
+ "username": "jack",
+ "plugins": {
+ "key-auth": {
+ "key": "auth-one"
+ },
+ "basic-auth": {
+ "username": "jack",
+ "password": "123456"
+ }
+ },
+ "desc": "test description"
+ }`
+
+ exportR := `{
+ "components": {
+ "securitySchemes": {
+ "api_key": {
+ "in": "header",
+ "name": "X-XSRF-TOKEN",
+ "type": "apiKey"
+ },
+ "basicAuth": {
+ "in": "header",
+ "name": "basicAuth",
+ "type": "basicAuth"
+ }
+ }
+ },
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {}
+ }
+ }`
+
+ var route *entity.Route
+ var consumer *entity.Consumer
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(c), &consumer)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreConsumer := &store.MockInterface{}
+ mStoreConsumer.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(consumer, nil)
+
+ h := Handler{routeStore: mStore, consumerStore: mStoreConsumer}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 14.Export all routes
+func TestExportRoutesAll(t *testing.T) {
+ input := &store.ListInput{}
+ //*entity.Route
+ r1 := `{
+ "name": "aaaa",
+ "status": 1,
+ "uri": "/hello_",
+ "host": "*.bar.com",
+ "methods": [ "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`
+
+ r2 := `{
+ "name": "aaaa2",
+ "status": 1,
+ "uris": ["/hello_2"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`
+
+ exportR1 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello_": {
+ "post": {
+ "operationId": "aaaaPOST",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "x-apisix-enable_websocket": false,
+ "x-apisix-host": "*.bar.com",
+ "x-apisix-plugins": {},
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ },
+ "/hello_2": {
+ "get": {
+ "operationId": "aaaa2GET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "x-apisix-enable_websocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-plugins": {},
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var route2 *entity.Route
+ var routes []*entity.Route
+ err := json.Unmarshal([]byte(r1), &route)
+ err = json.Unmarshal([]byte(r2), &route2)
+ mStore := &store.MockInterface{}
+ getCalled := false
+
+ routes = append(routes, route)
+ routes = append(routes, route2)
+
+ mStore.On("List", mock.Anything).Run(func(args mock.Arguments) {
+ getCalled = true
+ }).Return(func(input store.ListInput) *store.ListOutput {
+ var returnData []interface{}
+ for _, c := range routes {
+ returnData = append(returnData, c)
+ }
+ return &store.ListOutput{
+ Rows: returnData,
+ TotalSize: len(returnData),
+ }
+ }, nil)
+
+ h := Handler{routeStore: mStore}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportAllRoutes(ctx)
+ assert.Nil(t, err)
+ ret1, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR1), string(ret1))
+ assert.NotNil(t, ret1)
+ assert.True(t, getCalled)
+}
+
+//15.Create service according to upstream1 ID
+// Create route according to upstream2 ID and service ID
+func TestExportRoutesCreateByUpstreamIDAndServiceID2(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ us := `{
+ "id": "u1",
+ "nodes": [
+ {
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }
+ ],
+ "type": "roundrobin"
+ }`
+
+ us2 := `{
+ "id": "u2",
+ "nodes": [
+ {
+ "host": "172.16.238.20",
+ "port": 1981,
+ "weight": 1
+ }
+ ],
+ "type": "roundrobin"
+ }`
+
+ s := `{
+ "id": "s1",
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "enable_websocket":true,
+ "upstream_id": "u2"
+ }`
+
+ r := `{
+ "name": "route_all",
+ "desc": "所有",
+ "status": 1,
+ "methods": ["GET"],
+ "priority": 0,
+ "service_id": "s1",
+ "labels": {
+ "test": "1",
+ "API_VERSION": "v1"
+ },
+ "vars": [
+ ["arg_name", "==", "test"]
+ ],
+ "uri": "/hello",
+ "enable_websocket":false,
+ "plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "upstream_id": "u1"
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "route_allGET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "summary": "所有",
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "API_VERSION": "v1",
+ "test": "1"
+ },
+ "x-apisix-plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "id": "u1",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "x-apisix-vars": [
+ ["arg_name", "==", "test"]
+ ]
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ var upstream *entity.Upstream
+ var upstream2 *entity.Upstream
+ var upstreams []*entity.Upstream
+
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+ err = json.Unmarshal([]byte(us), &upstream)
+ err = json.Unmarshal([]byte(us2), &upstream2)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ upstreams = append(upstreams, upstream)
+ upstreams = append(upstreams, upstream2)
+ getCalled := true
+
+ mStoreUpstream := &store.MockInterface{}
+ mStoreUpstream.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(upstream, nil)
+
+ mStoreUpstream.On("List", mock.Anything).Run(func(args mock.Arguments) {
+ getCalled = true
+ }).Return(func(input store.ListInput) *store.ListOutput {
+ var returnData []interface{}
+ for _, c := range upstreams {
+ returnData = append(returnData, c)
+ }
+ return &store.ListOutput{
+ Rows: returnData,
+ TotalSize: len(returnData),
+ }
+ }, nil)
+
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ h := Handler{routeStore: mStore, upstreamStore: mStoreUpstream, serviceStore: mStoreService}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+ assert.True(t, getCalled)
+}
+
+// 16.Add suffix when testing the same URI export "APISIX-REPEAT-URI-" + Millisecond time stamp: "APISIX-REPEAT-URI-1257894000000"
+func TestExportRoutesSameURI(t *testing.T) {
+ input := &store.ListInput{}
+ //*entity.Route
+ r1 := `{
+ "uris": ["/test-test"],
+ "name": "route_all",
+ "desc": "所有",
+ "methods": ["GET"],
+ "hosts": ["test.com"],
+ "status": 1,
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+}`
+
+ r2 := `{
+ "uris": ["/test-test"],
+ "name": "route_all",
+ "desc": "所有",
+ "methods": ["GET"],
+ "hosts": ["test.com"],
+ "status": 1,
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+}`
+
+ exportR1 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/test-test": {
+ "get": {
+ "operationId": "route_allGET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "summary": "所有",
+ "x-apisix-enable_websocket": false,
+ "x-apisix-hosts": ["test.com"],
+ "x-apisix-plugins": {},
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ },
+ "/test-test-APISIX-REPEAT-URI-`
+ var route *entity.Route
+ var route2 *entity.Route
+ var routes []*entity.Route
+ err := json.Unmarshal([]byte(r1), &route)
+ err = json.Unmarshal([]byte(r2), &route2)
+ mStore := &store.MockInterface{}
+ getCalled := false
+
+ routes = append(routes, route)
+ routes = append(routes, route2)
+
+ mStore.On("List", mock.Anything).Run(func(args mock.Arguments) {
+ getCalled = true
+ }).Return(func(input store.ListInput) *store.ListOutput {
+ var returnData []interface{}
+ for _, c := range routes {
+ returnData = append(returnData, c)
+ }
+ return &store.ListOutput{
+ Rows: returnData,
+ TotalSize: len(returnData),
+ }
+ }, nil)
+
+ h := Handler{routeStore: mStore}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportAllRoutes(ctx)
+ assert.Nil(t, err)
+ ret1, err := json.Marshal(ret)
+ if err != nil {
+ }
Review comment:
done
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] starsz commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
starsz commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r563474852
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
Review comment:
Hi, I think we need to avoid use context.Background here. We should use c.Context().
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
Review comment:
Ditto.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
Review comment:
style: It's better to have a empty line here. Just keep the same style.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
Review comment:
Ditto.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
Review comment:
Can we split `analysis route.URIs` to another function?
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
Review comment:
Ditto.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = parseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ //pathItem.Get = path
+ pathItem.Get = parsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ //pathItem.Post = path
+ pathItem.Post = parsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ //pathItem.Put = path
+ pathItem.Put = parsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ //pathItem.Delete = path
+ pathItem.Delete = parsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ //pathItem.Patch = path
+ pathItem.Patch = parsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ //pathItem.Head = path
+ pathItem.Head = parsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+func parseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
Review comment:
What's the label args use for?
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = parseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ //pathItem.Get = path
+ pathItem.Get = parsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ //pathItem.Post = path
+ pathItem.Post = parsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ //pathItem.Put = path
+ pathItem.Put = parsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ //pathItem.Delete = path
+ pathItem.Delete = parsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ //pathItem.Patch = path
+ pathItem.Patch = parsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ //pathItem.Head = path
+ pathItem.Head = parsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+func parseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
Review comment:
No, I didn't see the lables args was used.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r564251757
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
Review comment:
done
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r563551027
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
Review comment:
done.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (2b970a0) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/144741871b441b645d7fd58a368e3e6012043ed5?el=desc) (1447418) will **increase** coverage by `0.05%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
==========================================
+ Coverage 42.39% 42.44% +0.05%
==========================================
Files 31 31
Lines 1932 1932
==========================================
+ Hits 819 820 +1
+ Misses 1002 1001 -1
Partials 111 111
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (ø)` | |
| [api/internal/core/store/store.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmUuZ28=) | `80.00% <0.00%> (+0.62%)` | :arrow_up: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [1447418...2b970a0](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765473883
Test cases are good for me
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] membphis commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
membphis commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766524618
@Jaycean please merge the master branch, FE e2e test cases have been 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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] starsz commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
starsz commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r564430820
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+//ExportRoutes Export data by passing route ID, such as "R1" or multiple route parameters, such as "R1,R2"
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(c.Context(), id)
+ if err != nil {
+ return nil, err
Review comment:
Need to return special info if err == data.ErrNotFound.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+//ExportRoutes Export data by passing route ID, such as "R1" or multiple route parameters, such as "R1,R2"
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(c.Context(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+//ExportAllRoutes All routes can be directly exported without passing parameters
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+//RouteToOpenAPI3 Pass in route list parameter: []*entity.Route, convert route data to openapi3 and export processing function
+func (h *Handler) RouteToOpenAPI3(c droplet.Context, routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
Review comment:
Those variables can put in the cycle.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+//ExportRoutes Export data by passing route ID, such as "R1" or multiple route parameters, such as "R1,R2"
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(c.Context(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+//ExportAllRoutes All routes can be directly exported without passing parameters
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+//RouteToOpenAPI3 Pass in route list parameter: []*entity.Route, convert route data to openapi3 and export processing function
+func (h *Handler) RouteToOpenAPI3(c droplet.Context, routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(c.Context(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ //Parse upstream
+ _upstream, err := h.ParseRouteUpstream(c, route)
+
+ if err != nil {
+ log.Errorf("ParseRouteUpstream err: ", err)
+ return nil, err
+ } else if err == nil && _upstream != nil {
+ extensions["x-apisix-upstream"] = _upstream
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = ParseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // Parse Route URIs
+ paths, paramsRefs = ParseRouteUris(route, paths, paramsRefs, pathItem)
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = ParseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ pathItem.Get = ParsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ pathItem.Post = ParsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ pathItem.Put = ParsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ pathItem.Delete = ParsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ pathItem.Patch = ParsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ pathItem.Head = ParsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+//ParseLabels When service and route have labels at the same time, use route's label.
+//When route has no label, service sometimes uses service's label. This function is used to process this logic
+func ParseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
Review comment:
labels args is not used.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
Review comment:
GET `/apisix/admin/export/routes/:ids`
GET `/apisix/admin/export/routes`
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+//ExportRoutes Export data by passing route ID, such as "R1" or multiple route parameters, such as "R1,R2"
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(c.Context(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+//ExportAllRoutes All routes can be directly exported without passing parameters
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+//RouteToOpenAPI3 Pass in route list parameter: []*entity.Route, convert route data to openapi3 and export processing function
+func (h *Handler) RouteToOpenAPI3(c droplet.Context, routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(c.Context(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ //Parse upstream
+ _upstream, err := h.ParseRouteUpstream(c, route)
+
+ if err != nil {
+ log.Errorf("ParseRouteUpstream err: ", err)
+ return nil, err
+ } else if err == nil && _upstream != nil {
+ extensions["x-apisix-upstream"] = _upstream
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = ParseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // Parse Route URIs
+ paths, paramsRefs = ParseRouteUris(route, paths, paramsRefs, pathItem)
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = ParseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ pathItem.Get = ParsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ pathItem.Post = ParsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ pathItem.Put = ParsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ pathItem.Delete = ParsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ pathItem.Patch = ParsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ pathItem.Head = ParsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+//ParseLabels When service and route have labels at the same time, use route's label.
+//When route has no label, service sometimes uses service's label. This function is used to process this logic
+func ParseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
+ if route.Labels != nil {
+ return route.Labels, nil
+ } else if route.Labels == nil && route.ServiceID != nil {
+ if serviceLabels != nil {
+ return serviceLabels, nil
+ } else if serviceLabels == nil {
+ return nil, nil
+ }
+ }
+ return nil, nil
+}
+
+//ParsePathItem Convert data in route to openapi3
+func ParsePathItem(path openapi3.Operation, routeMethod string) *openapi3.Operation {
+ _path := &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + routeMethod,
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ return _path
+}
+
+// ParseRoutePlugins Merge service with plugin in route
+func ParseRoutePlugins(route *entity.Route, paramsRefs []*openapi3.ParameterRef, plugins map[string]interface{}, path openapi3.Operation, servicePlugins map[string]interface{}, secSchemas openapi3.SecuritySchemes, requestBody *openapi3.RequestBody) (openapi3.Operation, openapi3.SecuritySchemes, []*openapi3.ParameterRef, map[string]interface{}, error) {
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
Review comment:
Those lines can be deleted.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+//ExportRoutes Export data by passing route ID, such as "R1" or multiple route parameters, such as "R1,R2"
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(c.Context(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+//ExportAllRoutes All routes can be directly exported without passing parameters
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+//RouteToOpenAPI3 Pass in route list parameter: []*entity.Route, convert route data to openapi3 and export processing function
+func (h *Handler) RouteToOpenAPI3(c droplet.Context, routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(c.Context(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ //Parse upstream
+ _upstream, err := h.ParseRouteUpstream(c, route)
+
+ if err != nil {
+ log.Errorf("ParseRouteUpstream err: ", err)
+ return nil, err
+ } else if err == nil && _upstream != nil {
+ extensions["x-apisix-upstream"] = _upstream
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = ParseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // Parse Route URIs
+ paths, paramsRefs = ParseRouteUris(route, paths, paramsRefs, pathItem)
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = ParseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ pathItem.Get = ParsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ pathItem.Post = ParsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ pathItem.Put = ParsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ pathItem.Delete = ParsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ pathItem.Patch = ParsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ pathItem.Head = ParsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+//ParseLabels When service and route have labels at the same time, use route's label.
+//When route has no label, service sometimes uses service's label. This function is used to process this logic
+func ParseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
+ if route.Labels != nil {
+ return route.Labels, nil
+ } else if route.Labels == nil && route.ServiceID != nil {
+ if serviceLabels != nil {
+ return serviceLabels, nil
+ } else if serviceLabels == nil {
+ return nil, nil
+ }
+ }
+ return nil, nil
+}
+
+//ParsePathItem Convert data in route to openapi3
+func ParsePathItem(path openapi3.Operation, routeMethod string) *openapi3.Operation {
+ _path := &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + routeMethod,
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ return _path
+}
+
+// ParseRoutePlugins Merge service with plugin in route
+func ParseRoutePlugins(route *entity.Route, paramsRefs []*openapi3.ParameterRef, plugins map[string]interface{}, path openapi3.Operation, servicePlugins map[string]interface{}, secSchemas openapi3.SecuritySchemes, requestBody *openapi3.RequestBody) (openapi3.Operation, openapi3.SecuritySchemes, []*openapi3.ParameterRef, map[string]interface{}, error) {
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+
+ m["*/*"] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
Review comment:
Need some comments.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+//ExportRoutes Export data by passing route ID, such as "R1" or multiple route parameters, such as "R1,R2"
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(c.Context(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+//ExportAllRoutes All routes can be directly exported without passing parameters
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+//RouteToOpenAPI3 Pass in route list parameter: []*entity.Route, convert route data to openapi3 and export processing function
+func (h *Handler) RouteToOpenAPI3(c droplet.Context, routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(c.Context(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ //Parse upstream
+ _upstream, err := h.ParseRouteUpstream(c, route)
+
+ if err != nil {
+ log.Errorf("ParseRouteUpstream err: ", err)
+ return nil, err
+ } else if err == nil && _upstream != nil {
+ extensions["x-apisix-upstream"] = _upstream
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = ParseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // Parse Route URIs
+ paths, paramsRefs = ParseRouteUris(route, paths, paramsRefs, pathItem)
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = ParseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ pathItem.Get = ParsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ pathItem.Post = ParsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ pathItem.Put = ParsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ pathItem.Delete = ParsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ pathItem.Patch = ParsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ pathItem.Head = ParsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+//ParseLabels When service and route have labels at the same time, use route's label.
+//When route has no label, service sometimes uses service's label. This function is used to process this logic
+func ParseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
+ if route.Labels != nil {
+ return route.Labels, nil
+ } else if route.Labels == nil && route.ServiceID != nil {
+ if serviceLabels != nil {
+ return serviceLabels, nil
+ } else if serviceLabels == nil {
+ return nil, nil
+ }
+ }
+ return nil, nil
+}
+
+//ParsePathItem Convert data in route to openapi3
+func ParsePathItem(path openapi3.Operation, routeMethod string) *openapi3.Operation {
+ _path := &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + routeMethod,
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ return _path
+}
+
+// ParseRoutePlugins Merge service with plugin in route
+func ParseRoutePlugins(route *entity.Route, paramsRefs []*openapi3.ParameterRef, plugins map[string]interface{}, path openapi3.Operation, servicePlugins map[string]interface{}, secSchemas openapi3.SecuritySchemes, requestBody *openapi3.RequestBody) (openapi3.Operation, openapi3.SecuritySchemes, []*openapi3.ParameterRef, map[string]interface{}, error) {
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+
+ m["*/*"] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ continue
+ }
+ // analysis security plugins
+ securityEnv := &openapi3.SecurityRequirement{}
+ switch key {
+ case string(KeyAuth):
+ secSchemas["api_key"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewCSRFSecurityScheme()}
+ securityEnv.Authenticate("api_key", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(BasicAuth):
+ secSchemas["basicAuth"] = &openapi3.SecuritySchemeRef{Value: &openapi3.SecurityScheme{
+ Type: "basicAuth",
+ Name: "basicAuth",
+ In: "header",
+ }}
+ securityEnv.Authenticate("basicAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(JWTAuth):
+ secSchemas["bearerAuth"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewJWTSecurityScheme()}
+ securityEnv.Authenticate("bearerAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ }
+ plugins[key] = value
+ }
+ path.Security = secReq
+
+ if route.ServiceID != nil && servicePlugins != nil {
+ _servicePlugins, err := json.Marshal(servicePlugins)
+ if err != nil {
+ log.Errorf("MapToJson err: ", err)
+ return path, nil, nil, nil, err
+ }
+ _plugins, err := json.Marshal(plugins)
+ if err != nil {
+ log.Errorf("MapToJson err: ", err)
+ return path, nil, nil, nil, err
+ }
+ bytePlugins, err := utils.MergeJson(_servicePlugins, _plugins)
+ if err != nil {
+ log.Errorf("Plugins MergeJson err: ", err)
+ return path, nil, nil, nil, err
+ }
+ err = json.Unmarshal([]byte(bytePlugins), &plugins)
+ if err != nil {
+ log.Errorf("JsonToMapDemo err: ", err)
+ return path, nil, nil, nil, err
+ }
+ }
+ } else if route.Plugins == nil && route.ServiceID != nil {
+ plugins = servicePlugins
+ }
+ return path, secSchemas, paramsRefs, plugins, nil
+}
+
+// ParseRouteUris The URI and URIs of route are converted to paths URI in openapi3
+func ParseRouteUris(route *entity.Route, paths openapi3.Paths, paramsRefs []*openapi3.ParameterRef, pathItem *openapi3.PathItem) (openapi3.Paths, []*openapi3.ParameterRef) {
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ _time := time.Now().UnixNano() / 1e6
+ if strings.Contains(uri, "*") {
+ if _, ok := paths[strings.Split(uri, "*")[0]+"{params}"]; !ok {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ } else {
+ paths[strings.Split(uri, "*")[0]+"{params}"+"-APISIX-REPEAT-URI-"+strconv.FormatInt(_time, 10)] = pathItem
+ }
Review comment:
This can be done in closure.
##########
File path: api/internal/handler/data_loader/route_export_test.go
##########
@@ -0,0 +1,1864 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "strings"
+ "testing"
+
+ "github.com/apisix/manager-api/internal/core/entity"
+ "github.com/apisix/manager-api/internal/core/store"
+ "github.com/shiningrush/droplet"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/mock"
+)
+
+// 1.Export data as the route of URIs Hosts
+func TestExportRoutes1(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ //*entity.Route
+ r1 := `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`
+
+ exportR1 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enable_websocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPOST",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enable_websocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ err := json.Unmarshal([]byte(r1), &route)
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ h := Handler{routeStore: mStore}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ ret1, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR1), string(ret1))
+ assert.NotNil(t, ret1)
+}
+
+// 2.Export data as the route of URI host
+func TestExportRoutes2(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ //*entity.Route
+ r2 := `{
+ "name": "aaaa2",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uri": "/hello2",
+ "host": "*.bar.com",
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`
+
+ exportR2 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello2": {
+ "get": {
+ "operationId": "aaaa2GET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enable_websocket": false,
+ "x-apisix-host": "*.bar.com",
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaa2POST",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enable_websocket": false,
+ "x-apisix-host": "*.bar.com",
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ err := json.Unmarshal([]byte(r2), &route)
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ h := Handler{routeStore: mStore}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ ret1, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR2), string(ret1))
+ assert.NotNil(t, ret1)
+}
+
+// 3.Create a service that contains complete data and use the service_id create route
+func TestExportRoutesCreateByServiceId(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ //*entity.Route
+ r := `{
+ "methods": ["GET"],
+ "uri": "/hello",
+ "service_id": "s1"
+ }`
+
+ s := `{
+ "id": "s1",
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "GET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 0,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ h := Handler{routeStore: mStore, serviceStore: mStoreService}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 4.Create a service containing plugin and a route containing plugin to test the fusion of exported data
+func TestExportRoutesCreateByServiceId2(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ //*entity.Route
+ r := `{
+ "methods": ["GET"],
+ "uri": "/hello",
+ "service_id": "s1",
+ "enable_websocket":false,
+ "plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`
+
+ s := `{
+ "id": "s1",
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "GET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ },
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 0,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ h := Handler{routeStore: mStore, serviceStore: mStoreService}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 5.Create a service according to the upstream ID and a route according to the service ID
+func TestExportRoutesCreateByServiceId3(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ us := `{
+ "id": "u1",
+ "nodes": [
+ {
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }
+ ],
+ "type": "roundrobin"
+ }`
+
+ s := `{
+ "id": "s1",
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream_id": "u1"
+ }`
+
+ r := `{
+ "methods": ["GET"],
+ "uri": "/hello",
+ "service_id": "s1",
+ "enable_websocket":false,
+ "plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1981,
+ "weight": 1
+ }]
+ }
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "GET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ },
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 0,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1981,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ var upstream *entity.Upstream
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+ err = json.Unmarshal([]byte(us), &upstream)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ mStoreUpstream := &store.MockInterface{}
+ mStoreUpstream.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(upstream, nil)
+
+ h := Handler{routeStore: mStore, serviceStore: mStoreService, upstreamStore: mStoreUpstream}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 6.Create and export route according to upstream ID
+func TestExportRoutesCreateByUpstreamId(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ us := `{
+ "id": "u1",
+ "nodes": [
+ {
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }
+ ],
+ "type": "roundrobin"
+ }`
+
+ r := `{
+ "methods": ["GET"],
+ "uri": "/hello",
+ "enable_websocket":false,
+ "plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "upstream_id": "u1"
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "GET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enable_websocket": false,
+ "x-apisix-plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 0,
+ "x-apisix-upstream": {
+ "id": "u1",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var upstream *entity.Upstream
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(us), &upstream)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreUpstream := &store.MockInterface{}
+ mStoreUpstream.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(upstream, nil)
+
+ h := Handler{routeStore: mStore, upstreamStore: mStoreUpstream}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 7.Create route according to upstream ID and service ID
+func TestExportRoutesCreateByUpstreamIdandServiceId(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ us := `{
+ "id": "u1",
+ "nodes": [
+ {
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }
+ ],
+ "type": "roundrobin"
+ }`
+
+ s := `{
+ "id": "s1",
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream_id": "u1"
+ }`
+
+ r := `{
+ "name": "route_all",
+ "desc": "所有",
+ "status": 1,
+ "methods": ["GET"],
+ "priority": 0,
+ "service_id": "s1",
+ "labels": {
+ "test": "1",
+ "API_VERSION": "v1"
+ },
+ "vars": [
+ ["arg_name", "==", "test"]
+ ],
+ "uri": "/hello",
+ "enable_websocket":false,
+ "plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "upstream_id": "u1"
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "route_allGET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "summary": "所有",
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "API_VERSION": "v1",
+ "test": "1"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ },
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "id": "u1",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "x-apisix-vars": [
+ ["arg_name", "==", "test"]
+ ]
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ var upstream *entity.Upstream
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+ err = json.Unmarshal([]byte(us), &upstream)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreUpstream := &store.MockInterface{}
+ mStoreUpstream.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(upstream, nil)
+
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ h := Handler{routeStore: mStore, upstreamStore: mStoreUpstream, serviceStore: mStoreService}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 8.Creating route using service ID does not contain upstream data
+func TestExportRoutesCreateByServiceIdNoUpstream(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ us := `{
+ "id": "u1",
+ "nodes": [
+ {
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }
+ ],
+ "type": "roundrobin"
+ }`
+
+ s := `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "enable_websocket":true,
+ "upstream_id": "6"
+ }`
+
+ r := `{
+ "name": "route_all",
+ "desc": "所有",
+ "status": 1,
+ "methods": ["GET"],
+ "priority": 0,
+ "service_id": "s5",
+ "labels": {
+ "test": "1",
+ "API_VERSION": "v1"
+ },
+ "vars": [
+ ["arg_name", "==", "test"]
+ ],
+ "uri": "/hello",
+ "enable_websocket":false,
+ "plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ }
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "route_allGET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "summary": "所有",
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "API_VERSION": "v1",
+ "test": "1"
+ },
+ "x-apisix-plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "id": "u1",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "x-apisix-vars": [
+ ["arg_name", "==", "test"]
+ ]
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ var upstream *entity.Upstream
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+ err = json.Unmarshal([]byte(us), &upstream)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreUpstream := &store.MockInterface{}
+ mStoreUpstream.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(upstream, nil)
+
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ h := Handler{routeStore: mStore, upstreamStore: mStoreUpstream, serviceStore: mStoreService}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 9.Create a service with label data and a route with label data, and export the route.
+// Label is the original data of the route
+func TestExportRoutesCreateByLabel(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ s := `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "enable_websocket":true,
+ "labels": {
+ "build": "10"
+ }
+ }`
+
+ r := `{
+ "name": "route_all",
+ "desc": "所有",
+ "status": 1,
+ "methods": ["GET"],
+ "service_id": "s1",
+ "labels": {
+ "test": "1",
+ "API_VERSION": "v1"
+ },
+ "uri": "/hello",
+ "enable_websocket":false,
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "route_allGET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "summary": "所有",
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "API_VERSION": "v1",
+ "test": "1"
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ h := Handler{routeStore: mStore, serviceStore: mStoreService}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 10.Create a service with label data and a route without label data, and export the route.
+// Label is the data of the service
+func TestExportRoutesCreateByLabel2(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ s := `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "enable_websocket":true,
+ "labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ }
+ }`
+
+ r := `{
+ "name": "route_all",
+ "desc": "所有",
+ "status": 1,
+ "methods": ["GET"],
+ "service_id": "s2",
+ "vars": [
+ ["arg_name", "==", "test"]
+ ],
+ "uri": "/hello"
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "route_allGET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "summary": "所有",
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-vars": [
+ ["arg_name", "==", "test"]
+ ]
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ h := Handler{routeStore: mStore, serviceStore: mStoreService}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 11.Test export route request_ validation data correctness
+func TestExportRoutesCreateByRequestValidation(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ r := `{
+ "uris": ["/test-test"],
+ "name": "route_all",
+ "desc": "所有",
+ "methods": ["GET"],
+ "hosts": ["test.com"],
+ "plugins": {
+ "request-validation": {
+ "body_schema": {
+ "properties": {
+ "boolean_payload": {
+ "type": "boolean"
+ },
+ "required_payload": {
+ "type": "string"
+ }
+ },
+ "required": ["required_payload"],
+ "type": "object"
+ },
+ "disable": false,
+ "header_schema": {
+ "properties": {
+ "test": {
+ "enum": "test-enum",
+ "type": "string"
+ }
+ },
+ "type": "string"
+ }
+ }
+ },
+ "status": 1
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/test-test": {
+ "get": {
+ "operationId": "route_allGET",
+ "parameters": [{
+ "in": "header",
+ "name": "test",
+ "schema": {
+ "type": "string"
+ }
+ }],
+ "requestBody": {
+ "content": {
+ "*/*": {
+ "schema": {
+ "properties": {
+ "boolean_payload": {
+ "type": "boolean"
+ },
+ "required_payload": {
+ "type": "string"
+ }
+ },
+ "required": ["required_payload"],
+ "type": "object"
+ }
+ }
+ }
+ },
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "summary": "所有",
+ "x-apisix-enable_websocket": false,
+ "x-apisix-hosts": ["test.com"],
+ "x-apisix-plugins": {},
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ err := json.Unmarshal([]byte(r), &route)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ h := Handler{routeStore: mStore}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 12.Export route create by jwt-auth plugin
+func TestExportRoutesCreateByJWTAuth(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ r := `{
+ "uri": "/hello",
+ "plugins": {
+ "jwt-auth": {}
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`
+
+ c := `{
+ "username": "jack",
+ "plugins": {
+ "jwt-auth": {
+ "key": "user-key",
+ "secret": "my-secret-key",
+ "algorithm": "HS256"
+ }
+ },
+ "desc": "test description"
+ }`
+
+ exportR := `{
+ "components": {
+ "securitySchemes": {
+ "bearerAuth": {
+ "bearerFormat": "JWT",
+ "scheme": "bearer",
+ "type": "http"
+ }
+ }
+ },
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {}
+ }
+ }`
+
+ var route *entity.Route
+ var consumer *entity.Consumer
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(c), &consumer)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreConsumer := &store.MockInterface{}
+ mStoreConsumer.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(consumer, nil)
+
+ h := Handler{routeStore: mStore, consumerStore: mStoreConsumer}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 13.Export route create by apikey-auth plugin basic-auth plugin
+func TestExportRoutesCreateByKeyAuthAndBasicAuth(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ r := `{
+ "uri": "/hello",
+ "plugins": {
+ "key-auth": {},
+ "basic-auth": {}
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`
+
+ c := `{
+ "username": "jack",
+ "plugins": {
+ "key-auth": {
+ "key": "auth-one"
+ },
+ "basic-auth": {
+ "username": "jack",
+ "password": "123456"
+ }
+ },
+ "desc": "test description"
+ }`
+
+ exportR := `{
+ "components": {
+ "securitySchemes": {
+ "api_key": {
+ "in": "header",
+ "name": "X-XSRF-TOKEN",
+ "type": "apiKey"
+ },
+ "basicAuth": {
+ "in": "header",
+ "name": "basicAuth",
+ "type": "basicAuth"
+ }
+ }
+ },
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {}
+ }
+ }`
+
+ var route *entity.Route
+ var consumer *entity.Consumer
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(c), &consumer)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ mStoreConsumer := &store.MockInterface{}
+ mStoreConsumer.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(consumer, nil)
+
+ h := Handler{routeStore: mStore, consumerStore: mStoreConsumer}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+}
+
+// 14.Export all routes
+func TestExportRoutesAll(t *testing.T) {
+ input := &store.ListInput{}
+ //*entity.Route
+ r1 := `{
+ "name": "aaaa",
+ "status": 1,
+ "uri": "/hello_",
+ "host": "*.bar.com",
+ "methods": [ "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`
+
+ r2 := `{
+ "name": "aaaa2",
+ "status": 1,
+ "uris": ["/hello_2"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`
+
+ exportR1 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello_": {
+ "post": {
+ "operationId": "aaaaPOST",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "x-apisix-enable_websocket": false,
+ "x-apisix-host": "*.bar.com",
+ "x-apisix-plugins": {},
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ },
+ "/hello_2": {
+ "get": {
+ "operationId": "aaaa2GET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "x-apisix-enable_websocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-plugins": {},
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var route2 *entity.Route
+ var routes []*entity.Route
+ err := json.Unmarshal([]byte(r1), &route)
+ err = json.Unmarshal([]byte(r2), &route2)
+ mStore := &store.MockInterface{}
+ getCalled := false
+
+ routes = append(routes, route)
+ routes = append(routes, route2)
+
+ mStore.On("List", mock.Anything).Run(func(args mock.Arguments) {
+ getCalled = true
+ }).Return(func(input store.ListInput) *store.ListOutput {
+ var returnData []interface{}
+ for _, c := range routes {
+ returnData = append(returnData, c)
+ }
+ return &store.ListOutput{
+ Rows: returnData,
+ TotalSize: len(returnData),
+ }
+ }, nil)
+
+ h := Handler{routeStore: mStore}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportAllRoutes(ctx)
+ assert.Nil(t, err)
+ ret1, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR1), string(ret1))
+ assert.NotNil(t, ret1)
+ assert.True(t, getCalled)
+}
+
+//15.Create service according to upstream1 ID
+// Create route according to upstream2 ID and service ID
+func TestExportRoutesCreateByUpstreamIDAndServiceID2(t *testing.T) {
+ input := &ExportInput{IDs: "1"}
+ us := `{
+ "id": "u1",
+ "nodes": [
+ {
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }
+ ],
+ "type": "roundrobin"
+ }`
+
+ us2 := `{
+ "id": "u2",
+ "nodes": [
+ {
+ "host": "172.16.238.20",
+ "port": 1981,
+ "weight": 1
+ }
+ ],
+ "type": "roundrobin"
+ }`
+
+ s := `{
+ "id": "s1",
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "enable_websocket":true,
+ "upstream_id": "u2"
+ }`
+
+ r := `{
+ "name": "route_all",
+ "desc": "所有",
+ "status": 1,
+ "methods": ["GET"],
+ "priority": 0,
+ "service_id": "s1",
+ "labels": {
+ "test": "1",
+ "API_VERSION": "v1"
+ },
+ "vars": [
+ ["arg_name", "==", "test"]
+ ],
+ "uri": "/hello",
+ "enable_websocket":false,
+ "plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "upstream_id": "u1"
+ }`
+
+ exportR := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "route_allGET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "summary": "所有",
+ "x-apisix-enable_websocket": false,
+ "x-apisix-labels": {
+ "API_VERSION": "v1",
+ "test": "1"
+ },
+ "x-apisix-plugins": {
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "id": "u1",
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "x-apisix-vars": [
+ ["arg_name", "==", "test"]
+ ]
+ }
+ }
+ }
+ }`
+ var route *entity.Route
+ var service *entity.Service
+ var upstream *entity.Upstream
+ var upstream2 *entity.Upstream
+ var upstreams []*entity.Upstream
+
+ err := json.Unmarshal([]byte(r), &route)
+ err = json.Unmarshal([]byte(s), &service)
+ err = json.Unmarshal([]byte(us), &upstream)
+ err = json.Unmarshal([]byte(us2), &upstream2)
+
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(route, nil)
+
+ upstreams = append(upstreams, upstream)
+ upstreams = append(upstreams, upstream2)
+ getCalled := true
+
+ mStoreUpstream := &store.MockInterface{}
+ mStoreUpstream.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(upstream, nil)
+
+ mStoreUpstream.On("List", mock.Anything).Run(func(args mock.Arguments) {
+ getCalled = true
+ }).Return(func(input store.ListInput) *store.ListOutput {
+ var returnData []interface{}
+ for _, c := range upstreams {
+ returnData = append(returnData, c)
+ }
+ return &store.ListOutput{
+ Rows: returnData,
+ TotalSize: len(returnData),
+ }
+ }, nil)
+
+ mStoreService := &store.MockInterface{}
+ mStoreService.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ }).Return(service, nil)
+
+ h := Handler{routeStore: mStore, upstreamStore: mStoreUpstream, serviceStore: mStoreService}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportRoutes(ctx)
+ assert.Nil(t, err)
+ _ret, err := json.Marshal(ret)
+ if err != nil {
+ }
+ assert.Equal(t, replaceStr(exportR), string(_ret))
+ assert.NotNil(t, _ret)
+ assert.True(t, getCalled)
+}
+
+// 16.Add suffix when testing the same URI export "APISIX-REPEAT-URI-" + Millisecond time stamp: "APISIX-REPEAT-URI-1257894000000"
+func TestExportRoutesSameURI(t *testing.T) {
+ input := &store.ListInput{}
+ //*entity.Route
+ r1 := `{
+ "uris": ["/test-test"],
+ "name": "route_all",
+ "desc": "所有",
+ "methods": ["GET"],
+ "hosts": ["test.com"],
+ "status": 1,
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+}`
+
+ r2 := `{
+ "uris": ["/test-test"],
+ "name": "route_all",
+ "desc": "所有",
+ "methods": ["GET"],
+ "hosts": ["test.com"],
+ "status": 1,
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+}`
+
+ exportR1 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/test-test": {
+ "get": {
+ "operationId": "route_allGET",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "summary": "所有",
+ "x-apisix-enable_websocket": false,
+ "x-apisix-hosts": ["test.com"],
+ "x-apisix-plugins": {},
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ },
+ "/test-test-APISIX-REPEAT-URI-`
+ var route *entity.Route
+ var route2 *entity.Route
+ var routes []*entity.Route
+ err := json.Unmarshal([]byte(r1), &route)
+ err = json.Unmarshal([]byte(r2), &route2)
+ mStore := &store.MockInterface{}
+ getCalled := false
+
+ routes = append(routes, route)
+ routes = append(routes, route2)
+
+ mStore.On("List", mock.Anything).Run(func(args mock.Arguments) {
+ getCalled = true
+ }).Return(func(input store.ListInput) *store.ListOutput {
+ var returnData []interface{}
+ for _, c := range routes {
+ returnData = append(returnData, c)
+ }
+ return &store.ListOutput{
+ Rows: returnData,
+ TotalSize: len(returnData),
+ }
+ }, nil)
+
+ h := Handler{routeStore: mStore}
+ ctx := droplet.NewContext()
+ ctx.SetInput(input)
+
+ ret, err := h.ExportAllRoutes(ctx)
+ assert.Nil(t, err)
+ ret1, err := json.Marshal(ret)
+ if err != nil {
+ }
Review comment:
`assert.Nil(t, err)` or `_ `
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r563493790
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
Review comment:
no problem.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = parseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ //pathItem.Get = path
+ pathItem.Get = parsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ //pathItem.Post = path
+ pathItem.Post = parsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ //pathItem.Put = path
+ pathItem.Put = parsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ //pathItem.Delete = path
+ pathItem.Delete = parsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ //pathItem.Patch = path
+ pathItem.Patch = parsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ //pathItem.Head = path
+ pathItem.Head = parsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+func parseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
Review comment:
When service and route have labels at the same time, use route's label. When route has no label, service sometimes uses service's label. This function is used to process this logic
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
Review comment:
No problem, but there are some problems that need to be solved by voting. I'll deal with them after the results come out
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
Review comment:
done.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
Review comment:
done.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
Review comment:
done.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
Review comment:
done.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
Review comment:
done.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] juzhiyuan edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
juzhiyuan edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766602482
because it's using `URI` as paths's `Key`, then data gets covered would happen. I would prefer adding a global prefix to avoid data lost.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766587292
> @liuxiran @nic-chen
> The unit test and test cases have been supplemented. In the process of testing, I found a problem:
>
> * At present, the path data structure of OpenAPI 3 is used to store URIs, and the data structure is map [string]. If the URI is repeated, the data will be covered, if the repeated URIs are directly covered, only one URI can be exported, which will lead to the loss of user data.
> * My idea is to add a uniform suffix after the same URI when exporting, so that the import can recognize the URI
> For example:
>
> ```
> {
> "components": {},
> "info": {
> "title": "RoutesExport",
> "version": "3.0.0"
> },
> "openapi": "3.0.0",
> "paths": {
> "/hello": {
> "get": {}
> }
> "/hello-repeaturi1": {
> "get": {}
> }
> "/hello-repeaturi2": {
> "get": {}
> }
> }
> }
> ```
>
> * My method also has the risk of repeating the user named URI
> * Do you have any better suggestions? We can discuss them. Thks.
>OpenAPI defines a unique operation as a combination of a path and an HTTP method. This means that two GET or two POST methods for the same path are not allowed – even if they have different parameters (parameters have no effect on uniqueness)
refer from https://swagger.io/docs/specification/paths-and-operations/
After communicating with @Jaycean and @nic-chen there are two alternative solutions:
- A: add a uniform suffix after the same URI when exporting, and add a description to the user guide.
This solution can meet the platform-wide data is not lost, the disadvantage is that when users need to use the swagger file to generate documents or sdk, they need to modified manually.
- B: return error when exporting routes with same URI
In this way exported OAS3.0 file can be used directly, with the disadvantage that there may be data loss for APISIX. The remedy is that we provide additional import and export of platform-wide data, in accordance with the apisix data format.
Neither scheme is perfect though, which one do you perfer? any suggestions are welcome, thanks a lot
@Jaycean @nic-chen @membphis @starsz @juzhiyuan @imjoey
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r564215393
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = parseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ //pathItem.Get = path
+ pathItem.Get = parsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ //pathItem.Post = path
+ pathItem.Post = parsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ //pathItem.Put = path
+ pathItem.Put = parsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ //pathItem.Delete = path
+ pathItem.Delete = parsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ //pathItem.Patch = path
+ pathItem.Patch = parsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ //pathItem.Head = path
+ pathItem.Head = parsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+func parseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
Review comment:
I don't quite understand your question. Do you mean you don't see users or business scenarios using this field? I don't know if my understanding is correct, because more coverage possibilities are considered everywhere to ensure that the data is not lost. After discussing with nic-chen, this processing function is added.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (ce2c1e4) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/b2908468d4c8df96bde32c088710a2ee7fae67a9?el=desc) (b290846) will **decrease** coverage by `0.05%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
==========================================
- Coverage 42.44% 42.39% -0.06%
==========================================
Files 31 31
Lines 1932 1932
==========================================
- Hits 820 819 -1
- Misses 1001 1002 +1
Partials 111 111
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (ø)` | |
| [api/internal/core/store/store.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmUuZ28=) | `79.37% <0.00%> (-0.63%)` | :arrow_down: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [b290846...ce2c1e4](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765308727
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] membphis commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
membphis commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766524618
@Jaycean please merge the master branch, FE e2e test cases have been 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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-764447946
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r564605816
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+//ExportRoutes Export data by passing route ID, such as "R1" or multiple route parameters, such as "R1,R2"
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(c.Context(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+//ExportAllRoutes All routes can be directly exported without passing parameters
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.RouteToOpenAPI3(c, routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+//RouteToOpenAPI3 Pass in route list parameter: []*entity.Route, convert route data to openapi3 and export processing function
+func (h *Handler) RouteToOpenAPI3(c droplet.Context, routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(c.Context(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ //Parse upstream
+ _upstream, err := h.ParseRouteUpstream(c, route)
+
+ if err != nil {
+ log.Errorf("ParseRouteUpstream err: ", err)
+ return nil, err
+ } else if err == nil && _upstream != nil {
+ extensions["x-apisix-upstream"] = _upstream
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = ParseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // Parse Route URIs
+ paths, paramsRefs = ParseRouteUris(route, paths, paramsRefs, pathItem)
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = ParseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ pathItem.Get = ParsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ pathItem.Post = ParsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ pathItem.Put = ParsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ pathItem.Delete = ParsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ pathItem.Patch = ParsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ pathItem.Head = ParsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+//ParseLabels When service and route have labels at the same time, use route's label.
+//When route has no label, service sometimes uses service's label. This function is used to process this logic
+func ParseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
+ if route.Labels != nil {
+ return route.Labels, nil
+ } else if route.Labels == nil && route.ServiceID != nil {
+ if serviceLabels != nil {
+ return serviceLabels, nil
+ } else if serviceLabels == nil {
+ return nil, nil
+ }
+ }
+ return nil, nil
+}
+
+//ParsePathItem Convert data in route to openapi3
+func ParsePathItem(path openapi3.Operation, routeMethod string) *openapi3.Operation {
+ _path := &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + routeMethod,
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ return _path
+}
+
+// ParseRoutePlugins Merge service with plugin in route
+func ParseRoutePlugins(route *entity.Route, paramsRefs []*openapi3.ParameterRef, plugins map[string]interface{}, path openapi3.Operation, servicePlugins map[string]interface{}, secSchemas openapi3.SecuritySchemes, requestBody *openapi3.RequestBody) (openapi3.Operation, openapi3.SecuritySchemes, []*openapi3.ParameterRef, map[string]interface{}, error) {
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+
+ m["*/*"] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ continue
+ }
+ // analysis security plugins
+ securityEnv := &openapi3.SecurityRequirement{}
+ switch key {
+ case string(KeyAuth):
+ secSchemas["api_key"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewCSRFSecurityScheme()}
+ securityEnv.Authenticate("api_key", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(BasicAuth):
+ secSchemas["basicAuth"] = &openapi3.SecuritySchemeRef{Value: &openapi3.SecurityScheme{
+ Type: "basicAuth",
+ Name: "basicAuth",
+ In: "header",
+ }}
+ securityEnv.Authenticate("basicAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(JWTAuth):
+ secSchemas["bearerAuth"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewJWTSecurityScheme()}
+ securityEnv.Authenticate("bearerAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ }
+ plugins[key] = value
+ }
+ path.Security = secReq
+
+ if route.ServiceID != nil && servicePlugins != nil {
+ _servicePlugins, err := json.Marshal(servicePlugins)
+ if err != nil {
+ log.Errorf("MapToJson err: ", err)
+ return path, nil, nil, nil, err
+ }
+ _plugins, err := json.Marshal(plugins)
+ if err != nil {
+ log.Errorf("MapToJson err: ", err)
+ return path, nil, nil, nil, err
+ }
+ bytePlugins, err := utils.MergeJson(_servicePlugins, _plugins)
+ if err != nil {
+ log.Errorf("Plugins MergeJson err: ", err)
+ return path, nil, nil, nil, err
+ }
+ err = json.Unmarshal([]byte(bytePlugins), &plugins)
+ if err != nil {
+ log.Errorf("JsonToMapDemo err: ", err)
+ return path, nil, nil, nil, err
+ }
+ }
+ } else if route.Plugins == nil && route.ServiceID != nil {
+ plugins = servicePlugins
+ }
+ return path, secSchemas, paramsRefs, plugins, nil
+}
+
+// ParseRouteUris The URI and URIs of route are converted to paths URI in openapi3
+func ParseRouteUris(route *entity.Route, paths openapi3.Paths, paramsRefs []*openapi3.ParameterRef, pathItem *openapi3.PathItem) (openapi3.Paths, []*openapi3.ParameterRef) {
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ _time := time.Now().UnixNano() / 1e6
+ if strings.Contains(uri, "*") {
+ if _, ok := paths[strings.Split(uri, "*")[0]+"{params}"]; !ok {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ } else {
+ paths[strings.Split(uri, "*")[0]+"{params}"+"-APISIX-REPEAT-URI-"+strconv.FormatInt(_time, 10)] = pathItem
+ }
Review comment:
done
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765076940
> > > Hi, @Jaycean. Need unit test.
> >
> >
> > At present, the function interface needs to export the existing route data and ensure the correctness of the exported data, so I think E2E should be written more perfectly. I think if the simple unit test only tests whether the interface can export data, it does not represent whether the function of the interface is correct, and if the relevant route data is not created, the interface will not work normally use.
>
> Not agree with you.
> First of all, unit testing is very important. It helps find bugs easily and quickly if someone changes the code.And unit testing can cover more code than e2e test.
> For example, in this case.
> We need to test these situations at least:
>
> 1. route can't found
> 2. Uris contain "*"
> 3. plugins with `request-validation`
> 4. different route method
> ...
>
> > and if the relevant route data is not created, the interface will not work normally use.
>
> We can mock data in etcd.
> See https://github.com/apache/apisix-dashboard/blob/master/api/internal/handler/server_info/server_info_test.go for more details.
@starsz Now I have encountered a problem in unit testing, exporting the data that needs to create route, upstream and service. After reading the example you gave me, I still don't know how to write it. Can you give me some advice.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766587292
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (934fab6) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/8852e630f0c2890d6607ab56925f3c0aaf6144d8?el=desc) (8852e63) will **decrease** coverage by `16.23%`.
> The diff coverage is `65.26%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
===========================================
- Coverage 62.75% 46.51% -16.24%
===========================================
Files 41 34 -7
Lines 2360 2238 -122
===========================================
- Hits 1481 1041 -440
- Misses 703 1062 +359
+ Partials 176 135 -41
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (-62.50%)` | :arrow_down: |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `65.26% <65.26%> (ø)` | |
| [api/internal/filter/request\_id.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9yZXF1ZXN0X2lkLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `0.00% <0.00%> (-70.41%)` | :arrow_down: |
| [api/internal/filter/cors.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9jb3JzLmdv) | `0.00% <0.00%> (-66.67%)` | :arrow_down: |
| [api/internal/filter/schema.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9zY2hlbWEuZ28=) | `0.00% <0.00%> (-55.47%)` | :arrow_down: |
| [api/internal/handler/server\_info/server\_info.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc2VydmVyX2luZm8vc2VydmVyX2luZm8uZ28=) | `57.14% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/utils/closer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2Nsb3Nlci5nbw==) | `0.00% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/handler/consumer/consumer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvY29uc3VtZXIvY29uc3VtZXIuZ28=) | `63.82% <0.00%> (-27.66%)` | :arrow_down: |
| ... and [26 more](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [8852e63...934fab6](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] tokers commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r557299600
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,432 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "fmt"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
Review comment:
It would be better to put these variable initializations inside a var block.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-764207165
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] juzhiyuan edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
juzhiyuan edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766602482
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] imjoey commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
imjoey commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r554653173
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,349 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "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/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+ "reflect"
+ "regexp"
+ "strings"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.URI != "" {
+ extensions["x-apisix-uri"] = route.URI
+ }
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-serviceProtocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+ // analysis route.URIs
+ for _, uri := range route.Uris {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+ plugins := make(map[string]interface{})
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ for k, v := range value.(map[string]interface{}) {
+ if k == "header_schema" {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&v)
+ err := json.Unmarshal(reqBytes, requestValidation)
Review comment:
@Jaycean Additional check is also required here.
```suggestion
err := json.Unmarshal(reqBytes, requestValidation)
if err != nil {
// log error and return back to client
}
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (1a61fd8) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/1385fc200b15eff4ffee8fd4165e7ba414dcaba0?el=desc) (1385fc2) will **increase** coverage by `1.70%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
==========================================
+ Coverage 42.44% 44.14% +1.70%
==========================================
Files 31 31
Lines 1932 1948 +16
==========================================
+ Hits 820 860 +40
+ Misses 1001 976 -25
- Partials 111 112 +1
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (ø)` | |
| [api/internal/core/store/store.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmUuZ28=) | `78.88% <0.00%> (-1.12%)` | :arrow_down: |
| [api/internal/core/store/store\_mock.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVfbW9jay5nbw==) | `0.00% <0.00%> (ø)` | |
| [api/internal/handler/ssl/ssl.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc3NsL3NzbC5nbw==) | `29.60% <0.00%> (+0.39%)` | :arrow_up: |
| [api/internal/handler/upstream/upstream.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvdXBzdHJlYW0vdXBzdHJlYW0uZ28=) | `20.16% <0.00%> (+0.67%)` | :arrow_up: |
| [api/internal/handler/service/service.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc2VydmljZS9zZXJ2aWNlLmdv) | `27.83% <0.00%> (+0.75%)` | :arrow_up: |
| [api/internal/utils/utils.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL3V0aWxzLmdv) | `59.09% <0.00%> (+1.27%)` | :arrow_up: |
| [api/internal/handler/route/route.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvcm91dGUvcm91dGUuZ28=) | `46.49% <0.00%> (+3.05%)` | :arrow_up: |
| [api/internal/filter/authentication.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9hdXRoZW50aWNhdGlvbi5nbw==) | `76.47% <0.00%> (+76.47%)` | :arrow_up: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [1385fc2...1a61fd8](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765076940
> > > Hi, @Jaycean. Need unit test.
> >
> >
> > At present, the function interface needs to export the existing route data and ensure the correctness of the exported data, so I think E2E should be written more perfectly. I think if the simple unit test only tests whether the interface can export data, it does not represent whether the function of the interface is correct, and if the relevant route data is not created, the interface will not work normally use.
>
> Not agree with you.
> First of all, unit testing is very important. It helps find bugs easily and quickly if someone changes the code.And unit testing can cover more code than e2e test.
> For example, in this case.
> We need to test these situations at least:
>
> 1. route can't found
> 2. Uris contain "*"
> 3. plugins with `request-validation`
> 4. different route method
> ...
>
> > and if the relevant route data is not created, the interface will not work normally use.
>
> We can mock data in etcd.
> See https://github.com/apache/apisix-dashboard/blob/master/api/internal/handler/server_info/server_info_test.go for more details.
Now I have encountered a problem in unit testing, exporting the data that needs to create route, upstream and service. After reading the example you gave me, I still don't know how to write it. Can you give me some advice.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r561519973
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
Review comment:
it would be better to change the test case desc
```suggestion
Desc: "create route with uris and hosts",
```
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR1 + "}}",
+ },
+ }
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ exportStrR2 := `
+ "/hello2": {
+ "get": {
+ "operationId": "aaaa2Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaa2Post",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR2 = replaceStr(exportStrR2)
+
+ tests2 := []HttpTestCase{
+ {
+ Desc: "hit route2 that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route2",
Review comment:
```suggestion
Desc: "create route2 with uri and hosts to test whether the uri parsing is correct ",
```
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
Review comment:
it would be better to change the test case desc
```suggestion
Desc: "create route with uris and hosts to test whether the uris parsing is correct",
```
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR1 + "}}",
+ },
+ }
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ exportStrR2 := `
+ "/hello2": {
+ "get": {
+ "operationId": "aaaa2Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaa2Post",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR2 = replaceStr(exportStrR2)
+
+ tests2 := []HttpTestCase{
+ {
+ Desc: "hit route2 that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r2",
+ Body: `{
+ "name": "aaaa2",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uri": "/hello2",
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "}}",
+ },
+ }
+ for _, tc := range tests2 {
+ testCaseCheck(tc, t)
+ }
+
+ tests3 := []HttpTestCase{
+ {
+ Desc: "export route route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1,r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "," + exportStrR1 + "}}",
+ },
+ {
+ Desc: "delete the route just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "bar.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "delete the route2 just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route2 just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "bar.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ }
+ for _, tc := range tests3 {
+ testCaseCheck(tc, t)
+ }
+
+ serviceStrS1 := `
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "enable_websocket": true
+ `
+ serviceStrS1 = replaceStr(serviceStrS1)
+
+ exportStrR3 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-serviceID": "s1",
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ exportStrR3 = replaceStr(exportStrR3)
+
+ // The test case tests the creation of a complete service.
+ // And according to the service_ ID to create a route.
+ // Test whether the plugin upstream label in the service is integrated with route and exported.
+ tests4 := []HttpTestCase{
+ {
+ Desc: "create service with all options",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ Body: `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "create_time":1602883670,
+ "update_time":1602893670,
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`,
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "get the service s1",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectCode: http.StatusOK,
+ ExpectBody: serviceStrS1,
+ },
+ {
+ Desc: "create route3 using the service just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r3",
+ Body: `{
+ "methods": ["GET"],
+ "uri": "/hello",
+ "service_id": "s1"
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "export route3",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r3",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"code\":0,\"message\":\"\",\"data\":" + exportStrR3,
+ },
+ {
+ Desc: "delete the route3 just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r3",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route3 just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello",
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "delete the service1",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ Sleep: sleepTime,
+ },
+ }
+ for _, tc := range tests4 {
+ testCaseCheck(tc, t)
+ }
+
+ serviceStrS2 := `
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "enable_websocket": true`
+ serviceStrS2 = replaceStr(serviceStrS2)
+
+ exportStrR4 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ },
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-serviceID": "s2",
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ exportStrR4 = replaceStr(exportStrR4)
+ // This test case tests the creation of a complete service.
+ // And create a complete route, export rules as route upstream data for high priority, direct use.
+ // However, if the data in the service (label, plugins) does not exist in the route, it will be fused and exported.
+ // If it exists, the data in route will be used first
+ tests5 := []HttpTestCase{
+ {
+ Desc: "create service with all options",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s2",
+ Headers: map[string]string{"Authorization": token},
+ Body: `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "create_time":1602883670,
+ "update_time":1602893670,
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`,
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "get the service s2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectCode: http.StatusOK,
+ ExpectBody: serviceStrS2,
+ },
+ {
+ Desc: "create route4 using the service just created",
Review comment:
you'd better to update the desc too
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR1 + "}}",
+ },
+ }
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ exportStrR2 := `
+ "/hello2": {
+ "get": {
+ "operationId": "aaaa2Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaa2Post",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR2 = replaceStr(exportStrR2)
+
+ tests2 := []HttpTestCase{
+ {
+ Desc: "hit route2 that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r2",
+ Body: `{
+ "name": "aaaa2",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uri": "/hello2",
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "}}",
+ },
+ }
+ for _, tc := range tests2 {
+ testCaseCheck(tc, t)
+ }
+
+ tests3 := []HttpTestCase{
+ {
+ Desc: "export route route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1,r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "," + exportStrR1 + "}}",
+ },
+ {
+ Desc: "delete the route just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "bar.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "delete the route2 just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route2 just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "bar.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ }
+ for _, tc := range tests3 {
+ testCaseCheck(tc, t)
+ }
+
+ serviceStrS1 := `
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "enable_websocket": true
+ `
+ serviceStrS1 = replaceStr(serviceStrS1)
+
+ exportStrR3 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-serviceID": "s1",
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ exportStrR3 = replaceStr(exportStrR3)
+
+ // The test case tests the creation of a complete service.
+ // And according to the service_ ID to create a route.
+ // Test whether the plugin upstream label in the service is integrated with route and exported.
+ tests4 := []HttpTestCase{
+ {
+ Desc: "create service with all options",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ Body: `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "create_time":1602883670,
+ "update_time":1602893670,
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`,
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "get the service s1",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectCode: http.StatusOK,
+ ExpectBody: serviceStrS1,
+ },
+ {
+ Desc: "create route3 using the service just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r3",
+ Body: `{
+ "methods": ["GET"],
+ "uri": "/hello",
+ "service_id": "s1"
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "export route3",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r3",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"code\":0,\"message\":\"\",\"data\":" + exportStrR3,
+ },
+ {
+ Desc: "delete the route3 just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r3",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route3 just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello",
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "delete the service1",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ Sleep: sleepTime,
+ },
+ }
+ for _, tc := range tests4 {
+ testCaseCheck(tc, t)
+ }
+
+ serviceStrS2 := `
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "enable_websocket": true`
+ serviceStrS2 = replaceStr(serviceStrS2)
+
+ exportStrR4 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ },
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-serviceID": "s2",
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ exportStrR4 = replaceStr(exportStrR4)
+ // This test case tests the creation of a complete service.
+ // And create a complete route, export rules as route upstream data for high priority, direct use.
+ // However, if the data in the service (label, plugins) does not exist in the route, it will be fused and exported.
+ // If it exists, the data in route will be used first
+ tests5 := []HttpTestCase{
+ {
+ Desc: "create service with all options",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s2",
+ Headers: map[string]string{"Authorization": token},
+ Body: `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "create_time":1602883670,
+ "update_time":1602893670,
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`,
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "get the service s2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectCode: http.StatusOK,
+ ExpectBody: serviceStrS2,
+ },
+ {
+ Desc: "create route4 using the service just created",
Review comment:
you'd better update the desc too
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765308727
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] starsz commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
starsz commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757607795
> > Hi, @Jaycean. Need unit test.
>
> At present, the function interface needs to export the existing route data and ensure the correctness of the exported data, so I think E2E should be written more perfectly. I think if the simple unit test only tests whether the interface can export data, it does not represent whether the function of the interface is correct, and if the relevant route data is not created, the interface will not work normally use.
Not agree with you.
First of all, unit testing is very important. It helps find bugs easily and quickly if someone changes the code.And unit testing can cover more code than e2e test.
For example, in this case.
We need to test these situations at least:
1. route can't found
2. Uris contain "*"
3. plugins with `request-validation`
4. different route method
...
> and if the relevant route data is not created, the interface will not work normally use.
We can mock data in etcd.
See https://github.com/apache/apisix-dashboard/blob/master/api/internal/handler/server_info/server_info_test.go for more details.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] juzhiyuan merged pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
juzhiyuan merged pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] imjoey commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
imjoey commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r554654802
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,357 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.URI != "" {
+ extensions["x-apisix-uri"] = route.URI
+ }
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-serviceProtocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+ // analysis route.URIs
+ for _, uri := range route.Uris {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+ plugins := make(map[string]interface{})
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ for k, v := range value.(map[string]interface{}) {
+ if k == "header_schema" {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&v)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+ if k == "body_schema" {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&v)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for _, va := range route.Vars.([]interface{}) {
+ compile := regexp.MustCompile("^http_.*")
+ match := compile.Match([]byte(va.([]interface{})[0].(string)))
+ if match {
+ m[va.([]interface{})[2].(string)] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ }
+ }
+ continue
+ }
Review comment:
@Jaycean If only caring about the keys that equals to `body_schema` and `header_schema`, IMHO, there is no need to iterate the entire map. Just use
```go
if valueMap, ok := value.(map[string]interface{}); ok {
if hsVal, ok := valueMap["header_schema"]; ok {
// Deal with header schema
}
if bsVal, ok := valueMap["body_schema"]; ok {
// Deal with body schema
}
}
```
BTW: It would be better to check every type assertion in Golang.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r561660169
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR1 + "}}",
+ },
+ }
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ exportStrR2 := `
+ "/hello2": {
+ "get": {
+ "operationId": "aaaa2Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaa2Post",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR2 = replaceStr(exportStrR2)
+
+ tests2 := []HttpTestCase{
+ {
+ Desc: "hit route2 that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r2",
+ Body: `{
+ "name": "aaaa2",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uri": "/hello2",
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "}}",
+ },
+ }
+ for _, tc := range tests2 {
+ testCaseCheck(tc, t)
+ }
+
+ tests3 := []HttpTestCase{
+ {
+ Desc: "export route route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1,r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "," + exportStrR1 + "}}",
+ },
+ {
+ Desc: "delete the route just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "bar.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "delete the route2 just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route2 just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "bar.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ }
+ for _, tc := range tests3 {
+ testCaseCheck(tc, t)
+ }
+
+ serviceStrS1 := `
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "enable_websocket": true
+ `
+ serviceStrS1 = replaceStr(serviceStrS1)
+
+ exportStrR3 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-serviceID": "s1",
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ exportStrR3 = replaceStr(exportStrR3)
+
+ // The test case tests the creation of a complete service.
+ // And according to the service_ ID to create a route.
+ // Test whether the plugin upstream label in the service is integrated with route and exported.
+ tests4 := []HttpTestCase{
+ {
+ Desc: "create service with all options",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ Body: `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "create_time":1602883670,
+ "update_time":1602893670,
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`,
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "get the service s1",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectCode: http.StatusOK,
+ ExpectBody: serviceStrS1,
+ },
+ {
+ Desc: "create route3 using the service just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r3",
+ Body: `{
+ "methods": ["GET"],
+ "uri": "/hello",
+ "service_id": "s1"
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "export route3",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r3",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"code\":0,\"message\":\"\",\"data\":" + exportStrR3,
+ },
+ {
+ Desc: "delete the route3 just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r3",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route3 just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello",
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "delete the service1",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ Sleep: sleepTime,
+ },
+ }
+ for _, tc := range tests4 {
+ testCaseCheck(tc, t)
+ }
+
+ serviceStrS2 := `
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "enable_websocket": true`
+ serviceStrS2 = replaceStr(serviceStrS2)
+
+ exportStrR4 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ },
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-serviceID": "s2",
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ exportStrR4 = replaceStr(exportStrR4)
+ // This test case tests the creation of a complete service.
+ // And create a complete route, export rules as route upstream data for high priority, direct use.
+ // However, if the data in the service (label, plugins) does not exist in the route, it will be fused and exported.
+ // If it exists, the data in route will be used first
+ tests5 := []HttpTestCase{
+ {
+ Desc: "create service with all options",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s2",
+ Headers: map[string]string{"Authorization": token},
+ Body: `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "create_time":1602883670,
+ "update_time":1602893670,
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`,
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "get the service s2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectCode: http.StatusOK,
+ ExpectBody: serviceStrS2,
+ },
+ {
+ Desc: "create route4 using the service just created",
Review comment:
done.
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR1 + "}}",
+ },
+ }
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ exportStrR2 := `
+ "/hello2": {
+ "get": {
+ "operationId": "aaaa2Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaa2Post",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR2 = replaceStr(exportStrR2)
+
+ tests2 := []HttpTestCase{
+ {
+ Desc: "hit route2 that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route2",
Review comment:
done.
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
Review comment:
done.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765308727
checklist:
- [x] export route with host **(test2 coverd)**
- [x] export route with hosts **(test1 coverd)**
- [x] export route with uri **(test2 coverd)**
- [x] export route with uris **(test1 coverd)**
- [x] export route with upstream
- [x] export route with upstream_id **(test8 coverd)**
- [x] export route with service_id(service create with upstream) **(test4 coverd)**
- [x] export route with upstream and service_id **(test6 coverd)**
- [ ] export route with service_id(service create with upstream_id)
- [ ] export route with labels and service_id(service create with labels)
- [ ] export route without labels and service_id(service create with labels)
- [ ] export route with plugins and service_id(service create with plugins which is not the same as route's plugins) **(test5 coverd)**
- [x] export route with basic-auth plugin
- [x] export route with jwt-auth plugin
- [x] export route with key-auth plugin
- [ ] export route with request-validation (body_schema and header_schema) plugin
- [x] export two routes **(test3 coverd)**
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r555622817
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,365 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
Review comment:
done.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765308727
checklist:
- [ ] export route with host
- [ ] export route with hosts
- [ ] export route with uri
- [ ] export route with uris
- [ ] export route with upstream
- [ ] export route with upstream_id
- [ ] export route with service_id(service create with upstream)
- [ ] export route with service_id(service create with upstream_id)
- [ ] export route with labels and service_id(service create with labels)
- [ ] export route without labels and service_id(service create with labels)
- [ ] export route with plugins and service_id(service create with plugins which is not the same as route's plugins)
- [ ] export route with basic-auth plugin
- [ ] export route with jwt-auth plugin
- [ ] export route with key-auth plugin
- [ ] export route with request-validation (body_schema and header_schema) plugin
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (3dbb918) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/bb0a0575f9207fc57aacf79ab5f9defb4cb06166?el=desc) (bb0a057) will **increase** coverage by `1.01%`.
> The diff coverage is `69.29%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
==========================================
+ Coverage 62.50% 63.51% +1.01%
==========================================
Files 41 42 +1
Lines 2360 2601 +241
==========================================
+ Hits 1475 1652 +177
- Misses 711 751 +40
- Partials 174 198 +24
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `62.50% <ø> (ø)` | |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `69.16% <69.16%> (ø)` | |
| [api/internal/route.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3JvdXRlLmdv) | `84.37% <100.00%> (+0.50%)` | :arrow_up: |
| [api/internal/utils/closer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2Nsb3Nlci5nbw==) | `33.33% <0.00%> (ø)` | |
| [api/internal/core/store/store.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmUuZ28=) | `86.70% <0.00%> (+0.63%)` | :arrow_up: |
| [api/internal/core/storage/etcd.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmFnZS9ldGNkLmdv) | `50.50% <0.00%> (+2.02%)` | :arrow_up: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `74.48% <0.00%> (+4.08%)` | :arrow_up: |
| [api/cmd/managerapi.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2NtZC9tYW5hZ2VyYXBpLmdv) | `77.08% <0.00%> (+6.25%)` | :arrow_up: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [bb0a057...3dbb918](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (0af3fd8) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/8852e630f0c2890d6607ab56925f3c0aaf6144d8?el=desc) (8852e63) will **decrease** coverage by `16.23%`.
> The diff coverage is `65.15%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
===========================================
- Coverage 62.75% 46.51% -16.24%
===========================================
Files 41 34 -7
Lines 2360 2240 -120
===========================================
- Hits 1481 1042 -439
- Misses 703 1063 +360
+ Partials 176 135 -41
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (-62.50%)` | :arrow_down: |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `65.15% <65.15%> (ø)` | |
| [api/internal/filter/request\_id.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9yZXF1ZXN0X2lkLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `0.00% <0.00%> (-70.41%)` | :arrow_down: |
| [api/internal/filter/cors.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9jb3JzLmdv) | `0.00% <0.00%> (-66.67%)` | :arrow_down: |
| [api/internal/filter/schema.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9zY2hlbWEuZ28=) | `0.00% <0.00%> (-55.47%)` | :arrow_down: |
| [api/internal/handler/server\_info/server\_info.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc2VydmVyX2luZm8vc2VydmVyX2luZm8uZ28=) | `57.14% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/utils/closer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2Nsb3Nlci5nbw==) | `0.00% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/handler/consumer/consumer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvY29uc3VtZXIvY29uc3VtZXIuZ28=) | `63.82% <0.00%> (-27.66%)` | :arrow_down: |
| ... and [26 more](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [8852e63...0af3fd8](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766610459
In my opinion, from the perspective of apimix platform, I choose scheme a to satisfy the users of apimix platform A. The function that URI can be renamed is more open and friendly for users.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] juzhiyuan commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766607438
not sure how other platforms would resolve those issues, we may have a search.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (c9f8292) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/b659dfc6aff50ffbe7553824575a61aa69cbc409?el=desc) (b659dfc) will **decrease** coverage by `18.35%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
===========================================
- Coverage 62.67% 44.31% -18.36%
===========================================
Files 41 32 -9
Lines 2352 1963 -389
===========================================
- Hits 1474 870 -604
- Misses 705 981 +276
+ Partials 173 112 -61
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (-62.50%)` | :arrow_down: |
| [api/internal/filter/request\_id.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9yZXF1ZXN0X2lkLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `0.00% <0.00%> (-70.41%)` | :arrow_down: |
| [api/internal/filter/cors.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9jb3JzLmdv) | `0.00% <0.00%> (-66.67%)` | :arrow_down: |
| [api/internal/filter/schema.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9zY2hlbWEuZ28=) | `0.00% <0.00%> (-55.47%)` | :arrow_down: |
| [api/internal/handler/server\_info/server\_info.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc2VydmVyX2luZm8vc2VydmVyX2luZm8uZ28=) | `57.14% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/utils/closer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2Nsb3Nlci5nbw==) | `0.00% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/handler/consumer/consumer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvY29uc3VtZXIvY29uc3VtZXIuZ28=) | `63.82% <0.00%> (-27.66%)` | :arrow_down: |
| [api/internal/utils/json\_patch.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2pzb25fcGF0Y2guZ28=) | `34.48% <0.00%> (-24.14%)` | :arrow_down: |
| ... and [23 more](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [b659dfc...be327f1](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r563494858
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
Review comment:
No problem, but there are some problems that need to be solved by voting. I'll deal with them after the results come out
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] starsz commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
starsz commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r563474852
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
Review comment:
Hi, I think we need to avoid use context.Background here. We should use c.Context().
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
Review comment:
Ditto.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
Review comment:
style: It's better to have a empty line here. Just keep the same style.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
Review comment:
Ditto.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
Review comment:
Can we split `analysis route.URIs` to another function?
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
Review comment:
Ditto.
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = parseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ //pathItem.Get = path
+ pathItem.Get = parsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ //pathItem.Post = path
+ pathItem.Post = parsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ //pathItem.Put = path
+ pathItem.Put = parsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ //pathItem.Delete = path
+ pathItem.Delete = parsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ //pathItem.Patch = path
+ pathItem.Patch = parsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ //pathItem.Head = path
+ pathItem.Head = parsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+func parseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
Review comment:
What's the label args use for?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r555623623
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,365 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-serviceProtocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+ plugins := make(map[string]interface{})
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for _, va := range route.Vars.([]interface{}) {
+ compile := regexp.MustCompile("^http_.*")
+ match := compile.Match([]byte(va.([]interface{})[0].(string)))
+ if match {
+ m[va.([]interface{})[2].(string)] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ }
+ }
+ continue
+ }
+ // analysis security plugins
+ securityEnv := &openapi3.SecurityRequirement{}
+ switch key {
+ case string(KeyAuth):
+ secSchemas["api_key"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewCSRFSecurityScheme()}
+ securityEnv.Authenticate("api_key", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(BasicAuth):
+ secSchemas["basicAuth"] = &openapi3.SecuritySchemeRef{Value: &openapi3.SecurityScheme{
+ Type: "basicAuth",
+ Name: "basicAuth",
+ In: "header",
+ }}
+ securityEnv.Authenticate("basicAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(JWTAuth):
+ secSchemas["bearerAuth"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewJWTSecurityScheme()}
+ securityEnv.Authenticate("bearerAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ }
+ plugins[key] = value
+ }
+ path.Security = secReq
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enableWebsocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case "GET":
+ //pathItem.Get = path
+ pathItem.Get = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Get",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+
+ case "POST":
+ //pathItem.Post = path
+ pathItem.Post = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Post",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "PUT":
+ //pathItem.Put = path
+ pathItem.Put = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Put",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "DELETE":
+ //pathItem.Delete = path
+ pathItem.Delete = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Delete",
+ Parameters: path.Parameters,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "PATCH":
+ //pathItem.Patch = path
+ pathItem.Patch = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Patch",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "HEAD":
+ //pathItem.Head = path
+ pathItem.Head = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Head",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ }
+ }
+ }
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger
+}
Review comment:
It's really long at the moment. I'll try to take it apart
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,192 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "\"data\":{\"components\":{},\"info\":{\"title\":\"Routes Export\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{\"/hello_\":{\"get\":{\"operationId\":\"aaaaGet\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}},\"post\":{\"operationId\":\"aaaaPost\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\"
:{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}}}}}",
+ },
+ }
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ tests2 := []HttpTestCase{
+ {
+ Desc: "hit route2 that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r2",
+ Body: `{
+ "name": "aaaa2",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uri": "/hello2",
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "\"data\":{\"components\":{},\"info\":{\"title\":\"Routes Export\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{\"/hello2\":{\"get\":{\"operationId\":\"aaaa2Get\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}},\"post\":{\"operationId\":\"aaaa2Post\",\"requestBody\":{},\"responses\":{\"default\":{\"description\":\"\"}},\"security\":[],\"x-apisix-enableWebsocket\":false,\"x-apisix-hosts\":[\"foo.com\",\"*.bar.com\"],\"x-apisix-labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"x-apisix-plugins
\":{\"limit-count\":{\"count\":2,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"x-apisix-priority\":0,\"x-apisix-status\":1,\"x-apisix-upstream\":{\"nodes\":{\"172.16.238.20:1980\":1},\"type\":\"roundrobin\"}}}}}",
+ },
+ }
+ for _, tc := range tests2 {
+ testCaseCheck(tc, t)
+ }
+
+ tests3 := []HttpTestCase{
+
Review comment:
done.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766610459
In my opinion, from the perspective of apimix platform, I choose scheme a to satisfy the users of apimix platform. The function that URI can be renamed is more open and friendly for users.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] juzhiyuan edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
juzhiyuan edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766607438
not sure how other platforms would resolve those issues, we should have a search.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766539166
@liuxiran @nic-chen
The unit test and test cases have been supplemented. In the process of testing, I found a problem:
- At present, the path data structure of OpenAPI 3 is used to store URIs, and the data structure is map [string]. If the URI is repeated, the data will be covered, if the repeated URIs are directly covered, only one URI can be exported, which will lead to the loss of user data.
- My idea is to add a uniform suffix after the same URI when exporting, so that the import can recognize the URI
For example:
```
{
"components": {},
"info": {
"title": "RoutesExport",
"version": "3.0.0"
},
"openapi": "3.0.0",
"paths": {
"/hello": {
"get": {}
}
"/hello-repeaturi2": {
"get": {}
}
"/hello-repeaturi2": {
"get": {}
}
}
}
```
- My method also has the risk of repeating the user named URI
- Do you have any better suggestions? We can discuss them. Thks.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r555623307
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,365 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-serviceProtocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+ plugins := make(map[string]interface{})
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ if valueMap, ok := value.(map[string]interface{}); ok {
+ if hsVal, ok := valueMap["header_schema"]; ok {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&hsVal)
+ err := json.Unmarshal(reqBytes, requestValidation)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
+ for _, arr := range requestValidation.Required {
+ if arr == key1 {
+ param.Required = true
+ }
+ }
+ param.Name = key1
+ typeStr := value1.(map[string]interface{})
+ schema := &openapi3.Schema{Type: typeStr["type"].(string)}
+ param.Schema = &openapi3.SchemaRef{Value: schema}
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
+ }
+ }
+
+ if bsVal, ok := valueMap["body_schema"]; ok {
+ m := map[string]*openapi3.MediaType{}
+ reqBytes, _ := json.Marshal(&bsVal)
+ schema := &openapi3.Schema{}
+ err := json.Unmarshal(reqBytes, schema)
+ if err != nil {
+ log.Errorf("json marshal failed: %s", err)
+ }
+ for _, va := range route.Vars.([]interface{}) {
+ compile := regexp.MustCompile("^http_.*")
+ match := compile.Match([]byte(va.([]interface{})[0].(string)))
+ if match {
+ m[va.([]interface{})[2].(string)] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
+ requestBody.Content = m
+ }
+ }
+ }
+ }
+ continue
+ }
+ // analysis security plugins
+ securityEnv := &openapi3.SecurityRequirement{}
+ switch key {
+ case string(KeyAuth):
+ secSchemas["api_key"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewCSRFSecurityScheme()}
+ securityEnv.Authenticate("api_key", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(BasicAuth):
+ secSchemas["basicAuth"] = &openapi3.SecuritySchemeRef{Value: &openapi3.SecurityScheme{
+ Type: "basicAuth",
+ Name: "basicAuth",
+ In: "header",
+ }}
+ securityEnv.Authenticate("basicAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ case string(JWTAuth):
+ secSchemas["bearerAuth"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewJWTSecurityScheme()}
+ securityEnv.Authenticate("bearerAuth", " ")
+ secReq.With(*securityEnv)
+ continue
+ }
+ plugins[key] = value
+ }
+ path.Security = secReq
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enableWebsocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case "GET":
+ //pathItem.Get = path
+ pathItem.Get = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Get",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+
+ case "POST":
+ //pathItem.Post = path
+ pathItem.Post = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Post",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "PUT":
+ //pathItem.Put = path
+ pathItem.Put = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Put",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "DELETE":
+ //pathItem.Delete = path
+ pathItem.Delete = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Delete",
+ Parameters: path.Parameters,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "PATCH":
+ //pathItem.Patch = path
+ pathItem.Patch = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Patch",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
+ }
+ case "HEAD":
+ //pathItem.Head = path
+ pathItem.Head = &openapi3.Operation{
+ ExtensionProps: path.ExtensionProps,
+ Tags: path.Tags,
+ Summary: path.Summary,
+ Description: path.Description,
+ OperationID: path.OperationID + "Head",
+ Parameters: path.Parameters,
+ RequestBody: path.RequestBody,
+ Responses: path.Responses,
+ Callbacks: path.Callbacks,
+ Deprecated: path.Deprecated,
+ Security: path.Security,
+ Servers: path.Servers,
+ ExternalDocs: path.ExternalDocs,
Review comment:
done.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (0af3fd8) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/8852e630f0c2890d6607ab56925f3c0aaf6144d8?el=desc) (8852e63) will **increase** coverage by `0.10%`.
> The diff coverage is `68.67%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
==========================================
+ Coverage 62.75% 62.85% +0.10%
==========================================
Files 41 43 +2
Lines 2360 2660 +300
==========================================
+ Hits 1481 1672 +191
- Misses 703 783 +80
- Partials 176 205 +29
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `62.50% <ø> (ø)` | |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `68.56% <68.56%> (ø)` | |
| [api/internal/route.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3JvdXRlLmdv) | `84.37% <100.00%> (+0.50%)` | :arrow_up: |
| [api/cmd/managerapi.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2NtZC9tYW5hZ2VyYXBpLmdv) | `60.29% <0.00%> (-16.79%)` | :arrow_down: |
| [api/internal/conf/conf.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvbmYvY29uZi5nbw==) | `67.85% <0.00%> (ø)` | |
| [api/internal/utils/pid.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL3BpZC5nbw==) | `26.66% <0.00%> (ø)` | |
| [api/internal/core/store/store.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmUuZ28=) | `87.34% <0.00%> (+0.63%)` | :arrow_up: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [8852e63...0af3fd8](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766587292
> @liuxiran @nic-chen
> The unit test and test cases have been supplemented. In the process of testing, I found a problem:
>
> * At present, the path data structure of OpenAPI 3 is used to store URIs, and the data structure is map [string]. If the URI is repeated, the data will be covered, if the repeated URIs are directly covered, only one URI can be exported, which will lead to the loss of user data.
> * My idea is to add a uniform suffix after the same URI when exporting, so that the import can recognize the URI
> For example:
>
> ```
> {
> "components": {},
> "info": {
> "title": "RoutesExport",
> "version": "3.0.0"
> },
> "openapi": "3.0.0",
> "paths": {
> "/hello": {
> "get": {}
> }
> "/hello-repeaturi1": {
> "get": {}
> }
> "/hello-repeaturi2": {
> "get": {}
> }
> }
> }
> ```
>
> * My method also has the risk of repeating the user named URI
> * Do you have any better suggestions? We can discuss them. Thks.
>OpenAPI defines a unique operation as a combination of a path and an HTTP method. This means that two GET or two POST methods for the same path are not allowed – even if they have different parameters (parameters have no effect on uniqueness)
refer from https://swagger.io/docs/specification/paths-and-operations/
After communicating with @Jaycean and @nic-chen there are three alternative solutions:
- A: add a uniform suffix after the same URI when exporting, and add a description to the user guide.
This solution can meet the platform-wide data is not lost, the disadvantage is that when users need to use the swagger file to generate documents or sdk, they need to modified manually.
- B: return error when exporting routes with same URI
In this way exported OAS3.0 file can be used directly, with the disadvantage that there may be data loss for APISIX. The remedy is that we provide additional import and export of platform-wide data, in accordance with the apisix data format.
which one do you perfer? any suggestions are welcome, thanks a lot
@Jaycean @nic-chen @membphis @starsz @juzhiyuan @imjoey
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] starsz commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
starsz commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r564175992
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+
+ //Parse Route Plugins
+ path, secSchemas, paramsRefs, plugins, err = parseRoutePlugins(route, paramsRefs, plugins, path, servicePlugins, secSchemas, requestBody)
+ if err != nil {
+ log.Errorf("parseRoutePlugins err: ", err)
+ return nil, err
+ }
+ if plugins != nil {
+ extensions["x-apisix-plugins"] = plugins
+ }
+
+ extensions["x-apisix-priority"] = route.Priority
+ extensions["x-apisix-status"] = route.Status
+ extensions["x-apisix-enable_websocket"] = route.EnableWebsocket
+ path.Extensions = extensions
+ path.Parameters = paramsRefs
+ path.RequestBody = &openapi3.RequestBodyRef{Value: requestBody}
+ path.Responses = openapi3.NewResponses()
+
+ for i := range route.Methods {
+ switch strings.ToUpper(route.Methods[i]) {
+ case http.MethodGet:
+ //pathItem.Get = path
+ pathItem.Get = parsePathItem(path, http.MethodGet)
+ case http.MethodPost:
+ //pathItem.Post = path
+ pathItem.Post = parsePathItem(path, http.MethodPost)
+ case http.MethodPut:
+ //pathItem.Put = path
+ pathItem.Put = parsePathItem(path, http.MethodPut)
+ case http.MethodDelete:
+ //pathItem.Delete = path
+ pathItem.Delete = parsePathItem(path, http.MethodDelete)
+ case http.MethodPatch:
+ //pathItem.Patch = path
+ pathItem.Patch = parsePathItem(path, http.MethodPatch)
+ case http.MethodHead:
+ //pathItem.Head = path
+ pathItem.Head = parsePathItem(path, http.MethodHead)
+ }
+ }
+ }
+
+ components.SecuritySchemes = secSchemas
+ swagger := openapi3.Swagger{
+ OpenAPI: openApi,
+ Info: &openapi3.Info{Title: title, Version: openApi},
+ Paths: paths,
+ Components: *components,
+ }
+ return &swagger, nil
+}
+
+func parseLabels(route *entity.Route, serviceLabels map[string]string, labels map[string]string) (map[string]string, error) {
Review comment:
No, I didn't see the lables args was used.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766539166
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-764207165
it would be better to add two more test cases:
- export a route created with upstream id
- export a route created with service which is created with upstream id
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (0fd5542) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/b659dfc6aff50ffbe7553824575a61aa69cbc409?el=desc) (b659dfc) will **decrease** coverage by `13.94%`.
> The diff coverage is `49.58%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
===========================================
- Coverage 62.67% 48.72% -13.95%
===========================================
Files 41 42 +1
Lines 2352 2594 +242
===========================================
- Hits 1474 1264 -210
- Misses 705 1119 +414
- Partials 173 211 +38
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `62.50% <ø> (ø)` | |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `49.37% <49.37%> (ø)` | |
| [api/internal/route.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3JvdXRlLmdv) | `84.37% <100.00%> (+0.50%)` | :arrow_up: |
| [api/internal/core/store/query.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvcXVlcnkuZ28=) | `0.00% <0.00%> (-88.10%)` | :arrow_down: |
| [api/internal/utils/consts/api\_error.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2NvbnN0cy9hcGlfZXJyb3IuZ28=) | `17.39% <0.00%> (-78.27%)` | :arrow_down: |
| [api/internal/core/store/selector.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc2VsZWN0b3IuZ28=) | `0.00% <0.00%> (-75.93%)` | :arrow_down: |
| [api/internal/handler/plugin/plugin.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvcGx1Z2luL3BsdWdpbi5nbw==) | `16.66% <0.00%> (-70.00%)` | :arrow_down: |
| [api/internal/core/store/validate\_mock.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvdmFsaWRhdGVfbW9jay5nbw==) | `0.00% <0.00%> (-50.00%)` | :arrow_down: |
| [api/internal/filter/authentication.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9hdXRoZW50aWNhdGlvbi5nbw==) | `44.11% <0.00%> (-32.36%)` | :arrow_down: |
| [api/internal/core/store/store.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmUuZ28=) | `58.12% <0.00%> (-30.00%)` | :arrow_down: |
| ... and [15 more](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [b659dfc...0fd5542](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r557803573
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,432 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "fmt"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
Review comment:
Basicauth, keyauth and jwtauth are three kinds of authentication methods. I don't think they should be changed in the program, so I use constants
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r563551501
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
Review comment:
done.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r554767431
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,358 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.URI != "" {
+ extensions["x-apisix-uri"] = route.URI
+ }
Review comment:
`route.URI` need to be parsed into `paths[uri]`
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r563551574
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
Review comment:
done.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r556280298
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,298 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
Review comment:
There is a problem here. I'll modify the code and reprocess the data of upstream and plugin.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (7c1b83e) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/8852e630f0c2890d6607ab56925f3c0aaf6144d8?el=desc) (8852e63) will **decrease** coverage by `0.03%`.
> The diff coverage is `68.99%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
==========================================
- Coverage 62.75% 62.72% -0.04%
==========================================
Files 41 43 +2
Lines 2360 2653 +293
==========================================
+ Hits 1481 1664 +183
- Misses 703 784 +81
- Partials 176 205 +29
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `62.50% <ø> (ø)` | |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `68.87% <68.87%> (ø)` | |
| [api/internal/route.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3JvdXRlLmdv) | `84.37% <100.00%> (+0.50%)` | :arrow_up: |
| [api/cmd/managerapi.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2NtZC9tYW5hZ2VyYXBpLmdv) | `60.29% <0.00%> (-16.79%)` | :arrow_down: |
| [api/internal/core/storage/etcd.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmFnZS9ldGNkLmdv) | `48.48% <0.00%> (-2.03%)` | :arrow_down: |
| [api/internal/core/store/store.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmUuZ28=) | `86.07% <0.00%> (-0.64%)` | :arrow_down: |
| [api/internal/conf/conf.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvbmYvY29uZi5nbw==) | `67.85% <0.00%> (ø)` | |
| [api/internal/utils/pid.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL3BpZC5nbw==) | `26.66% <0.00%> (ø)` | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [8852e63...7c1b83e](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (9cd063f) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/b659dfc6aff50ffbe7553824575a61aa69cbc409?el=desc) (b659dfc) will **decrease** coverage by `18.35%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
===========================================
- Coverage 62.67% 44.31% -18.36%
===========================================
Files 41 32 -9
Lines 2352 1963 -389
===========================================
- Hits 1474 870 -604
- Misses 705 981 +276
+ Partials 173 112 -61
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (-62.50%)` | :arrow_down: |
| [api/internal/filter/request\_id.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9yZXF1ZXN0X2lkLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `0.00% <0.00%> (-70.41%)` | :arrow_down: |
| [api/internal/filter/cors.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9jb3JzLmdv) | `0.00% <0.00%> (-66.67%)` | :arrow_down: |
| [api/internal/filter/schema.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9zY2hlbWEuZ28=) | `0.00% <0.00%> (-55.47%)` | :arrow_down: |
| [api/internal/handler/server\_info/server\_info.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc2VydmVyX2luZm8vc2VydmVyX2luZm8uZ28=) | `57.14% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/utils/closer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2Nsb3Nlci5nbw==) | `0.00% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/handler/consumer/consumer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvY29uc3VtZXIvY29uc3VtZXIuZ28=) | `63.82% <0.00%> (-27.66%)` | :arrow_down: |
| [api/internal/utils/json\_patch.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2pzb25fcGF0Y2guZ28=) | `34.48% <0.00%> (-24.14%)` | :arrow_down: |
| ... and [23 more](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [b659dfc...9cd063f](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r556212051
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,298 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
Review comment:
when export a route with `upstreamId`, we have to export the `uptream` which contained in the `upstreamid`
when export a route with `serviceid`, we have to export the `upstream` and the `plugins` which contained in the `serviceid`
reference: https://github.com/apache/apisix-dashboard/issues/825#issuecomment-748009952
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (217105a) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/ab5b1d94582fab0267d134ef342d19fde6114554?el=desc) (ab5b1d9) will **decrease** coverage by `16.59%`.
> The diff coverage is `63.48%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
===========================================
- Coverage 63.05% 46.45% -16.60%
===========================================
Files 41 33 -8
Lines 2360 2202 -158
===========================================
- Hits 1488 1023 -465
- Misses 697 1047 +350
+ Partials 175 132 -43
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (-62.50%)` | :arrow_down: |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `63.48% <63.48%> (ø)` | |
| [api/internal/filter/request\_id.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9yZXF1ZXN0X2lkLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `0.00% <0.00%> (-74.49%)` | :arrow_down: |
| [api/internal/filter/cors.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9jb3JzLmdv) | `0.00% <0.00%> (-66.67%)` | :arrow_down: |
| [api/internal/filter/schema.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9zY2hlbWEuZ28=) | `0.00% <0.00%> (-55.47%)` | :arrow_down: |
| [api/internal/handler/server\_info/server\_info.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc2VydmVyX2luZm8vc2VydmVyX2luZm8uZ28=) | `57.14% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/utils/closer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2Nsb3Nlci5nbw==) | `0.00% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/handler/consumer/consumer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvY29uc3VtZXIvY29uc3VtZXIuZ28=) | `63.82% <0.00%> (-27.66%)` | :arrow_down: |
| ... and [25 more](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [ab5b1d9...217105a](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (21ccc50) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/1385fc200b15eff4ffee8fd4165e7ba414dcaba0?el=desc) (1385fc2) will **not change** coverage.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
=======================================
Coverage 42.44% 42.44%
=======================================
Files 31 31
Lines 1932 1932
=======================================
Hits 820 820
Misses 1001 1001
Partials 111 111
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (ø)` | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [1385fc2...21ccc50](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r561519973
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
Review comment:
it would be better to change the test case desc
```suggestion
Desc: "create route with uris and hosts to test whether the uris parsing is correct",
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (0fd5542) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/b659dfc6aff50ffbe7553824575a61aa69cbc409?el=desc) (b659dfc) will **decrease** coverage by `1.45%`.
> The diff coverage is `49.58%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
==========================================
- Coverage 62.67% 61.21% -1.46%
==========================================
Files 41 42 +1
Lines 2352 2594 +242
==========================================
+ Hits 1474 1588 +114
- Misses 705 812 +107
- Partials 173 194 +21
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `62.50% <ø> (ø)` | |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `49.37% <49.37%> (ø)` | |
| [api/internal/route.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3JvdXRlLmdv) | `84.37% <100.00%> (+0.50%)` | :arrow_up: |
| [api/internal/core/storage/etcd.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmFnZS9ldGNkLmdv) | `42.69% <0.00%> (-4.50%)` | :arrow_down: |
| [api/internal/core/store/store.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmUuZ28=) | `86.87% <0.00%> (-1.25%)` | :arrow_down: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [b659dfc...0fd5542](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (b4f5310) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/b659dfc6aff50ffbe7553824575a61aa69cbc409?el=desc) (b659dfc) will **decrease** coverage by `18.35%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
===========================================
- Coverage 62.67% 44.31% -18.36%
===========================================
Files 41 32 -9
Lines 2352 1963 -389
===========================================
- Hits 1474 870 -604
- Misses 705 981 +276
+ Partials 173 112 -61
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (-62.50%)` | :arrow_down: |
| [api/internal/filter/request\_id.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9yZXF1ZXN0X2lkLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `0.00% <0.00%> (-70.41%)` | :arrow_down: |
| [api/internal/filter/cors.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9jb3JzLmdv) | `0.00% <0.00%> (-66.67%)` | :arrow_down: |
| [api/internal/filter/schema.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9zY2hlbWEuZ28=) | `0.00% <0.00%> (-55.47%)` | :arrow_down: |
| [api/internal/handler/server\_info/server\_info.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc2VydmVyX2luZm8vc2VydmVyX2luZm8uZ28=) | `57.14% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/utils/closer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2Nsb3Nlci5nbw==) | `0.00% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/handler/consumer/consumer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvY29uc3VtZXIvY29uc3VtZXIuZ28=) | `63.82% <0.00%> (-27.66%)` | :arrow_down: |
| [api/internal/utils/json\_patch.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2pzb25fcGF0Y2guZ28=) | `34.48% <0.00%> (-24.14%)` | :arrow_down: |
| ... and [23 more](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [b659dfc...b4f5310](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-765308727
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r554771834
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,358 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.URI != "" {
+ extensions["x-apisix-uri"] = route.URI
+ }
Review comment:
Thks for review.
Just now, a bug was detected, which did not parse the URI in the create route, but only parsed the URI. I will deal with this problem later. At the same time, x-apisix-uri really does not need to be parsed
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (7414998) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/bb0a0575f9207fc57aacf79ab5f9defb4cb06166?el=desc) (bb0a057) will **increase** coverage by `0.82%`.
> The diff coverage is `69.29%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
==========================================
+ Coverage 62.50% 63.32% +0.82%
==========================================
Files 41 42 +1
Lines 2360 2601 +241
==========================================
+ Hits 1475 1647 +172
- Misses 711 757 +46
- Partials 174 197 +23
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `62.50% <ø> (ø)` | |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `69.16% <69.16%> (ø)` | |
| [api/internal/route.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3JvdXRlLmdv) | `84.37% <100.00%> (+0.50%)` | :arrow_up: |
| [api/internal/core/store/store.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmUuZ28=) | `85.44% <0.00%> (-0.64%)` | :arrow_down: |
| [api/internal/core/storage/etcd.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmFnZS9ldGNkLmdv) | `50.50% <0.00%> (+2.02%)` | :arrow_up: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `74.48% <0.00%> (+4.08%)` | :arrow_up: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [bb0a057...7414998](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (9ed0deb) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/b659dfc6aff50ffbe7553824575a61aa69cbc409?el=desc) (b659dfc) will **decrease** coverage by `18.19%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
===========================================
- Coverage 62.67% 44.47% -18.20%
===========================================
Files 41 32 -9
Lines 2352 1965 -387
===========================================
- Hits 1474 874 -600
- Misses 705 980 +275
+ Partials 173 111 -62
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (-62.50%)` | :arrow_down: |
| [api/internal/filter/request\_id.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9yZXF1ZXN0X2lkLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `0.00% <0.00%> (-70.41%)` | :arrow_down: |
| [api/internal/filter/cors.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9jb3JzLmdv) | `0.00% <0.00%> (-66.67%)` | :arrow_down: |
| [api/internal/filter/schema.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9zY2hlbWEuZ28=) | `0.00% <0.00%> (-55.47%)` | :arrow_down: |
| [api/internal/handler/server\_info/server\_info.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc2VydmVyX2luZm8vc2VydmVyX2luZm8uZ28=) | `57.14% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/utils/closer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2Nsb3Nlci5nbw==) | `0.00% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/handler/consumer/consumer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvY29uc3VtZXIvY29uc3VtZXIuZ28=) | `63.82% <0.00%> (-27.66%)` | :arrow_down: |
| [api/internal/utils/json\_patch.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2pzb25fcGF0Y2guZ28=) | `34.48% <0.00%> (-24.14%)` | :arrow_down: |
| ... and [23 more](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [b659dfc...9ed0deb](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (600c7c2) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/b659dfc6aff50ffbe7553824575a61aa69cbc409?el=desc) (b659dfc) will **decrease** coverage by `11.99%`.
> The diff coverage is `69.29%`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
===========================================
- Coverage 62.67% 50.67% -12.00%
===========================================
Files 41 42 +1
Lines 2352 2601 +249
===========================================
- Hits 1474 1318 -156
- Misses 705 1065 +360
- Partials 173 218 +45
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `62.50% <ø> (ø)` | |
| [api/internal/handler/data\_loader/route\_export.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvZGF0YV9sb2FkZXIvcm91dGVfZXhwb3J0Lmdv) | `69.16% <69.16%> (ø)` | |
| [api/internal/route.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3JvdXRlLmdv) | `84.37% <100.00%> (+0.50%)` | :arrow_up: |
| [api/internal/core/store/query.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvcXVlcnkuZ28=) | `0.00% <0.00%> (-88.10%)` | :arrow_down: |
| [api/internal/utils/consts/api\_error.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2NvbnN0cy9hcGlfZXJyb3IuZ28=) | `17.39% <0.00%> (-78.27%)` | :arrow_down: |
| [api/internal/core/store/selector.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc2VsZWN0b3IuZ28=) | `0.00% <0.00%> (-75.93%)` | :arrow_down: |
| [api/internal/handler/plugin/plugin.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvcGx1Z2luL3BsdWdpbi5nbw==) | `16.66% <0.00%> (-70.00%)` | :arrow_down: |
| [api/internal/core/store/validate\_mock.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvdmFsaWRhdGVfbW9jay5nbw==) | `0.00% <0.00%> (-50.00%)` | :arrow_down: |
| [api/internal/filter/authentication.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9hdXRoZW50aWNhdGlvbi5nbw==) | `44.11% <0.00%> (-32.36%)` | :arrow_down: |
| [api/internal/core/store/store.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmUuZ28=) | `56.32% <0.00%> (-31.80%)` | :arrow_down: |
| ... and [17 more](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [b659dfc...600c7c2](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-764447946
> it would be better to add three more test cases:
>
> * export a route created with upstream id
> * export a route created with service which is created with upstream id
> * export a route created with `basic-auth` plugin enabled
> it would be better to add three more test cases:
>
> * export a route created with upstream id
> * export a route created with service which is created with upstream id
> * export a route created with `basic-auth` plugin enabled
I have added test cases for these three kinds of exports. Auth authentication has added the route export test cases opened by JWT plugin, and the following basic auth and API key will also be added.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r561537853
##########
File path: api/test/e2e/route_export_test.go
##########
@@ -0,0 +1,716 @@
+/*
+ * 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 e2e
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestRoute_Export(t *testing.T) {
+ exportStrR1 := `
+ "/hello_": {
+ "get": {
+ "operationId": "aaaaGet",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaaPost",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR1 = replaceStr(exportStrR1)
+
+ tests := []HttpTestCase{
+ {
+ Desc: "hit route that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "name": "aaaa",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uris": ["/hello_"],
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR1 + "}}",
+ },
+ }
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ exportStrR2 := `
+ "/hello2": {
+ "get": {
+ "operationId": "aaaa2Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ },
+ "post": {
+ "operationId": "aaaa2Post",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-hosts": ["foo.com", "*.bar.com"],
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 2,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }
+ }`
+ exportStrR2 = replaceStr(exportStrR2)
+
+ tests2 := []HttpTestCase{
+ {
+ Desc: "hit route2 that not exist",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "foo.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ },
+ {
+ Desc: "create route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r2",
+ Body: `{
+ "name": "aaaa2",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "status": 1,
+ "uri": "/hello2",
+ "hosts": ["foo.com", "*.bar.com"],
+ "methods": ["GET", "POST"],
+ "upstream": {
+ "nodes": {
+ "172.16.238.20:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "export route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "}}",
+ },
+ }
+ for _, tc := range tests2 {
+ testCaseCheck(tc, t)
+ }
+
+ tests3 := []HttpTestCase{
+ {
+ Desc: "export route route2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r1,r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "," + exportStrR1 + "}}",
+ },
+ {
+ Desc: "delete the route just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello_",
+ Headers: map[string]string{"Host": "bar.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "delete the route2 just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route2 just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello2",
+ Headers: map[string]string{"Host": "bar.com"},
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ }
+ for _, tc := range tests3 {
+ testCaseCheck(tc, t)
+ }
+
+ serviceStrS1 := `
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "enable_websocket": true
+ `
+ serviceStrS1 = replaceStr(serviceStrS1)
+
+ exportStrR3 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-serviceID": "s1",
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ exportStrR3 = replaceStr(exportStrR3)
+
+ // The test case tests the creation of a complete service.
+ // And according to the service_ ID to create a route.
+ // Test whether the plugin upstream label in the service is integrated with route and exported.
+ tests4 := []HttpTestCase{
+ {
+ Desc: "create service with all options",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ Body: `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "create_time":1602883670,
+ "update_time":1602893670,
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`,
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "get the service s1",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectCode: http.StatusOK,
+ ExpectBody: serviceStrS1,
+ },
+ {
+ Desc: "create route3 using the service just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r3",
+ Body: `{
+ "methods": ["GET"],
+ "uri": "/hello",
+ "service_id": "s1"
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "export route3",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/routes/export/r3",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "{\"code\":0,\"message\":\"\",\"data\":" + exportStrR3,
+ },
+ {
+ Desc: "delete the route3 just created",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r3",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "hit the route3 just deleted",
+ Object: APISIXExpect(t),
+ Method: http.MethodGet,
+ Path: "/hello",
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
+ Sleep: sleepTime,
+ },
+ {
+ Desc: "delete the service1",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ Sleep: sleepTime,
+ },
+ }
+ for _, tc := range tests4 {
+ testCaseCheck(tc, t)
+ }
+
+ serviceStrS2 := `
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ },
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ }
+ },
+ "labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "enable_websocket": true`
+ serviceStrS2 = replaceStr(serviceStrS2)
+
+ exportStrR4 := `{
+ "components": {},
+ "info": {
+ "title": "RoutesExport",
+ "version": "3.0.0"
+ },
+ "openapi": "3.0.0",
+ "paths": {
+ "/hello": {
+ "get": {
+ "operationId": "Get",
+ "requestBody": {},
+ "responses": {
+ "default": {
+ "description": ""
+ }
+ },
+ "security": [],
+ "x-apisix-enableWebsocket": false,
+ "x-apisix-labels": {
+ "build": "16",
+ "env": "production",
+ "version": "v2"
+ },
+ "x-apisix-plugins": {
+ "limit-count": {
+ "count": 100,
+ "key": "remote_addr",
+ "rejected_code": 503,
+ "time_window": 60
+ },
+ "prometheus": {
+ "disable": false
+ }
+ },
+ "x-apisix-priority": 0,
+ "x-apisix-serviceID": "s2",
+ "x-apisix-status": 1,
+ "x-apisix-upstream": {
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }],
+ "type": "roundrobin"
+ }
+ }
+ }
+ }
+ }`
+ exportStrR4 = replaceStr(exportStrR4)
+ // This test case tests the creation of a complete service.
+ // And create a complete route, export rules as route upstream data for high priority, direct use.
+ // However, if the data in the service (label, plugins) does not exist in the route, it will be fused and exported.
+ // If it exists, the data in route will be used first
+ tests5 := []HttpTestCase{
+ {
+ Desc: "create service with all options",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s2",
+ Headers: map[string]string{"Authorization": token},
+ Body: `{
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": {
+ "build":"16",
+ "env":"production",
+ "version":"v2"
+ },
+ "enable_websocket":true,
+ "plugins": {
+ "limit-count": {
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "create_time":1602883670,
+ "update_time":1602893670,
+ "nodes": [{
+ "host": "172.16.238.20",
+ "port": 1980,
+ "weight": 1
+ }]
+ }
+ }`,
+ ExpectStatus: http.StatusOK,
+ },
+ {
+ Desc: "get the service s2",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s2",
+ Headers: map[string]string{"Authorization": token},
+ ExpectCode: http.StatusOK,
+ ExpectBody: serviceStrS2,
+ },
+ {
+ Desc: "create route4 using the service just created",
Review comment:
you'd better to update the desc too
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] juzhiyuan commented on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-766602482
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r564320594
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,478 @@
+/*
+ * 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 data_loader
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
Review comment:
> Question:
>
> 1. Why the first API's method is `POST`
> 2. "/apisix/admin/exportall/routes" is not very good. cc @liuxiran
1. `GET` would be better
2. In fact, we didn't find a very satisfactory name either, do you have any suggestions? @starsz
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#issuecomment-757579417
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=h1) Report
> Merging [#1245](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=desc) (7414998) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/bb0a0575f9207fc57aacf79ab5f9defb4cb06166?el=desc) (bb0a057) will **decrease** coverage by `18.18%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #1245 +/- ##
===========================================
- Coverage 62.50% 44.31% -18.19%
===========================================
Files 41 32 -9
Lines 2360 1961 -399
===========================================
- Hits 1475 869 -606
- Misses 711 981 +270
+ Partials 174 111 -63
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/entity.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2VudGl0eS5nbw==) | `0.00% <ø> (-62.50%)` | :arrow_down: |
| [api/internal/filter/request\_id.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9yZXF1ZXN0X2lkLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [api/internal/core/store/storehub.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmVodWIuZ28=) | `0.00% <0.00%> (-70.41%)` | :arrow_down: |
| [api/internal/filter/cors.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9jb3JzLmdv) | `0.00% <0.00%> (-66.67%)` | :arrow_down: |
| [api/internal/filter/schema.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2ZpbHRlci9zY2hlbWEuZ28=) | `0.00% <0.00%> (-55.47%)` | :arrow_down: |
| [api/internal/handler/server\_info/server\_info.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvc2VydmVyX2luZm8vc2VydmVyX2luZm8uZ28=) | `57.14% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/utils/closer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2Nsb3Nlci5nbw==) | `0.00% <0.00%> (-33.34%)` | :arrow_down: |
| [api/internal/handler/consumer/consumer.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2hhbmRsZXIvY29uc3VtZXIvY29uc3VtZXIuZ28=) | `63.82% <0.00%> (-27.66%)` | :arrow_down: |
| [api/internal/utils/json\_patch.go](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL3V0aWxzL2pzb25fcGF0Y2guZ28=) | `34.48% <0.00%> (-24.14%)` | :arrow_down: |
| ... and [23 more](https://codecov.io/gh/apache/apisix-dashboard/pull/1245/diff?src=pr&el=tree-more) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=footer). Last update [bb0a057...7414998](https://codecov.io/gh/apache/apisix-dashboard/pull/1245?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r555622889
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,365 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "reflect"
+ "regexp"
+ "strings"
+
+ "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/log"
+ "github.com/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
Review comment:
done.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] liuxiran commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
liuxiran commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r564247908
##########
File path: api/internal/handler/data_loader/route_export.go
##########
@@ -0,0 +1,441 @@
+/*
+ * 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 data_loader
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "reflect"
+ "strings"
+
+ "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/log"
+ "github.com/apisix/manager-api/internal/utils"
+ "github.com/getkin/kin-openapi/openapi3"
+ "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"
+)
+
+type Handler struct {
+ routeStore store.Interface
+ upstreamStore store.Interface
+ serviceStore store.Interface
+ consumerStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ upstreamStore: store.GetStore(store.HubKeyUpstream),
+ serviceStore: store.GetStore(store.HubKeyService),
+ consumerStore: store.GetStore(store.HubKeyConsumer),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+ r.GET("/apisix/admin/exportall/routes", wgin.Wraps(h.ExportAllRoutes))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+
+ for _, id := range ids {
+ route, err := h.routeStore.Get(context.Background(), id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "RoutesExport"
+ service interface{}
+ err error
+)
+
+func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
+ routelist, err := h.routeStore.List(c.Context(), store.ListInput{})
+
+ if err != nil {
+ return nil, err
+ }
+
+ routes := []*entity.Route{}
+
+ for _, route := range routelist.Rows {
+ routes = append(routes, route.(*entity.Route))
+ }
+
+ swagger, err := h.routeToOpenApi3(routes)
+ if err != nil {
+ return nil, err
+ }
+ return swagger, nil
+}
+
+func (h *Handler) routeToOpenApi3(routes []*entity.Route) (*openapi3.Swagger, error) {
+ paths := openapi3.Paths{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ servicePlugins := make(map[string]interface{})
+ plugins := make(map[string]interface{})
+ serviceLabels := make(map[string]string)
+ labels := make(map[string]string)
+
+ for _, route := range routes {
+ extensions := make(map[string]interface{})
+ pathItem := &openapi3.PathItem{}
+ path := openapi3.Operation{}
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+
+ if route.ServiceID != nil {
+ serviceID := utils.InterfaceToString(route.ServiceID)
+ service, err = h.serviceStore.Get(context.Background(), serviceID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
+ }
+ return nil, err
+ }
+
+ _service := service.(*entity.Service)
+ servicePlugins = _service.Plugins
+ serviceLabels = _service.Labels
+ }
+
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ } else if route.UpstreamID != nil && route.Upstream == nil {
+ upstreamID := utils.InterfaceToString(route.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ } else if route.UpstreamID == nil && route.Upstream == nil && route.ServiceID != nil {
+ _service := service.(*entity.Service)
+ if _service.Upstream != nil {
+ extensions["x-apisix-upstream"] = _service.Upstream
+ } else if _service.Upstream == nil && _service.UpstreamID != nil {
+ upstreamID := utils.InterfaceToString(_service.UpstreamID)
+ upstream, err := h.upstreamStore.Get(context.Background(), upstreamID)
+ if err != nil {
+ if err == data.ErrNotFound {
+ return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
+ }
+ return nil, err
+ }
+ extensions["x-apisix-upstream"] = upstream
+ }
+ }
+
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+
+ //Parse Labels
+ labels, err = parseLabels(route, serviceLabels, labels)
+ if err != nil {
+ log.Errorf("parseLabels err: ", err)
+ return nil, err
+ }
+
+ if labels != nil {
+ extensions["x-apisix-labels"] = labels
+ }
+
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remote_addr"] = route.RemoteAddr
+ }
+
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remote_addrs"] = route.RemoteAddrs
+ }
+
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filter_func"] = route.FilterFunc
+ }
+
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-service_protocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+
+ // analysis route.URIs
+ routeURIs := []string{}
+ if route.URI != "" {
+ routeURIs = append(routeURIs, route.URI)
+ }
+
+ if route.Uris != nil {
+ routeURIs = route.Uris
+ }
+
+ for _, uri := range routeURIs {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
Review comment:
it seems that this case has been fixed, right @Jaycean ?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] Jaycean commented on a change in pull request #1245: feat(BE): Export route from OpenAPI Specification3.0
Posted by GitBox <gi...@apache.org>.
Jaycean commented on a change in pull request #1245:
URL: https://github.com/apache/apisix-dashboard/pull/1245#discussion_r554654425
##########
File path: api/internal/handler/route_export/route_export.go
##########
@@ -0,0 +1,349 @@
+/*
+ * 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 route_export
+
+import (
+ "encoding/json"
+ "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/getkin/kin-openapi/openapi3"
+ "github.com/gin-gonic/gin"
+ "github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/wrapper"
+ wgin "github.com/shiningrush/droplet/wrapper/gin"
+ "reflect"
+ "regexp"
+ "strings"
+)
+
+type Handler struct {
+ routeStore store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+ return &Handler{
+ routeStore: store.GetStore(store.HubKeyRoute),
+ }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+ r.POST("/apisix/admin/routes/export/:ids", wgin.Wraps(h.ExportRoutes,
+ wrapper.InputType(reflect.TypeOf(ExportInput{}))))
+}
+
+type ExportInput struct {
+ IDs string `auto_read:"ids,path"`
+}
+
+func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
+ input := c.Input().(*ExportInput)
+ ids := strings.Split(input.IDs, ",")
+ routes := []*entity.Route{}
+ for _, id := range ids {
+ route, err := h.routeStore.Get(id)
+ if err != nil {
+ return nil, err
+ }
+ routes = append(routes, route.(*entity.Route))
+
+ }
+ return routeToOpenApi3(routes), nil
+}
+
+type AuthType string
+
+const (
+ BasicAuth AuthType = "basic-auth"
+ KeyAuth AuthType = "key-auth"
+ JWTAuth AuthType = "jwt-auth"
+)
+
+var (
+ openApi = "3.0.0"
+ title = "Routes Export"
+)
+
+func routeToOpenApi3(routes []*entity.Route) *openapi3.Swagger {
+ paths := openapi3.Paths{}
+ pathItem := &openapi3.PathItem{}
+ path := &openapi3.Operation{}
+ paramsRefs := []*openapi3.ParameterRef{}
+ requestBody := &openapi3.RequestBody{}
+ components := &openapi3.Components{}
+ secSchemas := openapi3.SecuritySchemes{}
+ for _, route := range routes {
+
+ extensions := make(map[string]interface{})
+ path.Summary = route.Desc
+ path.OperationID = route.Name
+ if route.URI != "" {
+ extensions["x-apisix-uri"] = route.URI
+ }
+ if route.Upstream != nil {
+ extensions["x-apisix-upstream"] = route.Upstream
+ }
+ if route.Host != "" {
+ extensions["x-apisix-host"] = route.Host
+ }
+ if route.Hosts != nil {
+ extensions["x-apisix-hosts"] = route.Hosts
+ }
+ if route.Labels != nil {
+ extensions["x-apisix-labels"] = route.Labels
+ }
+ if route.RemoteAddr != "" {
+ extensions["x-apisix-remoteAddr"] = route.RemoteAddr
+ }
+ if route.RemoteAddrs != nil {
+ extensions["x-apisix-remoteAddrs"] = route.RemoteAddrs
+ }
+ if route.FilterFunc != "" {
+ extensions["x-apisix-filterFunc"] = route.FilterFunc
+ }
+ if route.ServiceID != nil {
+ extensions["x-apisix-serviceID"] = route.ServiceID
+ }
+ if route.UpstreamID != nil {
+ extensions["x-apisix-upstreamID"] = route.UpstreamID
+ }
+ if route.Script != nil {
+ extensions["x-apisix-script"] = route.Script
+ }
+ if route.ServiceProtocol != "" {
+ extensions["x-apisix-serviceProtocol"] = route.ServiceProtocol
+ }
+ if route.Vars != nil {
+ extensions["x-apisix-vars"] = route.Vars
+ }
+ // analysis route.URIs
+ for _, uri := range route.Uris {
+ if strings.Contains(uri, "*") {
+ paths[strings.Split(uri, "*")[0]+"{params}"] = pathItem
+ // add params introduce
+ paramsRefs = append(paramsRefs, &openapi3.ParameterRef{
+ Value: &openapi3.Parameter{
+ In: "path",
+ Name: "params",
+ Required: true,
+ Description: "params in path",
+ Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}}})
+ } else {
+ paths[uri] = pathItem
+ }
+ }
+ if route.Plugins != nil {
+ param := &openapi3.Parameter{}
+ secReq := &openapi3.SecurityRequirements{}
+ plugins := make(map[string]interface{})
+ // analysis plugins
+ for key, value := range route.Plugins {
+ // analysis proxy-rewrite plugin
+ //if key == "proxy-rewrite" {
+ // continue
+ //}
+
+ // analysis request-validation plugin
+ if key == "request-validation" {
+ for k, v := range value.(map[string]interface{}) {
+ if k == "header_schema" {
+ param.In = "header"
+ requestValidation := &entity.RequestValidation{}
+ reqBytes, _ := json.Marshal(&v)
+ err := json.Unmarshal(reqBytes, requestValidation)
Review comment:
Thks for checking.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org