Skip to content

Commit

Permalink
Implement Region/Zone server module and REST API to CB-Spider Server
Browse files Browse the repository at this point in the history
  • Loading branch information
powerkimhub committed Sep 20, 2023
1 parent 0cc452f commit f6541dd
Show file tree
Hide file tree
Showing 3 changed files with 285 additions and 12 deletions.
147 changes: 147 additions & 0 deletions api-runtime/common-runtime/RegionZoneHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Cloud Control Manager's Rest Runtime of CB-Spider.
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2023.09.

package commonruntime

import (
ccm "github.com/cloud-barista/cb-spider/cloud-control-manager"
cres "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
)

// ================ RegionZone Handler
func ListRegionZone(connectionName string) ([]*cres.RegionZoneInfo, error) {
cblog.Info("call ListRegionZone()")

// check empty and trim user inputs
connectionName, err := EmptyCheckAndTrim("connectionName", connectionName)
if err != nil {
cblog.Error(err)
return nil, err
}

cldConn, err := ccm.GetCloudConnection(connectionName)
if err != nil {
cblog.Error(err)
return nil, err
}

handler, err := cldConn.CreateRegionZoneHandler()
if err != nil {
cblog.Error(err)
return nil, err
}

infoList, err := handler.ListRegionZone()
if err != nil {
cblog.Error(err)
return nil, err
}

if infoList == nil || len(infoList) <= 0 {
infoList = []*cres.RegionZoneInfo{}
}

return infoList, nil
}

func GetRegionZone(connectionName string, nameID string) (*cres.RegionZoneInfo, error) {
cblog.Info("call GetRegionZone()")

// check empty and trim user inputs
connectionName, err := EmptyCheckAndTrim("connectionName", connectionName)
if err != nil {
cblog.Error(err)
return nil, err
}

nameID, err = EmptyCheckAndTrim("nameID", nameID)
if err != nil {
cblog.Error(err)
return nil, err
}

cldConn, err := ccm.GetCloudConnection(connectionName)
if err != nil {
cblog.Error(err)
return nil, err
}

handler, err := cldConn.CreateRegionZoneHandler()
if err != nil {
cblog.Error(err)
return nil, err
}
info, err := handler.GetRegionZone(nameID)
if err != nil {
cblog.Error(err)
return nil, err
}

return &info, nil
}

func ListOrgRegion(connectionName string) (string, error) {
cblog.Info("call ListOrgRegion()")

// check empty and trim user inputs
connectionName, err := EmptyCheckAndTrim("connectionName", connectionName)
if err != nil {
cblog.Error(err)
return "", err
}

cldConn, err := ccm.GetCloudConnection(connectionName)
if err != nil {
cblog.Error(err)
return "", err
}

handler, err := cldConn.CreateRegionZoneHandler()
if err != nil {
cblog.Error(err)
return "", err
}

infoList, err := handler.ListOrgRegion()
if err != nil {
cblog.Error(err)
return "", err
}

return infoList, nil
}

func ListOrgZone(connectionName string) (string, error) {
cblog.Info("call GetOrgRegionZone()")

// check empty and trim user inputs
connectionName, err := EmptyCheckAndTrim("connectionName", connectionName)
if err != nil {
cblog.Error(err)
return "", err
}

cldConn, err := ccm.GetCloudConnection(connectionName)
if err != nil {
cblog.Error(err)
return "", err
}

handler, err := cldConn.CreateRegionZoneHandler()
if err != nil {
cblog.Error(err)
return "", err
}
info, err := handler.ListOrgZone()
if err != nil {
cblog.Error(err)
return "", err
}

return info, nil
}
28 changes: 16 additions & 12 deletions api-runtime/rest-runtime/CBSpiderRuntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type StatusInfo struct {
Status string // PENDING | RUNNING | SUSPENDING | SUSPENDED | REBOOTING | TERMINATING | TERMINATED
}

