From 2229e922d7b40ab984ea9b13a504dde23b09e01d Mon Sep 17 00:00:00 2001 From: Alex Ellis Date: Fri, 22 Sep 2017 20:47:10 +0100 Subject: [PATCH] Add update endpoint/route Signed-off-by: Alex Ellis --- gateway/handlers/update_handler.go | 16 ++++ gateway/server.go | 9 +- .../tests/integration/createfunction_test.go | 90 ++----------------- 3 files changed, 29 insertions(+), 86 deletions(-) create mode 100644 gateway/handlers/update_handler.go diff --git a/gateway/handlers/update_handler.go b/gateway/handlers/update_handler.go new file mode 100644 index 00000000..aa638e01 --- /dev/null +++ b/gateway/handlers/update_handler.go @@ -0,0 +1,16 @@ +package handlers + +import ( + "net/http" + + "github.com/alexellis/faas/gateway/metrics" + "github.com/docker/docker/client" +) + +// MakeUpdateFunctionHandler request to update an existing function with new configuration such as image, parameters etc. +func MakeUpdateFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Client, maxRestarts uint64) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + defer r.Body.Close() + w.WriteHeader(http.StatusNotImplemented) + } +} diff --git a/gateway/server.go b/gateway/server.go index fc537c81..d6688062 100644 --- a/gateway/server.go +++ b/gateway/server.go @@ -30,11 +30,12 @@ type handlerSet struct { ListFunctions http.HandlerFunc Alert http.HandlerFunc RoutelessProxy http.HandlerFunc + UpdateFunction http.HandlerFunc // QueuedProxy - queue work and return synchronous response QueuedProxy http.HandlerFunc - // AsyncReport - report a defered execution result + // AsyncReport - report a deferred execution result AsyncReport http.HandlerFunc } @@ -80,12 +81,16 @@ func main() { faasHandlers.ListFunctions = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions) faasHandlers.DeployFunction = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions) faasHandlers.DeleteFunction = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions) + faasHandlers.UpdateFunction = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions) + alertHandler := plugin.NewExternalServiceQuery(*config.FunctionsProviderURL) faasHandlers.Alert = internalHandlers.MakeAlertHandler(alertHandler) metrics.AttachExternalWatcher(*config.FunctionsProviderURL, metricsOptions, "func", time.Second*5) } else { + + // How many times to reschedule a function. maxRestarts := uint64(5) faasHandlers.Proxy = internalHandlers.MakeProxy(metricsOptions, true, dockerClient, &logger) @@ -93,6 +98,7 @@ func main() { faasHandlers.ListFunctions = internalHandlers.MakeFunctionReader(metricsOptions, dockerClient) faasHandlers.DeployFunction = internalHandlers.MakeNewFunctionHandler(metricsOptions, dockerClient, maxRestarts) faasHandlers.DeleteFunction = internalHandlers.MakeDeleteFunctionHandler(metricsOptions, dockerClient) + faasHandlers.UpdateFunction = internalHandlers.MakeUpdateFunctionHandler(metricsOptions, dockerClient, maxRestarts) faasHandlers.Alert = internalHandlers.MakeAlertHandler(internalHandlers.NewSwarmServiceQuery(dockerClient)) @@ -124,6 +130,7 @@ func main() { r.HandleFunc("/system/functions", listFunctions).Methods("GET") r.HandleFunc("/system/functions", faasHandlers.DeployFunction).Methods("POST") r.HandleFunc("/system/functions", faasHandlers.DeleteFunction).Methods("DELETE") + r.HandleFunc("/system/functions", faasHandlers.UpdateFunction).Methods("UPDATE") if faasHandlers.QueuedProxy != nil { r.HandleFunc("/async-function/{name:[-a-zA-Z_0-9]+}/", faasHandlers.QueuedProxy).Methods("POST") diff --git a/gateway/tests/integration/createfunction_test.go b/gateway/tests/integration/createfunction_test.go index 3f62d0d2..de7dcce9 100644 --- a/gateway/tests/integration/createfunction_test.go +++ b/gateway/tests/integration/createfunction_test.go @@ -4,101 +4,21 @@ package inttests import ( - "encoding/json" "net/http" - "strings" "testing" - - "github.com/alexellis/faas/gateway/requests" ) -func createFunction(request requests.CreateFunctionRequest) (string, int, error) { - marshalled, _ := json.Marshal(request) - return fireRequest("http://localhost:8080/system/functions", http.MethodPost, string(marshalled)) -} - -func deleteFunction(name string) (string, int, error) { - marshalled, _ := json.Marshal(requests.DeleteFunctionRequest{name}) - return fireRequest("http://localhost:8080/system/functions", http.MethodDelete, string(marshalled)) -} - -func TestCreate_ValidRequest(t *testing.T) { - request := requests.CreateFunctionRequest{ - Service: "test_resizer", - Image: "functions/resizer", - Network: "func_functions", - EnvProcess: "", - } - - _, code, err := createFunction(request) +func TestCreate_ValidJson_InvalidFunction(t *testing.T) { + reqBody := `{}` + _, code, err := fireRequest("http://localhost:8080/system/functions", http.MethodPost, reqBody) if err != nil { t.Log(err) t.Fail() } - expectedErrorCode := http.StatusOK - if code != expectedErrorCode { - t.Errorf("Got HTTP code: %d, want %d\n", code, expectedErrorCode) - return - } - - deleteFunction("test_resizer") -} - -func TestCreate_InvalidImage(t *testing.T) { - request := requests.CreateFunctionRequest{ - Service: "test_resizer", - Image: "a b c", - Network: "func_functions", - EnvProcess: "", - } - - body, code, err := createFunction(request) - - if err != nil { - t.Log(err) - t.Fail() - } - - expectedErrorCode := http.StatusBadRequest - if code != expectedErrorCode { - t.Errorf("Got HTTP code: %d, want %d\n", code, expectedErrorCode) - return - } - - expectedErrorSlice := "is not a valid repository/tag" - if !strings.Contains(body, expectedErrorSlice) { - t.Errorf("Error message %s does not contain: %s\n", body, expectedErrorSlice) - return - } -} - -func TestCreate_InvalidNetwork(t *testing.T) { - request := requests.CreateFunctionRequest{ - Service: "test_resizer", - Image: "functions/resizer", - Network: "non_existent_network", - EnvProcess: "", - } - - body, code, err := createFunction(request) - - if err != nil { - t.Log(err) - t.Fail() - } - - expectedErrorCode := http.StatusBadRequest - if code != expectedErrorCode { - t.Errorf("Got HTTP code: %d, want %d\n", code, expectedErrorCode) - return - } - - expectedErrorSlice := "network non_existent_network not found" - if !strings.Contains(body, expectedErrorSlice) { - t.Errorf("Error message %s does not contain: %s\n", body, expectedErrorSlice) - return + if code != http.StatusBadRequest { + t.Errorf("Got HTTP code: %d, want %d\n", code, http.StatusBadRequest) } }