Adding more validation when creating volumes
diff --git a/rackspace/blockstorage/v1/volumes/delegate.go b/rackspace/blockstorage/v1/volumes/delegate.go
index a0b2380..4f14454 100644
--- a/rackspace/blockstorage/v1/volumes/delegate.go
+++ b/rackspace/blockstorage/v1/volumes/delegate.go
@@ -1,11 +1,25 @@
package volumes
import (
+ "fmt"
+
"github.com/rackspace/gophercloud"
os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
"github.com/rackspace/gophercloud/pagination"
)
+type CreateOpts struct {
+ os.CreateOpts
+}
+
+func (opts CreateOpts) ToVolumeCreateMap() (map[string]interface{}, error) {
+ if opts.Size < 75 || opts.Size > 1024 {
+ return nil, fmt.Errorf("Size field must be between 75 and 1024")
+ }
+
+ return opts.CreateOpts.ToVolumeCreateMap()
+}
+
// Create will create a new Volume based on the values in CreateOpts. To extract
// the Volume object from the response, call the Extract method on the
// CreateResult.
diff --git a/rackspace/blockstorage/v1/volumes/delegate_test.go b/rackspace/blockstorage/v1/volumes/delegate_test.go
index 3c50d21..56b86bd 100644
--- a/rackspace/blockstorage/v1/volumes/delegate_test.go
+++ b/rackspace/blockstorage/v1/volumes/delegate_test.go
@@ -65,14 +65,25 @@
os.MockCreateResponse(t)
- options := os.CreateOpts{Size: 4}
- n, err := Create(fake.ServiceClient(), options).Extract()
+ n, err := Create(fake.ServiceClient(), CreateOpts{os.CreateOpts{Size: 40}}).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Size, 4)
th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
}
+func TestSizeRange(t *testing.T) {
+ _, err := Create(fake.ServiceClient(), CreateOpts{os.CreateOpts{Size: 1}}).Extract()
+ if err == nil {
+ t.Fatalf("Expected error, got none")
+ }
+
+ _, err = Create(fake.ServiceClient(), CreateOpts{os.CreateOpts{Size: 2000}}).Extract()
+ if err == nil {
+ t.Fatalf("Expected error, got none")
+ }
+}
+
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()