snapshot.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 azure
  15. import (
  16. "net/url"
  17. "strings"
  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/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type SnapshotSku struct {
  25. Name string
  26. Tier string
  27. }
  28. type SSnapshot struct {
  29. multicloud.SResourceBase
  30. AzureTags
  31. region *SRegion
  32. ID string
  33. Name string
  34. Location string
  35. ManagedBy string
  36. Sku *SnapshotSku
  37. Properties DiskProperties
  38. Type string
  39. }
  40. func (self *SSnapshot) GetId() string {
  41. return self.ID
  42. }
  43. func (self *SSnapshot) GetGlobalId() string {
  44. return strings.ToLower(self.ID)
  45. }
  46. func (self *SSnapshot) GetName() string {
  47. return self.Name
  48. }
  49. func (self *SSnapshot) GetStatus() string {
  50. switch self.Properties.ProvisioningState {
  51. case "Succeeded":
  52. return api.SNAPSHOT_READY
  53. default:
  54. log.Errorf("Unknow azure snapshot %s status: %s", self.ID, self.Properties.ProvisioningState)
  55. return api.SNAPSHOT_UNKNOWN
  56. }
  57. }
  58. func (self *SRegion) CreateSnapshot(diskId, name, desc string) (*SSnapshot, error) {
  59. params := map[string]interface{}{
  60. "Name": name,
  61. "Location": self.Name,
  62. "Properties": map[string]interface{}{
  63. "CreationData": map[string]string{
  64. "CreateOption": "Copy",
  65. "SourceResourceID": diskId,
  66. },
  67. },
  68. "Type": "Microsoft.Compute/snapshots",
  69. }
  70. snapshot := &SSnapshot{region: self}
  71. return snapshot, self.create("", jsonutils.Marshal(params), snapshot)
  72. }
  73. func (self *SSnapshot) Delete() error {
  74. return self.region.DeleteSnapshot(self.ID)
  75. }
  76. func (self *SSnapshot) GetSizeMb() int32 {
  77. return self.Properties.DiskSizeGB.Int32() * 1024
  78. }
  79. func (self *SRegion) DeleteSnapshot(snapshotId string) error {
  80. return self.del(snapshotId)
  81. }
  82. type AccessURIOutput struct {
  83. AccessSas string
  84. }
  85. type AccessProperties struct {
  86. Output AccessURIOutput
  87. }
  88. type AccessURI struct {
  89. Name string
  90. Properties AccessProperties
  91. }
  92. func (self *SRegion) GrantAccessSnapshot(snapshotId string) (string, error) {
  93. params := map[string]interface{}{
  94. "access": "Read",
  95. "durationInSeconds": 3600 * 24,
  96. }
  97. body, err := self.perform(snapshotId, "beginGetAccess", jsonutils.Marshal(params))
  98. if err != nil {
  99. return "", err
  100. }
  101. accessURI := AccessURI{}
  102. return accessURI.Properties.Output.AccessSas, body.Unmarshal(&accessURI)
  103. }
  104. func (self *SSnapshot) Refresh() error {
  105. snapshot, err := self.region.GetSnapshot(self.ID)
  106. if err != nil {
  107. return err
  108. }
  109. return jsonutils.Update(self, snapshot)
  110. }
  111. func (self *SRegion) GetISnapshotById(snapshotId string) (cloudprovider.ICloudSnapshot, error) {
  112. return self.GetSnapshot(snapshotId)
  113. }
  114. func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  115. snapshots, err := self.ListSnapshots()
  116. if err != nil {
  117. return nil, err
  118. }
  119. ret := []cloudprovider.ICloudSnapshot{}
  120. for i := range snapshots {
  121. snapshots[i].region = self
  122. ret = append(ret, &snapshots[i])
  123. }
  124. return ret, nil
  125. }
  126. func (self *SSnapshot) GetDiskId() string {
  127. return strings.ToLower(self.Properties.CreationData.SourceResourceID)
  128. }
  129. func (self *SSnapshot) GetDiskType() string {
  130. return ""
  131. }
  132. func (self *SSnapshot) GetProjectId() string {
  133. return getResourceGroup(self.ID)
  134. }
  135. func (region *SRegion) GetSnapshot(snapshotId string) (*SSnapshot, error) {
  136. snapshot := SSnapshot{region: region}
  137. return &snapshot, region.get(snapshotId, url.Values{}, &snapshot)
  138. }
  139. func (region *SRegion) ListSnapshots() ([]SSnapshot, error) {
  140. result := []SSnapshot{}
  141. err := region.list("Microsoft.Compute/snapshots", url.Values{}, &result)
  142. if err != nil {
  143. return nil, err
  144. }
  145. return result, nil
  146. }