From b059dcca00244ce3b2f08ed2457d8d1fc1ff9490 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Wed, 14 Aug 2019 13:51:13 -0700 Subject: [PATCH] Cleanup around APIRoots --- pkg/resources/schema.go | 2 +- .../norman/pkg/store/apiroot/apiroot.go | 37 ++++++++++++------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/pkg/resources/schema.go b/pkg/resources/schema.go index 0a1c8f9..5eac5c7 100644 --- a/pkg/resources/schema.go +++ b/pkg/resources/schema.go @@ -20,7 +20,7 @@ func SchemaFactory(getter proxy.ClientGetter, as *accesscontrol.AccessStore, k8s counts.Register(baseSchema) subscribe.Register(baseSchema) apigroups.Register(baseSchema, k8s.Discovery()) - apiroot.Register(baseSchema, []string{"v1"}, nil) + apiroot.Register(baseSchema, []string{"v1"}, []string{"proxy:/apis"}) common.Register(collection, getter) diff --git a/vendor/github.com/rancher/norman/pkg/store/apiroot/apiroot.go b/vendor/github.com/rancher/norman/pkg/store/apiroot/apiroot.go index d39da6c..2c66438 100644 --- a/vendor/github.com/rancher/norman/pkg/store/apiroot/apiroot.go +++ b/vendor/github.com/rancher/norman/pkg/store/apiroot/apiroot.go @@ -2,6 +2,7 @@ package apiroot import ( "net/http" + "strings" "github.com/rancher/norman/pkg/store/empty" "github.com/rancher/norman/pkg/types" @@ -26,19 +27,18 @@ func APIRootFormatter(apiOp *types.APIRequest, resource *types.RawResource) { if path == "" { return } - delete(resource.Values, "path") resource.Links["root"] = apiOp.URLBuilder.RelativeToRoot(path) - resource.Links["schemas"] = apiOp.URLBuilder.RelativeToRoot(path) - data, _ := resource.Values["apiVersion"].(map[string]interface{}) - apiVersion := apiVersionFromMap(apiOp.Schemas, data) + if data, isAPIRoot := resource.Values["apiVersion"].(map[string]interface{}); isAPIRoot { + apiVersion := apiVersionFromMap(apiOp.Schemas, data) + resource.Links["self"] = apiOp.URLBuilder.RelativeToRoot(apiVersion) - resource.Links["self"] = apiOp.URLBuilder.RelativeToRoot(apiVersion) - - for _, schema := range apiOp.Schemas.Schemas() { - addCollectionLink(apiOp, schema, resource.Links) + resource.Links["schemas"] = apiOp.URLBuilder.RelativeToRoot(path) + for _, schema := range apiOp.Schemas.Schemas() { + addCollectionLink(apiOp, schema, resource.Links) + } } return @@ -72,11 +72,16 @@ func NewAPIRootStore(versions []string, roots []string) types.Store { } func (a *APIRootStore) ByID(apiOp *types.APIRequest, schema *types.Schema, id string) (types.APIObject, error) { - for _, version := range a.versions { - if version == id { - return types.ToAPI(apiVersionToAPIRootMap(version)), nil + list, err := a.List(apiOp, schema, nil) + if err != nil { + return types.APIObject{}, nil + } + for _, item := range list.List() { + if item["id"] == id { + return types.ToAPI(item), nil } } + return types.APIObject{}, nil } @@ -90,9 +95,13 @@ func (a *APIRootStore) List(apiOp *types.APIRequest, schema *types.Schema, opt * } for _, root := range a.roots { - roots = append(roots, map[string]interface{}{ - "path": root, - }) + parts := strings.SplitN(root, ":", 2) + if len(parts) == 2 { + roots = append(roots, map[string]interface{}{ + "id": parts[0], + "path": parts[1], + }) + } } return types.ToAPI(roots), nil