sfs-turbo.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 hcso
  15. import (
  16. "fmt"
  17. "strings"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
  22. api "yunion.io/x/cloudmux/pkg/apis/compute"
  23. "yunion.io/x/cloudmux/pkg/cloudprovider"
  24. "yunion.io/x/cloudmux/pkg/multicloud"
  25. "yunion.io/x/cloudmux/pkg/multicloud/huawei"
  26. )
  27. type SfsTurbo struct {
  28. multicloud.SNasBase
  29. huawei.HuaweiTags
  30. region *SRegion
  31. EnterpriseProjectId string
  32. Actions []string
  33. AvailCapacity float64
  34. AvailabilityZone string
  35. AzName string
  36. CreatedAt time.Time
  37. CryptKeyId string
  38. ExpandType string
  39. ExportLocation string
  40. Id string
  41. Name string
  42. PayModel string
  43. Region string
  44. SecurityGroupId string
  45. ShareProto string
  46. ShareType string
  47. Size float64
  48. Status string
  49. SubStatus string
  50. SubnetId string
  51. VpcId string
  52. Description string
  53. }
  54. func (self *SfsTurbo) GetName() string {
  55. return self.Name
  56. }
  57. func (self *SfsTurbo) GetId() string {
  58. return self.Id
  59. }
  60. func (self *SfsTurbo) GetGlobalId() string {
  61. return self.Id
  62. }
  63. func (self *SfsTurbo) GetFileSystemType() string {
  64. return "SFS Turbo"
  65. }
  66. func (self *SfsTurbo) Refresh() error {
  67. sf, err := self.region.GetSfsTurbo(self.Id)
  68. if err != nil {
  69. return errors.Wrapf(err, "GetSfsTurbo")
  70. }
  71. return jsonutils.Update(self, sf)
  72. }
  73. func (self *SfsTurbo) GetBillingType() string {
  74. if self.PayModel == "0" {
  75. return billing_api.BILLING_TYPE_POSTPAID
  76. }
  77. return billing_api.BILLING_TYPE_PREPAID
  78. }
  79. func (self *SfsTurbo) GetStorageType() string {
  80. if len(self.ExpandType) == 0 {
  81. return strings.ToLower(self.ShareType)
  82. }
  83. return strings.ToLower(self.ShareType) + ".enhanced"
  84. }
  85. func (self *SfsTurbo) GetProtocol() string {
  86. return self.ShareProto
  87. }
  88. func (self *SfsTurbo) GetStatus() string {
  89. switch self.Status {
  90. case "100":
  91. return api.NAS_STATUS_CREATING
  92. case "200":
  93. return api.NAS_STATUS_AVAILABLE
  94. case "300":
  95. return api.NAS_STATUS_UNKNOWN
  96. case "303":
  97. return api.NAS_STATUS_CREATE_FAILED
  98. case "400":
  99. return api.NAS_STATUS_DELETING
  100. case "800":
  101. return api.NAS_STATUS_UNAVAILABLE
  102. default:
  103. return self.Status
  104. }
  105. }
  106. func (self *SfsTurbo) GetCreatedAt() time.Time {
  107. return self.CreatedAt
  108. }
  109. func (self *SfsTurbo) GetCapacityGb() int64 {
  110. return int64(self.Size)
  111. }
  112. func (self *SfsTurbo) GetUsedCapacityGb() int64 {
  113. return int64(self.Size - self.AvailCapacity)
  114. }
  115. func (self *SfsTurbo) GetMountTargetCountLimit() int {
  116. return 1
  117. }
  118. func (self *SfsTurbo) GetZoneId() string {
  119. return self.AvailabilityZone
  120. }
  121. func (self *SfsTurbo) GetMountTargets() ([]cloudprovider.ICloudMountTarget, error) {
  122. mt := &sMoutTarget{sfs: self}
  123. return []cloudprovider.ICloudMountTarget{mt}, nil
  124. }
  125. func (self *SfsTurbo) CreateMountTarget(opts *cloudprovider.SMountTargetCreateOptions) (cloudprovider.ICloudMountTarget, error) {
  126. return nil, errors.Wrap(cloudprovider.ErrNotSupported, "CreateMountTarget")
  127. }
  128. func (self *SfsTurbo) Delete() error {
  129. return self.region.DeleteSfsTurbo(self.Id)
  130. }
  131. func (self *SRegion) GetICloudFileSystems() ([]cloudprovider.ICloudFileSystem, error) {
  132. sfs, err := self.GetSfsTurbos()
  133. if err != nil {
  134. return nil, errors.Wrapf(err, "self.GetSfsTurbos")
  135. }
  136. ret := []cloudprovider.ICloudFileSystem{}
  137. for i := range sfs {
  138. sfs[i].region = self
  139. ret = append(ret, &sfs[i])
  140. }
  141. return ret, nil
  142. }
  143. func (self *SRegion) GetICloudFileSystemById(id string) (cloudprovider.ICloudFileSystem, error) {
  144. sf, err := self.GetSfsTurbo(id)
  145. if err != nil {
  146. return nil, errors.Wrapf(err, "GetSfsTurbo(%s)", id)
  147. }
  148. return sf, nil
  149. }
  150. func (self *SRegion) GetSfsTurbos() ([]SfsTurbo, error) {
  151. queues := make(map[string]string)
  152. sfs := make([]SfsTurbo, 0, 2)
  153. err := doListAllWithOffset(self.ecsClient.SfsTurbos.List, queues, &sfs)
  154. if err != nil {
  155. return nil, errors.Wrapf(err, "doListAllWithOffset")
  156. }
  157. return sfs, nil
  158. }
  159. func (self *SRegion) GetSfsTurbo(id string) (*SfsTurbo, error) {
  160. sf := &SfsTurbo{region: self}
  161. err := DoGet(self.ecsClient.SfsTurbos.Get, id, nil, &sf)
  162. return sf, errors.Wrapf(err, "self.ecsClient.SfsTurbos.Get")
  163. }
  164. func (self *SRegion) DeleteSfsTurbo(id string) error {
  165. return DoDelete(self.ecsClient.SfsTurbos.Delete, id, nil, nil)
  166. }
  167. func (self *SRegion) GetSysDefaultSecgroupId() (string, error) {
  168. secs, err := self.GetSecurityGroups("default", "")
  169. if err != nil {
  170. return "", errors.Wrapf(err, "GetSecurityGroups")
  171. }
  172. if len(secs) > 0 {
  173. return secs[0].ID, nil
  174. }
  175. return "", fmt.Errorf("not found default security group")
  176. }
  177. func (self *SRegion) CreateICloudFileSystem(opts *cloudprovider.FileSystemCraeteOptions) (cloudprovider.ICloudFileSystem, error) {
  178. fs, err := self.CreateSfsTurbo(opts)
  179. if err != nil {
  180. return nil, errors.Wrapf(err, "CreateSfsTurbo")
  181. }
  182. return fs, nil
  183. }
  184. func (self *SRegion) CreateSfsTurbo(opts *cloudprovider.FileSystemCraeteOptions) (*SfsTurbo, error) {
  185. secId, err := self.GetSysDefaultSecgroupId()
  186. if err != nil {
  187. return nil, errors.Wrapf(err, "GetSysDefaultSecgroupId")
  188. }
  189. metadata := map[string]string{}
  190. if strings.HasSuffix(opts.StorageType, ".enhanced") {
  191. metadata["expand_type"] = "bandwidth"
  192. }
  193. params := map[string]interface{}{
  194. "share": map[string]interface{}{
  195. "name": opts.Name,
  196. "share_proto": strings.ToUpper(opts.Protocol),
  197. "share_type": strings.ToUpper(strings.TrimSuffix(opts.StorageType, ".enhanced")),
  198. "size": opts.Capacity,
  199. "availability_zone": opts.ZoneId,
  200. "vpc_id": opts.VpcId,
  201. "subnet_id": opts.NetworkId,
  202. "security_group_id": secId,
  203. "description": opts.Desc,
  204. "metadata": metadata,
  205. },
  206. }
  207. resp, err := self.ecsClient.SfsTurbos.Create(jsonutils.Marshal(params))
  208. if err != nil {
  209. return nil, errors.Wrapf(err, "Create")
  210. }
  211. id, err := resp.GetString("id")
  212. if err != nil {
  213. return nil, errors.Wrapf(err, "resp.GetString(id)")
  214. }
  215. return self.GetSfsTurbo(id)
  216. }
  217. func (self *SRegion) GetICloudAccessGroups() ([]cloudprovider.ICloudAccessGroup, error) {
  218. return []cloudprovider.ICloudAccessGroup{}, nil
  219. }
  220. func (self *SRegion) CreateICloudAccessGroup(opts *cloudprovider.SAccessGroup) (cloudprovider.ICloudAccessGroup, error) {
  221. return nil, errors.Wrapf(cloudprovider.ErrNotSupported, "CreateICloudAccessGroup")
  222. }
  223. func (self *SRegion) GetICloudAccessGroupById(id string) (cloudprovider.ICloudAccessGroup, error) {
  224. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "GetICloudAccessGroupById(%s)", id)
  225. }
  226. type sMoutTarget struct {
  227. sfs *SfsTurbo
  228. }
  229. func (self *sMoutTarget) GetName() string {
  230. return self.sfs.Name
  231. }
  232. func (self *sMoutTarget) GetGlobalId() string {
  233. return self.sfs.GetGlobalId()
  234. }
  235. func (self *sMoutTarget) GetAccessGroupId() string {
  236. return ""
  237. }
  238. func (self *sMoutTarget) GetDomainName() string {
  239. return self.sfs.ExportLocation
  240. }
  241. func (self *sMoutTarget) GetNetworkType() string {
  242. return api.NETWORK_TYPE_VPC
  243. }
  244. func (self *sMoutTarget) GetNetworkId() string {
  245. return self.sfs.SubnetId
  246. }
  247. func (self *sMoutTarget) GetVpcId() string {
  248. return self.sfs.VpcId
  249. }
  250. func (self *sMoutTarget) GetStatus() string {
  251. return api.MOUNT_TARGET_STATUS_AVAILABLE
  252. }
  253. func (self *sMoutTarget) Delete() error {
  254. return nil
  255. }