Skip to content

Commit 862dab9

Browse files
committed
Add Update support in V3 volume type
Add Update support in V3 volume type
1 parent f94909f commit 862dab9

5 files changed

Lines changed: 65 additions & 4 deletions

File tree

acceptance/openstack/blockstorage/v3/volumetypes_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/gophercloud/gophercloud/acceptance/clients"
99
"github.com/gophercloud/gophercloud/acceptance/tools"
1010
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumetypes"
11+
th "github.com/gophercloud/gophercloud/testhelper"
1112
)
1213

1314
func TestVolumeTypesList(t *testing.T) {
@@ -36,7 +37,7 @@ func TestVolumeTypesList(t *testing.T) {
3637
}
3738

3839
if len(allVolumeTypes) > 0 {
39-
vt, err := volumetypes.Get(client, allVolumes[0].ID).Extract()
40+
vt, err := volumetypes.Get(client, allVolumeTypes[0].ID).Extract()
4041
if err != nil {
4142
t.Fatalf("Error retrieving volume type: %v", err)
4243
}
@@ -65,3 +66,30 @@ func TestVolumeTypesCreate(t *testing.T) {
6566

6667
tools.PrintResource(t, vt)
6768
}
69+
70+
func TestVolumeTypesUpdate(t *testing.T) {
71+
client, err := clients.NewBlockStorageV3Client()
72+
if err != nil {
73+
t.Fatalf("Unable to create a blockstorage client: %v", err)
74+
}
75+
76+
createOpts := volumetypes.CreateOpts{
77+
Name: "create_for_update",
78+
PublicAccess: true,
79+
ExtraSpecs: map[string]string{"volume_backend_name": "fake_backend_name"},
80+
Description: "create_for_update",
81+
}
82+
83+
vt, err := volumetypes.Create(client, createOpts).Extract()
84+
if err != nil {
85+
t.Fatalf("Unable to create volumetype: %v", err)
86+
}
87+
88+
newVT, err := volumetypes.Update(client, vt.ID, volumetypes.UpdateOpts{Name: "updated_volume_type"}).Extract()
89+
if err != nil {
90+
t.Fatalf("Unable to update volumetype: %v", err)
91+
}
92+
th.AssertEquals(t, "updated_volume_type", newVT.Name)
93+
94+
tools.PrintResource(t, vt)
95+
}

openstack/blockstorage/v3/volumetypes/requests.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,38 @@ func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pa
9595
return VolumeTypePage{pagination.LinkedPageBase{PageResult: r}}
9696
})
9797
}
98+
99+
// UpdateOptsBuilder allows extensions to add additional parameters to the
100+
// Update request.
101+
type UpdateOptsBuilder interface {
102+
ToVolumeTypeUpdateMap() (map[string]interface{}, error)
103+
}
104+
105+
// UpdateOpts contain options for updating an existing Volume Type. This object is passed
106+
// to the volumetypes.Update function. For more information about the parameters, see
107+
// the Volume Type object.
108+
type UpdateOpts struct {
109+
Name string `json:"name,omitempty"`
110+
Description string `json:"description,omitempty"`
111+
IsPublic bool `json:"is_public"`
112+
}
113+
114+
// ToVolumeUpdateMap assembles a request body based on the contents of an
115+
// UpdateOpts.
116+
func (opts UpdateOpts) ToVolumeTypeUpdateMap() (map[string]interface{}, error) {
117+
return gophercloud.BuildRequestBody(opts, "volume_type")
118+
}
119+
120+
// Update will update the Volume Type with provided information. To extract the updated
121+
// Volume Type from the response, call the Extract method on the UpdateResult.
122+
func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
123+
b, err := opts.ToVolumeTypeUpdateMap()
124+
if err != nil {
125+
r.Err = err
126+
return
127+
}
128+
_, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
129+
OkCodes: []int{200},
130+
})
131+
return
132+
}

openstack/blockstorage/v3/volumetypes/results.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,3 @@ type CreateResult struct {
8787
type UpdateResult struct {
8888
commonResult
8989
}
90-

openstack/blockstorage/v3/volumetypes/testing/fixtures.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,3 @@ func MockUpdateResponse(t *testing.T) {
144144
}`)
145145
})
146146
}
147-

openstack/blockstorage/v3/volumetypes/urls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ func createURL(c *gophercloud.ServiceClient) string {
1515
}
1616

1717
func updateURL(c *gophercloud.ServiceClient, id string) string {
18-
return c.ServiceURL("types")
18+
return c.ServiceURL("types", id)
1919
}

0 commit comments

Comments
 (0)