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

Repackaging client.go to prevent pkg cycle #1968

Merged
merged 1 commit into from
Feb 10, 2025
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
37 changes: 19 additions & 18 deletions src/api/rest/server/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/rs/zerolog/log"

"github.com/cloud-barista/cb-tumblebug/src/core/common"
clientManager "github.com/cloud-barista/cb-tumblebug/src/core/common/client"
"github.com/cloud-barista/cb-tumblebug/src/core/model"
)

Expand All @@ -47,17 +48,17 @@ func RestInitConfig(c echo.Context) error {

if err := Validate(c, []string{"configId"}); err != nil {
log.Error().Err(err).Msg("")
return common.EndRequestWithLog(c, err, nil)
return clientManager.EndRequestWithLog(c, err, nil)
}

err := common.InitConfig(c.Param("configId"))
if err != nil {
err := fmt.Errorf("Failed to init the config " + c.Param("configId"))
return common.EndRequestWithLog(c, err, nil)
return clientManager.EndRequestWithLog(c, err, nil)
} else {
// return SendMessage(c, http.StatusOK, "The config "+c.Param("configId")+" has been initialized.")
content := map[string]string{"message": "The config " + c.Param("configId") + " has been initialized."}
return common.EndRequestWithLog(c, err, content)
return clientManager.EndRequestWithLog(c, err, content)
}
}

Expand All @@ -83,9 +84,9 @@ func RestGetConfig(c echo.Context) error {
content, err := common.GetConfig(c.Param("configId"))
if err != nil {
err := fmt.Errorf("Failed to find the config " + c.Param("configId"))
return common.EndRequestWithLog(c, err, nil)
return clientManager.EndRequestWithLog(c, err, nil)
} else {
return common.EndRequestWithLog(c, err, content)
return clientManager.EndRequestWithLog(c, err, content)
}
}

Expand All @@ -112,7 +113,7 @@ func RestGetAllConfig(c echo.Context) error {

configList, err := common.ListConfig()
content.Config = configList
return common.EndRequestWithLog(c, err, content)
return clientManager.EndRequestWithLog(c, err, content)
}

// RestPostConfig godoc
Expand All @@ -131,12 +132,12 @@ func RestPostConfig(c echo.Context) error {

u := &model.ConfigReq{}
if err := c.Bind(u); err != nil {
return common.EndRequestWithLog(c, err, nil)
return clientManager.EndRequestWithLog(c, err, nil)
}

log.Debug().Msg("[Creating or Updating Config]")
content, err := common.UpdateConfig(u)
return common.EndRequestWithLog(c, err, content)
return clientManager.EndRequestWithLog(c, err, content)

}

Expand All @@ -155,7 +156,7 @@ func RestInitAllConfig(c echo.Context) error {
err := common.InitAllConfig()
content := map[string]string{
"message": "All configs has been initialized"}
return common.EndRequestWithLog(c, err, content)
return clientManager.EndRequestWithLog(c, err, content)
}

