Skip to content

Commit

Permalink
Merge pull request cloud-barista#1429 from seokho-son/main
Browse files Browse the repository at this point in the history
Add forward any (GET) request to any (CB-Spider) internal system
  • Loading branch information
yunkon-kim authored Jan 17, 2024
2 parents d66ef5a + dfee4ae commit 278c733
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,48 @@ const docTemplate = `{
}
}
},
"/forward/{path}": {
"post": {
"description": "Forward any (GET) request to CB-Spider",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Admin] System utility"
],
"summary": "Forward any (GET) request to CB-Spider",
"parameters": [
{
"type": "string",
"default": "vmspec",
"description": "Internal call path to CB-Spider (path without /spider/ prefix) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] for more details",
"name": "path",
"in": "path",
"required": true
},
{
"description": "Request body (various formats) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] for more details",
"name": "Request",
"in": "body",
"schema": {
"type": "object"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "object",
"additionalProperties": true
}
}
}
}
},
"/health": {
"get": {
"description": "Check Tumblebug is alive",
Expand Down
42 changes: 42 additions & 0 deletions src/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,48 @@
}
}
},
"/forward/{path}": {
"post": {
"description": "Forward any (GET) request to CB-Spider",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Admin] System utility"
],
"summary": "Forward any (GET) request to CB-Spider",
"parameters": [
{
"type": "string",
"default": "vmspec",
"description": "Internal call path to CB-Spider (path without /spider/ prefix) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] for more details",
"name": "path",
"in": "path",
"required": true
},
{
"description": "Request body (various formats) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] for more details",
"name": "Request",
"in": "body",
"schema": {
"type": "object"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "object",
"additionalProperties": true
}
}
}
}
},
"/health": {
"get": {
"description": "Check Tumblebug is alive",
Expand Down
31 changes: 31 additions & 0 deletions src/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2715,6 +2715,37 @@ paths:
summary: Get registered ConnConfig info
tags:
- '[Admin] Multi-Cloud environment configuration'
/forward/{path}:
post:
consumes:
- application/json
description: Forward any (GET) request to CB-Spider
parameters:
- default: vmspec
description: Internal call path to CB-Spider (path without /spider/ prefix)
- see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc]
for more details
in: path
name: path
required: true
type: string
- description: Request body (various formats) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc]
for more details
in: body
name: Request
schema:
type: object
produces:
- application/json
responses:
"200":
description: OK
schema:
additionalProperties: true
type: object
summary: Forward any (GET) request to CB-Spider
tags:
- '[Admin] System utility'
/health:
get:
consumes:
Expand Down
38 changes: 38 additions & 0 deletions src/api/rest/server/common/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package common
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"

"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -443,3 +445,39 @@ func RestRegisterCspNativeResourcesAll(c echo.Context) error {
content, err := mcis.RegisterCspNativeResourcesAll(u.NsId, u.McisName, option, mcisFlag)
return common.EndRequestWithLog(c, reqID, err, content)
}

// RestForwardAnyReqToAny godoc
// @Summary Forward any (GET) request to CB-Spider
// @Description Forward any (GET) request to CB-Spider
// @Tags [Admin] System utility
// @Accept json
// @Produce json
// @Param path path string true "Internal call path to CB-Spider (path without /spider/ prefix) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] for more details"" default(vmspec)
// @Param Request body interface{} false "Request body (various formats) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] for more details"
// @Success 200 {object} map[string]interface{}
// @Router /forward/{path} [post]
func RestForwardAnyReqToAny(c echo.Context) error {
reqID := common.StartRequestWithLog(c)
reqPath := c.Param("*")
reqPath, err := url.PathUnescape(reqPath)
if err != nil {
return common.EndRequestWithLog(c, reqID, err, nil)
}

fmt.Printf("reqPath: %s\n", reqPath)

method := "GET"
var requestBody interface{}
if c.Request().Body != nil {
bodyBytes, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
return common.EndRequestWithLog(c, reqID, fmt.Errorf("Failed to read request body: %v", err), nil)
}
requestBody = bodyBytes
} else {
requestBody = common.NoBody
}

content, err := common.ForwardRequestToAny(reqPath, method, requestBody)
return common.EndRequestWithLog(c, reqID, err, content)
}
2 changes: 2 additions & 0 deletions src/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ func RunServer(port string) {
e.GET("/tumblebug/ns/:nsId/loadDefaultResource", rest_mcir.RestLoadDefaultResource)
e.DELETE("/tumblebug/ns/:nsId/defaultResources", rest_mcir.RestDelAllDefaultResources)

e.POST("/tumblebug/forward/*", rest_common.RestForwardAnyReqToAny)

// Route for NameSpace subgroup
g := e.Group("/tumblebug/ns", common.NsValidation())

Expand Down
38 changes: 38 additions & 0 deletions src/core/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,41 @@ func EndRequestWithLog(c echo.Context, reqID string, err error, responseData int

return c.JSON(http.StatusNotFound, map[string]string{"message": "Invalid Request ID"})
}

// ForwardRequestToAny forwards the given request to the specified path
func ForwardRequestToAny(reqPath string, method string, requestBody interface{}) (interface{}, error) {
client := resty.New()
var callResult interface{}

url := SpiderRestUrl + "/" + reqPath

var requestBodyBytes []byte
var ok bool
if requestBodyBytes, ok = requestBody.([]byte); !ok {
return nil, fmt.Errorf("requestBody is not []byte type")
}

var requestBodyMap map[string]interface{}
err := json.Unmarshal(requestBodyBytes, &requestBodyMap)
if err != nil {
return nil, fmt.Errorf("JSON unmarshal error: %v", err)
}

err = ExecuteHttpRequest(
client,
method,
url,
nil,
SetUseBody(requestBodyMap),
&requestBodyMap,
&callResult,
MediumDuration,
)

if err != nil {
CBLog.Error(err)
return nil, err
}

return callResult, nil
}

0 comments on commit 278c733

Please sign in to comment.