snapshot.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 aws
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type SSnapshot struct {
  25. multicloud.SResourceBase
  26. AwsTags
  27. region *SRegion
  28. DataEncryptionKeyId string `xml:"dataEncryptionKeyId"`
  29. Description string `xml:"description"`
  30. Encrypted bool `xml:"encrypted"`
  31. KmsKeyId string `xml:"kmsKeyId"`
  32. OutpostArn string `xml:"outpostArn"`
  33. OwnerAlias string `xml:"ownerAlias"`
  34. OwnerId string `xml:"ownerId"`
  35. Progress string `xml:"progress"`
  36. SnapshotId string `xml:"snapshotId"`
  37. StartTime time.Time `xml:"startTime"`
  38. State string `xml:"status"`
  39. StateMessage string `xml:"statusMessage"`
  40. VolumeId string `xml:"volumeId"`
  41. VolumeSize int64 `xml:"volumeSize"`
  42. }
  43. func (self *SSnapshot) GetDiskType() string {
  44. return ""
  45. }
  46. func (self *SSnapshot) GetId() string {
  47. return self.SnapshotId
  48. }
  49. func (self *SSnapshot) GetName() string {
  50. name := self.AwsTags.GetName()
  51. if len(name) > 0 {
  52. return name
  53. }
  54. return self.SnapshotId
  55. }
  56. func (self *SSnapshot) GetGlobalId() string {
  57. return self.SnapshotId
  58. }
  59. func (self *SSnapshot) GetStatus() string {
  60. // pending | completed | error | recoverable | recovering
  61. switch self.State {
  62. case "pending":
  63. return api.SNAPSHOT_CREATING
  64. case "completed":
  65. return api.SNAPSHOT_READY
  66. case "error":
  67. return api.SNAPSHOT_UNKNOWN
  68. case "recoverable", "recovering":
  69. return api.SNAPSHOT_ROLLBACKING
  70. default:
  71. return api.SNAPSHOT_UNKNOWN
  72. }
  73. }
  74. func (self *SSnapshot) Refresh() error {
  75. snapshot, err := self.region.GetSnapshot(self.SnapshotId)
  76. if err != nil {
  77. return err
  78. }
  79. return jsonutils.Update(self, snapshot)
  80. }
  81. func (self *SSnapshot) GetSizeMb() int32 {
  82. return int32(self.VolumeSize) * 1024
  83. }
  84. func (self *SSnapshot) GetDiskId() string {
  85. return self.VolumeId
  86. }
  87. func (self *SSnapshot) Delete() error {
  88. return self.region.DeleteSnapshot(self.SnapshotId)
  89. }
  90. func (self *SRegion) GetSnapshot(id string) (*SSnapshot, error) {
  91. snapshots, err := self.GetSnapshots("", "", []string{id})
  92. if err != nil {
  93. return nil, err
  94. }
  95. for i := range snapshots {
  96. if snapshots[i].GetGlobalId() == id {
  97. return &snapshots[i], nil
  98. }
  99. }
  100. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  101. }
  102. func (self *SRegion) GetSnapshots(diskId string, name string, ids []string) ([]SSnapshot, error) {
  103. params := map[string]string{
  104. "Owner.1": "self",
  105. }
  106. idx := 1
  107. if len(diskId) > 0 {
  108. params[fmt.Sprintf("Filter.%d.Name", idx)] = "volume-id"
  109. params[fmt.Sprintf("Filter.%d.Value.1", idx)] = diskId
  110. idx++
  111. }
  112. if len(name) > 0 {
  113. params[fmt.Sprintf("Filter.%d.Name", idx)] = "tag:Name"
  114. params[fmt.Sprintf("Filter.%d.Value.1", idx)] = name
  115. idx++
  116. }
  117. for i, id := range ids {
  118. params[fmt.Sprintf("SnapshotId.%d", i+1)] = id
  119. }
  120. ret := []SSnapshot{}
  121. for {
  122. part := struct {
  123. SnapshotSet []SSnapshot `xml:"snapshotSet>item"`
  124. NextToken string `xml:"nextToken"`
  125. }{}
  126. err := self.ec2Request("DescribeSnapshots", params, &part)
  127. if err != nil {
  128. return nil, err
  129. }
  130. ret = append(ret, part.SnapshotSet...)
  131. if len(part.NextToken) == 0 || len(part.SnapshotSet) == 0 {
  132. break
  133. }
  134. params["NextToken"] = part.NextToken
  135. }
  136. return ret, nil
  137. }
  138. func (self *SRegion) GetISnapshotById(id string) (cloudprovider.ICloudSnapshot, error) {
  139. snapshot, err := self.GetSnapshot(id)
  140. if err != nil {
  141. return nil, err
  142. }
  143. snapshot.region = self
  144. return snapshot, nil
  145. }
  146. func (self *SRegion) CreateSnapshot(diskId, name, desc string) (*SSnapshot, error) {
  147. params := map[string]string{
  148. "VolumeId": diskId,
  149. }
  150. tagIdx := 1
  151. if len(name) > 0 {
  152. params[fmt.Sprintf("TagSpecification.%d.ResourceType", tagIdx)] = "snapshot"
  153. params[fmt.Sprintf("TagSpecification.%d.Tag.1.Key", tagIdx)] = "Name"
  154. params[fmt.Sprintf("TagSpecification.%d.Tag.1.Value", tagIdx)] = name
  155. tagIdx++
  156. }
  157. if len(desc) > 0 {
  158. params["Description"] = desc
  159. }
  160. ret := &SSnapshot{region: self}
  161. return ret, self.ec2Request("CreateSnapshot", params, ret)
  162. }
  163. func (self *SRegion) DeleteSnapshot(id string) error {
  164. params := map[string]string{
  165. "SnapshotId": id,
  166. }
  167. ret := struct{}{}
  168. return self.ec2Request("DeleteSnapshot", params, &ret)
  169. }
  170. func (self *SSnapshot) GetProjectId() string {
  171. return ""
  172. }
  173. func (self *SSnapshot) GetDescription() string {
  174. return self.AwsTags.GetDescription()
  175. }