Skip to content
Snippets Groups Projects
Commit 2229e922 authored by Alex Ellis's avatar Alex Ellis
Browse files

Add update endpoint/route


Signed-off-by: default avatarAlex Ellis <alexellis2@gmail.com>
parent e19803a3
No related branches found
No related tags found
No related merge requests found
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)
}
}
......@@ -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")
......
......@@ -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)
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)
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.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)
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment