Rename to v1 dir and add in more delegate logic
diff --git a/rackspace/blockstorage/snapshots/delegate_test.go b/rackspace/blockstorage/snapshots/delegate_test.go
deleted file mode 100644
index 1e77fbd..0000000
--- a/rackspace/blockstorage/snapshots/delegate_test.go
+++ /dev/null
@@ -1 +0,0 @@
-package snapshots
diff --git a/rackspace/blockstorage/snapshots/results.go b/rackspace/blockstorage/snapshots/results.go
index 5fc4986..c2c608e 100644
--- a/rackspace/blockstorage/snapshots/results.go
+++ b/rackspace/blockstorage/snapshots/results.go
@@ -3,6 +3,8 @@
import (
"github.com/rackspace/gophercloud"
os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
+
+ "github.com/mitchellh/mapstructure"
)
// Status is the type used to represent a snapshot's status
@@ -66,3 +68,18 @@
Common os.GetResult
commonResult
}
+
+// Extract will get the Snapshot object out of the commonResult object.
+func (r commonResult) Extract() (*Snapshot, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ var res struct {
+ Snapshot *Snapshot `json:"snapshot"`
+ }
+
+ err := mapstructure.Decode(r.Resp, &res)
+
+ return res.Snapshot, err
+}
diff --git a/rackspace/blockstorage/snapshots/delegate.go b/rackspace/blockstorage/v1/snapshots/delegate.go
similarity index 95%
rename from rackspace/blockstorage/snapshots/delegate.go
rename to rackspace/blockstorage/v1/snapshots/delegate.go
index b835136..fa87360 100644
--- a/rackspace/blockstorage/snapshots/delegate.go
+++ b/rackspace/blockstorage/v1/snapshots/delegate.go
@@ -57,7 +57,7 @@
// extract the Snapshot object from the response, call the Extract method on the
// CreateResult.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
- return CreateResult{Common: os.Create(client, opts)}
+ return CreateResult{os.Create(client, opts)}
}
// Delete will delete the existing Snapshot with the provided ID.
@@ -68,7 +68,7 @@
// Get retrieves the Snapshot with the provided ID. To extract the Snapshot
// object from the response, call the Extract method on the GetResult.
func Get(client *gophercloud.ServiceClient, id string) GetResult {
- return GetResult{Common: os.Get(client, id)}
+ return GetResult{os.Get(client, id)}
}
// List returns Snapshots.
diff --git a/rackspace/blockstorage/v1/snapshots/delegate_test.go b/rackspace/blockstorage/v1/snapshots/delegate_test.go
new file mode 100644
index 0000000..45ab0ac
--- /dev/null
+++ b/rackspace/blockstorage/v1/snapshots/delegate_test.go
@@ -0,0 +1,85 @@
+package snapshots
+
+import (
+ "testing"
+
+ os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
+ "github.com/rackspace/gophercloud/pagination"
+ th "github.com/rackspace/gophercloud/testhelper"
+ fake "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func TestList(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ os.MockListResponse(t)
+
+ count := 0
+
+ List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+ actual, err := ExtractSnapshots(page)
+ if err != nil {
+ t.Errorf("Failed to extract snapshots: %v", err)
+ return false, err
+ }
+
+ expected := []Snapshot{
+ Snapshot{
+ ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
+ Name: "snapshot-001",
+ },
+ Snapshot{
+ ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
+ Name: "snapshot-002",
+ },
+ }
+
+ th.CheckDeepEquals(t, expected, actual)
+
+ return true, nil
+ })
+
+ if count != 1 {
+ t.Errorf("Expected 1 page, got %d", count)
+ }
+}
+
+func TestGet(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ os.MockGetResponse(t)
+
+ v, err := Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
+ th.AssertNoErr(t, err)
+
+ th.AssertEquals(t, v.Name, "snapshot-001")
+ th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
+}
+
+func TestCreate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ os.MockCreateResponse(t)
+
+ options := &CreateOpts{VolumeID: "1234", Name: "snapshot-001"}
+ n, err := Create(fake.ServiceClient(), options).Extract()
+ th.AssertNoErr(t, err)
+
+ th.AssertEquals(t, n.VolumeID, "1234")
+ th.AssertEquals(t, n.Name, "snapshot-001")
+ th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
+}
+
+func TestDelete(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ os.MockDeleteResponse(t)
+
+ err := Delete(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
+ th.AssertNoErr(t, err)
+}
diff --git a/rackspace/blockstorage/v1/snapshots/results.go b/rackspace/blockstorage/v1/snapshots/results.go
new file mode 100644
index 0000000..b35e719
--- /dev/null
+++ b/rackspace/blockstorage/v1/snapshots/results.go
@@ -0,0 +1,103 @@
+package snapshots
+
+import (
+ "github.com/rackspace/gophercloud"
+ os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
+ "github.com/rackspace/gophercloud/pagination"
+
+ "github.com/mitchellh/mapstructure"
+)
+
+// Status is the type used to represent a snapshot's status
+type Status string
+
+// Constants to use for supported statuses
+const (
+ Creating Status = "CREATING"
+ Available Status = "AVAILABLE"
+ Deleting Status = "DELETING"
+ Error Status = "ERROR"
+ DeleteError Status = "ERROR_DELETING"
+)
+
+// Snapshot is the Rackspace representation of an external block storage device.
+type Snapshot struct {
+ // The timestamp when this snapshot was created.
+ CreatedAt string `mapstructure:"created_at"`
+
+ // The human-readable description for this snapshot.
+ Description string `mapstructure:"display_description"`
+
+ // The human-readable name for this snapshot.
+ Name string `mapstructure:"display_name"`
+
+ // The UUID for this snapshot.
+ ID string `mapstructure:"id"`
+
+ // The random metadata associated with this snapshot. Note: unlike standard
+ // OpenStack snapshots, this cannot actually be set.
+ Metadata map[string]string `mapstructure:"metadata"`
+
+ // Indicates the current progress of the snapshot's backup procedure.
+ Progress string `mapstructure:"os-extended-snapshot-attributes:progress"`
+
+ // The project ID.
+ ProjectID string `mapstructure:"os-extended-snapshot-attributes:project_id"`
+
+ // The size of the volume which this snapshot backs up.
+ Size int `mapstructure:"size"`
+
+ // The status of the snapshot.
+ Status Status `mapstructure:"status"`
+
+ // The ID of the volume which this snapshot seeks to back up.
+ VolumeID string `mapstructure:"volume_id"`
+}
+
+type commonResult struct {
+ gophercloud.CommonResult
+}
+
+// CreateResult represents the result of a create operation
+type CreateResult struct {
+ os.CreateResult
+}
+
+// GetResult represents the result of a get operation
+type GetResult struct {
+ os.GetResult
+}
+
+func commonExtract(resp map[string]interface{}, err error) (*Snapshot, error) {
+ if err != nil {
+ return nil, err
+ }
+
+ var respStruct struct {
+ Snapshot *Snapshot `json:"snapshot"`
+ }
+
+ err = mapstructure.Decode(resp, &respStruct)
+
+ return respStruct.Snapshot, err
+}
+
+// Extract will get the Snapshot object out of the GetResult object.
+func (r GetResult) Extract() (*Snapshot, error) {
+ return commonExtract(r.Resp, r.Err)
+}
+
+// Extract will get the Snapshot object out of the CreateResult object.
+func (r CreateResult) Extract() (*Snapshot, error) {
+ return commonExtract(r.Resp, r.Err)
+}
+
+// ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
+func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) {
+ var response struct {
+ Snapshots []Snapshot `json:"snapshots"`
+ }
+
+ err := mapstructure.Decode(page.(os.ListResult).Body, &response)
+ return response.Snapshots, err
+}