Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit cf0c7fe

Browse files
committed
Update blockstorage v2 API
1 parent da54614 commit cf0c7fe

File tree

17 files changed

+1080
-164
lines changed

17 files changed

+1080
-164
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// +build acceptance blockstorage
2+
3+
package extensions
4+
5+
import (
6+
"os"
7+
"testing"
8+
9+
"github.com/rackspace/gophercloud"
10+
"github.com/rackspace/gophercloud/openstack"
11+
"github.com/rackspace/gophercloud/openstack/blockstorage/v2/extensions/volumeactions"
12+
"github.com/rackspace/gophercloud/openstack/blockstorage/v2/volumes"
13+
th "github.com/rackspace/gophercloud/testhelper"
14+
)
15+
16+
func newClient(t *testing.T) (*gophercloud.ServiceClient, error) {
17+
ao, err := openstack.AuthOptionsFromEnv()
18+
th.AssertNoErr(t, err)
19+
20+
client, err := openstack.AuthenticatedClient(ao)
21+
th.AssertNoErr(t, err)
22+
23+
return openstack.NewBlockStorageV2(client, gophercloud.EndpointOpts{
24+
Region: os.Getenv("OS_REGION_NAME"),
25+
})
26+
}
27+
28+
func TestVolumeActions(t *testing.T) {
29+
client, err := newClient(t)
30+
th.AssertNoErr(t, err)
31+
32+
cv, err := volumes.Create(client, &volumes.CreateOpts{
33+
Size: 1,
34+
Name: "blockv2-volume",
35+
}).Extract()
36+
th.AssertNoErr(t, err)
37+
defer func() {
38+
err = volumes.WaitForStatus(client, cv.ID, "available", 60)
39+
th.AssertNoErr(t, err)
40+
err = volumes.Delete(client, cv.ID).ExtractErr()
41+
th.AssertNoErr(t, err)
42+
}()
43+
44+
err = volumes.WaitForStatus(client, cv.ID, "available", 60)
45+
th.AssertNoErr(t, err)
46+
47+
_, err = volumeactions.Attach(client, cv.ID, &volumeactions.AttachOpts{
48+
MountPoint: "/mnt",
49+
Mode: "rw",
50+
InstanceUUID: "50902f4f-a974-46a0-85e9-7efc5e22dfdd",
51+
}).Extract()
52+
th.AssertNoErr(t, err)
53+
54+
err = volumes.WaitForStatus(client, cv.ID, "in-use", 60)
55+
th.AssertNoErr(t, err)
56+
57+
_, err = volumeactions.Detach(client, cv.ID).Extract()
58+
th.AssertNoErr(t, err)
59+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// +build acceptance blockstorage
2+
3+
package v2
4+
5+
import (
6+
"os"
7+
"testing"
8+
9+
"github.com/rackspace/gophercloud"
10+
"github.com/rackspace/gophercloud/openstack"
11+
"github.com/rackspace/gophercloud/openstack/blockstorage/v2/volumes"
12+
"github.com/rackspace/gophercloud/pagination"
13+
th "github.com/rackspace/gophercloud/testhelper"
14+
)
15+
16+
func newClient(t *testing.T) (*gophercloud.ServiceClient, error) {
17+
ao, err := openstack.AuthOptionsFromEnv()
18+
th.AssertNoErr(t, err)
19+
20+
client, err := openstack.AuthenticatedClient(ao)
21+
th.AssertNoErr(t, err)
22+
23+
return openstack.NewBlockStorageV2(client, gophercloud.EndpointOpts{
24+
Region: os.Getenv("OS_REGION_NAME"),
25+
})
26+
}
27+
28+
func TestVolumes(t *testing.T) {
29+
client, err := newClient(t)
30+
th.AssertNoErr(t, err)
31+
32+
cv, err := volumes.Create(client, &volumes.CreateOpts{
33+
Size: 1,
34+
Name: "blockv2-volume",
35+
}).Extract()
36+
th.AssertNoErr(t, err)
37+
defer func() {
38+
err = volumes.WaitForStatus(client, cv.ID, "available", 60)
39+
th.AssertNoErr(t, err)
40+
err = volumes.Delete(client, cv.ID).ExtractErr()
41+
th.AssertNoErr(t, err)
42+
}()
43+
44+
_, err = volumes.Update(client, cv.ID, &volumes.UpdateOpts{
45+
Name: "blockv2-updated-volume",
46+
}).Extract()
47+
th.AssertNoErr(t, err)
48+
49+
v, err := volumes.Get(client, cv.ID).Extract()
50+
th.AssertNoErr(t, err)
51+
t.Logf("Got volume: %+v\n", v)
52+
53+
if v.Name != "blockv2-updated-volume" {
54+
t.Errorf("Unable to update volume: Expected name: blockv2-updated-volume\nActual name: %s", v.Name)
55+
}
56+
57+
err = volumes.List(client, &volumes.ListOpts{Name: "blockv2-updated-volume"}).EachPage(func(page pagination.Page) (bool, error) {
58+
vols, err := volumes.ExtractVolumes(page)
59+
th.CheckEquals(t, 1, len(vols))
60+
return true, err
61+
})
62+
th.AssertNoErr(t, err)
63+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Package volumeactions provides information and interaction with volumes in the
2+
// OpenStack Block Storage service. A volume is a detachable block storage
3+
// device, akin to a USB hard drive. It can only be attached to one instance at
4+
// a time.
5+
package volumeactions
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package volumeactions
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
8+
th "github.com/rackspace/gophercloud/testhelper"
9+
fake "github.com/rackspace/gophercloud/testhelper/client"
10+
)
11+
12+
func MockAttachResponse(t *testing.T) {
13+
th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
14+
func(w http.ResponseWriter, r *http.Request) {
15+
th.TestMethod(t, r, "POST")
16+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
17+
th.TestHeader(t, r, "Content-Type", "application/json")
18+
th.TestHeader(t, r, "Accept", "application/json")
19+
th.TestJSONRequest(t, r, `
20+
{
21+
"os-attach":
22+
{
23+
"mountpoint": "/mnt",
24+
"mode": "rw",
25+
"instance_uuid": "50902f4f-a974-46a0-85e9-7efc5e22dfdd"
26+
}
27+
}
28+
`)
29+
30+
w.Header().Add("Content-Type", "application/json")
31+
w.WriteHeader(http.StatusAccepted)
32+
33+
fmt.Fprintf(w, `{}`)
34+
})
35+
}
36+
37+
func MockDetachResponse(t *testing.T) {
38+
th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
39+
func(w http.ResponseWriter, r *http.Request) {
40+
th.TestMethod(t, r, "POST")
41+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
42+
th.TestHeader(t, r, "Content-Type", "application/json")
43+
th.TestHeader(t, r, "Accept", "application/json")
44+
th.TestJSONRequest(t, r, `
45+
{
46+
"os-detach": {}
47+
}
48+
`)
49+
50+
w.Header().Add("Content-Type", "application/json")
51+
w.WriteHeader(http.StatusAccepted)
52+
53+
fmt.Fprintf(w, `{}`)
54+
})
55+
}
56+
57+
func MockReserveResponse(t *testing.T) {
58+
th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
59+
func(w http.ResponseWriter, r *http.Request) {
60+
th.TestMethod(t, r, "POST")
61+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
62+
th.TestHeader(t, r, "Content-Type", "application/json")
63+
th.TestHeader(t, r, "Accept", "application/json")
64+
th.TestJSONRequest(t, r, `
65+
{
66+
"os-reserve": {}
67+
}
68+
`)
69+
70+
w.Header().Add("Content-Type", "application/json")
71+
w.WriteHeader(http.StatusAccepted)
72+
73+
fmt.Fprintf(w, `{}`)
74+
})
75+
}
76+
77+
func MockUnreserveResponse(t *testing.T) {
78+
th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
79+
func(w http.ResponseWriter, r *http.Request) {
80+
th.TestMethod(t, r, "POST")
81+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
82+
th.TestHeader(t, r, "Content-Type", "application/json")
83+
th.TestHeader(t, r, "Accept", "application/json")
84+
th.TestJSONRequest(t, r, `
85+
{
86+
"os-unreserve": {}
87+
}
88+
`)
89+
90+
w.Header().Add("Content-Type", "application/json")
91+
w.WriteHeader(http.StatusAccepted)
92+
93+
fmt.Fprintf(w, `{}`)
94+
})
95+
}
96+
97+
func MockInitializeConnectionResponse(t *testing.T) {
98+
th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
99+
func(w http.ResponseWriter, r *http.Request) {
100+
th.TestMethod(t, r, "POST")
101+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
102+
th.TestHeader(t, r, "Content-Type", "application/json")
103+
th.TestHeader(t, r, "Accept", "application/json")
104+
th.TestJSONRequest(t, r, `
105+
{
106+
"os-initialize_connection":
107+
{
108+
"connector":
109+
{
110+
"ip":"127.0.0.1",
111+
"host":"stack",
112+
"initiator":"iqn.1994-05.com.redhat:17cf566367d2",
113+
"multipath": false,
114+
"platform": "x86_64",
115+
"os_type": "linux2"
116+
}
117+
}
118+
}
119+
`)
120+
121+
w.Header().Add("Content-Type", "application/json")
122+
w.WriteHeader(http.StatusAccepted)
123+
124+
fmt.Fprintf(w, `{
125+
"connection_info": {
126+
"data": {
127+
"target_portals": [
128+
"172.31.17.48:3260"
129+
],
130+
"auth_method": "CHAP",
131+
"auth_username": "5MLtcsTEmNN5jFVcT6ui",
132+
"access_mode": "rw",
133+
"target_lun": 0,
134+
"volume_id": "cd281d77-8217-4830-be95-9528227c105c",
135+
"target_luns": [
136+
0
137+
],
138+
"target_iqns": [
139+
"iqn.2010-10.org.openstack:volume-cd281d77-8217-4830-be95-9528227c105c"
140+
],
141+
"auth_password": "x854ZY5Re3aCkdNL",
142+
"target_discovered": false,
143+
"encrypted": false,
144+
"qos_specs": null,
145+
"target_iqn": "iqn.2010-10.org.openstack:volume-cd281d77-8217-4830-be95-9528227c105c",
146+
"target_portal": "172.31.17.48:3260"
147+
},
148+
"driver_volume_type": "iscsi"
149+
}
150+
}`)
151+
})
152+
}
153+
154+
func MockTerminateConnectionResponse(t *testing.T) {
155+
th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
156+
func(w http.ResponseWriter, r *http.Request) {
157+
th.TestMethod(t, r, "POST")
158+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
159+
th.TestHeader(t, r, "Content-Type", "application/json")
160+
th.TestHeader(t, r, "Accept", "application/json")
161+
th.TestJSONRequest(t, r, `
162+
{
163+
"os-terminate_connection":
164+
{
165+
"connector":
166+
{
167+
"ip":"127.0.0.1",
168+
"host":"stack",
169+
"initiator":"iqn.1994-05.com.redhat:17cf566367d2",
170+
"multipath": false,
171+
"platform": "x86_64",
172+
"os_type": "linux2"
173+
}
174+
}
175+
}
176+
`)
177+
178+
w.Header().Add("Content-Type", "application/json")
179+
w.WriteHeader(http.StatusAccepted)
180+
181+
fmt.Fprintf(w, `{}`)
182+
})
183+
}

0 commit comments

Comments
 (0)