unit tests; consistency with the other OpenStack services
diff --git a/openstack/blockstorage/v1/snapshots/requests.go b/openstack/blockstorage/v1/snapshots/requests.go
index 5e9256d..f823b3d 100644
--- a/openstack/blockstorage/v1/snapshots/requests.go
+++ b/openstack/blockstorage/v1/snapshots/requests.go
@@ -1,9 +1,11 @@
package snapshots
import (
- "github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/utils"
+ "github.com/rackspace/gophercloud/pagination"
+
+ "github.com/racker/perigee"
)
type CreateOpts struct {
@@ -14,7 +16,7 @@
VolumeID string
}
-func Create(client *gophercloud.ServiceClient, opts CreateOpts) (*Snapshot, error) {
+func Create(client *gophercloud.ServiceClient, opts *CreateOpts) CreateResult {
type snapshot struct {
Description *string `json:"display_description,omitempty"`
Force bool `json:"force,omitempty"`
@@ -37,38 +39,86 @@
reqBody.Snapshot.Force = opts.Force
- type response struct {
- Snapshot Snapshot `json:"snapshot"`
- }
-
- var respBody response
-
- _, err := perigee.Request("POST", snapshotsURL(client), perigee.Options{
+ var res CreateResult
+ _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{
MoreHeaders: client.Provider.AuthenticatedHeaders(),
OkCodes: []int{200, 201},
ReqBody: &reqBody,
- Results: &respBody,
+ Results: &res.Resp,
})
- if err != nil {
- return nil, err
- }
-
- return &respBody.Snapshot, nil
+ return res
}
-func Delete(client *gophercloud.ServiceClient, id string) error {
- _, err := perigee.Request("Delete", snapshotURL(client, id), perigee.Options{
+func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
+ var res DeleteResult
+ _, res.Err = perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
MoreHeaders: client.Provider.AuthenticatedHeaders(),
+ OkCodes: []int{204},
})
- return err
+ return res
}
func Get(client *gophercloud.ServiceClient, id string) GetResult {
- var gr GetResult
- _, err := perigee.Request("GET", snapshotURL(client, id), perigee.Options{
- Results: &gr.r,
+ var res GetResult
+ _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
+ Results: &res.Resp,
MoreHeaders: client.Provider.AuthenticatedHeaders(),
+ OkCodes: []int{200},
})
- gr.err = err
- return gr
+ return res
+}
+
+type ListOpts struct {
+ Name string `q:"display_name"`
+ Status string `q:"status"`
+ VolumeID string `q:"volume_id"`
+}
+
+func List(client *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager {
+ url := listURL(client)
+ if opts != nil {
+ query, err := gophercloud.BuildQueryString(opts)
+ if err != nil {
+ return pagination.Pager{Err: err}
+ }
+ url += query.String()
+ }
+
+ createPage := func(r pagination.LastHTTPResponse) pagination.Page {
+ return ListResult{pagination.SinglePageBase(r)}
+ }
+ return pagination.NewPager(client, url, createPage)
+}
+
+type UpdateOpts struct {
+ Description string
+ Name string
+}
+
+func Update(client *gophercloud.ServiceClient, id string, opts *UpdateOpts) UpdateResult {
+ type update struct {
+ Description *string `json:"display_description,omitempty"`
+ Name *string `json:"display_name,omitempty"`
+ }
+
+ type request struct {
+ Volume update `json:"snapshot"`
+ }
+
+ reqBody := request{
+ Volume: update{},
+ }
+
+ reqBody.Volume.Description = utils.MaybeString(opts.Description)
+ reqBody.Volume.Name = utils.MaybeString(opts.Name)
+
+ var res UpdateResult
+
+ _, res.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{
+ MoreHeaders: client.Provider.AuthenticatedHeaders(),
+ OkCodes: []int{200},
+ ReqBody: &reqBody,
+ Results: &res.Resp,
+ })
+ return res
}