Skip to content

Commit

Permalink
Merge pull request #1098 from ioito/hotfix/qx-cephfs-set-quota
Browse files Browse the repository at this point in the history
fix(cephfs): support set quota
  • Loading branch information
ioito authored Oct 28, 2024
2 parents df8c7db + 065a50f commit 47c8a5c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
5 changes: 5 additions & 0 deletions pkg/cloudprovider/mount_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ type SMountTargetCreateOptions struct {
NetworkId string
FileSystemId string
}

type SFileSystemSetQuotaInput struct {
MaxFiles int64
MaxGb int64
}
2 changes: 2 additions & 0 deletions pkg/cloudprovider/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,8 @@ type ICloudFileSystem interface {
GetMountTargets() ([]ICloudMountTarget, error)
CreateMountTarget(opts *SMountTargetCreateOptions) (ICloudMountTarget, error)

SetQuota(input *SFileSystemSetQuotaInput) error

Delete() error
}

Expand Down
51 changes: 37 additions & 14 deletions pkg/multicloud/cephfs/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (
api "yunion.io/x/cloudmux/pkg/apis/compute"
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/cloudmux/pkg/multicloud"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
)

type SCephFsDir struct {
multicloud.SVirtualResourceBase
multicloud.SBillingBase
multicloud.SNasBase
multicloud.STagBase
client *SCephFSClient

Expand Down Expand Up @@ -96,6 +96,37 @@ func (dir *SCephFsDir) Delete() error {
return dir.client.DeleteDir(dir.client.fsId, dir.Path)
}

func (dir *SCephFsDir) Refresh() error {
dirs, err := dir.client.GetCephDirs(dir.client.fsId)
if err != nil {
return err
}
for i := range dirs {
if dirs[i].GetGlobalId() == dir.GetGlobalId() {
return jsonutils.Update(dir, &dirs[i])
}
}
return errors.Wrapf(cloudprovider.ErrNotFound, dir.Path)
}

func (dir *SCephFsDir) SetQuota(input *cloudprovider.SFileSystemSetQuotaInput) error {
return dir.client.SetQuota(dir.client.fsId, dir.Path, input.MaxGb, input.MaxFiles)
}

func (cli *SCephFSClient) SetQuota(fsId, path string, maxGb, maxFiles int64) error {
path = fmt.Sprintf("/%s", strings.TrimPrefix(path, "/"))
res := fmt.Sprintf("cephfs/%s/quota?path=%s", fsId, path)
params := map[string]interface{}{}
if maxFiles > 0 {
params["max_files"] = fmt.Sprintf("%d", maxFiles)
}
if maxGb > 0 {
params["max_bytes"] = fmt.Sprintf("%d", maxGb*1024*1024*1024)
}
_, err := cli.put(res, params)
return err
}

func (cli *SCephFSClient) GetCephDirs(fsId string) ([]SCephFsDir, error) {
res := fmt.Sprintf("cephfs/%s/ls_dir", fsId)
params := url.Values{}
Expand All @@ -108,6 +139,9 @@ func (cli *SCephFSClient) GetCephDirs(fsId string) ([]SCephFsDir, error) {
if err != nil {
return nil, err
}
for i := range ret {
ret[i].client = cli
}
return ret, nil
}

Expand All @@ -118,7 +152,6 @@ func (cli *SCephFSClient) GetICloudFileSystems() ([]cloudprovider.ICloudFileSyst
}
ret := []cloudprovider.ICloudFileSystem{}
for i := range dirs {
dirs[i].client = cli
ret = append(ret, &dirs[i])
}
return ret, nil
Expand All @@ -130,7 +163,6 @@ func (cli *SCephFSClient) GetICloudFileSystemById(id string) (cloudprovider.IClo
return nil, err
}
for i := range dirs {
dirs[i].client = cli
if dirs[i].GetGlobalId() == id {
return &dirs[i], nil
}
Expand All @@ -152,20 +184,11 @@ func (cli *SCephFSClient) DeleteDir(fsId, path string) error {
return err
}

func (cli *SCephFSClient) SetDirQuota(fsId, path string, maxBytes int64) error {
res := fmt.Sprintf("cephfs/%s/quota", fsId)
_, err := cli.put(res, map[string]interface{}{
"path": fmt.Sprintf("/%s", strings.TrimPrefix(path, "/")),
"max_bytes": maxBytes,
})
return err
}

func (cli *SCephFSClient) CreateICloudFileSystem(opts *cloudprovider.FileSystemCraeteOptions) (cloudprovider.ICloudFileSystem, error) {
err := cli.CreateDir(cli.fsId, opts.Name)
if err != nil {
return nil, err
}
cli.SetDirQuota(cli.fsId, opts.Name, opts.Capacity*1024*1024*1024)
cli.SetQuota(cli.fsId, opts.Name, opts.Capacity, 0)
return cli.GetICloudFileSystemById("/" + opts.Name)
}
5 changes: 3 additions & 2 deletions pkg/multicloud/cephfs/shell/cephfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ func init() {
type NasDirSetQuotaOption struct {
FS_ID string
PATH string
MaxBytes int64
MaxMb int64
MaxFiles int64
}
shellutils.R(&NasDirSetQuotaOption{}, "cephfs-dir-set-quota", "Set cephfs dir quota", func(cli *cephfs.SCephFSClient, args *NasDirSetQuotaOption) error {
return cli.SetDirQuota(args.FS_ID, args.PATH, args.MaxBytes)
return cli.SetQuota(args.FS_ID, args.PATH, args.MaxMb, args.MaxFiles)
})

}
9 changes: 9 additions & 0 deletions pkg/multicloud/nas_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@

package multicloud

import (
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/pkg/errors"
)

type SNasBase struct {
SVirtualResourceBase
SBillingBase
}

func (self *SNasBase) SetQuota(input *cloudprovider.SFileSystemSetQuotaInput) error {
return errors.Wrapf(cloudprovider.ErrNotImplemented, "SetQuota")
}

0 comments on commit 47c8a5c

Please sign in to comment.