Skip to content

Commit

Permalink
[feat] api get portal quota
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed May 24, 2024
1 parent efb6d4d commit 69e977c
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 2 deletions.
51 changes: 51 additions & 0 deletions test/portal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
lserr "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/sdk_error"
lsportalV1 "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/services/portal/v1"
lsportalV2 "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/services/portal/v2"
ltesting "testing"
)

Expand Down Expand Up @@ -63,3 +64,53 @@ func TestGetPortalInfoFailure(t *ltesting.T) {
t.Log("RESULT:", err)
t.Log("PASS")
}

func TestListAllQuotaSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
quotas, err := vngcloud.VServerGateway().V2().PortalService().ListAllQuotaUsed()

if err != nil {
t.Errorf("Expect error to be nil but got %+v", err)
}

if quotas == nil {
t.Errorf("Expect quotas not to be nil but got nil")
}

t.Log("RESULT:", quotas)
t.Log("PASS")
}

func TestGetQuotaByNameFailure(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lsportalV2.NewGetQuotaByNameRequest("fake-quota-name")
quota, err := vngcloud.VServerGateway().V2().PortalService().GetQuotaByName(opt)

if err == nil {
t.Errorf("Expect error but got nil")
}

if quota != nil {
t.Errorf("Expect quota to be nil but got %+v", quota)
}

t.Log("RESULT:", err)
t.Log("PASS")
}

func TestGetQuotaByNamePass(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lsportalV2.NewGetQuotaByNameRequest(lsportalV2.QtVolumeAttachLimit)
quota, err := vngcloud.VServerGateway().V2().PortalService().GetQuotaByName(opt)

if err != nil {
t.Errorf("Expect error to be nil but got %+v", err)
}

if quota == nil {
t.Errorf("Expect quota not to be nil but got nil")
}

t.Log("RESULT:", quota)
t.Log("PASS")
}
22 changes: 22 additions & 0 deletions vngcloud/entity/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,25 @@ type Portal struct {
ProjectID string
UserID int
}

type Quota struct {
Description string
Name string
Type string
Limit int
Used int
}

type ListQuotas struct {
Items []*Quota
}

func (s *ListQuotas) FindQuotaByName(name string) *Quota {
for _, q := range s.Items {
if q.Name == name {
return q
}
}

return nil
}
6 changes: 6 additions & 0 deletions vngcloud/sdk_error/error_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,9 @@ const (
EcVServerServerDeleteDeletingServer = ErrorCode("VngCloudVServerServerDeleteDeletingServer")
EcVServerServerDeleteBillingServer = ErrorCode("VngCloudVServerServerDeleteBillingServer")
)

// vServer quota

const (
EcVServerQuotaNotFound = ErrorCode("VngCloudVServerQuotaNotFound")
)
11 changes: 11 additions & 0 deletions vngcloud/sdk_error/quota.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sdk_error

import lfmt "fmt"

func WithErrorQuotaNotFound(_ IErrorRespone) func(sdkError ISdkError) {
return func(sdkError ISdkError) {
sdkError.WithErrorCode(EcVServerQuotaNotFound).
WithMessage("Quota not found").
WithErrors(lfmt.Errorf("quota not found"))
}
}
7 changes: 5 additions & 2 deletions vngcloud/services/portal/iportal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package portal

import (
lsentity "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/entity"
lsdkErr "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/sdk_error"
lserr "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/sdk_error"
lsportalV1 "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/services/portal/v1"
lsportalV2 "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/services/portal/v2"
)

type IPortalServiceV1 interface {
GetPortalInfo(popts lsportalV1.IGetPortalInfoRequest) (*lsentity.Portal, lsdkErr.ISdkError)
GetPortalInfo(popts lsportalV1.IGetPortalInfoRequest) (*lsentity.Portal, lserr.ISdkError)
}

type IPortalServiceV2 interface {
ListAllQuotaUsed() (*lsentity.ListQuotas, lserr.ISdkError)
GetQuotaByName(popts lsportalV2.IGetQuotaByNameRequest) (*lsentity.Quota, lserr.ISdkError)
}
5 changes: 5 additions & 0 deletions vngcloud/services/portal/v2/irequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package v2

type IGetQuotaByNameRequest interface {
GetName() QuotaName
}
42 changes: 42 additions & 0 deletions vngcloud/services/portal/v2/portal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 *PortalServiceV2) ListAllQuotaUsed() (*lsentity.ListQuotas, lserr.ISdkError) {
url := listAllQuotaUsedUrl(s.PortalClient)
resp := new(ListAllQuotaUsedResponse)
errResp := lserr.NewErrorResponse(lserr.NormalErrorType)
req := lsclient.NewRequest().
WithOkCodes(200).
WithJsonResponse(resp).
WithJsonError(errResp)

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

return resp.ToEntityListQuotas(), nil
}

func (s *PortalServiceV2) GetQuotaByName(popts IGetQuotaByNameRequest) (*lsentity.Quota, lserr.ISdkError) {
listQuotas, sdkErr := s.ListAllQuotaUsed()
if sdkErr != nil {
return nil, sdkErr
}

quota := listQuotas.FindQuotaByName(string(popts.GetName()))
if quota == nil {
return nil, lserr.ErrorHandler(nil, lserr.WithErrorQuotaNotFound(nil)).WithKVparameters("quotaName", popts.GetName())
}

return quota, nil
}

func (s *PortalServiceV2) getProjectId() string {
return s.PortalClient.GetProjectId()
}
15 changes: 15 additions & 0 deletions vngcloud/services/portal/v2/portal_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package v2

type GetQuotaByNameRequest struct {
Name QuotaName
}

func NewGetQuotaByNameRequest(pname QuotaName) IGetQuotaByNameRequest {
return &GetQuotaByNameRequest{
Name: pname,
}
}

func (s *GetQuotaByNameRequest) GetName() QuotaName {
return s.Name
}
58 changes: 58 additions & 0 deletions vngcloud/services/portal/v2/portal_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package v2

import (
lstrconv "strconv"

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

const (
QtVolumeAttachLimit = QuotaName("VOLUME_PER_SERVER") // The maximum number of volumes that you can attach to an server
)

type (
Quota struct {
Description string `json:"description,omitempty"`
Name QuotaName `json:"quotaName"`
Type QuotaType `json:"type"`
Used string `json:"used"`
Limit int `json:"limit"`
}

QuotaType string
QuotaName string
)

type ListAllQuotaUsedResponse struct {
Data []Quota `json:"data"`
}

func (s *ListAllQuotaUsedResponse) ToEntityListQuotas() *lsentity.ListQuotas {
listQuotas := &lsentity.ListQuotas{
Items: make([]*lsentity.Quota, 0),
}
for _, q := range s.Data {
listQuotas.Items = append(listQuotas.Items, q.ToEntityQuota())
}

return listQuotas
}

func (s *Quota) ToEntityQuota() *lsentity.Quota {
var (
used int
err error
)

if used, err = lstrconv.Atoi(s.Used); err != nil {
used = 0
}

return &lsentity.Quota{
Description: s.Description,
Name: string(s.Name),
Type: string(s.Type),
Limit: s.Limit,
Used: used,
}
}
10 changes: 10 additions & 0 deletions vngcloud/services/portal/v2/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package v2

import lsclient "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/client"

func listAllQuotaUsedUrl(psc lsclient.IServiceClient) string {
return psc.ServiceURL(
psc.GetProjectId(),
"quotas",
"quotaUsed")
}

0 comments on commit 69e977c

Please sign in to comment.