Skip to content

Commit f2b3f5a

Browse files
committed
Support for generic REST client
This commit adds support for a generic REST actions. These actions allow direct REST communication while still leveraging some of the core Gophercloud client features.
1 parent afce78e commit f2b3f5a

File tree

5 files changed

+606
-0
lines changed

5 files changed

+606
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// +build acceptance restclient
2+
3+
package restclient
4+
5+
import (
6+
"testing"
7+
8+
acc_clients "github.com/gophercloud/gophercloud/acceptance/clients"
9+
acc_tools "github.com/gophercloud/gophercloud/acceptance/tools"
10+
11+
th "github.com/gophercloud/gophercloud/testhelper"
12+
cc "github.com/gophercloud/utils/openstack/clientconfig"
13+
"github.com/gophercloud/utils/openstack/restclient"
14+
)
15+
16+
func TestRESTClient(t *testing.T) {
17+
acc_clients.RequireAdmin(t)
18+
19+
// This will be populated by environment variables.
20+
clientOpts := &cc.ClientOpts{}
21+
22+
computeClient, err := cc.NewServiceClient("compute", clientOpts)
23+
th.AssertNoErr(t, err)
24+
25+
// Test creating a flavor
26+
flavorName := acc_tools.RandomString("TESTACC-", 8)
27+
flavorID := acc_tools.RandomString("TESTACC-", 8)
28+
flavorOpts := map[string]interface{}{
29+
"name": flavorName,
30+
"ram": 512,
31+
"vcpus": 1,
32+
"disk": 5,
33+
"id": flavorID,
34+
}
35+
36+
postOpts := &restclient.PostOpts{
37+
Params: map[string]interface{}{"flavor": flavorOpts},
38+
}
39+
40+
postURL := computeClient.ServiceURL("flavors")
41+
postRes := restclient.Post(computeClient, postURL, postOpts)
42+
th.AssertNoErr(t, postRes.Err)
43+
flavorResult, err := postRes.Extract()
44+
th.AssertNoErr(t, postRes.Err)
45+
acc_tools.PrintResource(t, flavorResult)
46+
47+
// Test deleting a flavor
48+
defer func() {
49+
deleteURL := computeClient.ServiceURL("flavors", flavorID)
50+
deleteRes := restclient.Delete(computeClient, deleteURL, nil)
51+
th.AssertNoErr(t, deleteRes.Err)
52+
err = deleteRes.ExtractErr()
53+
th.AssertNoErr(t, err)
54+
}()
55+
56+
// Test retrieving a flavor
57+
getURL := computeClient.ServiceURL("flavors", flavorID)
58+
getRes := restclient.Get(computeClient, getURL, nil)
59+
th.AssertNoErr(t, getRes.Err)
60+
61+
flavorResult, err = getRes.Extract()
62+
th.AssertNoErr(t, err)
63+
64+
flavor := flavorResult["flavor"].(map[string]interface{})
65+
66+
acc_tools.PrintResource(t, flavor)
67+
68+
th.AssertEquals(t, flavor["disk"], float64(5))
69+
th.AssertEquals(t, flavor["id"], flavorID)
70+
th.AssertEquals(t, flavor["name"], flavorName)
71+
th.AssertEquals(t, flavor["ram"], float64(512))
72+
th.AssertEquals(t, flavor["swap"], "")
73+
th.AssertEquals(t, flavor["vcpus"], float64(1))
74+
75+
// Test listing flavors
76+
getOpts := &restclient.GetOpts{
77+
Query: map[string]interface{}{
78+
"limit": 2,
79+
},
80+
}
81+
82+
getURL = computeClient.ServiceURL("flavors")
83+
getRes = restclient.Get(computeClient, getURL, getOpts)
84+
th.AssertNoErr(t, getRes.Err)
85+
flavorResult, err = getRes.Extract()
86+
th.AssertNoErr(t, err)
87+
88+
flavors := flavorResult["flavors"].([]interface{})
89+
acc_tools.PrintResource(t, flavors)
90+
th.AssertEquals(t, len(flavors), 2)
91+
}

openstack/restclient/doc.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Package restclient provides generic REST functions.
2+
3+
Example of a GET request
4+
5+
getURL := computeClient.ServiceURL("flavors", flavorID)
6+
getRes := restclient.Get(computeClient, getURL, nil)
7+
if err != nil {
8+
panic(err)
9+
}
10+
11+
flavorResult, err = getRes.Extract()
12+
if err != nil {
13+
panic(err)
14+
}
15+
16+
flavor := flavorResult["flavor"].(map[string]interface{})
17+
fmt.Printf("%v\n", flavor)
18+
19+
Example of a POST request
20+
21+
flavorOpts := map[string]interface{}{
22+
"name": "some-name",
23+
"ram": 512,
24+
"vcpus": 1,
25+
"disk": 5,
26+
"id": "some-id",
27+
}
28+
29+
postOpts := &restclient.PostOpts{
30+
Params: map[string]interface{}{"flavor": flavorOpts},
31+
}
32+
33+
postURL := computeClient.ServiceURL("flavors")
34+
postRes := restclient.Post(computeClient, postURL, postOpts)
35+
if err != nil {
36+
panic(err)
37+
}
38+
39+
flavorResult, err := postRes.Extract()
40+
if err != nil {
41+
panic(err)
42+
}
43+
44+
fmt.Printf("%v\n", flavor)
45+
46+
Example of a DELETE Request
47+
48+
deleteURL := computeClient.ServiceURL("flavors", "flavor-id")
49+
deleteRes := restclient.Delete(computeClient, deleteURL, nil)
50+
if err != nil {
51+
panic(err)
52+
}
53+
54+
err = deleteRes.ExtractErr()
55+
if err != nil {
56+
panic(err)
57+
}
58+
*/
59+
package restclient

0 commit comments

Comments
 (0)