snapshot.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 cloudpods
  15. import (
  16. "yunion.io/x/cloudmux/pkg/cloudprovider"
  17. "yunion.io/x/cloudmux/pkg/multicloud"
  18. "yunion.io/x/jsonutils"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  21. )
  22. type SSnapshot struct {
  23. multicloud.SVirtualResourceBase
  24. CloudpodsTags
  25. region *SRegion
  26. api.SnapshotDetails
  27. }
  28. func (self *SSnapshot) GetName() string {
  29. return self.Name
  30. }
  31. func (self *SSnapshot) GetId() string {
  32. return self.Id
  33. }
  34. func (self *SSnapshot) GetGlobalId() string {
  35. return self.Id
  36. }
  37. func (self *SSnapshot) GetStatus() string {
  38. return self.Status
  39. }
  40. func (self *SSnapshot) GetProjectId() string {
  41. return self.TenantId
  42. }
  43. func (self *SSnapshot) GetSizeMb() int32 {
  44. return int32(self.Size)
  45. }
  46. func (self *SSnapshot) GetDiskId() string {
  47. return self.DiskId
  48. }
  49. func (self *SSnapshot) GetDiskType() string {
  50. return self.DiskType
  51. }
  52. func (self *SSnapshot) Delete() error {
  53. return self.region.cli.delete(&modules.Snapshots, self.Id)
  54. }
  55. func (self *SSnapshot) Refresh() error {
  56. snapshot, err := self.region.GetSnapshot(self.Id)
  57. if err != nil {
  58. return err
  59. }
  60. return jsonutils.Update(self, snapshot)
  61. }
  62. func (self *SRegion) GetSnapshots(diskId string) ([]SSnapshot, error) {
  63. ret := []SSnapshot{}
  64. params := map[string]interface{}{}
  65. if len(diskId) > 0 {
  66. params["disk_id"] = diskId
  67. }
  68. return ret, self.list(&modules.Snapshots, params, &ret)
  69. }
  70. func (self *SRegion) GetSnapshot(id string) (*SSnapshot, error) {
  71. snapshot := &SSnapshot{region: self}
  72. return snapshot, self.cli.get(&modules.Snapshots, id, nil, snapshot)
  73. }
  74. func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  75. snapshots, err := self.GetSnapshots("")
  76. if err != nil {
  77. return nil, err
  78. }
  79. ret := []cloudprovider.ICloudSnapshot{}
  80. for i := range snapshots {
  81. snapshots[i].region = self
  82. ret = append(ret, &snapshots[i])
  83. }
  84. return ret, nil
  85. }
  86. func (self *SRegion) GetISnapshotById(id string) (cloudprovider.ICloudSnapshot, error) {
  87. snapshot, err := self.GetSnapshot(id)
  88. if err != nil {
  89. return nil, err
  90. }
  91. return snapshot, nil
  92. }