snapshot.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 zstack
  15. import (
  16. "fmt"
  17. "net/url"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. type SSnapshot struct {
  24. multicloud.SResourceBase
  25. ZStackTags
  26. region *SRegion
  27. ZStackBasic
  28. PrimaryStorageUUID string `json:"primaryStorageUuid"`
  29. VolumeUUID string `json:"volumeUuid"`
  30. VolumeType string `json:"volumeType"`
  31. Format string `json:"format"`
  32. Latest bool `json:"latest"`
  33. Size int `json:"size"`
  34. State string `json:"state"`
  35. Status string `json:"status"`
  36. }
  37. func (snapshot *SSnapshot) GetId() string {
  38. return snapshot.UUID
  39. }
  40. func (snapshot *SSnapshot) GetName() string {
  41. return snapshot.Name
  42. }
  43. func (snapshot *SSnapshot) GetStatus() string {
  44. switch snapshot.Status {
  45. case "Ready":
  46. return api.SNAPSHOT_READY
  47. default:
  48. log.Errorf("unknown snapshot %s(%s) status %s", snapshot.Name, snapshot.UUID, snapshot.Status)
  49. return api.SNAPSHOT_UNKNOWN
  50. }
  51. }
  52. func (snapshot *SSnapshot) GetSizeMb() int32 {
  53. return int32(snapshot.Size / 1024 / 1024)
  54. }
  55. func (snapshot *SSnapshot) GetDiskId() string {
  56. return snapshot.VolumeUUID
  57. }
  58. func (snapshot *SSnapshot) GetDiskType() string {
  59. switch snapshot.VolumeType {
  60. case "Root":
  61. return api.DISK_TYPE_SYS
  62. default:
  63. return api.DISK_TYPE_DATA
  64. }
  65. }
  66. func (snapshot *SSnapshot) Refresh() error {
  67. new, err := snapshot.region.GetSnapshot(snapshot.UUID)
  68. if err != nil {
  69. return err
  70. }
  71. return jsonutils.Update(snapshot, new)
  72. }
  73. func (snapshot *SSnapshot) GetGlobalId() string {
  74. return snapshot.UUID
  75. }
  76. func (snapshot *SSnapshot) IsEmulated() bool {
  77. return false
  78. }
  79. func (region *SRegion) GetSnapshot(snapshotId string) (*SSnapshot, error) {
  80. snapshot := &SSnapshot{region: region}
  81. return snapshot, region.client.getResource("volume-snapshots", snapshotId, snapshot)
  82. }
  83. func (region *SRegion) GetSnapshots(snapshotId string, diskId string) ([]SSnapshot, error) {
  84. snapshots := []SSnapshot{}
  85. params := url.Values{}
  86. if len(snapshotId) > 0 {
  87. params.Add("q", "uuid="+snapshotId)
  88. }
  89. if len(diskId) > 0 {
  90. params.Add("q", "volumeUuid="+diskId)
  91. }
  92. if err := region.client.listAll("volume-snapshots", params, &snapshots); err != nil {
  93. return nil, err
  94. }
  95. for i := 0; i < len(snapshots); i++ {
  96. snapshots[i].region = region
  97. }
  98. return snapshots, nil
  99. }
  100. func (snapshot *SSnapshot) Delete() error {
  101. return snapshot.region.DeleteSnapshot(snapshot.UUID)
  102. }
  103. func (region *SRegion) DeleteSnapshot(snapshotId string) error {
  104. return region.client.delete("volume-snapshots", snapshotId, "Enforcing")
  105. }
  106. func (snapshot *SSnapshot) GetProjectId() string {
  107. return ""
  108. }
  109. func (region *SRegion) CreateSnapshot(name, diskId, desc string) (*SSnapshot, error) {
  110. params := map[string]interface{}{
  111. "params": map[string]string{
  112. "name": name,
  113. "description": desc,
  114. },
  115. }
  116. resource := fmt.Sprintf("volumes/%s/volume-snapshots", diskId)
  117. resp, err := region.client.post(resource, jsonutils.Marshal(params))
  118. if err != nil {
  119. return nil, err
  120. }
  121. snapshot := &SSnapshot{region: region}
  122. return snapshot, resp.Unmarshal(snapshot, "inventory")
  123. }