diff --git a/function/service.go b/function/service.go index 04f0191..ac59125 100644 --- a/function/service.go +++ b/function/service.go @@ -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 diff --git a/httpapi/httpapi.go b/httpapi/httpapi.go index 6cafb5d..6e772c6 100644 --- a/httpapi/httpapi.go +++ b/httpapi/httpapi.go @@ -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) @@ -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) { diff --git a/libkv/function.go b/libkv/function.go index 4f24ae3..5b53b8e 100644 --- a/libkv/function.go +++ b/libkv/function.go @@ -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) diff --git a/libkv/function_test.go b/libkv/function_test.go index 99df7dc..e8d2015 100644 --- a/libkv/function_test.go +++ b/libkv/function_test.go @@ -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) } @@ -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."}) } @@ -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"}) } @@ -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") }