//ex) {"POST", "/driver", registerCloudDriver}
// ex) {"POST", "/driver", registerCloudDriver}
type route struct {
method, path string
function echo.HandlerFunc
Expand Down Expand Up @@ -224,6 +224,12 @@ func RunServer() {

//-------------------------------------------------------------------//

//----------RegionZone Handler
{"GET", "/regionzone", ListRegionZone},
{"GET", "/regionzone/:Name", GetRegionZone},
{"GET", "/orgregion", ListOrgRegion},
{"GET", "/orgzone", ListOrgZone},

//----------Image Handler
{"POST", "/vmimage", CreateImage},
{"GET", "/vmimage", ListImage},
Expand Down Expand Up @@ -334,7 +340,6 @@ func RunServer() {
{"GET", "/allnlb", ListAllNLB},
{"DELETE", "/cspnlb/:Id", DeleteCSPNLB},


//----------Disk Handler
{"POST", "/regdisk", RegisterDisk},
{"DELETE", "/regdisk/:Name", UnregisterDisk},
Expand All @@ -352,7 +357,6 @@ func RunServer() {
{"GET", "/alldisk", ListAllDisk},
{"DELETE", "/cspdisk/:Id", DeleteCSPDisk},


//----------MyImage Handler
{"POST", "/regmyimage", RegisterMyImage},
{"DELETE", "/regmyimage/:Name", UnregisterMyImage},
Expand Down Expand Up @@ -391,7 +395,7 @@ func RunServer() {
{"GET", "/nscluster", AllClusterList},

//-------------------------------------------------------------------//
//----------Additional Info
//----------Additional Info
{"GET", "/cspresourcename/:Name", GetCSPResourceName},
{"GET", "/cspresourceinfo/:Name", GetCSPResourceInfo},
//----------AnyCall Handler
Expand Down Expand Up @@ -442,7 +446,7 @@ func RunServer() {

}

//================ REST API Server: setup & start
// ================ REST API Server: setup & start
func ApiServer(routes []route) {
e := echo.New()

Expand All @@ -451,14 +455,14 @@ func ApiServer(routes []route) {
e.Use(middleware.Logger())
e.Use(middleware.Recover())

cbspiderRoot := os.Getenv("CBSPIDER_ROOT")
cbspiderRoot := os.Getenv("CBSPIDER_ROOT")

// for HTTP Access Log
e.Logger.SetOutput(&lumberjack.Logger{
Filename: cbspiderRoot+"/log/http-access.log",
MaxSize: 10, // megabytes
MaxBackups: 10, // number of backups
MaxAge: 31, // days
Filename: cbspiderRoot + "/log/http-access.log",
MaxSize: 10, // megabytes
MaxBackups: 10, // number of backups
MaxAge: 31, // days
})

API_USERNAME := os.Getenv("API_USERNAME")
Expand Down Expand Up @@ -506,15 +510,15 @@ func ApiServer(routes []route) {
e.Logger.Fatal(e.Start(cr.ServerPort))
}

//================ API Info
// ================ API Info
func apiInfo(c echo.Context) error {
cblog.Info("call apiInfo()")

apiInfo := "api info"
return c.String(http.StatusOK, apiInfo)
}

//================ Endpoint Info
// ================ Endpoint Info
func EndpointInfo(c echo.Context) error {
cblog.Info("call endpointInfo()")

Expand Down
122 changes: 122 additions & 0 deletions api-runtime/rest-runtime/RegionZoneRest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Cloud Control Manager's Rest Runtime of CB-Spider.
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2023.09.

package restruntime

import (
"net/http"

cmrt "github.com/cloud-barista/cb-spider/api-runtime/common-runtime"
cres "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
"github.com/labstack/echo/v4"
)

// ================ RegionZone Handler
func ListRegionZone(c echo.Context) error {
cblog.Info("call ListRegionZone()")

var req struct {
ConnectionName string
}

if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}

// Call common-runtime API
result, err := cmrt.ListRegionZone(req.ConnectionName)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

var jsonResult struct {
Result []*cres.RegionZoneInfo `json:"regionzone"`
}
jsonResult.Result = result
return c.JSON(http.StatusOK, &jsonResult)
}

func GetRegionZone(c echo.Context) error {
cblog.Info("call GetRegionZone()")

var req struct {
ConnectionName string
}

if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}

// Call common-runtime API
result, err := cmrt.GetRegionZone(req.ConnectionName, c.Param("Name"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

return c.JSON(http.StatusOK, result)
}

func ListOrgRegion(c echo.Context) error {
cblog.Info("call ListOrgRegion()")

var req struct {
ConnectionName string
}

if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}

// Call common-runtime API
result, err := cmrt.ListOrgRegion(req.ConnectionName)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

return c.String(http.StatusOK, result)
}

func ListOrgZone(c echo.Context) error {
cblog.Info("call ListOrgZone()")

var req struct {
ConnectionName string
}

if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}

// Call common-runtime API
result, err := cmrt.ListOrgZone(req.ConnectionName)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

return c.String(http.StatusOK, result)
}

0 comments on commit f6541dd

Please sign in to comment.