diff --git a/api-runtime/common-runtime/RegionZoneHandler.go b/api-runtime/common-runtime/RegionZoneHandler.go new file mode 100644 index 000000000..110794cda --- /dev/null +++ b/api-runtime/common-runtime/RegionZoneHandler.go @@ -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 +} diff --git a/api-runtime/rest-runtime/CBSpiderRuntime.go b/api-runtime/rest-runtime/CBSpiderRuntime.go index 67f48ad4a..0b7cfa420 100644 --- a/api-runtime/rest-runtime/CBSpiderRuntime.go +++ b/api-runtime/rest-runtime/CBSpiderRuntime.go @@ -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 @@ -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}, @@ -334,7 +340,6 @@ func RunServer() { {"GET", "/allnlb", ListAllNLB}, {"DELETE", "/cspnlb/:Id", DeleteCSPNLB}, - //----------Disk Handler {"POST", "/regdisk", RegisterDisk}, {"DELETE", "/regdisk/:Name", UnregisterDisk}, @@ -352,7 +357,6 @@ func RunServer() { {"GET", "/alldisk", ListAllDisk}, {"DELETE", "/cspdisk/:Id", DeleteCSPDisk}, - //----------MyImage Handler {"POST", "/regmyimage", RegisterMyImage}, {"DELETE", "/regmyimage/:Name", UnregisterMyImage}, @@ -391,7 +395,7 @@ func RunServer() { {"GET", "/nscluster", AllClusterList}, //-------------------------------------------------------------------// - //----------Additional Info + //----------Additional Info {"GET", "/cspresourcename/:Name", GetCSPResourceName}, {"GET", "/cspresourceinfo/:Name", GetCSPResourceInfo}, //----------AnyCall Handler @@ -442,7 +446,7 @@ func RunServer() { } -//================ REST API Server: setup & start +// ================ REST API Server: setup & start func ApiServer(routes []route) { e := echo.New() @@ -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") @@ -506,7 +510,7 @@ 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()") @@ -514,7 +518,7 @@ func apiInfo(c echo.Context) error { return c.String(http.StatusOK, apiInfo) } -//================ Endpoint Info +// ================ Endpoint Info func EndpointInfo(c echo.Context) error { cblog.Info("call endpointInfo()") diff --git a/api-runtime/rest-runtime/RegionZoneRest.go b/api-runtime/rest-runtime/RegionZoneRest.go new file mode 100644 index 000000000..922f3b76f --- /dev/null +++ b/api-runtime/rest-runtime/RegionZoneRest.go @@ -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) +}