This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.go
89 lines (72 loc) · 2.14 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
package azurecompute
import (
"fmt"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to"
)
func getSnapshotsClient() compute.SnapshotsClient {
snapshotsClient := compute.NewSnapshotsClient(subscription)
snapshotsClient.Authorizer = autorest.NewBearerAuthorizer(token)
return snapshotsClient
}
type SnapshotIn struct {
ResourceGroup string
SnapshotName string `json:"snapshotname,omitempty"`
SourceImageID string `json:"sourceimageid,omitempty"`
Location string `json:"location,omitempty"`
}
func (s SnapshotIn) CreateSnapshot() (ss compute.Snapshot, err error) {
snapshotsClient := getSnapshotsClient()
future, err := snapshotsClient.CreateOrUpdate(
ctx,
s.ResourceGroup,
s.SnapshotName,
compute.Snapshot{
Location: to.StringPtr(s.Location),
DiskProperties: &compute.DiskProperties{
OsType: compute.Linux,
CreationData: &compute.CreationData{
CreateOption: compute.Copy,
SourceResourceID: to.StringPtr(s.SourceImageID),
},
},
},
)
if err != nil {
return ss, fmt.Errorf("cannot create snapshot: %v", err)
}
err = future.WaitForCompletion(ctx, snapshotsClient.Client)
if err != nil {
return ss, fmt.Errorf("cannot get the snapshot create or update future response: %v", err)
}
return future.Result(snapshotsClient)
}
func (s SnapshotIn) DeleteSnapshot() (ar autorest.Response, err error) {
snapshotsClient := getSnapshotsClient()
future, err := snapshotsClient.Delete(
ctx,
s.ResourceGroup,
s.SnapshotName,
)
if err != nil {
return ar, fmt.Errorf("cannot delete snapshot: %v", err)
}
err = future.WaitForCompletion(ctx, snapshotsClient.Client)
if err != nil {
return ar, fmt.Errorf("cannot get the snapshot delete future response: %v", err)
}
return future.Result(snapshotsClient)
}
func (s SnapshotIn) GetSnapshot() (ss compute.Snapshot, err error) {
snapshotsClient := getSnapshotsClient()
future, err := snapshotsClient.Get(
ctx,
s.ResourceGroup,
s.SnapshotName,
)
if err != nil {
return ss, fmt.Errorf("cannot get snapshot: %v", err)
}
return future, err
}