Skip to content

Commit

Permalink
[feat] api list all servers by secgroup ID api
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed Sep 10, 2024
1 parent 164a1f6 commit 6a02004
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions vngcloud/entity/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ type (
Uuid string
}
)

type ListServers struct {
Items []*Server
}
3 changes: 3 additions & 0 deletions vngcloud/services/network/inetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ type INetworkServiceV2 interface {
GetAllAddressPairByVirtualSubnetId(popts lsnetworkSvcV2.IGetAllAddressPairByVirtualSubnetIdRequest) ([]*lsentity.AddressPair, lserr.IError)
SetAddressPairInVirtualSubnet(popts lsnetworkSvcV2.ISetAddressPairInVirtualSubnetRequest) (*lsentity.AddressPair, lserr.IError)
DeleteAddressPair(popts lsnetworkSvcV2.IDeleteAddressPairRequest) lserr.IError

// Servers
ListAllServersBySecgroupId(popts lsnetworkSvcV2.IListAllServersBySubnetIdRequest) (*lsentity.ListServers, lserr.IError)
}
4 changes: 4 additions & 0 deletions vngcloud/services/network/v2/irequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ type IDeleteAddressPairRequest interface {
ParseUserAgent() string
GetAddressPairID() string
}

type IListAllServersBySubnetIdRequest interface {
GetSecgroupId() string
}
27 changes: 27 additions & 0 deletions vngcloud/services/network/v2/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package v2

import (
lsclient "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/client"
lsentity "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/entity"
lserr "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/sdk_error"
)

func (s *NetworkServiceV2) ListAllServersBySecgroupId(popts IListAllServersBySubnetIdRequest) (*lsentity.ListServers, lserr.IError) {
url := listAllServersBySecgroupIdUrl(s.VserverClient, popts)
resp := new(ListAllServersBySecgroupIdResponse)
errResp := lserr.NewErrorResponse(lserr.NormalErrorType)
req := lsclient.NewRequest().
WithOkCodes(200).
WithJsonResponse(resp).
WithJsonError(errResp)

if _, sdkErr := s.VserverClient.Get(url, req); sdkErr != nil {
return nil, lserr.SdkErrorHandler(sdkErr, errResp,
lserr.WithErrorSecgroupNotFound(errResp)).
WithKVparameters(
"secgroupId", popts.GetSecgroupId(),
"projectId", s.getProjectId())
}

return resp.ToEntityListServers(), nil
}
1 change: 1 addition & 0 deletions vngcloud/services/network/v2/server_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package v2
25 changes: 25 additions & 0 deletions vngcloud/services/network/v2/server_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package v2

import lsentity "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/entity"

type ListAllServersBySecgroupIdResponse struct {
Data []struct {
Name string `json:"name"`
UUID string `json:"uuid"`
Status string `json:"status"`
} `json:"data"`
}

func (s *ListAllServersBySecgroupIdResponse) ToEntityListServers() *lsentity.ListServers {
servers := make([]*lsentity.Server, 0)
for _, server := range s.Data {
servers = append(servers, &lsentity.Server{
Name: server.Name,
Uuid: server.UUID,
Status: server.Status,
})
}
return &lsentity.ListServers{
Items: servers,
}
}
8 changes: 8 additions & 0 deletions vngcloud/services/network/v2/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,11 @@ func deleteAddressPairUrl(psc lsclient.IServiceClient, popts IDeleteAddressPairR
"addressPairs",
popts.GetAddressPairID())
}

func listAllServersBySecgroupIdUrl(psc lsclient.IServiceClient, popts IListAllServersBySubnetIdRequest) string {
return psc.ServiceURL(
psc.GetProjectId(),
"secgroups",
popts.GetSecgroupId(),
"servers")
}

0 comments on commit 6a02004

Please sign in to comment.