Skip to content

Commit

Permalink
Add API to retrieve Subnet info from a VPC, and add 'force' option to…
Browse files Browse the repository at this point in the history
… Swagger description for RemoveSubnet.
  • Loading branch information
powerkimhub committed Sep 9, 2024
1 parent e120c3d commit c1f4920
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 36 deletions.
91 changes: 91 additions & 0 deletions api-runtime/common-runtime/VPC-SubnetManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,97 @@ func AddSubnet(connectionName string, rsType string, vpcName string, reqInfo cre
return &info, nil
}

// (1) get spiderIID(NameId)
// (2) get resource(driverIID)
// (3) set ResourceInfo(userIID)
func GetSubnet(connectionName string, vpcName string, nameID string) (*cres.SubnetInfo, error) {
cblog.Info("call GetSubnet()")

// (1) check empty and trim user inputs
connectionName, err := EmptyCheckAndTrim("connectionName", connectionName)
if err != nil {
cblog.Error(err)
return nil, err
}

vpcName, err = EmptyCheckAndTrim("vpcName", vpcName)
if err != nil {
cblog.Error(err)
return nil, err
}

nameID, err = EmptyCheckAndTrim("nameID", nameID)
if err != nil {
cblog.Error(err)
return nil, err
}

// (2) Get Cloud Connection
cldConn, err := ccm.GetCloudConnection(connectionName)
if err != nil {
cblog.Error(err)
return nil, err
}

// (3) Get VPC Handler
handler, err := cldConn.CreateVPCHandler()
if err != nil {
cblog.Error(err)
return nil, err
}

// (4) Use vpcSPLock for locking based on VPC and Subnet nameID
vpcSPLock.RLock(connectionName, vpcName)
defer vpcSPLock.RUnlock(connectionName, vpcName)

// (5) Get VPC IID Info from infostore
var iidInfo VPCIIDInfo
err = infostore.GetByConditions(&iidInfo, CONNECTION_NAME_COLUMN, connectionName, NAME_ID_COLUMN, vpcName)
if err != nil {
cblog.Error(err)
return nil, err
}

// (6) Get VPC Info using handler.GetVPC() and driverIID
vpcInfo, err := handler.GetVPC(getDriverIID(cres.IID{NameId: iidInfo.NameId, SystemId: iidInfo.SystemId}))
if err != nil {
cblog.Error(err)
return nil, err
}

// (7) Set user IID (NameId) for the VPC resource
vpcInfo.IId = getUserIID(cres.IID{NameId: iidInfo.NameId, SystemId: iidInfo.SystemId})

// (8) Search for the subnet by nameID within the VPC's SubnetInfoList
for _, subnetInfo := range vpcInfo.SubnetInfoList {

// (9) Get Subnet IID Info from infostore
var subnetIIDInfo SubnetIIDInfo
err := infostore.GetByConditionsAndContain(&subnetIIDInfo, CONNECTION_NAME_COLUMN, connectionName,
OWNER_VPC_NAME_COLUMN, vpcInfo.IId.NameId, SYSTEM_ID_COLUMN, getMSShortID(subnetInfo.IId.SystemId))
if err != nil {
if checkNotFoundError(err) {
cblog.Info("Subnet not found in infostore:", err)
continue
}
cblog.Error(err)
return nil, err
}

if subnetIIDInfo.NameId == nameID {
subnetInfo.IId = getUserIID(cres.IID{NameId: subnetIIDInfo.NameId, SystemId: subnetIIDInfo.SystemId})
if subnetInfo.Zone == "" {
subnetInfo.Zone = subnetIIDInfo.ZoneId
}

return &subnetInfo, nil
}
}

// If no matching subnet was found
return nil, fmt.Errorf("Subnet with nameID %s not found in VPC %s", nameID, vpcName)
}

// (1) get spiderIID
// (2) delete Resource(SystemId)
// (3) delete IID
Expand Down
3 changes: 2 additions & 1 deletion api-runtime/rest-runtime/CBSpiderRuntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var cblog *logrus.Logger

