-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
efb6d4d
commit 69e977c
Showing
10 changed files
with
225 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package v2 | ||
|
||
type IGetQuotaByNameRequest interface { | ||
GetName() QuotaName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |