Make / and /v1 work

This commit is contained in:
Darren Shepherd
2019-08-14 13:39:08 -07:00
parent 59a6ee577e
commit 6d2b0f54ca
8 changed files with 55 additions and 20 deletions

View File

@@ -6,6 +6,7 @@ import (
"github.com/rancher/naok/pkg/resources/common" "github.com/rancher/naok/pkg/resources/common"
"github.com/rancher/naok/pkg/resources/counts" "github.com/rancher/naok/pkg/resources/counts"
"github.com/rancher/naok/pkg/resources/schema" "github.com/rancher/naok/pkg/resources/schema"
"github.com/rancher/norman/pkg/store/apiroot"
"github.com/rancher/norman/pkg/store/proxy" "github.com/rancher/norman/pkg/store/proxy"
"github.com/rancher/norman/pkg/subscribe" "github.com/rancher/norman/pkg/subscribe"
"github.com/rancher/norman/pkg/types" "github.com/rancher/norman/pkg/types"
@@ -19,6 +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)
common.Register(collection, getter) common.Register(collection, getter)

View File

@@ -36,6 +36,7 @@ func NewHandler(cfg *rest.Config, sf schema.Factory) (http.Handler, error) {
K8sResource: a.apiHandler(k8sAPI), K8sResource: a.apiHandler(k8sAPI),
GenericResource: a.apiHandler(nil), GenericResource: a.apiHandler(nil),
K8sProxy: proxy, K8sProxy: proxy,
APIRoot: a.apiHandler(apiRoot),
}), nil }), nil
} }

View File

@@ -36,3 +36,7 @@ func k8sAPI(sf schema.Factory, apiOp *types.APIRequest) {
apiOp.Namespaces = []string{namespace} apiOp.Namespaces = []string{namespace}
} }
} }
func apiRoot(sf schema.Factory, apiOp *types.APIRequest) {
apiOp.Type = "apiRoot"
}

View File