// @title CB-Spider REST API
// @version latest
// @description **🕷️ [User Guide](https://github.com/cloud-barista/cb-spider/wiki/features-and-usages)** **🕷️ [Simple Guide](https://github.com/cloud-barista/cb-spider/wiki/Simple-Sample-API-Guide)**
// @description **🕷️ [User Guide](https://github.com/cloud-barista/cb-spider/wiki/features-and-usages)** **🕷️ [API Guide](https://github.com/cloud-barista/cb-spider/wiki/REST-API-Examples)**

// @contact.name API Support
// @contact.url http://cloud-barista.github.io
Expand Down Expand Up @@ -267,6 +267,7 @@ func RunServer() {
{"DELETE", "/vpc/:Name", DeleteVPC},
//-- for subnet
{"POST", "/vpc/:VPCName/subnet", AddSubnet},
{"GET", "/vpc/:VPCName/subnet/:Name", GetSubnet},
{"DELETE", "/vpc/:VPCName/subnet/:SubnetName", RemoveSubnet},
{"DELETE", "/vpc/:VPCName/cspsubnet/:Id", RemoveCSPSubnet},
//-- for management
Expand Down
2 changes: 1 addition & 1 deletion api-runtime/rest-runtime/ClusterRest.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func ChangeNodeGroupScaling(c echo.Context) error {
// @Produce json
// @Param ConnectionRequest body restruntime.ConnectionRequest true "Request body for deleting a Cluster"
// @Param Name path string true "The name of the Cluster to delete"
// @Param force query string false "Force delete the Cluster"
// @Param force query string false "Force delete the Cluster. ex) true or false(default: false)"
// @Success 200 {object} BooleanInfo "Result of the delete operation"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid JSON structure or missing fields"
// @Failure 404 {object} SimpleMsg "Resource Not Found"
Expand Down
2 changes: 1 addition & 1 deletion api-runtime/rest-runtime/DiskRest.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func IncreaseDiskSize(c echo.Context) error {
// @Produce json
// @Param ConnectionRequest body restruntime.ConnectionRequest true "Request body for deleting a Disk"
// @Param Name path string true "The name of the Disk to delete"
// @Param force query string false "Force delete the Disk"
// @Param force query string false "Force delete the Disk. ex) true or false(default: false)"
// @Success 200 {object} BooleanInfo "Result of the delete operation"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid JSON structure or missing fields"
// @Failure 404 {object} SimpleMsg "Resource Not Found"
Expand Down
2 changes: 1 addition & 1 deletion api-runtime/rest-runtime/KeyPairRest.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func GetKey(c echo.Context) error {
// @Produce json
// @Param ConnectionRequest body restruntime.ConnectionRequest true "Request body for deleting a KeyPair"
// @Param Name path string true "The name of the KeyPair to delete"
// @Param force query string false "Force delete the KeyPair"
// @Param force query string false "Force delete the KeyPair. ex) true or false(default: false)"
// @Success 200 {object} BooleanInfo "Result of the delete operation"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid JSON structure or missing fields"
// @Failure 404 {object} SimpleMsg "Resource Not Found"
Expand Down
2 changes: 1 addition & 1 deletion api-runtime/rest-runtime/MyImageRest.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func GetMyImage(c echo.Context) error {
// @Produce json
// @Param ConnectionRequest body restruntime.ConnectionRequest true "Request body for deleting a MyImage"
// @Param Name path string true "The name of the MyImage to delete"
// @Param force query string false "Force delete the MyImage"
// @Param force query string false "Force delete the MyImage. ex) true or false(default: false)"
// @Success 200 {object} BooleanInfo "Result of the delete operation"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid JSON structure or missing fields"
// @Failure 404 {object} SimpleMsg "Resource Not Found"
Expand Down
2 changes: 1 addition & 1 deletion api-runtime/rest-runtime/NLBRest.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ func GetVMGroupHealthInfo(c echo.Context) error {
// @Produce json
// @Param ConnectionRequest body restruntime.ConnectionRequest true "Request body for deleting an NLB"
// @Param Name path string true "The name of the NLB to delete"
// @Param force query string false "Force delete the NLB"
// @Param force query string false "Force delete the NLB. ex) true or false(default: false)"
// @Success 200 {object} BooleanInfo "Result of the delete operation"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid JSON structure or missing fields"
// @Failure 404 {object} SimpleMsg "Resource Not Found"
Expand Down
2 changes: 1 addition & 1 deletion api-runtime/rest-runtime/SecurityGroupRest.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func GetSecurity(c echo.Context) error {
// @Produce json
// @Param ConnectionRequest body restruntime.ConnectionRequest true "Request body for deleting a SecurityGroup"
// @Param Name path string true "The name of the SecurityGroup to delete"
// @Param force query string false "Force delete the SecurityGroup"
// @Param force query string false "Force delete the SecurityGroup. ex) true or false(default: false)"
// @Success 200 {object} BooleanInfo "Result of the delete operation"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid JSON structure or missing fields"
// @Failure 404 {object} SimpleMsg "Resource Not Found"
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 @@ -404,7 +404,7 @@ func GetCSPVM(c echo.Context) error {
// @Produce json
// @Param ConnectionRequest body restruntime.ConnectionRequest true "Request body for terminating a VM"
// @Param Name path string true "The name of the VM to terminate"
// @Param force query string false "Force terminate the VM"
// @Param force query string false "Force terminate the VM. ex) true or false(default: false)"
// @Success 200 {object} VMStatusResponse "Result of the terminate operation"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid JSON structure or missing fields"
// @Failure 404 {object} SimpleMsg "Resource Not Found"
Expand Down
55 changes: 54 additions & 1 deletion api-runtime/rest-runtime/VPC-SubnetRest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
package restruntime

import (
"strings"

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"
Expand Down Expand Up @@ -381,6 +383,56 @@ func AddSubnet(c echo.Context) error {
return c.JSON(http.StatusOK, result)
}

// getSubnet godoc
// @ID get-subnet
// @Summary Get Subnet
// @Description Retrieve a specific Subnet from a VPC.
// @Tags [VPC Management]
// @Accept json
// @Produce json
// @Param ConnectionName query string true "The name of the Connection to get a Subnet for"
// @Param VPCName path string true "The name of the VPC"
// @Param SubnetName path string true "The name of the Subnet to retrieve"
// @Success 200 {object} cres.SubnetInfo "Details of the requested Subnet"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid parameters"
// @Failure 404 {object} SimpleMsg "Resource Not Found"
// @Failure 500 {object} SimpleMsg "Internal Server Error"
// @Router /vpc/{VPCName}/subnet/{SubnetName} [get]
func GetSubnet(c echo.Context) error {
cblog.Info("call GetSubnet()")

var req ConnectionRequest

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")
}

vpcName := c.Param("VPCName")
subnetName := c.Param("Name")

// Validate that connectionName, vpcName, and subnetName are not empty
if req.ConnectionName == "" || vpcName == "" || subnetName == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Missing required parameters")
}

// Call common-runtime GetSubnet function
result, err := cmrt.GetSubnet(req.ConnectionName, vpcName, subnetName)
if err != nil {
if strings.Contains(err.Error(), "not found") {
return echo.NewHTTPError(http.StatusNotFound, "Subnet not found")
}
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

// Return the subnet info as JSON
return c.JSON(http.StatusOK, result)
}

// removeSubnet godoc
// @ID remove-subnet
// @Summary Remove Subnet
Expand All @@ -390,6 +442,7 @@ func AddSubnet(c echo.Context) error {
// @Produce json
// @Param VPCName path string true "The name of the VPC"
// @Param SubnetName path string true "The name of the Subnet to remove"
// @Param force query string false "Force delete the VPC. ex) true or false(default: false)"
// @Param ConnectionRequest body restruntime.ConnectionRequest true "Request body for removing a Subnet"
// @Success 200 {object} BooleanInfo "Result of the remove operation"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid JSON structure or missing fields"
Expand Down Expand Up @@ -501,7 +554,7 @@ func GetVPC(c echo.Context) error {
// @Produce json
// @Param ConnectionRequest body restruntime.ConnectionRequest true "Request body for deleting a VPC"
// @Param Name path string true "The name of the VPC to delete"
// @Param force query string false "Force delete the VPC"
// @Param force query string false "Force delete the VPC. ex) true or false(default: false)"
// @Success 200 {object} BooleanInfo "Result of the delete operation"
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid JSON structure or missing fields"
// @Failure 404 {object} SimpleMsg "Resource Not Found"
Expand Down
Loading

0 comments on commit c1f4920

Please sign in to comment.