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

Add NLB mgmt feature #1158

Merged
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
536 changes: 536 additions & 0 deletions src/api/rest/docs/docs.go

Large diffs are not rendered by default.

536 changes: 536 additions & 0 deletions src/api/rest/docs/swagger.json

Large diffs are not rendered by default.

369 changes: 369 additions & 0 deletions src/api/rest/docs/swagger.yaml

Large diffs are not rendered by default.

219 changes: 219 additions & 0 deletions src/api/rest/server/mcis/nlb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
/*
Copyright 2019 The Cloud-Barista Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package mcis is to handle REST API for mcis
package mcis

import (
"fmt"
"net/http"

"github.com/cloud-barista/cb-tumblebug/src/core/common"
"github.com/cloud-barista/cb-tumblebug/src/core/mcis"
"github.com/labstack/echo/v4"
)

// RestPostNLB godoc
// @Summary Create NLB
// @Description Create NLB
// @Tags [Infra resource] NLB management
// @Accept json
// @Produce json
// @Param nsId path string true "Namespace ID" default(ns01)
// @Param option query string false "Option: [required params for register] connectionName, name, cspNLBId" Enums(register)
// @Param nlbReq body mcis.TbNLBReq true "Details of the NLB object"
// @Success 200 {object} mcis.TbNLBInfo
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /ns/{nsId}/nlb [post]
func RestPostNLB(c echo.Context) error {

nsId := c.Param("nsId")

optionFlag := c.QueryParam("option")

u := &mcis.TbNLBReq{}
if err := c.Bind(u); err != nil {
return err
}

fmt.Println("[POST NLB]")

content, err := mcis.CreateNLB(nsId, u, optionFlag)

if err != nil {
common.CBLog.Error(err)
mapA := map[string]string{"message": err.Error()}
return c.JSON(http.StatusInternalServerError, &mapA)
}
return c.JSON(http.StatusCreated, content)
}

/* function RestPutNLB not yet implemented
// RestPutNLB godoc
// @Summary Update NLB
// @Description Update NLB
// @Tags [Infra resource] NLB management
// @Accept json
// @Produce json
// @Param nlbInfo body mcis.TbNLBInfo true "Details of the NLB object"
// @Success 200 {object} mcis.TbNLBInfo
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /ns/{nsId}/nlb/{nlbId} [put]
*/
func RestPutNLB(c echo.Context) error {
//nsId := c.Param("nsId")

return nil
}

// RestGetNLB godoc
// @Summary Get NLB
// @Description Get NLB
// @Tags [Infra resource] NLB management
// @Accept json
// @Produce json
// @Param nsId path string true "Namespace ID" default(ns01)
// @Param nlbId path string true "NLB ID"
// @Success 200 {object} mcis.TbNLBInfo
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /ns/{nsId}/nlb/{nlbId} [get]
func RestGetNLB(c echo.Context) error {

nsId := c.Param("nsId")

resourceId := c.Param("resourceId")

res, err := mcis.GetNLB(nsId, resourceId)
if err != nil {
mapA := map[string]string{"message": "Failed to find the NLB " + resourceId}
return c.JSON(http.StatusNotFound, &mapA)
} else {
return c.JSON(http.StatusOK, &res)
}
}

// Response structure for RestGetAllNLB
type RestGetAllNLBResponse struct {
NLB []mcis.TbNLBInfo `json:"nlb"`
}

// RestGetAllNLB godoc
// @Summary List all NLBs or NLBs' ID
// @Description List all NLBs or NLBs' ID
// @Tags [Infra resource] NLB management
// @Accept json
// @Produce json
// @Param nsId path string true "Namespace ID" default(ns01)
// @Param option query string false "Option" Enums(id)
// @Param filterKey query string false "Field key for filtering (ex: cspNLBName)"
// @Param filterVal query string false "Field value for filtering (ex: ns01-alibaba-ap-northeast-1-vpc)"
// @Success 200 {object} JSONResult{[DEFAULT]=RestGetAllNLBResponse,[ID]=common.IdList} "Different return structures by the given option param"
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /ns/{nsId}/nlb [get]
func RestGetAllNLB(c echo.Context) error {

nsId := c.Param("nsId")

optionFlag := c.QueryParam("option")
filterKey := c.QueryParam("filterKey")
filterVal := c.QueryParam("filterVal")

if optionFlag == "id" {
content := common.IdList{}
var err error
content.IdList, err = mcis.ListNLBId(nsId)
if err != nil {
mapA := map[string]string{"message": "Failed to list NLBs' ID; " + err.Error()}
return c.JSON(http.StatusNotFound, &mapA)
}

return c.JSON(http.StatusOK, &content)
} else {

resourceList, err := mcis.ListNLB(nsId, filterKey, filterVal)
if err != nil {
mapA := map[string]string{"message": "Failed to list NLBs; " + err.Error()}
return c.JSON(http.StatusNotFound, &mapA)
}

var content struct {
NLB []mcis.TbNLBInfo `json:"nlb"`
}

content.NLB = resourceList.([]mcis.TbNLBInfo) // type assertion (interface{} -> array)
return c.JSON(http.StatusOK, &content)
// return c.JSON(http.StatusBadRequest, nil)
}
}

