snapshot.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 ecloud
  15. import (
  16. "strings"
  17. "time"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. )
  22. type SSnapshot struct {
  23. multicloud.SResourceBase
  24. EcloudTags
  25. region *SRegion
  26. SCreateTime
  27. BackupType string `json:"backupType,omitempty"`
  28. CreateBy string `json:"createBy,omitempty"`
  29. Description string `json:"description,omitempty"`
  30. EcType string `json:"ecType,omitempty"`
  31. Id string `json:"id"`
  32. Name string `json:"name,omitempty"`
  33. Size int `json:"size"`
  34. VolumeId string `json:"volumeId,omitempty"`
  35. VolumeType string `json:"volumeType,omitempty"`
  36. Status string `json:"status,omitempty"`
  37. IsSystem bool `json:"isSystem,omitempty"`
  38. SystemDisk string `json:"systemDisk,omitempty"`
  39. // EBS 快照列表返回 createTime
  40. CreateTime string `json:"createTime,omitempty"`
  41. }
  42. func (s *SSnapshot) GetId() string {
  43. return s.Id
  44. }
  45. func (s *SSnapshot) GetName() string {
  46. return s.Name
  47. }
  48. func (s *SSnapshot) GetStatus() string {
  49. switch strings.ToLower(s.Status) {
  50. case "available", "in_use", "active":
  51. return api.SNAPSHOT_READY
  52. case "creating", "attaching", "backing_up", "saving", "queued":
  53. return api.SNAPSHOT_CREATING
  54. case "deleting":
  55. return api.SNAPSHOT_DELETING
  56. case "error_deleting":
  57. return api.SNAPSHOT_DELETE_FAILED
  58. case "error":
  59. return api.SNAPSHOT_FAILED
  60. default:
  61. return api.SNAPSHOT_UNKNOWN
  62. }
  63. }
  64. func (s *SSnapshot) GetSizeMb() int32 {
  65. return int32(s.Size)
  66. }
  67. func (s *SSnapshot) GetDiskId() string {
  68. if s.IsSystem {
  69. return s.SystemDisk
  70. }
  71. return s.VolumeId
  72. }
  73. func (s *SSnapshot) GetCreatedAt() time.Time {
  74. if s.CreateTime != "" {
  75. if t, err := time.Parse("2006-01-02 15:04:05", s.CreateTime); err == nil {
  76. return t
  77. }
  78. if t, err := time.Parse(time.RFC3339, s.CreateTime); err == nil {
  79. return t
  80. }
  81. }
  82. return s.SCreateTime.GetCreatedAt()
  83. }
  84. func (s *SSnapshot) GetDiskType() string {
  85. if s.IsSystem {
  86. return api.DISK_TYPE_SYS
  87. }
  88. return api.DISK_TYPE_DATA
  89. }
  90. func (s *SSnapshot) GetGlobalId() string {
  91. return s.Id
  92. }
  93. func (s *SSnapshot) Delete() error {
  94. if s.region == nil {
  95. return cloudprovider.ErrNotImplemented
  96. }
  97. return s.region.DeleteEbsSnapshot(s.Id)
  98. }
  99. func (s *SSnapshot) GetProjectId() string {
  100. return ""
  101. }
  102. func (s *SRegion) GetSnapshots(snapshotId string, parentId string, isSystem bool) ([]SSnapshot, error) {
  103. return nil, cloudprovider.ErrNotImplemented
  104. }