From 9b62d598bb2895ac423e0dbf9d03f30857dd95bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Cluseau?= Date: Mon, 15 Apr 2019 18:56:31 +0100 Subject: [PATCH] factorize filter clauses --- cmd/dkl-local-server/main.go | 2 +- cmd/dkl-local-server/ws.go | 58 +++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/cmd/dkl-local-server/main.go b/cmd/dkl-local-server/main.go index 61f09d0..0c2681f 100644 --- a/cmd/dkl-local-server/main.go +++ b/cmd/dkl-local-server/main.go @@ -37,7 +37,7 @@ func main() { go casCleaner() apiutils.Setup(func() { - restful.Add(buildWS()) + registerWS(restful.DefaultContainer) }) swaggerui.HandleAt("/swagger-ui/") diff --git a/cmd/dkl-local-server/ws.go b/cmd/dkl-local-server/ws.go index d5b3b85..302bde8 100644 --- a/cmd/dkl-local-server/ws.go +++ b/cmd/dkl-local-server/ws.go @@ -11,35 +11,53 @@ import ( "novit.nc/direktil/pkg/localconfig" ) -func buildWS() *restful.WebService { - ws := &restful.WebService{} +func registerWS(rest *restful.Container) { + // Admin API + adminWS := &restful.WebService{} + ws := adminWS + ws.Filter(adminAuth). + HeaderParameter("Authorization", "Admin bearer token") - // configs API - ws.Route(ws.POST("/configs").Filter(adminAuth).To(wsUploadConfig). + // - configs API + ws.Route(ws.POST("/configs").To(wsUploadConfig). Doc("Upload a new current configuration, archiving the previous one")) - // clusters API - ws.Route(ws.GET("/clusters").Filter(adminAuth).To(wsListClusters). + // - clusters API + ws.Route(ws.GET("/clusters").To(wsListClusters). Doc("List clusters")) - ws.Route(ws.GET("/clusters/{cluster-name}").Filter(adminAuth).To(wsCluster). + ws.Route(ws.GET("/clusters/{cluster-name}").To(wsCluster). Doc("Get cluster details")) - ws.Route(ws.GET("/clusters/{cluster-name}/addons").Filter(adminAuth).To(wsClusterAddons). + ws.Route(ws.GET("/clusters/{cluster-name}/addons").To(wsClusterAddons). Produces(mime.YAML). Doc("Get cluster addons"). Returns(http.StatusOK, "OK", nil). Returns(http.StatusNotFound, "The cluster does not exists or does not have addons defined", nil)) - ws.Route(ws.GET("/clusters/{cluster-name}/passwords").Filter(adminAuth).To(wsClusterPasswords). + ws.Route(ws.GET("/clusters/{cluster-name}/passwords").To(wsClusterPasswords). Doc("List cluster's passwords")) - ws.Route(ws.GET("/clusters/{cluster-name}/passwords/{password-name}").Filter(adminAuth).To(wsClusterPassword). + ws.Route(ws.GET("/clusters/{cluster-name}/passwords/{password-name}").To(wsClusterPassword). Doc("Get cluster's password")) - ws.Route(ws.PUT("/clusters/{cluster-name}/passwords/{password-name}").Filter(adminAuth).To(wsClusterSetPassword). + ws.Route(ws.PUT("/clusters/{cluster-name}/passwords/{password-name}").To(wsClusterSetPassword). Doc("Set cluster's password")) - // hosts API - ws.Route(ws.GET("/hosts").Filter(hostsAuth).To(wsListHosts). + (&wsHost{ + prefix: "/hosts/{host-name}", + hostDoc: "given host", + getHost: func(req *restful.Request) string { + return req.PathParameter("host-name") + }, + }).register(adminWS, func(rb *restful.RouteBuilder) { + }) + + // Hosts API + hostsWS := &restful.WebService{} + ws = adminWS + ws.Filter(hostsAuth). + HeaderParameter("Authorization", "Host or admin bearer token") + + ws.Route(ws.GET("/hosts").To(wsListHosts). Doc("List hosts")) (&wsHost{ @@ -50,17 +68,9 @@ func buildWS() *restful.WebService { rb.Notes("In this case, the host is detected from the remote IP") }) - (&wsHost{ - prefix: "/hosts/{host-name}", - hostDoc: "given host", - getHost: func(req *restful.Request) string { - return req.PathParameter("host-name") - }, - }).register(ws, func(rb *restful.RouteBuilder) { - rb.Filter(adminAuth) - }) - - return ws + // register the web services + rest.Add(adminWS) + rest.Add(hostsWS) } func detectHost(req *restful.Request) string {