// RestDelNLB godoc
// @Summary Delete NLB
// @Description Delete NLB
// @Tags [Infra resource] NLB management
// @Accept json
// @Produce json
// @Param nsId path string true "Namespace ID" default(ns01)
// @Param nlbId path string true "NLB ID"
// @Success 200 {object} common.SimpleMsg
// @Failure 404 {object} common.SimpleMsg
// @Router /ns/{nsId}/nlb/{nlbId} [delete]
func RestDelNLB(c echo.Context) error {

nsId := c.Param("nsId")

resourceId := c.Param("resourceId")

forceFlag := c.QueryParam("force")

err := mcis.DelNLB(nsId, resourceId, forceFlag)
if err != nil {
common.CBLog.Error(err)
mapA := map[string]string{"message": err.Error()}
return c.JSON(http.StatusInternalServerError, &mapA)
}

mapA := map[string]string{"message": "The NLB " + resourceId + " has been deleted"}
return c.JSON(http.StatusOK, &mapA)
}

// RestDelAllNLB godoc
// @Summary Delete all NLBs
// @Description Delete all NLBs
// @Tags [Infra resource] NLB management
// @Accept json
// @Produce json
// @Param nsId path string true "Namespace ID" default(ns01)
// @Param match query string false "Delete resources containing matched ID-substring only" default()
// @Success 200 {object} common.IdList
// @Failure 404 {object} common.SimpleMsg
// @Router /ns/{nsId}/nlb [delete]
func RestDelAllNLB(c echo.Context) error {

nsId := c.Param("nsId")

forceFlag := c.QueryParam("force")
subString := c.QueryParam("match")

output, err := mcis.DelAllNLB(nsId, subString, forceFlag)
if err != nil {
common.CBLog.Error(err)
mapA := map[string]string{"message": err.Error()}
return c.JSON(http.StatusConflict, &mapA)
}

return c.JSON(http.StatusOK, output)
}
8 changes: 8 additions & 0 deletions src/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ func RunServer(port string) {
g.POST("/:nsId/network/mcis/:mcisId", rest_mcis.RestPostConfigureCloudAdaptiveNetworkToMcis)
g.PUT("/:nsId/network/mcis/:mcisId", rest_mcis.RestPutInjectCloudInformationForCloudAdaptiveNetwork)

// Network Load Balancer
g.POST("/:nsId/nlb", rest_mcis.RestPostNLB)
g.GET("/:nsId/nlb/:resourceId", rest_mcis.RestGetNLB)
g.GET("/:nsId/nlb", rest_mcis.RestGetAllNLB)
// g.PUT("/:nsId/nlb/:resourceId", rest_mcis.RestPutNLB)
g.DELETE("/:nsId/nlb/:resourceId", rest_mcis.RestDelNLB)
g.DELETE("/:nsId/nlb", rest_mcis.RestDelAllNLB)

//MCIR Management
g.POST("/:nsId/resources/image", rest_mcir.RestPostImage)
g.GET("/:nsId/resources/image/:resourceId", rest_mcir.RestGetResource)
Expand Down
1 change: 1 addition & 0 deletions src/core/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const (
StrSpec string = "spec"
StrVNet string = "vNet"
StrSubnet string = "subnet"
StrNLB string = "nlb"
StrVM string = "vm"
StrDefaultResourceName string = "-systemdefault-"
// StrFirewallRule string = "firewallRule"
Expand Down
Loading