Skip to content

Commit

Permalink
Support for generic REST client
Browse files Browse the repository at this point in the history
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
jtopjian committed Jun 20, 2018
1 parent afce78e commit f2b3f5a
Show file tree
Hide file tree
Showing 5 changed files with 606 additions and 0 deletions.
91 changes: 91 additions & 0 deletions acceptance/openstack/restclient/restclient_test.go
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)
}
59 changes: 59 additions & 0 deletions openstack/restclient/doc.go
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
Loading

0 comments on commit f2b3f5a

Please sign in to comment.