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
/
serverDisk.go
128 lines (105 loc) · 3.25 KB
/
serverDisk.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
package main
import (
"fmt"
"encoding/json"
"github.com/gophercloud/gophercloud/functiontest/common"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach"
)
func main() {
fmt.Println("main start...")
//provider, err := common.AuthAKSK()
provider, err := common.AuthToken()
if err != nil {
fmt.Println("get provider client failed")
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
sc, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{})
if err != nil {
fmt.Println("get compute v2 client failed")
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
//TestTestGetVolumeAttachList(sc)
//TestGetVolumeAttached(sc)
//TestDeleteVolumeAttached(sc)
TestDetachVolumeWithFlag(sc)
fmt.Println("main end...")
}
func TestTestGetVolumeAttachList(sc *gophercloud.ServiceClient) {
serverId := "4a5e7286-8da5-4bf8-9658-88f5fc604d2e"
page, err := volumeattach.List(sc, serverId).AllPages()
if err != nil {
fmt.Println(err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode", ue.ErrCode)
fmt.Println("ErrMessage", ue.ErrMessage)
}
return
}
fmt.Println("Test server get VolumeAttached success!")
volumeList, err := volumeattach.ExtractVolumeAttachments(page)
if err != nil {
fmt.Println(err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode", ue.ErrCode)
fmt.Println("ErrMessage", ue.ErrMessage)
}
return
}
p, _ := json.MarshalIndent(volumeList, "", " ")
fmt.Println(string(p))
}
func TestGetVolumeAttached(sc *gophercloud.ServiceClient) {
id := "609bdc81-b131-4886-8d02-160efae60185"
serverId := "4a5e7286-8da5-4bf8-9658-88f5fc604d2e"
resp, err := volumeattach.Get(sc, serverId, id).Extract()
if err != nil {
fmt.Println(err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode", ue.ErrCode)
fmt.Println("ErrMessage", ue.ErrMessage)
}
return
}
fmt.Println("Test server get VolumeAttached success!")
b, _ := json.MarshalIndent(*resp, "", " ")
fmt.Println(string(b))
}
func TestDeleteVolumeAttached(sc *gophercloud.ServiceClient) {
id := "128a7bf965154373wea7b73c89eb6b65aa"
serverId := "4a5e7286-8da5-4bf8-9658-88f5fc604d2e"
err := volumeattach.Delete(sc, serverId, id).ExtractErr()
if err != nil {
fmt.Println(err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode", ue.ErrCode)
fmt.Println("ErrMessage", ue.ErrMessage)
}
return
}
fmt.Println("Test server del VolumeAttached success!")
}
//TestDetachVolumeWithFlag tests detachment of volume with delete flag
func TestDetachVolumeWithFlag(sc *gophercloud.ServiceClient) {
volumeID := "{volumeID}"
serverID := "{serverID}"
err := volumeattach.DeleteWithFlag(sc, serverID, volumeID, 0).ExtractErr()
if err != nil {
fmt.Println(err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode", ue.ErrCode)
fmt.Println("ErrMessage", ue.ErrMessage)
}
return
}
fmt.Println("Test server detach volume with delete flag success!")
}