-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
606 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// +build acceptance restclient | ||
|
||
package restclient | ||
|
||
import ( | ||
"testing" | ||
|
||
acc_clients "github.com/gophercloud/gophercloud/acceptance/clients" | ||
acc_tools "github.com/gophercloud/gophercloud/acceptance/tools" | ||
|
||
th "github.com/gophercloud/gophercloud/testhelper" | ||
cc "github.com/gophercloud/utils/openstack/clientconfig" | ||
"github.com/gophercloud/utils/openstack/restclient" | ||
) | ||
|
||
func TestRESTClient(t *testing.T) { | ||
acc_clients.RequireAdmin(t) | ||
|
||
// This will be populated by environment variables. | ||
clientOpts := &cc.ClientOpts{} | ||
|
||
computeClient, err := cc.NewServiceClient("compute", clientOpts) | ||
th.AssertNoErr(t, err) | ||
|
||
// Test creating a flavor | ||
flavorName := acc_tools.RandomString("TESTACC-", 8) | ||
flavorID := acc_tools.RandomString("TESTACC-", 8) | ||
flavorOpts := map[string]interface{}{ | ||
"name": flavorName, | ||
"ram": 512, | ||
"vcpus": 1, | ||
"disk": 5, | ||
"id": flavorID, | ||
} | ||
|
||
postOpts := &restclient.PostOpts{ | ||
Params: map[string]interface{}{"flavor": flavorOpts}, | ||
} | ||
|
||
postURL := computeClient.ServiceURL("flavors") | ||
postRes := restclient.Post(computeClient, postURL, postOpts) | ||
th.AssertNoErr(t, postRes.Err) | ||
flavorResult, err := postRes.Extract() | ||
th.AssertNoErr(t, postRes.Err) | ||
acc_tools.PrintResource(t, flavorResult) | ||
|
||
// Test deleting a flavor | ||
defer func() { | ||
deleteURL := computeClient.ServiceURL("flavors", flavorID) | ||
deleteRes := restclient.Delete(computeClient, deleteURL, nil) | ||
th.AssertNoErr(t, deleteRes.Err) | ||
err = deleteRes.ExtractErr() | ||
th.AssertNoErr(t, err) | ||
}() | ||
|
||
// Test retrieving a flavor | ||
getURL := computeClient.ServiceURL("flavors", flavorID) | ||
getRes := restclient.Get(computeClient, getURL, nil) | ||
th.AssertNoErr(t, getRes.Err) | ||
|
||
flavorResult, err = getRes.Extract() | ||
th.AssertNoErr(t, err) | ||
|
||
flavor := flavorResult["flavor"].(map[string]interface{}) | ||
|
||
acc_tools.PrintResource(t, flavor) | ||
|
||
th.AssertEquals(t, flavor["disk"], float64(5)) | ||
th.AssertEquals(t, flavor["id"], flavorID) | ||
th.AssertEquals(t, flavor["name"], flavorName) | ||
th.AssertEquals(t, flavor["ram"], float64(512)) | ||
th.AssertEquals(t, flavor["swap"], "") | ||
th.AssertEquals(t, flavor["vcpus"], float64(1)) | ||
|
||
// Test listing flavors | ||
getOpts := &restclient.GetOpts{ | ||
Query: map[string]interface{}{ | ||
"limit": 2, | ||
}, | ||
} | ||
|
||
getURL = computeClient.ServiceURL("flavors") | ||
getRes = restclient.Get(computeClient, getURL, getOpts) | ||
th.AssertNoErr(t, getRes.Err) | ||
flavorResult, err = getRes.Extract() | ||
th.AssertNoErr(t, err) | ||
|
||
flavors := flavorResult["flavors"].([]interface{}) | ||
acc_tools.PrintResource(t, flavors) | ||
th.AssertEquals(t, len(flavors), 2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* Package restclient provides generic REST functions. | ||
Example of a GET request | ||
getURL := computeClient.ServiceURL("flavors", flavorID) | ||
getRes := restclient.Get(computeClient, getURL, nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
flavorResult, err = getRes.Extract() | ||
if err != nil { | ||
panic(err) | ||
} | ||
flavor := flavorResult["flavor"].(map[string]interface{}) | ||
fmt.Printf("%v\n", flavor) | ||
Example of a POST request | ||
flavorOpts := map[string]interface{}{ | ||
"name": "some-name", | ||
"ram": 512, | ||
"vcpus": 1, | ||
"disk": 5, | ||
"id": "some-id", | ||
} | ||
postOpts := &restclient.PostOpts{ | ||
Params: map[string]interface{}{"flavor": flavorOpts}, | ||
} | ||
postURL := computeClient.ServiceURL("flavors") | ||
postRes := restclient.Post(computeClient, postURL, postOpts) | ||
if err != nil { | ||
panic(err) | ||
} | ||
flavorResult, err := postRes.Extract() | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("%v\n", flavor) | ||
Example of a DELETE Request | ||
deleteURL := computeClient.ServiceURL("flavors", "flavor-id") | ||
deleteRes := restclient.Delete(computeClient, deleteURL, nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = deleteRes.ExtractErr() | ||
if err != nil { | ||
panic(err) | ||
} | ||
*/ | ||
package restclient |
Oops, something went wrong.