Skip to content

Commit

Permalink
Enhance APIs to create, read, and delete vNet
Browse files Browse the repository at this point in the history
  • Loading branch information
yunkon-kim committed Aug 26, 2024
1 parent 83fa38c commit 5044675
Show file tree
Hide file tree
Showing 6 changed files with 798 additions and 163 deletions.
88 changes: 74 additions & 14 deletions src/api/rest/server/resource/vnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,63 @@ import (
// @Accept json
// @Produce json
// @Param nsId path string true "Namespace ID" default(default)
// @Param option query string false "Option: [required params for register] connectionName, name, cspVNetId" Enums(register)
// @Param vNetReq body resource.TbVNetReq true "Details for an VNet object"
// @Success 200 {object} resource.TbVNetInfo
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /ns/{nsId}/resources/vNet [post]
func RestPostVNet(c echo.Context) error {
reqID, idErr := common.StartRequestWithLog(c)
if idErr != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()})

// [Note] Input
// nsId and req will be checked inside of the CreateVNet function
nsId := c.Param("nsId")
err := common.CheckString(nsId)
req := &resource.TbVNetReq{}
if err := c.Bind(req); err != nil {
return c.JSON(http.StatusBadRequest, common.SimpleMsg{Message: err.Error()})
}

// [Note] Process
content, err := resource.CreateVNet(nsId, req)
if err != nil {
return c.JSON(http.StatusInternalServerError, common.SimpleMsg{Message: err.Error()})
}

// [Note] Output
return c.JSON(http.StatusCreated, content)
}