// RestGetRequest godoc
Expand All @@ -173,7 +174,7 @@ func RestInitAllConfig(c echo.Context) error {
func RestGetRequest(c echo.Context) error {
reqId := c.Param("reqId")

if details, ok := common.RequestMap.Load(reqId); ok {
if details, ok := clientManager.RequestMap.Load(reqId); ok {
return Send(c, http.StatusOK, details)
}

Expand Down Expand Up @@ -206,11 +207,11 @@ func RestGetAllRequests(c echo.Context) error {
timeLimit = time.Now().Add(-time.Duration(minutes) * time.Minute)
}

var allRequests []common.RequestDetails
var allRequests []clientManager.RequestDetails

// Filtering the requests
common.RequestMap.Range(func(key, value interface{}) bool {
if details, ok := value.(common.RequestDetails); ok {
clientManager.RequestMap.Range(func(key, value interface{}) bool {
if details, ok := value.(clientManager.RequestDetails); ok {
if (statusFilter == "" || strings.ToLower(details.Status) == statusFilter) &&
(methodFilter == "" || strings.ToLower(details.RequestInfo.Method) == methodFilter) &&
(urlFilter == "" || strings.Contains(strings.ToLower(details.RequestInfo.URL), urlFilter)) &&
Expand Down Expand Up @@ -239,7 +240,7 @@ func RestGetAllRequests(c echo.Context) error {
}
}

return Send(c, http.StatusOK, map[string][]common.RequestDetails{"requests": allRequests})
return Send(c, http.StatusOK, map[string][]clientManager.RequestDetails{"requests": allRequests})
}

// RestDeleteRequest godoc
Expand All @@ -255,8 +256,8 @@ func RestGetAllRequests(c echo.Context) error {
func RestDeleteRequest(c echo.Context) error {
reqId := c.Param("reqId")

if _, ok := common.RequestMap.Load(reqId); ok {
common.RequestMap.Delete(reqId)
if _, ok := clientManager.RequestMap.Load(reqId); ok {
clientManager.RequestMap.Delete(reqId)
return SendMessage(c, http.StatusOK, "Request deleted successfully")
}

Expand All @@ -273,8 +274,8 @@ func RestDeleteRequest(c echo.Context) error {
// @Success 200 {object} model.SimpleMsg
// @Router /requests [delete]
func RestDeleteAllRequests(c echo.Context) error {
common.RequestMap.Range(func(key, value interface{}) bool {
common.RequestMap.Delete(key)
clientManager.RequestMap.Range(func(key, value interface{}) bool {
clientManager.RequestMap.Delete(key)
return true
})

Expand Down
22 changes: 11 additions & 11 deletions src/api/rest/server/common/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"github.com/labstack/echo/v4"

"github.com/cloud-barista/cb-tumblebug/src/core/common"
clientManager "github.com/cloud-barista/cb-tumblebug/src/core/common/client"
"github.com/cloud-barista/cb-tumblebug/src/core/common/label"
"github.com/cloud-barista/cb-tumblebug/src/core/model"
)
Expand All @@ -46,7 +46,7 @@ func RestCreateOrUpdateLabel(c echo.Context) error {
// Parse the incoming request body to get the labels
var labelReq model.Label
if err := c.Bind(&labelReq); err != nil {
return common.EndRequestWithLog(c, fmt.Errorf("Invalid request body"), nil)
return clientManager.EndRequestWithLog(c, fmt.Errorf("Invalid request body"), nil)
}

// Get the resource key
Expand All @@ -55,10 +55,10 @@ func RestCreateOrUpdateLabel(c echo.Context) error {
// Create or update the label in the KV store
err := label.CreateOrUpdateLabel(labelType, uid, resourceKey, labelReq.Labels)
if err != nil {
return common.EndRequestWithLog(c, err, nil)
return clientManager.EndRequestWithLog(c, err, nil)
}

return common.EndRequestWithLog(c, nil, map[string]string{"message": "Label created or updated successfully"})
return clientManager.EndRequestWithLog(c, nil, map[string]string{"message": "Label created or updated successfully"})
}

// RestRemoveLabel godoc
Expand All @@ -84,10 +84,10 @@ func RestRemoveLabel(c echo.Context) error {
// Remove the label from the KV store
err := label.RemoveLabel(labelType, uid, key)
if err != nil {
return common.EndRequestWithLog(c, err, nil)
return clientManager.EndRequestWithLog(c, err, nil)
}

return common.EndRequestWithLog(c, nil, map[string]string{"message": "Label removed successfully"})
return clientManager.EndRequestWithLog(c, nil, map[string]string{"message": "Label removed successfully"})
}

// RestGetLabels godoc
Expand All @@ -111,10 +111,10 @@ func RestGetLabels(c echo.Context) error {
// Get the labels from the KV store
labelInfo, err := label.GetLabels(labelType, uid)
if err != nil {
return common.EndRequestWithLog(c, err, nil)
return clientManager.EndRequestWithLog(c, err, nil)
}

return common.EndRequestWithLog(c, nil, labelInfo)
return clientManager.EndRequestWithLog(c, nil, labelInfo)
}

// ResourcesResponse is a struct to wrap the results of a label selector query
Expand Down Expand Up @@ -149,15 +149,15 @@ func RestGetResourcesByLabelSelector(c echo.Context) error {
// Get resources based on the label selector
resources, err := label.GetResourcesByLabelSelector(labelType, labelSelector)
if err != nil {
return common.EndRequestWithLog(c, err, nil)
return clientManager.EndRequestWithLog(c, err, nil)
}

// Wrap the results in a JSON object
response := ResourcesResponse{
Results: resources,
}

return common.EndRequestWithLog(c, nil, response)
return clientManager.EndRequestWithLog(c, nil, response)
}

// RestGetSystemLabelInfo godoc
Expand All @@ -182,5 +182,5 @@ func RestGetSystemLabelInfo(c echo.Context) error {
LabelTypes: labelTypes,
}

return common.EndRequestWithLog(c, nil, systemLabelInfo)
return clientManager.EndRequestWithLog(c, nil, systemLabelInfo)
}
29 changes: 15 additions & 14 deletions src/api/rest/server/common/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@ import (
"github.com/labstack/echo/v4"

"github.com/cloud-barista/cb-tumblebug/src/core/common"
clientManager "github.com/cloud-barista/cb-tumblebug/src/core/common/client"
"github.com/cloud-barista/cb-tumblebug/src/core/model"
)

func RestCheckNs(c echo.Context) error {

if err := Validate(c, []string{"nsId"}); err != nil {
return common.EndRequestWithLog(c, err, nil)
return clientManager.EndRequestWithLog(c, err, nil)
}

nsId := c.Param("nsId")
err := common.CheckString(nsId)
if err != nil {
return common.EndRequestWithLog(c, err, nil)
return clientManager.EndRequestWithLog(c, err, nil)
}
content, err := common.CheckNs(nsId)
return common.EndRequestWithLog(c, err, content)
return clientManager.EndRequestWithLog(c, err, content)
}

// RestDelAllNs godoc
Expand All @@ -50,7 +51,7 @@ func RestDelAllNs(c echo.Context) error {

err := common.DelAllNs()
content := map[string]string{"message": "All namespaces has been deleted"}
return common.EndRequestWithLog(c, err, content)
return clientManager.EndRequestWithLog(c, err, content)
}

// RestDelNs godoc
Expand All @@ -67,12 +68,12 @@ func RestDelAllNs(c echo.Context) error {
func RestDelNs(c echo.Context) error {

if err := Validate(c, []string{"nsId"}); err != nil {
return common.EndRequestWithLog(c, err, nil)
return clientManager.EndRequestWithLog(c, err, nil)
}

err := common.DelNs(c.Param("nsId"))
content := map[string]string{"message": "The ns " + c.Param("nsId") + " has been deleted"}
return common.EndRequestWithLog(c, err, content)
return clientManager.EndRequestWithLog(c, err, content)
}

// JSONResult's data field will be overridden by the specific type
Expand Down Expand Up @@ -109,11 +110,11 @@ func RestGetAllNs(c echo.Context) error {
content := model.IdList{}
var err error
content.IdList, err = common.ListNsId()
return common.EndRequestWithLog(c, err, content)
return clientManager.EndRequestWithLog(c, err, content)
} else {
nsList, err := common.ListNs()
content.Ns = nsList
return common.EndRequestWithLog(c, err, content)
return clientManager.EndRequestWithLog(c, err, content)
}
}

Expand All @@ -132,11 +133,11 @@ func RestGetAllNs(c echo.Context) error {
func RestGetNs(c echo.Context) error {

if err := Validate(c, []string{"nsId"}); err != nil {
return common.EndRequestWithLog(c, err, nil)
return clientManager.EndRequestWithLog(c, err, nil)
}

content, err := common.GetNs(c.Param("nsId"))
return common.EndRequestWithLog(c, err, content)
return clientManager.EndRequestWithLog(c, err, content)
}

// RestPostNs godoc
Expand All @@ -155,11 +156,11 @@ func RestPostNs(c echo.Context) error {

u := &model.NsReq{}
if err := c.Bind(u); err != nil {
return common.EndRequestWithLog(c, err, nil)
return clientManager.EndRequestWithLog(c, err, nil)
}

content, err := common.CreateNs(u)
return common.EndRequestWithLog(c, err, content)
return clientManager.EndRequestWithLog(c, err, content)

}

Expand All @@ -180,9 +181,9 @@ func RestPutNs(c echo.Context) error {

u := &model.NsReq{}
if err := c.Bind(u); err != nil {
return common.EndRequestWithLog(c, err, nil)
return clientManager.EndRequestWithLog(c, err, nil)
}

content, err := common.UpdateNs(c.Param("nsId"), u)
return common.EndRequestWithLog(c, err, content)
return clientManager.EndRequestWithLog(c, err, content)
}
Loading