Skip to content

Commit

Permalink
Add swagger defintion for Tag Management Rest API
Browse files Browse the repository at this point in the history
  • Loading branch information
powerkimhub committed Oct 2, 2024
1 parent fb6dcdd commit 9b6066a
Show file tree
Hide file tree
Showing 9 changed files with 1,229 additions and 129 deletions.
18 changes: 8 additions & 10 deletions api-runtime/rest-runtime/CBSpiderRuntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ func RunServer() {

//----------SecurityGroup Handler
{"GET", "/getsecuritygroupowner", GetSGOwnerVPC},
{"POST", "/getsecuritygroupowner", GetSGOwnerVPC},
{"POST", "/regsecuritygroup", RegisterSecurity},
{"DELETE", "/regsecuritygroup/:Name", UnregisterSecurity},

Expand Down Expand Up @@ -328,6 +329,7 @@ func RunServer() {
*/
//----------VM Handler
{"GET", "/getvmusingresources", GetVMUsingRS},
{"POST", "/getvmusingresources", GetVMUsingRS},
{"POST", "/regvm", RegisterVM},
{"DELETE", "/regvm/:Name", UnregisterVM},

Expand Down Expand Up @@ -627,10 +629,9 @@ func endpointInfo(c echo.Context) error {
endpointInfo := fmt.Sprintf("\n <CB-Spider> Multi-Cloud Infrastructure Federation Framework\n")
adminWebURL := "http://" + cr.ServiceIPorName + cr.ServicePort + "/spider/adminweb"
endpointInfo += fmt.Sprintf(" - AdminWeb: %s\n", adminWebURL)
restEndPoint := "http://" + cr.ServiceIPorName + cr.ServicePort + "/spider"
endpointInfo += fmt.Sprintf(" - REST API: %s\n", restEndPoint)
// swaggerURL := "http://" + cr.ServiceIPorName + cr.ServicePort + "/spider/swagger/index.html"
// endpointInfo += fmt.Sprintf(" - Swagger : %s\n", swaggerURL)
swaggerURL := "http://" + cr.ServiceIPorName + cr.ServicePort + "/spider/api"
endpointInfo += fmt.Sprintf(" - Swagger UI: %s\n", swaggerURL)

// gRPCServer := "grpc://" + cr.ServiceIPorName + cr.GoServicePort
// endpointInfo += fmt.Sprintf(" - Go API: %s\n", gRPCServer)

Expand Down Expand Up @@ -716,11 +717,8 @@ func spiderBanner() {
adminWebURL := "http://" + cr.ServiceIPorName + cr.ServicePort + "/spider/adminweb"
fmt.Printf(" - AdminWeb: %s\n", adminWebURL)

// REST API EndPoint
restEndPoint := "http://" + cr.ServiceIPorName + cr.ServicePort + "/spider"
fmt.Printf(" - REST API: %s\n", restEndPoint)

// Swagger
// swaggerURL := "http://" + cr.ServiceIPorName + cr.ServicePort + "/spider/swagger/index.html"
// fmt.Printf(" - Swagger : %s\n", swaggerURL)
swaggerURL := "http://" + cr.ServiceIPorName + cr.ServicePort + "/spider/api"
fmt.Printf(" - Swagger UI: %s\n", swaggerURL)

}
150 changes: 104 additions & 46 deletions api-runtime/rest-runtime/TagRest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// 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, 2024.

package restruntime

Expand All @@ -22,19 +20,32 @@ import (

//================ Tag Handler

type tagAddReq struct {
ConnectionName string
type TagAddRequest struct {
ConnectionName string `json:"ConnectionName" validate:"required" example:"aws-connection"`
ReqInfo struct {
ResourceType cres.RSType
ResourceName string
Tag cres.KeyValue
}
ResourceType cres.RSType `json:"ResourceType" validate:"required" example:"VPC"`
ResourceName string `json:"ResourceName" validate:"required" example:"vpc-01"`
Tag cres.KeyValue `json:"Tag" validate:"required"`
} `json:"ReqInfo" validate:"required"`
}

// addTag godoc
// @ID add-tag
// @Summary Add Tag
// @Description Add a tag to a specified resource.
// @Tags [Tag Management]
// @Accept json
// @Produce json
// @Param TagAddRequest body restruntime.TagAddRequest true "Request body for adding a tag"
// @Success 200 {object} cres.KeyValue "Details of the added tag"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid JSON structure or missing fields"
// @Failure 404 {object} SimpleMsg "Resource Not Found"
// @Failure 500 {object} SimpleMsg "Internal Server Error"
// @Router /tag [post]
func AddTag(c echo.Context) error {
cblog.Info("call AddTag()")

req := tagAddReq{}
req := TagAddRequest{}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
Expand All @@ -48,82 +59,129 @@ func AddTag(c echo.Context) error {
return c.JSON(http.StatusOK, result)
}

type tagListReq struct {
ConnectionName string
ReqInfo struct {
ResourceType cres.RSType
ResourceName string
}
}

// listTag godoc
// @ID list-tag
// @Summary List Tags
// @Description Retrieve a list of tags for a specified resource.
// @Tags [Tag Management]
// @Accept json
// @Produce json
// @Param ConnectionName query string true "Connection Name. ex) aws-connection"
// @Param ResourceType query string true "Resource Type. ex) VPC"
// @Param ResourceName query string true "Resource Name. ex) vpc-01"
// @Success 200 {object} []cres.KeyValue "List of tags"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid JSON structure or missing fields"
// @Failure 404 {object} SimpleMsg "Resource Not Found"
// @Failure 500 {object} SimpleMsg "Internal Server Error"
// @Router /tag [get]
func ListTag(c echo.Context) error {
cblog.Info("call ListTag()")

req := tagListReq{}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
// Retrieve query parameters
connectionName := c.QueryParam("ConnectionName")
resourceType := c.QueryParam("ResourceType")
resourceName := c.QueryParam("ResourceName")

if connectionName == "" || resourceType == "" || resourceName == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Missing required query parameters")
}

// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
// Convert resourceType to cres.RSType
rType, err := cres.StringToRSType(resourceType)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}

// Log the resource type using RSTypeString()
cblog.Infof("Listing tags for resource type: %s", cres.RSTypeString(rType))

// Call common-runtime API
result, err := cmrt.ListTag(req.ConnectionName, req.ReqInfo.ResourceType, req.ReqInfo.ResourceName)
result, err := cmrt.ListTag(connectionName, rType, resourceName)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

var jsonResult struct {
Result []cres.KeyValue `json:"tag"`
Result []cres.KeyValue `json:"tag"`
ResourceType string `json:"resourceType"`
}
jsonResult.Result = result
return c.JSON(http.StatusOK, &jsonResult)
}
jsonResult.ResourceType = cres.RSTypeString(rType) // Include the resource type in a human-readable format

type tagGetReq struct {
ConnectionName string
ReqInfo struct {
ResourceType cres.RSType
ResourceName string
}
return c.JSON(http.StatusOK, &jsonResult)
}

// getTag godoc
// @ID get-tag
// @Summary Get Tag
// @Description Retrieve a specific tag for a specified resource.
// @Tags [Tag Management]
// @Accept json
// @Produce json
// @Param ConnectionName query string true "Connection Name. ex) aws-connection"
// @Param ResourceType query string true "Resource Type. ex) VPC"
// @Param ResourceName query string true "Resource Name. ex) vpc-01"
// @Param Key path string true "The key of the tag to retrieve"
// @Success 200 {object} cres.KeyValue "Details of the tag"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid query parameters"
// @Failure 404 {object} SimpleMsg "Resource Not Found"
// @Failure 500 {object} SimpleMsg "Internal Server Error"
// @Router /tag/{Key} [get]
func GetTag(c echo.Context) error {
cblog.Info("call GetTag()")

req := tagGetReq{}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
// Retrieve query parameters
connectionName := c.QueryParam("ConnectionName")
resourceType := c.QueryParam("ResourceType")
resourceName := c.QueryParam("ResourceName")
tagKey := c.Param("Key")

if connectionName == "" || resourceType == "" || resourceName == "" || tagKey == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Missing required query parameters")
}

// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
// Convert resourceType to cres.RSType
rType, err := cres.StringToRSType(resourceType)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid resource type")
}

// Call common-runtime API
result, err := cmrt.GetTag(req.ConnectionName, req.ReqInfo.ResourceType, req.ReqInfo.ResourceName, c.Param("Key"))
result, err := cmrt.GetTag(connectionName, rType, resourceName, tagKey)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

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

type tagRemoveReq struct {
ConnectionName string
// tagRemoveReq represents the request body for removing a Tag from a Resource.
type TagRemoveRequest struct {
ConnectionName string `json:"ConnectionName" validate:"required" example:"aws-connection"`
ReqInfo struct {
ResourceType cres.RSType
ResourceName string
}
ResourceType cres.RSType `json:"ResourceType" validate:"required" example:"VPC"`
ResourceName string `json:"ResourceName" validate:"required" example:"vpc-01"`
} `json:"ReqInfo" validate:"required"`
}

// removeTag godoc
// @ID remove-tag
// @Summary Remove Tag
// @Description Remove a specific tag from a specified resource.
// @Tags [Tag Management]
// @Accept json
// @Produce json
// @Param TagRemoveRequest body restruntime.TagRemoveRequest true "Request body for removing a specific tag"
// @Param Key path string true "The key of the tag to remove"
// @Success 200 {object} BooleanInfo "Result of the remove operation"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid JSON structure or missing fields"
// @Failure 404 {object} SimpleMsg "Resource Not Found"
// @Failure 500 {object} SimpleMsg "Internal Server Error"
// @Router /tag/{Key} [delete]
func RemoveTag(c echo.Context) error {
cblog.Info("call RemoveTag()")

req := tagRemoveReq{}
req := TagRemoveRequest{}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion api-runtime/rest-runtime/VMRest.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type VMUsingResources struct {
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid query parameters"
// @Failure 404 {object} SimpleMsg "Resource Not Found"
// @Failure 500 {object} SimpleMsg "Internal Server Error"
// @Router /getvmusingresources [get]
// @Router /getvmusingresources [post]
func GetVMUsingRS(c echo.Context) error {
cblog.Info("call GetVMUsingRS()")

Expand Down
Loading

0 comments on commit 9b6066a

Please sign in to comment.