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

Commit

Permalink
Merge pull request #1703 from endocode/dongsu/dynamic-metadata
Browse files Browse the repository at this point in the history
api,functional: dynamic metadata - fix bugs and add functional tests
  • Loading branch information
Dongsu Park authored Nov 11, 2016
2 parents 868a18f + 0823b91 commit 79001ad
Show file tree
Hide file tree
Showing 4 changed files with 491 additions and 10 deletions.
10 changes: 6 additions & 4 deletions api/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ type machinesResource struct {

type machineMetadataOp struct {
Operation string `json:"op"`
Path string
Value string
Path string `json:"path"`
Value struct {
Value string `json:"value"`
}
}

func (mr *machinesResource) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -100,7 +102,7 @@ func (mr *machinesResource) patch(rw http.ResponseWriter, req *http.Request) {
return
}

if op.Operation != "remove" && len(op.Value) == 0 {
if op.Operation != "remove" && len(op.Value.Value) == 0 {
sendError(rw, http.StatusBadRequest, errors.New("invalid value: add and replace require a value"))
return
}
Expand All @@ -119,7 +121,7 @@ func (mr *machinesResource) patch(rw http.ResponseWriter, req *http.Request) {
return
}
} else {
err := mr.cAPI.SetMachineMetadata(machID, key, op.Value)
err := mr.cAPI.SetMachineMetadata(machID, key, op.Value.Value)
if err != nil {
sendError(rw, http.StatusInternalServerError, err)
return
Expand Down
8 changes: 4 additions & 4 deletions api/machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ func TestExtractMachinePage(t *testing.T) {

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

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

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

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

func TestMachinesPatchBadPath(t *testing.T) {
reqBody := `
[{"op": "add", "path": "/XXX/foo", "value": "bar"}]
[{"op": "add", "path": "/XXX/foo", "value": { "value": "bar" }}]
`

resource, rw := fakeMachinesSetup()
Expand Down
Loading

0 comments on commit 79001ad

Please sign in to comment.