This repository has been archived by the owner on May 8, 2024. It is now read-only.
forked from huaweicloud/huaweicloud-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshots.go
101 lines (85 loc) · 2.38 KB
/
snapshots.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
package main
import (
"encoding/json"
"fmt"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/auth/aksk"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/snapshots"
)
func main() {
fmt.Println("main start...")
opts := aksk.AKSKOptions{
IdentityEndpoint: "https://iam.xxx.yyy.com/v3",
ProjectID: "{ProjectID}",
AccessKey: "your AK string",
SecretKey: "your SK string",
Cloud: "yyy.com",
Region: "xxx",
DomainID: "{DomainID}",
}
provider, err_auth := openstack.AuthenticatedClient(opts)
if err_auth != nil {
fmt.Println("get provider client failed: ", err_auth)
return
}
sc, err := openstack.NewBlockStorageV2(provider, gophercloud.EndpointOpts{})
if err != nil {
fmt.Println("get BlockStorage v2 client failed")
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
SnapshotsListDetails(sc)
SnapshotsCreate(sc)
fmt.Println("main end...")
}
// list snapshot detail
func SnapshotsListDetails(sc *gophercloud.ServiceClient) {
fmt.Println("start list snapshot detail...")
allPages, err := snapshots.Detail(sc, snapshots.ListOpts{Limit: 5}).AllPages()
if err != nil {
fmt.Println(err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
allData, err1 := snapshots.ExtractSnapshots(allPages)
if err1 != nil {
fmt.Println(err1)
return
}
fmt.Println("list snapshot detail success!")
for _, v := range allData.SnapshotsLinks {
b, _ := json.MarshalIndent(v, "", " ")
fmt.Println(string(b))
}
for _, v := range allData.Snapshots {
b, _ := json.MarshalIndent(v, "", " ")
fmt.Println(string(b))
}
}
// create snapshot
func SnapshotsCreate(sc *gophercloud.ServiceClient) {
fmt.Println("srart create snapshot...")
opts := snapshots.CreateOpts{
VolumeID: "xxx",
Name: "xxx",
}
resp, err := snapshots.Create(sc, opts).Extract()
if err != nil {
fmt.Println(err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
fmt.Println("create snapshot success!")
b, _ := json.MarshalIndent(resp, "", " ")
fmt.Println(string(b))
}