-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (144 loc) · 3.75 KB
/
main.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
174
package main
import (
"fmt"
"github.com/dynuc/gophercloud/functiontest/common"
"github.com/dynuc/gophercloud"
"github.com/dynuc/gophercloud/openstack"
v1 "github.com/dynuc/gophercloud/openstack/blockstorage/v1/volumes"
"github.com/dynuc/gophercloud/openstack/blockstorage/v2/volumes"
v3 "github.com/dynuc/gophercloud/openstack/blockstorage/v3/volumes"
"encoding/json"
)
func main() {
fmt.Println("main start...")
//provider, err := common.AuthToken()
provider, err := common.AuthAKSK()
if err != nil {
fmt.Println("get provider client failed")
fmt.Println(err.Error())
return
}
sc, err := openstack.NewBlockStorageV2(provider, gophercloud.EndpointOpts{})
if err != nil {
fmt.Println("get BlockStorage v2 client failed")
fmt.Println(err.Error())
return
}
TestGetVolumes(sc)
TestGetQuotaSet(sc)
fmt.Println("main end...")
}
func TestGetVolumes(sc *gophercloud.ServiceClient) {
volume, err := volumes.Get(sc, "9547e93c-2180-4f95-8190-ea5140854c86").Extract()
if err != nil {
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
fmt.Println("get volume success!")
fmt.Println("volume:", volume)
}
func TestGetQuotaSet(sc *gophercloud.ServiceClient) {
var vLimit, vInUse, gLimit, gInUse int
qs, err := volumes.GetQuotaSet(sc, "128a7bf965154373a7b73c89eb6b65aa")
if err != nil {
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
var bt volumes.BaseType
for k, v := range qs.QuoSet {
if k == "gigabytes" {
if data, ok := v.(map[string]interface{}); ok {
b, err := json.Marshal(data)
if err != nil {
return
}
err = json.Unmarshal(b, &bt)
if err != nil {
return
}
gLimit = bt.Limit
gInUse = bt.InUse
}
}
if k == "volumes" {
if data, ok := v.(map[string]interface{}); ok {
b, err := json.Marshal(data)
if err != nil {
return
}
err = json.Unmarshal(b, &bt)
if err != nil {
return
}
vLimit = bt.Limit
vInUse = bt.InUse
}
}
}
fmt.Println(vLimit, vInUse, gLimit, gInUse)
for k, v := range qs.QuoSet {
fmt.Println("type is :", k)
if data, ok := v.(map[string]interface{}); ok {
for b1, b2 := range data {
fmt.Println(b1, b2)
}
} else {
fmt.Println("value is ", v)
}
}
fmt.Println("get quota set success!")
}
func TestListVolumes(provider *gophercloud.ProviderClient) {
//v1 client
clientV1, err := openstack.NewBlockStorageV1(provider, gophercloud.EndpointOpts{})
if err != nil {
fmt.Println("get BlockStorage v1 client failed")
fmt.Println(err.Error())
return
}
fmt.Println("V1 Endpoint:", clientV1.Endpoint)
fmt.Println("V1 IdentityBase:", clientV1.IdentityBase)
//v3 client
clientV3, err := openstack.NewBlockStorageV3(provider, gophercloud.EndpointOpts{})
if err != nil {
fmt.Println("get BlockStorage v3 client failed")
fmt.Println(err.Error())
return
}
fmt.Println("V3 Endpoint:", clientV3.Endpoint)
fmt.Println("V3 IdentityBase:", clientV3.IdentityBase)
///////////////////////////////////
//查询所有v3 Volumes
listOptsV3 := v3.ListOpts{}
allPagesV3, err := v3.List(clientV3, listOptsV3).AllPages()
if err != nil {
// 异常处理
panic(err)
}
allVolumesV3, err := v3.ExtractVolumes(allPagesV3)
if err != nil {
// 异常处理
panic(err)
}
fmt.Println(allVolumesV3)
////////////////////////////////////
//查询所有v1 Volumes
listOptsV1 := v1.ListOpts{}
allPagesV1, err := v1.List(clientV1, listOptsV1).AllPages()
if err != nil {
// 异常处理
panic(err)
}
allVolumesV1, err := v1.ExtractVolumes(allPagesV1)
if err != nil {
// 异常处理
panic(err)
}
fmt.Println(allVolumesV1)
}