Skip to content

Commit d952b0b

Browse files
committed
Flavor Extra Spec Update
1 parent c364b06 commit d952b0b

7 files changed

Lines changed: 100 additions & 1 deletion

File tree

acceptance/openstack/compute/v2/flavors_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ func TestFlavorExtraSpecsCRUD(t *testing.T) {
181181
t.Fatalf("Unable to delete ExtraSpec: %v\n", err)
182182
}
183183

184+
updateOpts := flavors.ExtraSpecsOpts{
185+
"hw:cpu_thread_policy": "CPU-THREAD-POLICY-BETTER",
186+
}
187+
updatedExtraSpec, err := flavors.UpdateExtraSpec(client, flavor.ID, "hw:cpu_thread_policy", updateOpts).Extract()
188+
if err != nil {
189+
t.Fatalf("Unable to update flavor extra_specs: %v", err)
190+
}
191+
tools.PrintResource(t, updatedExtraSpec)
192+
184193
allExtraSpecs, err := flavors.ListExtraSpecs(client, flavor.ID).Extract()
185194
if err != nil {
186195
t.Fatalf("Unable to get flavor extra_specs: %v", err)

openstack/compute/v2/flavors/doc.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ Example to Get Extra Specs for a Flavor
9999
100100
fmt.Printf("%+v", extraSpecs)
101101
102+
Example to Update Extra Specs for a Flavor
103+
104+
flavorID := "e91758d6-a54a-4778-ad72-0c73a1cb695b"
105+
106+
updateOpts := flavors.ExtraSpecsOpts{
107+
"hw:cpu_thread_policy": "CPU-THREAD-POLICY-UPDATED",
108+
}
109+
updatedExtraSpec, err := flavors.UpdateExtraSpec(computeClient, flavorID, "hw:cpu_thread_policy", updateOpts).Extract()
110+
if err != nil {
111+
panic(err)
112+
}
113+
114+
fmt.Printf("%+v", updatedExtraSpec)
115+
102116
Example to Delete an Extra Spec for a Flavor
103117
104118
flavorID := "e91758d6-a54a-4778-ad72-0c73a1cb695b"

openstack/compute/v2/flavors/requests.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,31 @@ func CreateExtraSpecs(client *gophercloud.ServiceClient, flavorID string, opts C
231231
return
232232
}
233233

234+
// UpdateExtraSpecOptsBuilder allows extensions to add additional parameters to the
235+
// Update request.
236+
type UpdateExtraSpecOptsBuilder interface {
237+
ToExtraSpecUpdateMap() (map[string]string, error)
238+
}
239+
240+
// ToExtraSpecUpdateMap assembles a body for an Update request based on the
241+
// contents of a ExtraSpecOpts.
242+
func (opts ExtraSpecsOpts) ToExtraSpecUpdateMap() (map[string]string, error) {
243+
return opts, nil
244+
}
245+
246+
// UpdateExtraSpec will updates the value of the specified flavor's extra spec for the given key.
247+
func UpdateExtraSpec(client *gophercloud.ServiceClient, flavorID, key string, opts UpdateExtraSpecOptsBuilder) (r UpdateExtraSpecResult) {
248+
b, err := opts.ToExtraSpecUpdateMap()
249+
if err != nil {
250+
r.Err = err
251+
return
252+
}
253+
_, r.Err = client.Put(extraSpecUpdateURL(client, flavorID, key), b, &r.Body, &gophercloud.RequestOpts{
254+
OkCodes: []int{200},
255+
})
256+
return
257+
}
258+
234259
// DeleteExtraSpec will delete the key-value pair with the given key for the given
235260
// flavor ID.
236261
func DeleteExtraSpec(client *gophercloud.ServiceClient, flavorID, key string) (r DeleteExtraSpecResult) {

openstack/compute/v2/flavors/results.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ type GetExtraSpecResult struct {
223223
extraSpecResult
224224
}
225225

226+
// UpdateExtraSpecResult contains the result of an Update operation. Call its
227+
// Extract method to interpret it as a map[string]interface.
228+
type UpdateExtraSpecResult struct {
229+
extraSpecResult
230+
}
231+
226232
// DeleteExtraSpecResult contains the result of a Delete operation. Call its
227233
// ExtractErr method to determine if the call succeeded or failed.
228234
type DeleteExtraSpecResult struct {

openstack/compute/v2/flavors/testing/fixtures.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@ const ExtraSpecsGetBody = `
1919
}
2020
`
2121

22-
// ExtraSpecGetBody provides a GET result of a particular extra_spec for a flavor
22+
// GetExtraSpecBody provides a GET result of a particular extra_spec for a flavor
2323
const GetExtraSpecBody = `
2424
{
2525
"hw:cpu_policy": "CPU-POLICY"
2626
}
2727
`
2828

29+
// UpdatedExtraSpecBody provides an PUT result of a particular updated extra_spec for a flavor
30+
const UpdatedExtraSpecBody = `
31+
{
32+
"hw:cpu_policy": "CPU-POLICY-2"
33+
}
34+
`
35+
2936
// ExtraSpecs is the expected extra_specs returned from GET on a flavor's extra_specs
3037
var ExtraSpecs = map[string]string{
3138
"hw:cpu_policy": "CPU-POLICY",
@@ -37,6 +44,11 @@ var ExtraSpec = map[string]string{
3744
"hw:cpu_policy": "CPU-POLICY",
3845
}
3946

47+
// UpdatedExtraSpec is the expected extra_spec returned from PUT on a flavor's extra_specs
48+
var UpdatedExtraSpec = map[string]string{
49+
"hw:cpu_policy": "CPU-POLICY-2",
50+
}
51+
4052
func HandleExtraSpecsListSuccessfully(t *testing.T) {
4153
th.Mux.HandleFunc("/flavors/1/os-extra_specs", func(w http.ResponseWriter, r *http.Request) {
4254
th.TestMethod(t, r, "GET")
@@ -79,6 +91,21 @@ func HandleExtraSpecsCreateSuccessfully(t *testing.T) {
7991
})
8092
}
8193

94+
func HandleExtraSpecUpdateSuccessfully(t *testing.T) {
95+
th.Mux.HandleFunc("/flavors/1/os-extra_specs/hw:cpu_policy", func(w http.ResponseWriter, r *http.Request) {
96+
th.TestMethod(t, r, "PUT")
97+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
98+
th.TestHeader(t, r, "Accept", "application/json")
99+
th.TestJSONRequest(t, r, `{
100+
"hw:cpu_policy": "CPU-POLICY-2"
101+
}`)
102+
103+
w.Header().Set("Content-Type", "application/json")
104+
w.WriteHeader(http.StatusOK)
105+
fmt.Fprintf(w, UpdatedExtraSpecBody)
106+
})
107+
}
108+
82109
func HandleExtraSpecDeleteSuccessfully(t *testing.T) {
83110
th.Mux.HandleFunc("/flavors/1/os-extra_specs/hw:cpu_policy", func(w http.ResponseWriter, r *http.Request) {
84111
th.TestMethod(t, r, "DELETE")

openstack/compute/v2/flavors/testing/requests_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,20 @@ func TestFlavorExtraSpecsCreate(t *testing.T) {
337337
th.CheckDeepEquals(t, expected, actual)
338338
}
339339

340+
func TestFlavorExtraSpecUpdate(t *testing.T) {
341+
th.SetupHTTP()
342+
defer th.TeardownHTTP()
343+
HandleExtraSpecUpdateSuccessfully(t)
344+
345+
updateOpts := flavors.ExtraSpecsOpts{
346+
"hw:cpu_policy": "CPU-POLICY-2",
347+
}
348+
expected := UpdatedExtraSpec
349+
actual, err := flavors.UpdateExtraSpec(fake.ServiceClient(), "1", "hw:cpu_policy", updateOpts).Extract()
350+
th.AssertNoErr(t, err)
351+
th.CheckDeepEquals(t, expected, actual)
352+
}
353+
340354
func TestFlavorExtraSpecDelete(t *testing.T) {
341355
th.SetupHTTP()
342356
defer th.TeardownHTTP()

openstack/compute/v2/flavors/urls.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ func extraSpecsCreateURL(client *gophercloud.ServiceClient, id string) string {
4040
return client.ServiceURL("flavors", id, "os-extra_specs")
4141
}
4242

43+
func extraSpecUpdateURL(client *gophercloud.ServiceClient, id, key string) string {
44+
return client.ServiceURL("flavors", id, "os-extra_specs", key)
45+
}
46+
4347
func extraSpecDeleteURL(client *gophercloud.ServiceClient, id, key string) string {
4448
return client.ServiceURL("flavors", id, "os-extra_specs", key)
4549
}

0 commit comments

Comments
 (0)