dirs.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cephfs
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. "yunion.io/x/jsonutils"
  23. "yunion.io/x/pkg/errors"
  24. )
  25. type SCephFsDir struct {
  26. multicloud.SNasBase
  27. multicloud.STagBase
  28. client *SCephFSClient
  29. Name string
  30. Path string
  31. Parent string
  32. Quotas struct {
  33. MaxFiles int64
  34. MaxBytes int64
  35. }
  36. }
  37. func (dir *SCephFsDir) GetId() string {
  38. return dir.Path
  39. }
  40. func (dir *SCephFsDir) GetGlobalId() string {
  41. return dir.Path
  42. }
  43. func (dir *SCephFsDir) GetName() string {
  44. return dir.Name
  45. }
  46. func (dir *SCephFsDir) GetStatus() string {
  47. return api.NAS_STATUS_AVAILABLE
  48. }
  49. func (dir *SCephFsDir) GetFileSystemType() string {
  50. return "standard"
  51. }
  52. func (dir *SCephFsDir) GetStorageType() string {
  53. return "capacity"
  54. }
  55. func (dir *SCephFsDir) GetProtocol() string {
  56. return "CephFS"
  57. }
  58. func (dir *SCephFsDir) GetCapacityGb() int64 {
  59. return dir.Quotas.MaxBytes / 1024 / 1024 / 1024
  60. }
  61. func (dir *SCephFsDir) GetUsedCapacityGb() int64 {
  62. return 0
  63. }
  64. func (dir *SCephFsDir) GetMountTargetCountLimit() int {
  65. return 0
  66. }
  67. func (dir *SCephFsDir) GetZoneId() string {
  68. return ""
  69. }
  70. func (dir *SCephFsDir) GetMountTargets() ([]cloudprovider.ICloudMountTarget, error) {
  71. return []cloudprovider.ICloudMountTarget{}, nil
  72. }
  73. func (dir *SCephFsDir) CreateMountTarget(opts *cloudprovider.SMountTargetCreateOptions) (cloudprovider.ICloudMountTarget, error) {
  74. return nil, cloudprovider.ErrNotSupported
  75. }
  76. func (dir *SCephFsDir) Delete() error {
  77. return dir.client.DeleteDir(dir.client.fsId, dir.Path)
  78. }
  79. func (dir *SCephFsDir) Refresh() error {
  80. dirs, err := dir.client.GetCephDirs(dir.client.fsId)
  81. if err != nil {
  82. return err
  83. }
  84. for i := range dirs {
  85. if dirs[i].GetGlobalId() == dir.GetGlobalId() {
  86. return jsonutils.Update(dir, &dirs[i])
  87. }
  88. }
  89. return errors.Wrapf(cloudprovider.ErrNotFound, "%s", dir.Path)
  90. }
  91. func (dir *SCephFsDir) SetQuota(input *cloudprovider.SFileSystemSetQuotaInput) error {
  92. return dir.client.SetQuota(dir.client.fsId, dir.Path, input.MaxGb, input.MaxFiles)
  93. }
  94. func (cli *SCephFSClient) SetQuota(fsId, path string, maxGb, maxFiles int64) error {
  95. path = fmt.Sprintf("/%s", strings.TrimPrefix(path, "/"))
  96. res := fmt.Sprintf("cephfs/%s/quota?path=%s", fsId, path)
  97. params := map[string]interface{}{}
  98. if maxFiles > 0 {
  99. params["max_files"] = fmt.Sprintf("%d", maxFiles)
  100. }
  101. if maxGb > 0 {
  102. params["max_bytes"] = fmt.Sprintf("%d", maxGb*1024*1024*1024)
  103. }
  104. _, err := cli.put(res, params)
  105. return err
  106. }
  107. func (cli *SCephFSClient) GetCephDirs(fsId string) ([]SCephFsDir, error) {
  108. res := fmt.Sprintf("cephfs/%s/ls_dir", fsId)
  109. params := url.Values{}
  110. resp, err := cli.list(res, params)
  111. if err != nil {
  112. return nil, err
  113. }
  114. ret := []SCephFsDir{}
  115. err = resp.Unmarshal(&ret)
  116. if err != nil {
  117. return nil, err
  118. }
  119. for i := range ret {
  120. ret[i].client = cli
  121. }
  122. return ret, nil
  123. }
  124. func (cli *SCephFSClient) GetICloudFileSystems() ([]cloudprovider.ICloudFileSystem, error) {
  125. dirs, err := cli.GetCephDirs(cli.fsId)
  126. if err != nil {
  127. return nil, err
  128. }
  129. ret := []cloudprovider.ICloudFileSystem{}
  130. for i := range dirs {
  131. ret = append(ret, &dirs[i])
  132. }
  133. return ret, nil
  134. }
  135. func (cli *SCephFSClient) GetICloudFileSystemById(id string) (cloudprovider.ICloudFileSystem, error) {
  136. dirs, err := cli.GetCephDirs(cli.fsId)
  137. if err != nil {
  138. return nil, err
  139. }
  140. for i := range dirs {
  141. if dirs[i].GetGlobalId() == id {
  142. return &dirs[i], nil
  143. }
  144. }
  145. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  146. }
  147. func (cli *SCephFSClient) CreateDir(fsId, path string) error {
  148. res := fmt.Sprintf("cephfs/%s/tree", fsId)
  149. _, err := cli.post(res, map[string]interface{}{
  150. "path": fmt.Sprintf("/%s", strings.TrimPrefix(path, "/")),
  151. })
  152. return err
  153. }
  154. func (cli *SCephFSClient) DeleteDir(fsId, path string) error {
  155. res := fmt.Sprintf("cephfs/%s/tree?path=%s", fsId, fmt.Sprintf("/%s", strings.TrimPrefix(path, "/")))
  156. _, err := cli.delete(res, map[string]interface{}{})
  157. return err
  158. }
  159. func (cli *SCephFSClient) CreateICloudFileSystem(opts *cloudprovider.FileSystemCraeteOptions) (cloudprovider.ICloudFileSystem, error) {
  160. err := cli.CreateDir(cli.fsId, opts.Name)
  161. if err != nil {
  162. return nil, err
  163. }
  164. cli.SetQuota(cli.fsId, opts.Name, opts.Capacity, 0)
  165. return cli.GetICloudFileSystemById("/" + opts.Name)
  166. }