// RestPostVNetRegister godoc
// @ID PostVNetRegister
// @Summary Register the VNet created externally
// @Description Register the VNet created externally
// @Tags [Infra Resource] Network Management
// @Accept json
// @Produce json
// @Param nsId path string true "Namespace ID" default(default)
// @Param vNetRegisterReq body resource.TbVNetRegisterReq true "Inforamation required to register the VNet created externally"
// @Success 200 {object} resource.TbVNetInfo
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /ns/{nsId}/resources/vNet/register [post]
func RestPostVNetRegister(c echo.Context) error {

// [Note] Input
// nsId and req will be checked inside of the RegisterVNet function
nsId := c.Param("nsId")
optionFlag := c.QueryParam("option")
u := &resource.TbVNetReq{}
if err := c.Bind(u); err != nil {
return common.EndRequestWithLog(c, reqID, err, nil)
req := &resource.TbVNetRegisterReq{}
if err := c.Bind(req); err != nil {
return c.JSON(http.StatusBadRequest, common.SimpleMsg{Message: err.Error()})
}
content, err := resource.CreateVNet(nsId, u, optionFlag)
return common.EndRequestWithLog(c, reqID, err, content)

// [Note] Process
content, err := resource.RegisterVNet(nsId, req)
if err != nil {
return c.JSON(http.StatusInternalServerError, common.SimpleMsg{Message: err.Error()})
}

// [Note] Output
return c.JSON(http.StatusCreated, content)
}

/*
Expand Down Expand Up @@ -87,8 +125,19 @@ func RestPutVNet(c echo.Context) error {
// @Failure 500 {object} common.SimpleMsg
// @Router /ns/{nsId}/resources/vNet/{vNetId} [get]
func RestGetVNet(c echo.Context) error {
// This is a dummy function for Swagger.
return nil
// [Note] Input
// nsId and vNetId will be checked inside of the DeleteVNet function
nsId := c.Param("nsId")
vNetId := c.Param("vNetId")

// [Note] Process
content, err := resource.GetVNet(nsId, vNetId)
if err != nil {
return c.JSON(http.StatusInternalServerError, common.SimpleMsg{Message: err.Error()})
}

// [Note] Output
return c.JSON(http.StatusCreated, content)
}

// Response structure for RestGetAllVNet
Expand Down Expand Up @@ -129,8 +178,19 @@ func RestGetAllVNet(c echo.Context) error {
// @Failure 404 {object} common.SimpleMsg
// @Router /ns/{nsId}/resources/vNet/{vNetId} [delete]
func RestDelVNet(c echo.Context) error {
// This is a dummy function for Swagger.
return nil
// [Note] Input
// nsId and vNetId will be checked inside of the DeleteVNet function
nsId := c.Param("nsId")
vNetId := c.Param("vNetId")

// [Note] Process
content, err := resource.DeleteVNet(nsId, vNetId)
if err != nil {
return c.JSON(http.StatusInternalServerError, common.SimpleMsg{Message: err.Error()})
}

// [Note] Output
return c.JSON(http.StatusCreated, content)
}

// RestDelAllVNet godoc
Expand Down
7 changes: 4 additions & 3 deletions src/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,12 @@ func RunServer(port string) {
g.POST("/:nsId/resources/securityGroup/:securityGroupId/rules", rest_resource.RestPostFirewallRules)
g.DELETE("/:nsId/resources/securityGroup/:securityGroupId/rules", rest_resource.RestDelFirewallRules)

g.POST("/:nsId/resources/vNet", rest_resource.RestPostVNet)
g.GET("/:nsId/resources/vNet/:resourceId", rest_resource.RestGetResource)
g.POST("/:nsId/resources/vNet", rest_resource.RestPostVNet) // clear
g.POST("/:nsId/resources/vNet/register", rest_resource.RestPostVNetRegister) // clear
g.GET("/:nsId/resources/vNet/:vNetId", rest_resource.RestGetVNet) // clear
g.GET("/:nsId/resources/vNet", rest_resource.RestGetAllResources)
g.PUT("/:nsId/resources/vNet/:resourceId", rest_resource.RestPutVNet)
g.DELETE("/:nsId/resources/vNet/:resourceId", rest_resource.RestDelResource)
g.DELETE("/:nsId/resources/vNet/:vNetId", rest_resource.RestDelVNet) // clear
g.DELETE("/:nsId/resources/vNet", rest_resource.RestDelAllResources)

g.POST("/:nsId/resources/vNet/:vNetId/subnet", rest_resource.RestPostSubnet)
Expand Down
4 changes: 2 additions & 2 deletions src/core/infra/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,14 +962,14 @@ func RegisterCspNativeResources(nsId string, connConfig string, mciId string, op
result.SystemMessage = err.Error()
}
for _, r := range inspectedResources.Resources.OnCspOnly.Info {
req := resource.TbVNetReq{}
req := resource.TbVNetRegisterReq{}
req.ConnectionName = connConfig
req.CspVNetId = r.IdByCsp
req.Description = "Ref name: " + r.RefNameOrId + ". CSP managed resource (registered to CB-TB)"
req.Name = req.ConnectionName + "-" + req.CspVNetId
req.Name = common.ChangeIdString(req.Name)

_, err = resource.CreateVNet(nsId, &req, optionFlag)
_, err = resource.RegisterVNet(nsId, &req)

registeredStatus = ""
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions src/core/resource/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -1225,13 +1225,13 @@ func CheckResource(nsId string, resourceType string, resourceId string) (bool, e

// Check parameters' emptiness
if nsId == "" {
err := fmt.Errorf("CheckResource failed; nsId given is null.")
err := fmt.Errorf("failed to check resource, the given nsId is null")
return false, err
} else if resourceType == "" {
err := fmt.Errorf("CheckResource failed; resourceType given is null.")
err := fmt.Errorf("failed to check resource, the given resourceType is null")
return false, err
} else if resourceId == "" {
err := fmt.Errorf("CheckResource failed; resourceId given is null.")
err := fmt.Errorf("failed to check resource, the given resourceId is null")
return false, err
}

Expand Down Expand Up @@ -1906,7 +1906,7 @@ func LoadSharedResource(nsId string, resType string, connectionName string) erro

common.PrintJsonPretty(reqTmp)

resultInfo, err := CreateVNet(nsId, &reqTmp, "")
resultInfo, err := CreateVNet(nsId, &reqTmp)
if err != nil {
log.Error().Err(err).Msg("Failed to create vNet")
return err
Expand Down
9 changes: 5 additions & 4 deletions src/core/resource/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func CreateSubnet(nsId string, vNetId string, req TbSubnetReq, objectOnly bool)
return oldVNet, err
}

if objectOnly == false { // then, call CB-Spider CreateSubnet API
requestBody := SpiderSubnetReqInfoWrapper{}
if !objectOnly { // then, call CB-Spider CreateSubnet API
requestBody := spiderSubnetAddReq{}
requestBody.ConnectionName = oldVNet.ConnectionName
requestBody.ReqInfo.Name = common.GenUid()
requestBody.ReqInfo.IPv4_CIDR = req.IPv4_CIDR
Expand All @@ -105,10 +105,11 @@ func CreateSubnet(nsId string, vNetId string, req TbSubnetReq, objectOnly bool)

client := resty.New().SetCloseConnection(true)

spiderVpcInfo := new(spiderVpcInfo)
resp, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(requestBody).
SetResult(&SpiderVPCInfo{}). // or SetResult(AuthSuccess{}).
SetResult(spiderVpcInfo). // or SetResult(AuthSuccess{}).
//SetError(&AuthError{}). // or SetError(AuthError{}).
Post(url)

Expand All @@ -122,7 +123,7 @@ func CreateSubnet(nsId string, vNetId string, req TbSubnetReq, objectOnly bool)
fmt.Println("HTTP Status code: " + strconv.Itoa(resp.StatusCode()))
switch {
case resp.StatusCode() >= 400 || resp.StatusCode() < 200:
err := fmt.Errorf(string(resp.Body()))
err := fmt.Errorf("%s", resp.Body())
log.Error().Err(err).Msg("")
content := TbVNetInfo{}
return content, err
Expand Down
Loading

0 comments on commit 5044675

Please sign in to comment.