Fix quota switch fail to get project size

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
wang yan 2019-08-14 18:35:38 +08:00
parent 54cb39b7a7
commit a947a4259d
3 changed files with 19 additions and 18 deletions

View File

@ -18,9 +18,7 @@ import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
"github.com/goharbor/harbor/src/common/models"
"strconv"
)
// AddBlobToProject ...
@ -108,17 +106,17 @@ func GetBlobsNotInProject(projectID int64, blobDigests ...string) ([]*models.Blo
// CountSizeOfProject ...
func CountSizeOfProject(pid int64) (int64, error) {
var res []orm.Params
num, err := GetOrmer().Raw(`SELECT sum(bb.size) FROM project_blob pb LEFT JOIN blob bb ON pb.blob_id = bb.id WHERE pb.project_id = ? `, pid).Values(&res)
var blobs []models.Blob
_, err := GetOrmer().Raw(`SELECT bb.id, bb.digest, bb.content_type, bb.size, bb.creation_time FROM project_blob pb LEFT JOIN blob bb ON pb.blob_id = bb.id WHERE pb.project_id = ? `, pid).QueryRows(&blobs)
if err != nil {
return -1, err
return 0, err
}
if num > 0 {
size, err := strconv.ParseInt(res[0]["sum"].(string), 0, 64)
if err != nil {
return -1, err
var size int64
for _, blob := range blobs {
size += blob.Size
}
return size, nil
}
return -1, err
return size, err
}

View File

@ -76,7 +76,7 @@ func (ia *InternalAPI) RenameAdmin() {
// QuotaSwitcher ...
type QuotaSwitcher struct {
Disabled bool
Enabled bool
}
// SwitchQuota ...
@ -86,14 +86,17 @@ func (ia *InternalAPI) SwitchQuota() {
ia.SendBadRequestError(err)
return
}
// From disable to enable, it needs to update the quota usage bases on the DB records.
if config.QuotaPerProjectEnable() == false && req.Disabled == true {
// quota per project from disable to enable, it needs to update the quota usage bases on the DB records.
if !config.QuotaPerProjectEnable() && req.Enabled {
if err := ia.ensureQuota(); err != nil {
ia.SendBadRequestError(err)
ia.SendInternalServerError(err)
return
}
}
defer config.GetCfgManager().Set(common.QuotaPerProjectEnable, req.Disabled)
defer func() {
config.GetCfgManager().Set(common.QuotaPerProjectEnable, req.Enabled)
config.GetCfgManager().Save()
}()
return
}

View File

@ -37,7 +37,7 @@ func TestSwitchQuota(t *testing.T) {
url: "/api/internal/switchquota",
credential: sysAdmin,
bodyJSON: &QuotaSwitcher{
Disabled: true,
Enabled: true,
},
},
code: http.StatusOK,