snapshot.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "fmt"
  17. commodels "github.com/jdcloud-api/jdcloud-sdk-go/services/common/models"
  18. "github.com/jdcloud-api/jdcloud-sdk-go/services/disk/apis"
  19. "github.com/jdcloud-api/jdcloud-sdk-go/services/disk/client"
  20. "github.com/jdcloud-api/jdcloud-sdk-go/services/disk/models"
  21. "yunion.io/x/log"
  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 SSnapshot struct {
  27. multicloud.SResourceBase
  28. JdcloudTags
  29. region *SRegion
  30. models.Snapshot
  31. disk *SDisk
  32. }
  33. func (s *SSnapshot) GetId() string {
  34. return s.SnapshotId
  35. }
  36. func (s *SSnapshot) GetName() string {
  37. return s.Name
  38. }
  39. func (s *SSnapshot) GetStatus() string {
  40. switch s.Status {
  41. case "creating":
  42. return api.SNAPSHOT_CREATING
  43. case "available", "in-use":
  44. return api.SNAPSHOT_READY
  45. case "deleting":
  46. return api.SNAPSHOT_DELETING
  47. case "error_create":
  48. return api.SNAPSHOT_FAILED
  49. case "error_delete":
  50. return api.SNAPSHOT_DELETE_FAILED
  51. default:
  52. return api.SNAPSHOT_UNKNOWN
  53. }
  54. }
  55. func (s *SSnapshot) GetSizeMb() int32 {
  56. return int32(s.SnapshotSizeGB) * 1024
  57. }
  58. func (s *SSnapshot) GetDiskId() string {
  59. return s.DiskId
  60. }
  61. func (s *SSnapshot) getDisk() (*SDisk, error) {
  62. if s.disk != nil {
  63. return s.disk, nil
  64. }
  65. disk, err := s.region.GetDiskById(s.DiskId)
  66. if err != nil {
  67. return disk, err
  68. }
  69. s.disk = disk
  70. return s.disk, nil
  71. }
  72. func (s *SSnapshot) GetDiskType() string {
  73. disk, err := s.getDisk()
  74. if err != nil {
  75. log.Errorf("unable to get disk %s", s.DiskId)
  76. return ""
  77. }
  78. return disk.GetDiskType()
  79. }
  80. func (s *SSnapshot) Refresh() error {
  81. return nil
  82. }
  83. func (s *SSnapshot) GetGlobalId() string {
  84. return s.GetId()
  85. }
  86. func (s *SSnapshot) IsEmulated() bool {
  87. return false
  88. }
  89. func (s *SSnapshot) Delete() error {
  90. return cloudprovider.ErrNotImplemented
  91. }
  92. func (s *SSnapshot) GetProjectId() string {
  93. return ""
  94. }
  95. func (r *SRegion) GetSnapshots(diskId string, pageNumber, pageSize int) ([]SSnapshot, int, error) {
  96. filters := []commodels.Filter{}
  97. if diskId != "" {
  98. filters = append(filters, commodels.Filter{
  99. Name: "diskId",
  100. Values: []string{diskId},
  101. })
  102. }
  103. req := apis.NewDescribeSnapshotsRequestWithAllParams(r.ID, &pageNumber, &pageSize, nil, filters)
  104. client := client.NewDiskClient(r.getCredential())
  105. client.Logger = Logger{debug: r.client.debug}
  106. resp, err := client.DescribeSnapshots(req)
  107. if err != nil {
  108. return nil, 0, err
  109. }
  110. if resp.Error.Code >= 400 {
  111. return nil, 0, fmt.Errorf("%s", resp.Error.Message)
  112. }
  113. snapshots := make([]SSnapshot, len(resp.Result.Snapshots))
  114. for i := range resp.Result.Snapshots {
  115. snapshots = append(snapshots, SSnapshot{
  116. region: r,
  117. Snapshot: resp.Result.Snapshots[i],
  118. })
  119. }
  120. return snapshots, resp.Result.TotalCount, nil
  121. }
  122. func (r *SRegion) GetSnapshotById(id string) (*SSnapshot, error) {
  123. req := apis.NewDescribeSnapshotRequest(r.ID, id)
  124. client := client.NewDiskClient(r.getCredential())
  125. client.Logger = Logger{debug: r.client.debug}
  126. resp, err := client.DescribeSnapshot(req)
  127. if err != nil {
  128. return nil, err
  129. }
  130. if resp.Error.Code >= 400 {
  131. return nil, fmt.Errorf("%s", resp.Error.Message)
  132. }
  133. snapshot := SSnapshot{
  134. region: r,
  135. Snapshot: resp.Result.Snapshot,
  136. }
  137. return &snapshot, nil
  138. }