Skip to content

Commit b7a71c0

Browse files
committed
Add Update support in V3 volume type
Add Update support in V3 volume type
1 parent a27b5be commit b7a71c0

File tree

6 files changed

+67
-7
lines changed

6 files changed

+67
-7
lines changed

acceptance/openstack/blockstorage/v3/volumetypes_test.go

Lines changed: 12 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) {
@@ -45,7 +46,7 @@ func TestVolumeTypesList(t *testing.T) {
4546
}
4647
}
4748

48-
func TestVolumeTypesCreateDestroy(t *testing.T) {
49+
func TestVolumeTypesCRUD(t *testing.T) {
4950
client, err := clients.NewBlockStorageV3Client()
5051
if err != nil {
5152
t.Fatalf("Unable to create a blockstorage client: %v", err)
@@ -66,4 +67,14 @@ func TestVolumeTypesCreateDestroy(t *testing.T) {
6667
tools.PrintResource(t, vt)
6768

6869
defer volumetypes.Delete(client, vt.ID)
70+
71+
var isPublic = false
72+
73+
newVT, err := volumetypes.Update(client, vt.ID, volumetypes.UpdateOpts{
74+
Name: "updated_volume_type",
75+
IsPublic: &isPublic,
76+
}).Extract()
77+
th.AssertEquals(t, "updated_volume_type", newVT.Name)
78+
79+
th.AssertEquals(t, false, newVT.IsPublic)
6980
}

openstack/blockstorage/v3/volumetypes/doc.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Example to list Volume Types
1919
2020
Example to show a Volume Type
2121
22-
typeID := "0fe36e73809d46aeae6705c39077b1b3"
22+
typeID := "7ffaca22-f646-41d4-b79d-d7e4452ef8cc"
2323
volumeType, err := volumetypes.Get(client, typeID).Extract()
2424
if err != nil{
2525
panic(err)
@@ -38,13 +38,26 @@ Example to create a Volume Type
3838
}
3939
fmt.Println(volumeType)
4040
41-
Example to delete a Volume TYpe
41+
Example to delete a Volume Type
4242
43-
typeID := "0fe36e73809d46aeae6705c39077b1b3"
43+
typeID := "7ffaca22-f646-41d4-b79d-d7e4452ef8cc"
4444
err := volumetypes.Delete(client, typeID).ExtractErr()
4545
if err != nil{
4646
panic(err)
4747
}
48+
49+
Example to update a Volume Type
50+
51+
typeID := "7ffaca22-f646-41d4-b79d-d7e4452ef8cc"
52+
volumetype, err = volumetypes.Update(client, typeID, volumetypes.UpdateOpts{
53+
Name: "volume_type_002",
54+
Description:"description_002",
55+
IsPublic:false,
56+
}).Extract()
57+
if err != nil{
58+
panic(err)
59+
}
60+
fmt.Println(volumetype)
4861
*/
4962

5063
package volumetypes

openstack/blockstorage/v3/volumetypes/requests.go

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

openstack/blockstorage/v3/volumetypes/results.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,3 @@ type DeleteResult struct {
9292
type UpdateResult struct {
9393
commonResult
9494
}
95-

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ func TestUpdate(t *testing.T) {
9898

9999
MockUpdateResponse(t)
100100

101-
options := volumetypes.UpdateOpts{Name: "vol-type-002"}
101+
var isPublic = true
102+
options := volumetypes.UpdateOpts{Name: "vol-type-002", IsPublic: &isPublic}
102103
v, err := volumetypes.Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
103104
th.AssertNoErr(t, err)
104105
th.CheckEquals(t, "vol-type-002", v.Name)
106+
th.CheckEquals(t, true, v.IsPublic)
105107
}

openstack/blockstorage/v3/volumetypes/urls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ func deleteURL(c *gophercloud.ServiceClient, id string) string {
1919
}
2020

2121
func updateURL(c *gophercloud.ServiceClient, id string) string {
22-
return c.ServiceURL("types")
22+
return c.ServiceURL("types", id)
2323
}

0 commit comments

Comments
 (0)