disk.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 jdcloud
  15. import (
  16. "context"
  17. "fmt"
  18. "time"
  19. commodels "github.com/jdcloud-api/jdcloud-sdk-go/services/common/models"
  20. "github.com/jdcloud-api/jdcloud-sdk-go/services/disk/apis"
  21. "github.com/jdcloud-api/jdcloud-sdk-go/services/disk/client"
  22. "github.com/jdcloud-api/jdcloud-sdk-go/services/disk/models"
  23. api "yunion.io/x/cloudmux/pkg/apis/compute"
  24. "yunion.io/x/cloudmux/pkg/cloudprovider"
  25. "yunion.io/x/cloudmux/pkg/multicloud"
  26. )
  27. type SDisk struct {
  28. storage *SStorage
  29. multicloud.SDisk
  30. JdcloudTags
  31. multicloud.SBillingBase
  32. models.Disk
  33. ImageId string
  34. IsSystemDisk bool
  35. }
  36. func (d *SDisk) GetBillingType() string {
  37. return billingType(&d.Charge)
  38. }
  39. func (d *SDisk) GetExpiredAt() time.Time {
  40. return expireAt(&d.Charge)
  41. }
  42. func (d *SDisk) GetId() string {
  43. return d.DiskId
  44. }
  45. func (d *SDisk) GetName() string {
  46. return d.Name
  47. }
  48. func (d *SDisk) GetGlobalId() string {
  49. return d.GetId()
  50. }
  51. func (d *SDisk) GetIops() int {
  52. return d.Iops
  53. }
  54. func (d *SDisk) GetStatus() string {
  55. switch d.Status {
  56. case "available", "in-use":
  57. return api.DISK_READY
  58. case "creating":
  59. return api.DISK_ALLOCATING
  60. case "extending":
  61. return api.DISK_RESIZING
  62. case "restoring":
  63. return api.DISK_RESET
  64. case "deleting":
  65. return api.DISK_DEALLOC
  66. case "error_create":
  67. return api.DISK_ALLOC_FAILED
  68. case "error_delete":
  69. return api.DISK_DEALLOC_FAILED
  70. case "error_restore":
  71. return api.DISK_RESET_FAILED
  72. case "error_extend":
  73. return api.DISK_RESIZE_FAILED
  74. default:
  75. return api.DISK_UNKNOWN
  76. }
  77. }
  78. func (d *SDisk) Refresh() error {
  79. return nil
  80. }
  81. func (d *SDisk) IsEmulated() bool {
  82. return false
  83. }
  84. func (d *SDisk) GetSysTags() map[string]string {
  85. return map[string]string{
  86. "hypervisor": api.HYPERVISOR_JDCLOUD,
  87. }
  88. }
  89. func (d *SDisk) GetProjectId() string {
  90. return ""
  91. }
  92. func (d *SDisk) GetIStorage() (cloudprovider.ICloudStorage, error) {
  93. return d.storage, nil
  94. }
  95. func (d *SDisk) GetDiskFormat() string {
  96. return "vhd"
  97. }
  98. func (d *SDisk) GetDiskSizeMB() int {
  99. return d.DiskSizeGB * 1024
  100. }
  101. func (d *SDisk) GetIsAutoDelete() bool {
  102. if len(d.Attachments) == 0 {
  103. return false
  104. }
  105. return true
  106. }
  107. func (d *SDisk) GetTemplateId() string {
  108. // TODO 通过快照创建的盘应该如何
  109. return d.ImageId
  110. }
  111. func (d *SDisk) GetDiskType() string {
  112. if d.IsSystemDisk {
  113. return api.DISK_TYPE_SYS
  114. }
  115. return api.DISK_TYPE_DATA
  116. }
  117. func (d *SDisk) GetFsFormat() string {
  118. return ""
  119. }
  120. func (d *SDisk) GetIsNonPersistent() bool {
  121. return false
  122. }
  123. func (d *SDisk) GetDriver() string {
  124. return "scsi"
  125. }
  126. func (d *SDisk) GetCacheMode() string {
  127. return "none"
  128. }
  129. func (d *SDisk) GetMountpoint() string {
  130. return ""
  131. }
  132. func (d *SDisk) GetAccessPath() string {
  133. return ""
  134. }
  135. func (d *SDisk) Delete(ctx context.Context) error {
  136. return cloudprovider.ErrNotImplemented
  137. }
  138. func (d *SDisk) CreateISnapshot(ctx context.Context, name string, desc string) (cloudprovider.ICloudSnapshot, error) {
  139. return nil, cloudprovider.ErrNotImplemented
  140. }
  141. func (d *SDisk) GetISnapshot(id string) (cloudprovider.ICloudSnapshot, error) {
  142. snapshot, err := d.storage.zone.region.GetSnapshotById(id)
  143. if err != nil {
  144. return nil, err
  145. }
  146. snapshot.disk = d
  147. return snapshot, nil
  148. }
  149. func (s *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  150. snapshots := make([]SSnapshot, 0)
  151. n := 1
  152. for {
  153. parts, total, err := s.storage.zone.region.GetSnapshots(s.DiskId, n, 100)
  154. if err != nil {
  155. return nil, err
  156. }
  157. snapshots = append(snapshots, parts...)
  158. if len(snapshots) >= total {
  159. break
  160. }
  161. n++
  162. }
  163. isnapshots := make([]cloudprovider.ICloudSnapshot, len(snapshots))
  164. for i := range snapshots {
  165. snapshots[i].disk = s
  166. isnapshots[i] = &snapshots[i]
  167. }
  168. return isnapshots, nil
  169. }
  170. func (s *SDisk) Resize(ctx context.Context, newSizeMB int64) error {
  171. return cloudprovider.ErrNotImplemented
  172. }
  173. func (s *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) {
  174. return "", cloudprovider.ErrNotImplemented
  175. }
  176. func (s *SDisk) Rebuild(ctx context.Context) error {
  177. return cloudprovider.ErrNotImplemented
  178. }
  179. func (r *SRegion) GetDisks(instanceId, zoneId, diskType string, diskIds []string, pageNumber, pageSize int) ([]SDisk, int, error) {
  180. filters := []commodels.Filter{}
  181. if instanceId != "" {
  182. filters = append(filters, commodels.Filter{
  183. Name: "instanceId",
  184. Values: []string{instanceId},
  185. }, commodels.Filter{
  186. Name: "instanceType",
  187. Values: []string{"vm"},
  188. })
  189. }
  190. if zoneId != "" {
  191. filters = append(filters, commodels.Filter{
  192. Name: "az",
  193. Values: []string{zoneId},
  194. })
  195. }
  196. if len(diskIds) > 0 {
  197. filters = append(filters, commodels.Filter{
  198. Name: "diskId",
  199. Values: diskIds,
  200. })
  201. }
  202. if diskType != "" {
  203. filters = append(filters, commodels.Filter{
  204. Name: "diskType",
  205. Values: []string{diskType},
  206. })
  207. }
  208. req := apis.NewDescribeDisksRequestWithAllParams(r.ID, &pageNumber, &pageSize, nil, filters)
  209. client := client.NewDiskClient(r.getCredential())
  210. client.Logger = Logger{debug: r.client.debug}
  211. resp, err := client.DescribeDisks(req)
  212. if err != nil {
  213. return nil, 0, err
  214. }
  215. if resp.Error.Code >= 400 {
  216. return nil, 0, fmt.Errorf("%s", resp.Error.Message)
  217. }
  218. total := resp.Result.TotalCount
  219. disks := make([]SDisk, 0, len(resp.Result.Disks))
  220. for i := range resp.Result.Disks {
  221. disks = append(disks, SDisk{
  222. Disk: resp.Result.Disks[i],
  223. })
  224. }
  225. return disks, total, nil
  226. }
  227. func (r *SRegion) GetDiskById(id string) (*SDisk, error) {
  228. req := apis.NewDescribeDiskRequest(r.ID, id)
  229. client := client.NewDiskClient(r.getCredential())
  230. client.Logger = Logger{debug: r.client.debug}
  231. resp, err := client.DescribeDisk(req)
  232. if err != nil {
  233. return nil, err
  234. }
  235. if resp.Error.Code >= 400 {
  236. return nil, fmt.Errorf("%s", resp.Error.Message)
  237. }
  238. return &SDisk{
  239. Disk: resp.Result.Disk,
  240. }, nil
  241. }