disk.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 ecloud
  15. import (
  16. "context"
  17. "fmt"
  18. "strings"
  19. "time"
  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. )
  26. type SDisk struct {
  27. storage *SStorage
  28. // TODO instance
  29. multicloud.SDisk
  30. EcloudTags
  31. multicloud.SBillingBase
  32. SZoneRegionBase
  33. SCreateTime
  34. ManualAttr SDiskManualAttr
  35. // 硬盘可挂载主机类型
  36. AttachServerTypes []string `json:"attachServerTypes,omitempty"`
  37. AvailabilityZone string `json:"region"`
  38. BackupId string `json:"backupId,omitempty"`
  39. Description string `json:"description,omitempty"`
  40. ID string `json:"id"`
  41. IsDelete bool `json:"isDelete,omitempty"`
  42. IsShare bool `json:"isShare,omitempty"`
  43. // 磁盘所在集群的ID
  44. Metadata string `json:"metadata,omitempty"`
  45. Name string `json:"name,omitempty"`
  46. OperationFlag string `json:"operationFlag,omitempty"`
  47. // 硬盘挂载主机ID列表
  48. ServerId []string `json:"serverIds,omitempty"`
  49. SizeGB int `json:"size"`
  50. SourceVolumeId string `json:"sourceVolumeId,omitempty"`
  51. Status string `json:"status,omitempty"`
  52. Type string `json:"type,omitempty"`
  53. VolumeType string `json:"volumeType,omitempty"`
  54. Iscsi bool `json:"iscsi,omitempty"`
  55. ProductType string `json:"productType,omitempty"`
  56. }
  57. type SDiskManualAttr struct {
  58. IsVirtual bool
  59. TemplateId string
  60. ServerId string
  61. }
  62. func (d *SDisk) GetBillingType() string {
  63. return billing_api.BILLING_TYPE_POSTPAID
  64. }
  65. func (d *SDisk) GetExpiredAt() time.Time {
  66. return time.Time{}
  67. }
  68. func (d *SDisk) GetId() string {
  69. return d.ID
  70. }
  71. func (d *SDisk) GetName() string {
  72. return d.Name
  73. }
  74. func (d *SDisk) GetGlobalId() string {
  75. return d.ID
  76. }
  77. func (d *SDisk) GetCreatedAt() time.Time {
  78. return d.SCreateTime.GetCreatedAt()
  79. }
  80. func (d *SDisk) GetStatus() string {
  81. if d.IsDelete {
  82. // TODO
  83. return ""
  84. }
  85. switch strings.ToLower(d.Status) {
  86. case "available", "in-use":
  87. return api.DISK_READY
  88. case "attaching":
  89. return api.DISK_ATTACHING
  90. case "backing_up":
  91. return api.DISK_BACKUP_STARTALLOC
  92. case "creating", "downloading":
  93. return api.DISK_ALLOCATING
  94. case "deleting":
  95. return api.DISK_DEALLOC
  96. case "uploading":
  97. return api.DISK_SAVING
  98. case "error":
  99. return api.DISK_ALLOC_FAILED
  100. case "error_deleting":
  101. return api.DISK_DEALLOC_FAILED
  102. case "restoring_backup":
  103. return api.DISK_REBUILD
  104. case "detaching":
  105. return api.DISK_DETACHING
  106. case "extending":
  107. return api.DISK_RESIZING
  108. case "error_extending":
  109. return api.DISK_RESIZE_FAILED
  110. case "error_restoring", "unrecognized":
  111. return api.DISK_UNKNOWN
  112. default:
  113. return api.DISK_UNKNOWN
  114. }
  115. }
  116. func (d *SDisk) Refresh() error {
  117. return nil
  118. }
  119. func (d *SDisk) IsEmulated() bool {
  120. return false
  121. }
  122. func (s *SDisk) GetSysTags() map[string]string {
  123. data := map[string]string{}
  124. data["hypervisor"] = api.HYPERVISOR_ECLOUD
  125. return data
  126. }
  127. func (s *SDisk) GetProjectId() string {
  128. return ""
  129. }
  130. func (s *SDisk) GetIStorage() (cloudprovider.ICloudStorage, error) {
  131. return s.storage, nil
  132. }
  133. func (s *SDisk) GetDiskFormat() string {
  134. return "vhd"
  135. }
  136. func (s *SDisk) GetDiskSizeMB() int {
  137. return s.SizeGB * 1024
  138. }
  139. func (s *SDisk) GetIsAutoDelete() bool {
  140. if s.GetDiskType() == api.DISK_TYPE_SYS {
  141. return true
  142. }
  143. return false
  144. }
  145. func (s *SDisk) GetTemplateId() string {
  146. if s.ManualAttr.TemplateId != "" {
  147. return s.ManualAttr.TemplateId
  148. }
  149. return ""
  150. }
  151. func (s *SDisk) GetDiskType() string {
  152. if s.ManualAttr.IsVirtual {
  153. return api.DISK_TYPE_SYS
  154. }
  155. return api.DISK_TYPE_DATA
  156. }
  157. func (s *SDisk) GetFsFormat() string {
  158. return ""
  159. }
  160. func (s *SDisk) GetIsNonPersistent() bool {
  161. return false
  162. }
  163. func (s *SDisk) GetDriver() string {
  164. return "scsi"
  165. }
  166. func (s *SDisk) GetCacheMode() string {
  167. return "none"
  168. }
  169. func (s *SDisk) GetMountpoint() string {
  170. return ""
  171. }
  172. func (s *SDisk) GetAccessPath() string {
  173. return ""
  174. }
  175. func (s *SDisk) Delete(ctx context.Context) error {
  176. if s.storage == nil {
  177. return fmt.Errorf("disk not attached to storage")
  178. }
  179. return s.storage.zone.region.PreDeleteVolume(s.ID)
  180. }
  181. func (s *SDisk) CreateISnapshot(ctx context.Context, name string, desc string) (cloudprovider.ICloudSnapshot, error) {
  182. if s.storage == nil {
  183. return nil, fmt.Errorf("disk not attached to storage")
  184. }
  185. r := s.storage.zone.region
  186. snapshotId, err := r.CreateEbsSnapshot(s.ID, name, desc)
  187. if err != nil {
  188. return nil, err
  189. }
  190. snapshots, err := r.GetSnapshots(snapshotId, s.ID, false)
  191. if err != nil || len(snapshots) == 0 {
  192. return nil, errors.Wrapf(err, "GetSnapshots after create")
  193. }
  194. for i := range snapshots {
  195. if snapshots[i].Id == snapshotId {
  196. return &snapshots[i], nil
  197. }
  198. }
  199. return &snapshots[0], nil
  200. }
  201. func (s *SDisk) GetISnapshot(id string) (cloudprovider.ICloudSnapshot, error) {
  202. parentId, isSystem := s.ID, false
  203. if s.ManualAttr.IsVirtual {
  204. parentId, isSystem = s.ManualAttr.ServerId, true
  205. }
  206. snapshots, err := s.storage.zone.region.GetSnapshots(id, parentId, isSystem)
  207. if err != nil {
  208. return nil, err
  209. }
  210. if len(snapshots) == 0 {
  211. return nil, cloudprovider.ErrNotFound
  212. }
  213. return &snapshots[0], nil
  214. }
  215. func (s *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  216. parentId, isSystem := s.ID, false
  217. if s.ManualAttr.IsVirtual {
  218. parentId, isSystem = s.ManualAttr.ServerId, true
  219. }
  220. snapshots, err := s.storage.zone.region.GetSnapshots("", parentId, isSystem)
  221. if err != nil {
  222. return nil, err
  223. }
  224. isnapshots := make([]cloudprovider.ICloudSnapshot, len(snapshots))
  225. for i := range snapshots {
  226. isnapshots[i] = &snapshots[i]
  227. }
  228. return isnapshots, nil
  229. }
  230. func (s *SDisk) Resize(ctx context.Context, newSizeMB int64) error {
  231. if s.storage == nil {
  232. return fmt.Errorf("disk not attached to storage")
  233. }
  234. newSizeGB := int64(newSizeMB / 1024)
  235. return s.storage.zone.region.ResizeDisk(ctx, s.ID, newSizeGB)
  236. }
  237. func (s *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) {
  238. return "", cloudprovider.ErrNotImplemented
  239. }
  240. func (s *SDisk) Rebuild(ctx context.Context) error {
  241. return cloudprovider.ErrNotImplemented
  242. }
  243. func (s *SRegion) GetDisks() ([]SDisk, error) {
  244. req := NewOpenApiEbsRequest(s.RegionId, "/api/v2/volume/volume/volume/list/with/server", nil, nil)
  245. disks := make([]SDisk, 0, 5)
  246. err := s.client.doList(context.Background(), req.Base(), &disks)
  247. if err != nil {
  248. return nil, err
  249. }
  250. return disks, nil
  251. }
  252. func (s *SRegion) GetDisk(id string) (*SDisk, error) {
  253. req := NewOpenApiEbsRequest(s.RegionId, fmt.Sprintf("/api/v2/volume/volume/volumeDetail/%s", id), nil, nil)
  254. var disk SDisk
  255. err := s.client.doGet(context.Background(), req.Base(), &disk)
  256. if err != nil {
  257. return nil, err
  258. }
  259. return &disk, nil
  260. }