Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix and update API handling #128

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const docTemplate = `{
"title": "{{.Title}}",
"contact": {
"name": "API Support",
"url": "http://cloud-barista.github.io",
"email": "[email protected]"
"url": "https://github.com/cloud-barista/cm-beetle/issues/new/choose"
},
"license": {
"name": "Apache 2.0",
Expand All @@ -36,6 +35,7 @@ const docTemplate = `{
"[Admin] System management"
],
"summary": "Check HTTP version of incoming request",
"operationId": "CheckHTTPVersion",
"parameters": [
{
"type": "string",
Expand Down Expand Up @@ -258,6 +258,7 @@ const docTemplate = `{
"[Admin] System management"
],
"summary": "Check Beetle is ready",
"operationId": "GetReadyz",
"parameters": [
{
"type": "string",
Expand Down Expand Up @@ -635,6 +636,7 @@ const docTemplate = `{
"[Test] Utility"
],
"summary": "Test tracing to Tumblebug",
"operationId": "TestTracing",
"parameters": [
{
"type": "string",
Expand Down Expand Up @@ -1234,15 +1236,15 @@ const docTemplate = `{
}
},
"externalDocs": {
"description": "▶▶▶ CB-Tumblebug REST API",
"description": "▶▶▶ CB-Tumblebug REST API (access via Beetle's reverse proxy)",
"url": "http://localhost:8056/tumblebug/api/index.html"
}
}`

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "latest",
Host: "",
Host: "localhost:8056",
BasePath: "/beetle",
Schemes: []string{},
Title: "CM-Beetle REST API",
Expand Down
9 changes: 6 additions & 3 deletions api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"title": "CM-Beetle REST API",
"contact": {
"name": "API Support",
"url": "http://cloud-barista.github.io",
"email": "[email protected]"
"url": "https://github.com/cloud-barista/cm-beetle/issues/new/choose"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "latest"
},
"host": "localhost:8056",
"basePath": "/beetle",
"paths": {
"/httpVersion": {
Expand All @@ -29,6 +29,7 @@
"[Admin] System management"
],
"summary": "Check HTTP version of incoming request",
"operationId": "CheckHTTPVersion",
"parameters": [
{
"type": "string",
Expand Down Expand Up @@ -251,6 +252,7 @@
"[Admin] System management"
],
"summary": "Check Beetle is ready",
"operationId": "GetReadyz",
"parameters": [
{
"type": "string",
Expand Down Expand Up @@ -628,6 +630,7 @@
"[Test] Utility"
],
"summary": "Test tracing to Tumblebug",
"operationId": "TestTracing",
"parameters": [
{
"type": "string",
Expand Down Expand Up @@ -1227,7 +1230,7 @@
}
},
"externalDocs": {
"description": "▶▶▶ CB-Tumblebug REST API",
"description": "▶▶▶ CB-Tumblebug REST API (access via Beetle's reverse proxy)",
"url": "http://localhost:8056/tumblebug/api/index.html"
}
}
9 changes: 6 additions & 3 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,13 @@ definitions:
$ref: '#/definitions/onprem.DiskProperty'
type: object
externalDocs:
description: ▶▶▶ CB-Tumblebug REST API
description: ▶▶▶ CB-Tumblebug REST API (access via Beetle's reverse proxy)
url: http://localhost:8056/tumblebug/api/index.html
host: localhost:8056
info:
contact:
email: [email protected]
name: API Support
url: http://cloud-barista.github.io
url: https://github.com/cloud-barista/cm-beetle/issues/new/choose
description: CM-Beetle REST API
license:
name: Apache 2.0
Expand All @@ -440,6 +440,7 @@ paths:
- application/json
description: Checks and logs the HTTP version of the incoming request to the
server console.
operationId: CheckHTTPVersion
parameters:
- description: 'Custom request ID (NOTE: It will be used as a trace ID.)'
in: header
Expand Down Expand Up @@ -592,6 +593,7 @@ paths:
consumes:
- application/json
description: Check Beetle is ready
operationId: GetReadyz
parameters:
- description: 'Custom request ID (NOTE: It will be used as a trace ID.)'
in: header
Expand Down Expand Up @@ -850,6 +852,7 @@ paths:
consumes:
- application/json
description: Test tracing to Tumblebug
operationId: TestTracing
parameters:
- description: 'Custom request ID (NOTE: It will be used as a trace ID.)'
in: header
Expand Down
7 changes: 4 additions & 3 deletions cmd/cm-beetle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,20 @@ func checkReadiness(url string) (bool, error) {
// @title CM-Beetle REST API
// @version latest
// @description CM-Beetle REST API
// // @termsOfService none

// @contact.name API Support
// @contact.url http://cloud-barista.github.io
// @contact.email [email protected]
// @contact.url https://github.com/cloud-barista/cm-beetle/issues/new/choose

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host localhost:8056
// @BasePath /beetle

// @securityDefinitions.basic BasicAuth

// @externalDocs.description ▶▶▶ CB-Tumblebug REST API
// @externalDocs.description ▶▶▶ CB-Tumblebug REST API (access via Beetle's reverse proxy)
// @externalDocs.url http://localhost:8056/tumblebug/api/index.html

func main() {
Expand Down
6 changes: 6 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -519,11 +519,14 @@ github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQ
github.com/zcalusic/sysinfo v1.0.2/go.mod h1:kluzTYflRWo6/tXVMJPdEjShsbPpsFRyy+p1mBQPC30=
github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0=
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738 h1:VcrIfasaLFkyjk6KNlXQSzO+B0fZcnECiDrKJsfxka0=
go.etcd.io/etcd/api/v3 v3.5.12 h1:W4sw5ZoU2Juc9gBWuLk5U6fHfNVyY1WC5g9uiXZio/c=
go.etcd.io/etcd/api/v3 v3.5.12/go.mod h1:Ot+o0SWSyT6uHhA56al1oCED0JImsRiU9Dc26+C2a+4=
go.etcd.io/etcd/client/pkg/v3 v3.5.12 h1:EYDL6pWwyOsylrQyLp2w+HkQ46ATiOvoEdMarindU2A=
go.etcd.io/etcd/client/pkg/v3 v3.5.12/go.mod h1:seTzl2d9APP8R5Y2hFL3NVlD6qC/dOT+3kvrqPyTas4=
go.etcd.io/etcd/client/v2 v2.305.7/go.mod h1:GQGT5Z3TBuAQGvgPfhR7VPySu/SudxmEkRq9BgzFU6s=
go.etcd.io/etcd/client/v2 v2.305.10/go.mod h1:m3CKZi69HzilhVqtPDcjhSGp+kA1OmbNn0qamH80xjA=
go.etcd.io/etcd/client/v2 v2.305.12/go.mod h1:aQ/yhsxMu+Oht1FOupSr60oBvcS9cKXHrzBpDsPTf9E=
go.etcd.io/etcd/client/v3 v3.5.12 h1:v5lCPXn1pf1Uu3M4laUE2hp/geOTc5uPcYYsNe1lDxg=
go.etcd.io/etcd/client/v3 v3.5.12/go.mod h1:tSbBCakoWmmddL+BKVAJHa9km+O/E+bumDe9mSbPiqw=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0/go.mod h1:Mjt1i1INqiaoZOMGR1RIUJN+i3ChKoFRqzrRQhlkbs0=
Expand Down Expand Up @@ -582,8 +585,11 @@ google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCID
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 h1:9+tzLLstTlPTRyJTh+ah5wIMsBW5c4tQwGTN3thOW9Y=
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9/go.mod h1:mqHbVIp48Muh7Ywss/AD6I5kNVKZMmAa/QEW58Gxp2s=
google.golang.org/genproto/googleapis/api v0.0.0-20240311132316-a219d84964c2 h1:rIo7ocm2roD9DcFIX67Ym8icoGCKSARAiPljFhh5suQ=
google.golang.org/genproto/googleapis/api v0.0.0-20240311132316-a219d84964c2/go.mod h1:O1cOfN1Cy6QEYr7VxtjOyP5AdAuR0aJ/MYZaaof623Y=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c h1:lfpJ/2rWPa/kJgxyyXM8PrNnfCzcmxJ265mADgwmvLI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk=
google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
Expand Down
16 changes: 9 additions & 7 deletions pkg/api/rest/common/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ type SimpleMessage struct {
common.SimpleMsg
}

// RestGetReadyz func check if CM-Beetle server is ready or not.
// RestGetReadyz godoc
// GetReadyz godoc
// @ID GetReadyz
// @Summary Check Beetle is ready
// @Description Check Beetle is ready
// @Tags [Admin] System management
Expand All @@ -85,7 +85,7 @@ type SimpleMessage struct {
// @Success 200 {object} SimpleMessage
// @Failure 503 {object} SimpleMessage
// @Router /readyz [get]
func RestGetReadyz(c echo.Context) error {
func GetReadyz(c echo.Context) error {

ctx := c.Request().Context() // Get context
log.Ctx(ctx).Info().Msg("RestGetReadyz called") // Log ctx to trace
Expand All @@ -99,7 +99,8 @@ func RestGetReadyz(c echo.Context) error {
return c.JSON(http.StatusOK, &message)
}

// RestCheckHTTPVersion godoc
// CheckHTTPVersion godoc
// @ID CheckHTTPVersion
// @Summary Check HTTP version of incoming request
// @Description Checks and logs the HTTP version of the incoming request to the server console.
// @Tags [Admin] System management
Expand All @@ -110,7 +111,7 @@ func RestGetReadyz(c echo.Context) error {
// @Failure 404 {object} SimpleMessage
// @Failure 500 {object} SimpleMessage
// @Router /httpVersion [get]
func RestCheckHTTPVersion(c echo.Context) error {
func CheckHTTPVersion(c echo.Context) error {
// Access the *http.Request object from the echo.Context
req := c.Request()

Expand All @@ -121,7 +122,8 @@ func RestCheckHTTPVersion(c echo.Context) error {
return c.JSON(http.StatusOK, &okMessage)
}

// RestGetTestTracingToTumblebug godoc
// TestTracing godoc
// @ID TestTracing
// @Summary Test tracing to Tumblebug
// @Description Test tracing to Tumblebug
// @Tags [Test] Utility
Expand All @@ -131,7 +133,7 @@ func RestCheckHTTPVersion(c echo.Context) error {
// @Success 200 {object} SimpleMessage
// @Failure 503 {object} SimpleMessage
// @Router /test/tracing [get]
func RestGetTestTracing(c echo.Context) error {
func TestTracing(c echo.Context) error {

ctx := c.Request().Context() // Get context
log.Ctx(ctx).Info().Msg("RestGetReadyz called") // Log ctx to trace
Expand Down
62 changes: 62 additions & 0 deletions pkg/api/rest/middlewares/tumblebug-init-checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package middlewares

import (
"fmt"
"net/http"

"github.com/cloud-barista/cm-beetle/pkg/config"
"github.com/cloud-barista/cm-beetle/pkg/core/common"
"github.com/go-resty/resty/v2"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"

tbResource "github.com/cloud-barista/cb-tumblebug/src/api/rest/server/resource"
)

var isTbInitalized bool = false

// TumblebugInitChecker
func TumblebugInitChecker(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {

if !isTbInitalized {
// Initialize resty client with basic auth
client := resty.New()
apiUser := config.Tumblebug.API.Username
apiPass := config.Tumblebug.API.Password
client.SetBasicAuth(apiUser, apiPass)

// set tumblebug rest url
epTumblebug := config.Tumblebug.RestUrl

// Search and set a target VM spec
method := "GET"
nsId := "system"
url := fmt.Sprintf("%s/ns/%s/resources/image", epTumblebug, nsId)

tbReqt := common.NoBody
tbResp := tbResource.RestGetAllImageResponse{}

err := common.ExecuteHttpRequest(
client,
method,
url,
nil,
common.SetUseBody(tbReqt),
&tbReqt,
&tbResp,
common.VeryShortDuration,
)

if err != nil {
log.Warn().Err(err).Msg("Tumblebug needs to be initialized.")
res := common.SimpleMsg{Message: "Tumblebug needs to be initialized. See https://github.com/cloud-barista/cb-tumblebug?tab=readme-ov-file#3-initialize-cb-tumblebug-to-configure-multi-cloud-info"}
return c.JSON(http.StatusServiceUnavailable, res)
}

isTbInitalized = true
}

return next(c)
}
}
10 changes: 7 additions & 3 deletions pkg/api/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ func RunServer(port string) {
gBeetle.GET("/api/*", echoSwagger.WrapHandler)

// System management APIs
gBeetle.GET("/readyz", rest_common.RestGetReadyz)
gBeetle.GET("/httpVersion", rest_common.RestCheckHTTPVersion)
gBeetle.GET("/readyz", rest_common.GetReadyz)
gBeetle.GET("/httpVersion", rest_common.CheckHTTPVersion)

// Test utility APIs
gBeetle.GET("/test/tracing", rest_common.RestGetTestTracing)
gBeetle.GET("/test/tracing", rest_common.TestTracing)

// Namespace API group
// gNamespace := gBeetle.Group("/ns")
Expand All @@ -217,6 +217,10 @@ func RunServer(port string) {

// Recommendation API group
gRecommendation := gBeetle.Group("/recommendation")
// Custom middleware to check if the Tumblebug is initialized
gRecommendation.Use(middlewares.TumblebugInitChecker)

// Recommendation APIs
gRecommendation.POST("/mci", controller.RecommendInfra)

// Migration API group
Expand Down