@@ -9,6 +9,7 @@ import (
type Handlers struct { type Handlers struct {
K8sResource http.Handler K8sResource http.Handler
GenericResource http.Handler GenericResource http.Handler
APIRoot http.Handler
K8sProxy http.Handler K8sProxy http.Handler
} }
@@ -18,6 +19,9 @@ func Routes(h Handlers) http.Handler {
m.StrictSlash(true) m.StrictSlash(true)
m.NotFoundHandler = h.K8sProxy m.NotFoundHandler = h.K8sProxy
m.Path("/").Handler(h.APIRoot)
m.Path("/{name:v1}").Handler(h.APIRoot)
m.Path("/v1/{type:schemas}/{name:.*}").Handler(h.GenericResource) m.Path("/v1/{type:schemas}/{name:.*}").Handler(h.GenericResource)
m.Path("/v1/{group}.{version}.{resource}").Handler(h.K8sResource) m.Path("/v1/{group}.{version}.{resource}").Handler(h.K8sResource)
m.Path("/v1/{group}.{version}.{resource}/{nameorns}").Handler(h.K8sResource) m.Path("/v1/{group}.{version}.{resource}/{nameorns}").Handler(h.K8sResource)

View File

@@ -1,6 +1,8 @@
package builtin package builtin
import ( import (
"net/http"
"github.com/rancher/norman/pkg/store/schema" "github.com/rancher/norman/pkg/store/schema"
"github.com/rancher/norman/pkg/types" "github.com/rancher/norman/pkg/types"
) )
@@ -52,30 +54,12 @@ var (
}, },
} }
APIRoot = types.Schema{
ID: "apiRoot",
CollectionMethods: []string{"GET"},
ResourceMethods: []string{"GET"},
ResourceFields: map[string]types.Field{
"apiVersion": {Type: "map[json]"},
"path": {Type: "string"},
},
Formatter: APIRootFormatter,
Store: NewAPIRootStore(nil, nil),
}
Schemas = types.EmptySchemas(). Schemas = types.EmptySchemas().
MustAddSchema(Schema). MustAddSchema(Schema).
MustAddSchema(Error). MustAddSchema(Error).
MustAddSchema(Collection). MustAddSchema(Collection)
MustAddSchema(APIRoot)
) )
func apiVersionFromMap(schemas *types.Schemas, apiVersion map[string]interface{}) string {
version, _ := apiVersion["version"].(string)
return version
}
func SchemaFormatter(apiOp *types.APIRequest, resource *types.RawResource) { func SchemaFormatter(apiOp *types.APIRequest, resource *types.RawResource) {
schema := apiOp.Schemas.Schema(resource.ID) schema := apiOp.Schemas.Schema(resource.ID)
if schema == nil { if schema == nil {
@@ -88,6 +72,13 @@ func SchemaFormatter(apiOp *types.APIRequest, resource *types.RawResource) {
} }
} }
func getSchemaCollectionLink(apiOp *types.APIRequest, schema *types.Schema) string {
if schema != nil && contains(schema.CollectionMethods, http.MethodGet) {
return apiOp.URLBuilder.Collection(schema)
}
return ""
}
func contains(list []string, needle string) bool { func contains(list []string, needle string) bool {
for _, v := range list { for _, v := range list {
if v == needle { if v == needle {

View File

@@ -1,4 +1,4 @@
package builtin package apiroot
import ( import (
"net/http" "net/http"
@@ -7,6 +7,20 @@ import (
"github.com/rancher/norman/pkg/types" "github.com/rancher/norman/pkg/types"
) )
func Register(schemas *types.Schemas, versions, roots []string) {
schemas.MustAddSchema(types.Schema{
ID: "apiRoot",
CollectionMethods: []string{"GET"},
ResourceMethods: []string{"GET"},
ResourceFields: map[string]types.Field{
"apiVersion": {Type: "map[json]"},
"path": {Type: "string"},
},
Formatter: APIRootFormatter,
Store: NewAPIRootStore(versions, roots),
})
}
func APIRootFormatter(apiOp *types.APIRequest, resource *types.RawResource) { func APIRootFormatter(apiOp *types.APIRequest, resource *types.RawResource) {
path, _ := resource.Values["path"].(string) path, _ := resource.Values["path"].(string)
if path == "" { if path == "" {
@@ -86,6 +100,7 @@ func (a *APIRootStore) List(apiOp *types.APIRequest, schema *types.Schema, opt *
func apiVersionToAPIRootMap(version string) map[string]interface{} { func apiVersionToAPIRootMap(version string) map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"id": version,
"type": "apiRoot", "type": "apiRoot",
"apiVersion": map[string]interface{}{ "apiVersion": map[string]interface{}{
"version": version, "version": version,
@@ -93,3 +108,17 @@ func apiVersionToAPIRootMap(version string) map[string]interface{} {
"path": "/" + version, "path": "/" + version,
} }
} }
func apiVersionFromMap(schemas *types.Schemas, apiVersion map[string]interface{}) string {
version, _ := apiVersion["version"].(string)
return version
}
func contains(list []string, needle string) bool {
for _, v := range list {
if v == needle {
return true
}
}
return false
}

View File

@@ -99,6 +99,9 @@ func (u *DefaultURLBuilder) Current() string {
} }
func (u *DefaultURLBuilder) RelativeToRoot(path string) string { func (u *DefaultURLBuilder) RelativeToRoot(path string) string {
if len(path) > 0 && path[0] != '/' {
return u.responseURLBase + "/" + path
}
return u.responseURLBase + path return u.responseURLBase + path
} }

1
vendor/modules.txt vendored
View File

@@ -42,6 +42,7 @@ github.com/pkg/errors
github.com/rancher/norman/pkg/authorization github.com/rancher/norman/pkg/authorization
github.com/rancher/norman/pkg/types github.com/rancher/norman/pkg/types
github.com/rancher/norman/pkg/types/convert github.com/rancher/norman/pkg/types/convert
github.com/rancher/norman/pkg/store/apiroot
github.com/rancher/norman/pkg/store/proxy github.com/rancher/norman/pkg/store/proxy
github.com/rancher/norman/pkg/subscribe github.com/rancher/norman/pkg/subscribe
github.com/rancher/norman/pkg/data github.com/rancher/norman/pkg/data