2019-02-01 07:28:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net"
|
2019-02-01 07:35:50 +00:00
|
|
|
"net/http"
|
2019-02-01 07:28:08 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/emicklei/go-restful"
|
2019-02-04 04:06:02 +00:00
|
|
|
"novit.nc/direktil/local-server/pkg/mime"
|
2019-02-04 02:56:43 +00:00
|
|
|
"novit.nc/direktil/pkg/localconfig"
|
2019-02-01 07:28:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func buildWS() *restful.WebService {
|
|
|
|
ws := &restful.WebService{}
|
|
|
|
|
2019-02-04 02:56:43 +00:00
|
|
|
// configs API
|
|
|
|
ws.Route(ws.POST("/configs").Filter(adminAuth).To(wsUploadConfig).
|
|
|
|
Doc("Upload a new current configuration, archiving the previous one"))
|
|
|
|
|
|
|
|
// clusters API
|
|
|
|
ws.Route(ws.GET("/clusters").Filter(adminAuth).To(wsListClusters).
|
|
|
|
Doc("List clusters"))
|
2019-02-04 04:46:03 +00:00
|
|
|
|
2019-02-04 02:56:43 +00:00
|
|
|
ws.Route(ws.GET("/clusters/{cluster-name}").Filter(adminAuth).To(wsCluster).
|
|
|
|
Doc("Get cluster details"))
|
2019-02-04 04:46:03 +00:00
|
|
|
|
2019-02-04 02:56:43 +00:00
|
|
|
ws.Route(ws.GET("/clusters/{cluster-name}/addons").Filter(adminAuth).To(wsClusterAddons).
|
2019-02-04 04:06:02 +00:00
|
|
|
Produces(mime.YAML).
|
2019-02-04 02:56:43 +00:00
|
|
|
Doc("Get cluster addons").
|
|
|
|
Returns(http.StatusOK, "OK", nil).
|
|
|
|
Returns(http.StatusNotFound, "The cluster does not exists or does not have addons defined", nil))
|
|
|
|
|
|
|
|
// hosts API
|
|
|
|
ws.Route(ws.GET("/hosts").Filter(hostsAuth).To(wsListHosts).
|
|
|
|
Doc("List hosts"))
|
2019-02-01 07:28:08 +00:00
|
|
|
|
|
|
|
(&wsHost{
|
2019-02-04 04:46:03 +00:00
|
|
|
prefix: "/me",
|
|
|
|
hostDoc: "detected host",
|
2019-02-01 07:28:08 +00:00
|
|
|
getHost: detectHost,
|
2019-02-04 02:56:43 +00:00
|
|
|
}).register(ws, func(rb *restful.RouteBuilder) {
|
|
|
|
rb.Notes("In this case, the host is detected from the remote IP")
|
|
|
|
})
|
2019-02-01 07:28:08 +00:00
|
|
|
|
|
|
|
(&wsHost{
|
2019-02-04 04:46:03 +00:00
|
|
|
prefix: "/hosts/{host-name}",
|
|
|
|
hostDoc: "given host",
|
2019-02-01 07:28:08 +00:00
|
|
|
getHost: func(req *restful.Request) string {
|
2019-02-04 02:56:43 +00:00
|
|
|
return req.PathParameter("host-name")
|
2019-02-01 07:28:08 +00:00
|
|
|
},
|
2019-02-04 02:56:43 +00:00
|
|
|
}).register(ws, func(rb *restful.RouteBuilder) {
|
|
|
|
rb.Filter(adminAuth)
|
|
|
|
})
|
2019-02-01 07:28:08 +00:00
|
|
|
|
|
|
|
return ws
|
|
|
|
}
|
|
|
|
|
|
|
|
func detectHost(req *restful.Request) string {
|
|
|
|
r := req.Request
|
|
|
|
remoteAddr := r.RemoteAddr
|
|
|
|
|
|
|
|
if *trustXFF {
|
|
|
|
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
|
|
|
|
remoteAddr = strings.Split(xff, ",")[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hostIP, _, err := net.SplitHostPort(remoteAddr)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
hostIP = remoteAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := readConfig()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
host := cfg.HostByIP(hostIP)
|
|
|
|
|
|
|
|
if host == nil {
|
|
|
|
log.Print("no host found for IP ", hostIP)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return host.Name
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:56:43 +00:00
|
|
|
func wsReadConfig(resp *restful.Response) *localconfig.Config {
|
|
|
|
cfg, err := readConfig()
|
2019-02-01 07:35:50 +00:00
|
|
|
if err != nil {
|
2019-02-04 02:56:43 +00:00
|
|
|
log.Print("failed to read config: ", err)
|
|
|
|
resp.WriteErrorString(http.StatusServiceUnavailable, "failed to read config")
|
|
|
|
return nil
|
2019-02-01 07:35:50 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 02:56:43 +00:00
|
|
|
return cfg
|
|
|
|
}
|
2019-02-01 07:35:50 +00:00
|
|
|
|
2019-02-04 02:56:43 +00:00
|
|
|
func wsNotFound(req *restful.Request, resp *restful.Response) {
|
|
|
|
http.NotFound(resp.ResponseWriter, req.Request)
|
|
|
|
}
|
2019-02-01 07:35:50 +00:00
|
|
|
|
2019-02-04 02:56:43 +00:00
|
|
|
func wsError(resp *restful.Response, err error) {
|
|
|
|
log.Print("request failed: ", err)
|
|
|
|
resp.WriteErrorString(
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError))
|
2019-02-01 07:28:08 +00:00
|
|
|
}
|