forked from yunionio/cloudpods
/
snapshot.go
173 lines (149 loc) · 4.01 KB
/
snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package azure
import (
"fmt"
"strings"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/compute/models"
)
type SnapshotSku struct {
Name string
Tier string
}
type SSnapshot struct {
region *SRegion
ID string
Name string
Location string
ManagedBy string
Sku *SnapshotSku
Properties DiskProperties
Type string
}
func (self *SSnapshot) GetId() string {
return self.ID
}
func (self *SSnapshot) GetGlobalId() string {
return strings.ToLower(self.ID)
}
func (self *SSnapshot) GetMetadata() *jsonutils.JSONDict {
return nil
}
func (self *SSnapshot) GetName() string {
return self.Name
}
func (self *SSnapshot) GetStatus() string {
switch self.Properties.ProvisioningState {
case "Succeeded":
return models.SNAPSHOT_READY
default:
log.Errorf("Unknow azure snapshot %s status: %s", self.ID, self.Properties.ProvisioningState)
return models.SNAPSHOT_UNKNOWN
}
}
func (self *SSnapshot) IsEmulated() bool {
return false
}
func (self *SRegion) CreateSnapshot(diskId, snapName, desc string) (*SSnapshot, error) {
disk, err := self.GetDisk(diskId)
if err != nil {
return nil, err
}
snapshot := SSnapshot{
region: self,
Name: snapName,
Location: self.Name,
Properties: DiskProperties{
CreationData: CreationData{
CreateOption: "Copy",
SourceResourceID: diskId,
},
DiskSizeGB: disk.Properties.DiskSizeGB,
},
Type: "Microsoft.Compute/snapshots",
}
return &snapshot, self.client.Create(jsonutils.Marshal(snapshot), &snapshot)
}
func (self *SSnapshot) Delete() error {
return self.region.DeleteSnapshot(self.ID)
}
func (self *SSnapshot) GetSize() int32 {
return self.Properties.DiskSizeGB
}
func (self *SRegion) DeleteSnapshot(snapshotId string) error {
return self.client.Delete(snapshotId)
}
type AccessURIOutput struct {
AccessSas string
}
type AccessProperties struct {
Output AccessURIOutput
}
type AccessURI struct {
Name string
Properties AccessProperties
}
func (self *SRegion) GrantAccessSnapshot(snapshotId string) (string, error) {
body, err := self.client.PerformAction(snapshotId, "beginGetAccess", fmt.Sprintf(`{"access": "Read", "durationInSeconds": %d}`, 3600*24))
if err != nil {
return "", err
}
accessURI := AccessURI{}
return accessURI.Properties.Output.AccessSas, body.Unmarshal(&accessURI)
}
func (self *SSnapshot) Refresh() error {
snapshot, err := self.region.GetSnapshotDetail(self.ID)
if err != nil {
return err
}
return jsonutils.Update(self, snapshot)
}
func (self *SRegion) GetISnapshotById(snapshotId string) (cloudprovider.ICloudSnapshot, error) {
if strings.HasPrefix(snapshotId, "https://") {
//TODO
return nil, cloudprovider.ErrNotImplemented
}
return self.GetSnapshotDetail(snapshotId)
}
func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
snapshots, err := self.GetSnapShots("")
if err != nil {
return nil, err
}
classicSnapshots := []SClassicSnapshot{}
storages, err := self.GetStorageAccounts()
if err != nil {
return nil, err
}
_, _classicSnapshots, err := self.GetStorageAccountsDisksWithSnapshots(storages...)
if err != nil {
return nil, err
}
classicSnapshots = append(classicSnapshots, _classicSnapshots...)
classicStorages, err := self.GetClassicStorageAccounts()
if err != nil {
return nil, err
}
_, _classicSnapshots, err = self.GetStorageAccountsDisksWithSnapshots(classicStorages...)
if err != nil {
return nil, err
}
classicSnapshots = append(classicSnapshots, _classicSnapshots...)
isnapshots := make([]cloudprovider.ICloudSnapshot, len(snapshots)+len(classicSnapshots))
for i := 0; i < len(snapshots); i++ {
snapshots[i].region = self
isnapshots[i] = &snapshots[i]
}
for i := 0; i < len(classicSnapshots); i++ {
classicSnapshots[i].region = self
isnapshots[len(snapshots)+i] = &classicSnapshots[i]
}
return isnapshots, nil
}
func (self *SSnapshot) GetDiskId() string {
return self.Properties.CreationData.SourceResourceID
}
func (self *SSnapshot) GetDiskType() string {
return ""
}