snapshot.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 google
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. )
  21. type SSnapshot struct {
  22. region *SRegion
  23. SResourceBase
  24. GoogleTags
  25. CreationTimestamp time.Time
  26. Status string
  27. SourceDisk string
  28. SourceDiskId string
  29. DiskSizeGb int32
  30. StorageBytes int
  31. StorageBytesStatus string
  32. Licenses []string
  33. LabelFingerprint string
  34. LicenseCodes []string
  35. StorageLocations []string
  36. Kind string
  37. }
  38. func (region *SRegion) GetSnapshots(disk string, maxResults int, pageToken string) ([]SSnapshot, error) {
  39. snapshots := []SSnapshot{}
  40. params := map[string]string{}
  41. if len(disk) > 0 {
  42. params["filter"] = fmt.Sprintf(`sourceDisk="%s"`, disk)
  43. }
  44. resource := "global/snapshots"
  45. return snapshots, region.List(resource, params, maxResults, pageToken, &snapshots)
  46. }
  47. func (region *SRegion) GetSnapshot(id string) (*SSnapshot, error) {
  48. snapshot := &SSnapshot{region: region}
  49. return snapshot, region.Get("global/snapshots", id, snapshot)
  50. }
  51. // CREATING, DELETING, FAILED, READY, or UPLOADING
  52. func (snapshot *SSnapshot) GetStatus() string {
  53. switch snapshot.Status {
  54. case "CREATING":
  55. return api.SNAPSHOT_CREATING
  56. case "DELETING":
  57. return api.SNAPSHOT_DELETING
  58. case "FAILED":
  59. return api.SNAPSHOT_UNKNOWN
  60. case "READY", "UPLOADING":
  61. return api.SNAPSHOT_READY
  62. default:
  63. return api.SNAPSHOT_UNKNOWN
  64. }
  65. }
  66. func (snapshot *SSnapshot) IsEmulated() bool {
  67. return false
  68. }
  69. func (self *SSnapshot) GetCreatedAt() time.Time {
  70. return self.CreationTimestamp
  71. }
  72. func (snapshot *SSnapshot) Refresh() error {
  73. _snapshot, err := snapshot.region.GetSnapshot(snapshot.Id)
  74. if err != nil {
  75. return err
  76. }
  77. return jsonutils.Update(snapshot, _snapshot)
  78. }
  79. func (snapshot *SSnapshot) GetSizeMb() int32 {
  80. return snapshot.DiskSizeGb * 1024
  81. }
  82. func (snapshot *SSnapshot) GetDiskId() string {
  83. return snapshot.SourceDisk
  84. }
  85. func (snapshot *SSnapshot) GetDiskType() string {
  86. if len(snapshot.Licenses) > 0 {
  87. return api.DISK_TYPE_SYS
  88. }
  89. return api.DISK_TYPE_DATA
  90. }
  91. func (snapshot *SSnapshot) Delete() error {
  92. return snapshot.region.Delete(snapshot.SelfLink)
  93. }
  94. func (snapshot *SSnapshot) GetProjectId() string {
  95. return snapshot.region.GetProjectId()
  96. }