Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 3d4d1de

Browse files
author
Dongsu Park
committed
Merge pull request #1703 from endocode/dongsu/dynamic-metadata
api,functional: dynamic metadata - fix bugs and add functional tests
2 parents 868a18f + 0823b91 commit 3d4d1de

4 files changed

Lines changed: 491 additions & 10 deletions

File tree

api/machines.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ type machinesResource struct {
4444

4545
type machineMetadataOp struct {
4646
Operation string `json:"op"`
47-
Path string
48-
Value string
47+
Path string `json:"path"`
48+
Value struct {
49+
Value string `json:"value"`
50+
}
4951
}
5052

5153
func (mr *machinesResource) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
@@ -100,7 +102,7 @@ func (mr *machinesResource) patch(rw http.ResponseWriter, req *http.Request) {
100102
return
101103
}
102104

103-
if op.Operation != "remove" && len(op.Value) == 0 {
105+
if op.Operation != "remove" && len(op.Value.Value) == 0 {
104106
sendError(rw, http.StatusBadRequest, errors.New("invalid value: add and replace require a value"))
105107
return
106108
}
@@ -119,7 +121,7 @@ func (mr *machinesResource) patch(rw http.ResponseWriter, req *http.Request) {
119121
return
120122
}
121123
} else {
122-
err := mr.cAPI.SetMachineMetadata(machID, key, op.Value)
124+
err := mr.cAPI.SetMachineMetadata(machID, key, op.Value.Value)
123125
if err != nil {
124126
sendError(rw, http.StatusInternalServerError, err)
125127
return

api/machines_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ func TestExtractMachinePage(t *testing.T) {
146146

147147
func TestMachinesPatchAddModify(t *testing.T) {
148148
reqBody := `
149-
[{"op": "add", "path": "/XXX/metadata/foo", "value": "bar"},
150-
{"op": "replace", "path": "/YYY/metadata/ping", "value": "splat"}]
149+
[{"op": "add", "path": "/XXX/metadata/foo", "value": { "value": "bar" }},
150+
{"op": "replace", "path": "/YYY/metadata/ping", "value": { "value": "splat" }}]
151151
`
152152

153153
resource, rw := fakeMachinesSetup()
@@ -218,7 +218,7 @@ func TestMachinesPatchDelete(t *testing.T) {
218218

219219
func TestMachinesPatchBadOp(t *testing.T) {
220220
reqBody := `
221-
[{"op": "noop", "path": "/XXX/metadata/foo", "value": "bar"}]
221+
[{"op": "noop", "path": "/XXX/metadata/foo", "value": { "value": "bar" }}]
222222
`
223223

224224
resource, rw := fakeMachinesSetup()
@@ -235,7 +235,7 @@ func TestMachinesPatchBadOp(t *testing.T) {
235235

236236
func TestMachinesPatchBadPath(t *testing.T) {
237237
reqBody := `
238-
[{"op": "add", "path": "/XXX/foo", "value": "bar"}]
238+
[{"op": "add", "path": "/XXX/foo", "value": { "value": "bar" }}]
239239
`
240240

241241
resource, rw := fakeMachinesSetup()

0 commit comments

Comments
 (0)