Skip to content

Commit

Permalink
Fix for UpdateFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdebrie committed Mar 2, 2018
1 parent 464619f commit af4eeb9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
2 changes: 1 addition & 1 deletion function/service.go
Expand Up @@ -3,7 +3,7 @@ package function
// Service represents service for managing functions.
type Service interface {
RegisterFunction(fn *Function) (*Function, error)
UpdateFunction(space string, fn *Function) (*Function, error)
UpdateFunction(fn *Function) (*Function, error)
GetFunction(space string, id ID) (*Function, error)
GetFunctions(space string) (Functions, error)
DeleteFunction(space string, id ID) error
Expand Down
6 changes: 3 additions & 3 deletions httpapi/httpapi.go
Expand Up @@ -129,9 +129,9 @@ func (h HTTPAPI) updateFunction(w http.ResponseWriter, r *http.Request, params h
return
}

space := params.ByName("space")
fn.Space = params.ByName("space")
fn.ID = function.ID(params.ByName("id"))
output, err := h.Functions.UpdateFunction(space, fn)
output, err := h.Functions.UpdateFunction(fn)
if err != nil {
if _, ok := err.(*function.ErrFunctionValidation); ok {
w.WriteHeader(http.StatusBadRequest)
Expand All @@ -146,7 +146,7 @@ func (h HTTPAPI) updateFunction(w http.ResponseWriter, r *http.Request, params h
encoder.Encode(output)
}

metricFunctionUpdateRequests.WithLabelValues(space).Inc()
metricFunctionUpdateRequests.WithLabelValues(fn.Space).Inc()
}

func (h HTTPAPI) deleteFunction(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
Expand Down
12 changes: 6 additions & 6 deletions libkv/function.go
Expand Up @@ -53,14 +53,14 @@ func (service Service) RegisterFunction(fn *function.Function) (*function.Functi
}

// UpdateFunction updates function configuration.
func (service Service) UpdateFunction(space string, fn *function.Function) (*function.Function, error) {
_, err := service.FunctionStore.Get(FunctionKey{space, fn.ID}.String(), &store.ReadOptions{Consistent: true})
if err != nil {
return nil, &function.ErrFunctionNotFound{ID: fn.ID}
func (service Service) UpdateFunction(fn *function.Function) (*function.Function, error) {
if err := service.validateFunction(fn); err != nil {
return nil, err
}

if err = service.validateFunction(fn); err != nil {
return nil, err
_, err := service.FunctionStore.Get(FunctionKey{fn.Space, fn.ID}.String(), &store.ReadOptions{Consistent: true})
if err != nil {
return nil, &function.ErrFunctionNotFound{ID: fn.ID}
}

byt, err := json.Marshal(fn)
Expand Down
10 changes: 4 additions & 6 deletions libkv/function_test.go
Expand Up @@ -93,7 +93,7 @@ func TestUpdateFunction(t *testing.T) {
ID: "testid",
Space: "default",
Provider: &function.Provider{Type: function.HTTPEndpoint, URL: "http://example1.com"}}
_, err := service.UpdateFunction("default", fn)
_, err := service.UpdateFunction(fn)

assert.Nil(t, err)
}
Expand All @@ -103,12 +103,10 @@ func TestUpdateFunction_ValidationError(t *testing.T) {
defer ctrl.Finish()

db := mock.NewMockStore(ctrl)
returned := []byte(`{"space":"default","functionId":"testid","provider":{"type":"http","url":"http://example.com"}}`)
db.EXPECT().Get("default/testid", gomock.Any()).Return(&store.KVPair{Value: returned}, nil)
service := &Service{FunctionStore: db, Log: zap.NewNop()}

fn := &function.Function{ID: "testid", Space: "default", Provider: &function.Provider{Type: function.HTTPEndpoint}}
_, err := service.UpdateFunction("default", fn)
_, err := service.UpdateFunction(fn)

assert.Equal(t, err, &function.ErrFunctionValidation{Message: "Missing required fields for HTTP endpoint."})
}
Expand All @@ -125,7 +123,7 @@ func TestUpdateFunction_NotFoundError(t *testing.T) {
ID: "testid",
Space: "default",
Provider: &function.Provider{Type: function.HTTPEndpoint, URL: "http://example.com"}}
_, err := service.UpdateFunction("default", fn)
_, err := service.UpdateFunction(fn)

assert.Equal(t, err, &function.ErrFunctionNotFound{ID: "testid"})
}
Expand All @@ -147,7 +145,7 @@ func TestUpdateFunction_PutError(t *testing.T) {
ID: "testid",
Space: "default",
Provider: &function.Provider{Type: function.HTTPEndpoint, URL: "http://example1.com"}}
_, err := service.UpdateFunction("default", fn)
_, err := service.UpdateFunction(fn)

assert.EqualError(t, err, "KV put error")
}
Expand Down

0 comments on commit af4eeb9

Please sign in to comment.