disks.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 ksyun
  15. import (
  16. "context"
  17. "fmt"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/pkg/errors"
  23. )
  24. type Attachment struct {
  25. InstanceID string `json:"InstanceId"`
  26. MountPoint string `json:"MountPoint"`
  27. DeleteWithInstance bool `json:"DeleteWithInstance"`
  28. }
  29. type HistoryAttachment struct {
  30. InstanceID string `json:"InstanceId"`
  31. AttachTime string `json:"AttachTime"`
  32. DetachTime string `json:"DetachTime"`
  33. MountPoint string `json:"MountPoint"`
  34. }
  35. type SDisk struct {
  36. storage *SStorage
  37. multicloud.SDisk
  38. SKsyunTags
  39. VolumeId string `json:"VolumeId"`
  40. VolumeName string `json:"VolumeName"`
  41. VolumeDesc string `json:"VolumeDesc,omitempty"`
  42. Size int `json:"Size"`
  43. VolumeStatus string `json:"VolumeStatus"`
  44. VolumeType string `json:"VolumeType"`
  45. VolumeCategory string `json:"VolumeCategory"`
  46. InstanceId string `json:"InstanceId"`
  47. AvailabilityZone string `json:"AvailabilityZone"`
  48. ChargeType string `json:"ChargeType"`
  49. InstanceTradeType int `json:"InstanceTradeType"`
  50. CreateTime string `json:"CreateTime"`
  51. Attachment []Attachment `json:"Attachment"`
  52. ProjectId string `json:"ProjectId"`
  53. ExpireTime string `json:"ExpireTime,omitempty"`
  54. HistoryAttachment []HistoryAttachment `json:"HistoryAttachment,omitempty"`
  55. DeleteWithInstance bool `json:"DeleteWithInstance"`
  56. }
  57. func (region *SRegion) GetDisk(diskId string) (*SDisk, error) {
  58. disks, err := region.GetDisks([]string{diskId}, "", "")
  59. if err != nil {
  60. return nil, err
  61. }
  62. for i := range disks {
  63. if disks[i].VolumeId == diskId {
  64. return &disks[i], nil
  65. }
  66. }
  67. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "disk %s", diskId)
  68. }
  69. func (region *SRegion) GetDisks(diskIds []string, storageType, zoneId string) ([]SDisk, error) {
  70. disks := []SDisk{}
  71. params := map[string]interface{}{
  72. "MaxResults": "1000",
  73. }
  74. for i, v := range diskIds {
  75. params[fmt.Sprintf("VolumeId.%d", i+1)] = v
  76. }
  77. if len(storageType) > 0 {
  78. params["VolumeType"] = storageType
  79. }
  80. for {
  81. resp, err := region.ebsRequest("DescribeVolumes", params)
  82. if err != nil {
  83. return nil, errors.Wrap(err, "list instance")
  84. }
  85. part := struct {
  86. RequestID string `json:"RequestId"`
  87. Volumes []SDisk `json:"Volumes"`
  88. TotalCount int `json:"TotalCount"`
  89. Marker int `json:"Marker"`
  90. }{}
  91. err = resp.Unmarshal(&part)
  92. if err != nil {
  93. return nil, errors.Wrap(err, "unmarshal instances")
  94. }
  95. disks = append(disks, part.Volumes...)
  96. if len(disks) >= part.TotalCount {
  97. break
  98. }
  99. params["Marker"] = fmt.Sprintf("%d", part.Marker)
  100. }
  101. if len(zoneId) > 0 {
  102. res := []SDisk{}
  103. for _, disk := range disks {
  104. if disk.AvailabilityZone == zoneId {
  105. res = append(res, disk)
  106. }
  107. }
  108. return res, nil
  109. }
  110. return disks, nil
  111. }
  112. func (region *SRegion) GetDiskByInstanceId(instanceId string) ([]SDisk, error) {
  113. params := map[string]interface{}{
  114. "MaxResults": "1000",
  115. }
  116. params["InstanceId"] = instanceId
  117. resp, err := region.ebsRequest("DescribeInstanceVolumes", params)
  118. if err != nil {
  119. return nil, errors.Wrap(err, "DescribeInstanceVolumes")
  120. }
  121. ret := struct {
  122. Attachments []SDisk `json:"Attachments"`
  123. }{}
  124. err = resp.Unmarshal(&ret)
  125. if err != nil {
  126. return nil, errors.Wrap(err, "unmarshal Attachments")
  127. }
  128. return ret.Attachments, nil
  129. }
  130. func (disk *SDisk) GetIStorage() (cloudprovider.ICloudStorage, error) {
  131. if disk.storage == nil {
  132. return nil, fmt.Errorf("disk %s(%s) missing storage", disk.VolumeName, disk.VolumeId)
  133. }
  134. return disk.storage, nil
  135. }
  136. func (disk *SDisk) GetIStorageId() string {
  137. if disk.storage == nil {
  138. return ""
  139. }
  140. return disk.storage.GetGlobalId()
  141. }
  142. func (disk *SDisk) GetDiskFormat() string {
  143. return ""
  144. }
  145. func (disk *SDisk) GetId() string {
  146. return disk.VolumeId
  147. }
  148. func (disk *SDisk) GetTags() (map[string]string, error) {
  149. tags, err := disk.storage.zone.region.ListTags("volume", disk.VolumeId)
  150. if err != nil {
  151. return nil, err
  152. }
  153. return tags.GetTags(), nil
  154. }
  155. func (disk *SDisk) GetGlobalId() string {
  156. return disk.VolumeId
  157. }
  158. func (disk *SDisk) GetName() string {
  159. return disk.VolumeName
  160. }
  161. func (disk *SDisk) GetProjectId() string {
  162. return disk.ProjectId
  163. }
  164. func (disk *SDisk) Refresh() error {
  165. if disk.VolumeType == api.STORAGE_KSYUN_LOCAL_SSD {
  166. return nil
  167. }
  168. disk, err := disk.storage.zone.region.GetDisk(disk.VolumeId)
  169. if err != nil {
  170. return err
  171. }
  172. return jsonutils.Update(disk, disk)
  173. }
  174. func (disk *SDisk) GetStatus() string {
  175. if disk.VolumeType == api.STORAGE_KSYUN_LOCAL_SSD {
  176. return api.DISK_READY
  177. }
  178. // creating、available、attaching、inuse、detaching、extending、deleting、error
  179. switch disk.VolumeStatus {
  180. case "available", "inuse", "in-use":
  181. return api.DISK_READY
  182. case "detaching":
  183. return api.DISK_DETACHING
  184. case "error":
  185. return api.DISK_UNKNOWN
  186. case "creating":
  187. return api.DISK_ALLOCATING
  188. default:
  189. return disk.VolumeStatus
  190. }
  191. }
  192. func (disk *SDisk) GetDiskSizeMB() int {
  193. return disk.Size * 1024
  194. }
  195. func (disk *SDisk) GetIsAutoDelete() bool {
  196. return disk.DeleteWithInstance
  197. }
  198. func (disk *SDisk) GetTemplateId() string {
  199. return ""
  200. }
  201. func (disk *SDisk) GetDiskType() string {
  202. if disk.VolumeCategory == "system" {
  203. return api.DISK_TYPE_SYS
  204. }
  205. return api.DISK_TYPE_DATA
  206. }
  207. func (disk *SDisk) GetFsFormat() string {
  208. return ""
  209. }
  210. func (disk *SDisk) GetIsNonPersistent() bool {
  211. return false
  212. }
  213. func (disk *SDisk) GetIops() int {
  214. return 0
  215. }
  216. func (disk *SDisk) GetDriver() string {
  217. return ""
  218. }
  219. func (disk *SDisk) GetCacheMode() string {
  220. return ""
  221. }
  222. func (disk *SDisk) GetMountpoint() string {
  223. return ""
  224. }
  225. func (disk *SDisk) GetAccessPath() string {
  226. return ""
  227. }
  228. func (disk *SDisk) Delete(ctx context.Context) error {
  229. return disk.storage.zone.region.DeleteDisk(disk.VolumeId)
  230. }
  231. func (disk *SDisk) CreateISnapshot(ctx context.Context, name string, desc string) (cloudprovider.ICloudSnapshot, error) {
  232. ret, err := disk.storage.zone.region.CreateSnapshot(disk.VolumeId, name, desc)
  233. if err != nil {
  234. return nil, err
  235. }
  236. return ret, nil
  237. }
  238. func (disk *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  239. snapshots, err := disk.storage.zone.region.GetSnapshots("", disk.VolumeId)
  240. if err != nil {
  241. return nil, err
  242. }
  243. ret := []cloudprovider.ICloudSnapshot{}
  244. for i := range snapshots {
  245. snapshots[i].region = disk.storage.zone.region
  246. ret = append(ret, &snapshots[i])
  247. }
  248. return ret, nil
  249. }
  250. func (disk *SDisk) GetExtSnapshotPolicyIds() ([]string, error) {
  251. return nil, cloudprovider.ErrNotSupported
  252. }
  253. func (disk *SDisk) Resize(ctx context.Context, newSizeMB int64) error {
  254. return disk.storage.zone.region.ResizeDisk(disk.VolumeId, newSizeMB)
  255. }
  256. func (disk *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) {
  257. return disk.VolumeId, disk.storage.zone.region.ResetDisk(disk.VolumeId, snapshotId)
  258. }
  259. func (disk *SDisk) Rebuild(ctx context.Context) error {
  260. return cloudprovider.ErrNotSupported
  261. }
  262. func (region *SRegion) DeleteDisk(id string) error {
  263. _, err := region.ebsRequest("DeleteVolume", map[string]interface{}{
  264. "VolumeId": id,
  265. "ForceDelete": "true",
  266. })
  267. return err
  268. }
  269. func (region *SRegion) ResizeDisk(id string, newSizeMB int64) error {
  270. _, err := region.ebsRequest("ResizeVolume", map[string]interface{}{
  271. "VolumeId": id,
  272. "Size": fmt.Sprintf("%d", newSizeMB/1024),
  273. })
  274. return err
  275. }
  276. func (region *SRegion) ResetDisk(id string, snapshotId string) error {
  277. _, err := region.ebsRequest("ResetVolume", map[string]interface{}{
  278. "VolumeId": id,
  279. "SnapshotId": snapshotId,
  280. })
  281. return err
  282. }
  283. func (region *SRegion) CreateDisk(storageType, zoneId string, opts *cloudprovider.DiskCreateConfig) (*SDisk, error) {
  284. params := map[string]interface{}{
  285. "VolumeName": opts.Name,
  286. "VolumeDesc": opts.Desc,
  287. "Size": fmt.Sprintf("%d", opts.SizeGb),
  288. "VolumeType": storageType,
  289. "AvailabilityZone": zoneId,
  290. "ChargeType": "HourlyInstantSettlement",
  291. }
  292. if len(opts.ProjectId) > 0 {
  293. params["ProjectId"] = opts.ProjectId
  294. }
  295. if len(opts.SnapshotId) > 0 {
  296. params["SnapshotId"] = opts.SnapshotId
  297. }
  298. resp, err := region.ebsRequest("CreateVolume", params)
  299. if err != nil {
  300. return nil, err
  301. }
  302. diskId, err := resp.GetString("VolumeId")
  303. if err != nil {
  304. return nil, err
  305. }
  306. return region.GetDisk(diskId)
  307. }