1
0
mirror of https://github.com/rancher/steve.git synced 2025-06-27 07:17:13 +00:00

Cleanup around APIRoots

This commit is contained in:
Darren Shepherd 2019-08-14 13:51:13 -07:00
parent 6d2b0f54ca
commit b059dcca00
2 changed files with 24 additions and 15 deletions

View File

@ -20,7 +20,7 @@ func SchemaFactory(getter proxy.ClientGetter, as *accesscontrol.AccessStore, k8s
counts.Register(baseSchema) counts.Register(baseSchema)
subscribe.Register(baseSchema) subscribe.Register(baseSchema)
apigroups.Register(baseSchema, k8s.Discovery()) apigroups.Register(baseSchema, k8s.Discovery())
apiroot.Register(baseSchema, []string{"v1"}, nil) apiroot.Register(baseSchema, []string{"v1"}, []string{"proxy:/apis"})
common.Register(collection, getter) common.Register(collection, getter)

View File

@ -2,6 +2,7 @@ package apiroot
import ( import (
"net/http" "net/http"
"strings"
"github.com/rancher/norman/pkg/store/empty" "github.com/rancher/norman/pkg/store/empty"
"github.com/rancher/norman/pkg/types" "github.com/rancher/norman/pkg/types"
@ -26,20 +27,19 @@ func APIRootFormatter(apiOp *types.APIRequest, resource *types.RawResource) {
if path == "" { if path == "" {
return return
} }
delete(resource.Values, "path") delete(resource.Values, "path")
resource.Links["root"] = apiOp.URLBuilder.RelativeToRoot(path) resource.Links["root"] = apiOp.URLBuilder.RelativeToRoot(path)
resource.Links["schemas"] = apiOp.URLBuilder.RelativeToRoot(path)
data, _ := resource.Values["apiVersion"].(map[string]interface{}) if data, isAPIRoot := resource.Values["apiVersion"].(map[string]interface{}); isAPIRoot {
apiVersion := apiVersionFromMap(apiOp.Schemas, data) apiVersion := apiVersionFromMap(apiOp.Schemas, data)
resource.Links["self"] = apiOp.URLBuilder.RelativeToRoot(apiVersion) resource.Links["self"] = apiOp.URLBuilder.RelativeToRoot(apiVersion)
resource.Links["schemas"] = apiOp.URLBuilder.RelativeToRoot(path)
for _, schema := range apiOp.Schemas.Schemas() { for _, schema := range apiOp.Schemas.Schemas() {
addCollectionLink(apiOp, schema, resource.Links) addCollectionLink(apiOp, schema, resource.Links)
} }
}
return 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) { func (a *APIRootStore) ByID(apiOp *types.APIRequest, schema *types.Schema, id string) (types.APIObject, error) {
for _, version := range a.versions { list, err := a.List(apiOp, schema, nil)
if version == id { if err != nil {
return types.ToAPI(apiVersionToAPIRootMap(version)), nil return types.APIObject{}, nil
}
for _, item := range list.List() {
if item["id"] == id {
return types.ToAPI(item), nil
} }
} }
return types.APIObject{}, nil return types.APIObject{}, nil
} }
@ -90,10 +95,14 @@ func (a *APIRootStore) List(apiOp *types.APIRequest, schema *types.Schema, opt *
} }
for _, root := range a.roots { for _, root := range a.roots {
parts := strings.SplitN(root, ":", 2)
if len(parts) == 2 {
roots = append(roots, map[string]interface{}{ roots = append(roots, map[string]interface{}{
"path": root, "id": parts[0],
"path": parts[1],
}) })
} }
}
return types.ToAPI(roots), nil return types.ToAPI(roots), nil
} }