Improve API for the web UI

This commit is contained in:
Ludovic Fernandez 2019-09-02 11:38:04 +02:00 committed by Traefiker Bot
parent 17554202f6
commit af9762cf32
41 changed files with 1200 additions and 199 deletions

View file

@ -1,6 +1,7 @@
package api
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
@ -137,6 +138,68 @@ func TestHandler_HTTP(t *testing.T) {
statusCode: http.StatusBadRequest,
},
},
{
desc: "routers filtered by status",
path: "/api/http/routers?status=enabled",
conf: runtime.Configuration{
Routers: map[string]*runtime.RouterInfo{
"test@myprovider": {
Router: &dynamic.Router{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar.other`)",
Middlewares: []string{"addPrefixTest", "auth"},
},
Status: runtime.StatusEnabled,
},
"bar@myprovider": {
Router: &dynamic.Router{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar`)",
Middlewares: []string{"auth", "addPrefixTest@anotherprovider"},
},
Status: runtime.StatusDisabled,
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/routers-filtered-status.json",
},
},
{
desc: "routers filtered by search",
path: "/api/http/routers?search=fii",
conf: runtime.Configuration{
Routers: map[string]*runtime.RouterInfo{
"test@myprovider": {
Router: &dynamic.Router{
EntryPoints: []string{"web"},
Service: "fii-service@myprovider",
Rule: "Host(`fii.bar.other`)",
Middlewares: []string{"addPrefixTest", "auth"},
},
Status: runtime.StatusEnabled,
},
"bar@myprovider": {
Router: &dynamic.Router{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar`)",
Middlewares: []string{"auth", "addPrefixTest@anotherprovider"},
},
Status: runtime.StatusDisabled,
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/routers-filtered-search.json",
},
},
{
desc: "one router by id",
path: "/api/http/routers/bar@myprovider",
@ -232,6 +295,45 @@ func TestHandler_HTTP(t *testing.T) {
si.UpdateServerStatus("http://127.0.0.2", "UP")
return si
}(),
"canary@myprovider": {
Service: &dynamic.Service{
Weighted: &dynamic.WeightedRoundRobin{
Services: nil,
Sticky: &dynamic.Sticky{
Cookie: &dynamic.Cookie{
Name: "chocolat",
Secure: true,
HTTPOnly: true,
},
},
},
},
Status: runtime.StatusEnabled,
UsedBy: []string{"foo@myprovider"},
},
"mirror@myprovider": {
Service: &dynamic.Service{
Mirroring: &dynamic.Mirroring{
Service: "one@myprovider",
Mirrors: []dynamic.MirrorService{
{
Name: "two@myprovider",
Percent: 10,
},
{
Name: "three@myprovider",
Percent: 15,
},
{
Name: "four@myprovider",
Percent: 80,
},
},
},
},
Status: runtime.StatusEnabled,
UsedBy: []string{"foo@myprovider"},
},
},
},
expected: expected{
@ -301,6 +403,100 @@ func TestHandler_HTTP(t *testing.T) {
jsonFile: "testdata/services-page2.json",
},
},
{
desc: "services filtered by status",
path: "/api/http/services?status=enabled",
conf: runtime.Configuration{
Services: map[string]*runtime.ServiceInfo{
"bar@myprovider": func() *runtime.ServiceInfo {
si := &runtime.ServiceInfo{
Service: &dynamic.Service{
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: "http://127.0.0.1",
},
},
},
},
UsedBy: []string{"foo@myprovider", "test@myprovider"},
Status: runtime.StatusEnabled,
}
si.UpdateServerStatus("http://127.0.0.1", "UP")
return si
}(),
"baz@myprovider": func() *runtime.ServiceInfo {
si := &runtime.ServiceInfo{
Service: &dynamic.Service{
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: "http://127.0.0.2",
},
},
},
},
UsedBy: []string{"foo@myprovider"},
Status: runtime.StatusDisabled,
}
si.UpdateServerStatus("http://127.0.0.2", "UP")
return si
}(),
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/services-filtered-status.json",
},
},
{
desc: "services filtered by search",
path: "/api/http/services?search=baz",
conf: runtime.Configuration{
Services: map[string]*runtime.ServiceInfo{
"bar@myprovider": func() *runtime.ServiceInfo {
si := &runtime.ServiceInfo{
Service: &dynamic.Service{
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: "http://127.0.0.1",
},
},
},
},
UsedBy: []string{"foo@myprovider", "test@myprovider"},
Status: runtime.StatusEnabled,
}
si.UpdateServerStatus("http://127.0.0.1", "UP")
return si
}(),
"baz@myprovider": func() *runtime.ServiceInfo {
si := &runtime.ServiceInfo{
Service: &dynamic.Service{
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: "http://127.0.0.2",
},
},
},
},
UsedBy: []string{"foo@myprovider"},
Status: runtime.StatusDisabled,
}
si.UpdateServerStatus("http://127.0.0.2", "UP")
return si
}(),
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/services-filtered-search.json",
},
},
{
desc: "one service by id",
path: "/api/http/services/bar@myprovider",
@ -411,6 +607,86 @@ func TestHandler_HTTP(t *testing.T) {
jsonFile: "testdata/middlewares.json",
},
},
{
desc: "middlewares filtered by status",
path: "/api/http/middlewares?status=enabled",
conf: runtime.Configuration{
Middlewares: map[string]*runtime.MiddlewareInfo{
"auth@myprovider": {
Middleware: &dynamic.Middleware{
BasicAuth: &dynamic.BasicAuth{
Users: []string{"admin:admin"},
},
},
UsedBy: []string{"bar@myprovider", "test@myprovider"},
Status: runtime.StatusEnabled,
},
"addPrefixTest@myprovider": {
Middleware: &dynamic.Middleware{
AddPrefix: &dynamic.AddPrefix{
Prefix: "/titi",
},
},
UsedBy: []string{"test@myprovider"},
Status: runtime.StatusDisabled,
},
"addPrefixTest@anotherprovider": {
Middleware: &dynamic.Middleware{
AddPrefix: &dynamic.AddPrefix{
Prefix: "/toto",
},
},
UsedBy: []string{"bar@myprovider"},
Status: runtime.StatusEnabled,
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/middlewares-filtered-status.json",
},
},
{
desc: "middlewares filtered by search",
path: "/api/http/middlewares?search=addprefixtest",
conf: runtime.Configuration{
Middlewares: map[string]*runtime.MiddlewareInfo{
"auth@myprovider": {
Middleware: &dynamic.Middleware{
BasicAuth: &dynamic.BasicAuth{
Users: []string{"admin:admin"},
},
},
UsedBy: []string{"bar@myprovider", "test@myprovider"},
Status: runtime.StatusEnabled,
},
"addPrefixTest@myprovider": {
Middleware: &dynamic.Middleware{
AddPrefix: &dynamic.AddPrefix{
Prefix: "/titi",
},
},
UsedBy: []string{"test@myprovider"},
Status: runtime.StatusDisabled,
},
"addPrefixTest@anotherprovider": {
Middleware: &dynamic.Middleware{
AddPrefix: &dynamic.AddPrefix{
Prefix: "/toto",
},
},
UsedBy: []string{"bar@myprovider"},
Status: runtime.StatusEnabled,
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/middlewares-filtered-search.json",
},
},
{
desc: "all middlewares, 1 res per page, want page 2",
path: "/api/http/middlewares?page=2&per_page=1",
@ -521,6 +797,8 @@ func TestHandler_HTTP(t *testing.T) {
rtConf := &test.conf
// To lazily initialize the Statuses.
rtConf.PopulateUsedBy()
rtConf.GetRoutersByEntryPoints(context.Background(), []string{"web"}, false)
handler := New(static.Configuration{API: &static.API{}, Global: &static.Global{}}, rtConf)
router := mux.NewRouter()
handler.Append(router)