Add doc for {namespace} path param, fixup and verify proxy paths

This commit is contained in:
derekwaynecarr 2014-12-11 13:59:51 -05:00
parent 14dd466e83
commit 6694a45542
3 changed files with 16 additions and 5 deletions

View File

@ -100,7 +100,7 @@ func indirectArbitraryPointer(ptrToObject interface{}) interface{} {
return reflect.Indirect(reflect.ValueOf(ptrToObject)).Interface() return reflect.Indirect(reflect.ValueOf(ptrToObject)).Interface()
} }
func registerResourceHandlers(ws *restful.WebService, version string, path string, storage RESTStorage, kinds map[string]reflect.Type, h restful.RouteFunction) { func registerResourceHandlers(ws *restful.WebService, version string, path string, storage RESTStorage, kinds map[string]reflect.Type, h restful.RouteFunction, namespaceScope bool) {
glog.V(3).Infof("Installing /%s/%s\n", version, path) glog.V(3).Infof("Installing /%s/%s\n", version, path)
object := storage.New() object := storage.New()
_, kind, err := api.Scheme.ObjectVersionAndKind(object) _, kind, err := api.Scheme.ObjectVersionAndKind(object)
@ -118,6 +118,11 @@ func registerResourceHandlers(ws *restful.WebService, version string, path strin
// See github.com/emicklei/go-restful/blob/master/jsr311.go for routing logic // See github.com/emicklei/go-restful/blob/master/jsr311.go for routing logic
// and status-code behavior // and status-code behavior
if namespaceScope {
path = "ns/{namespace}/" + path
ws.Param(ws.PathParameter("namespace", "object name and auth scope, such as for teams and projects").DataType("string"))
}
glog.V(3).Infof("Installing version=/%s, kind=/%s, path=/%s\n", version, kind, path)
ws.Route(ws.POST(path).To(h). ws.Route(ws.POST(path).To(h).
Doc("create a " + kind). Doc("create a " + kind).
@ -221,8 +226,10 @@ func (g *APIGroupVersion) InstallREST(container *restful.Container, root string,
} }
for path, storage := range g.handler.storage { for path, storage := range g.handler.storage {
registerResourceHandlers(ws, version, path, storage, kinds, h) // register legacy patterns where namespace is optional in path
registerResourceHandlers(ws, version, "ns/{namespace}/"+path, storage, kinds, h) registerResourceHandlers(ws, version, path, storage, kinds, h, false)
// register pattern where namespace is required in path
registerResourceHandlers(ws, version, path, storage, kinds, h, true)
} }
// TODO: port the rest of these. Sadly, if we don't, we'll have inconsistent // TODO: port the rest of these. Sadly, if we don't, we'll have inconsistent

View File

@ -90,8 +90,9 @@ func (r *ProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
} }
id := parts[1] id := parts[1]
rest := "" rest := ""
if len(parts) == 3 { if len(parts) > 2 {
rest = parts[2] proxyParts := parts[2:]
rest = strings.Join(proxyParts, "/")
} }
storage, ok := r.storage[kind] storage, ok := r.storage[kind]
if !ok { if !ok {

View File

@ -154,6 +154,9 @@ func TestProxy(t *testing.T) {
if e, a := item.reqBody, string(gotBody); e != a { if e, a := item.reqBody, string(gotBody); e != a {
t.Errorf("%v - expected %v, got %v", item.method, e, a) t.Errorf("%v - expected %v, got %v", item.method, e, a)
} }
if e, a := item.path, req.URL.Path; e != a {
t.Errorf("%v - expected %v, got %v", item.method, e, a)
}
fmt.Fprint(w, item.respBody) fmt.Fprint(w, item.respBody)
})) }))
defer proxyServer.Close() defer proxyServer.Close()