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

@@ -1,6 +1,8 @@
package builtin
import (
"net/http"
"github.com/rancher/norman/pkg/store/schema"
"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().
MustAddSchema(Schema).
MustAddSchema(Error).
MustAddSchema(Collection).
MustAddSchema(APIRoot)
MustAddSchema(Collection)
)
func apiVersionFromMap(schemas *types.Schemas, apiVersion map[string]interface{}) string {
version, _ := apiVersion["version"].(string)
return version
}
func SchemaFormatter(apiOp *types.APIRequest, resource *types.RawResource) {
schema := apiOp.Schemas.Schema(resource.ID)
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 {
for _, v := range list {
if v == needle {

View File

@@ -1,4 +1,4 @@
package builtin
package apiroot
import (
"net/http"
@@ -7,6 +7,20 @@ import (
"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) {
path, _ := resource.Values["path"].(string)
if path == "" {
@@ -86,6 +100,7 @@ func (a *APIRootStore) List(apiOp *types.APIRequest, schema *types.Schema, opt *
func apiVersionToAPIRootMap(version string) map[string]interface{} {
return map[string]interface{}{
"id": version,
"type": "apiRoot",
"apiVersion": map[string]interface{}{
"version": version,
@@ -93,3 +108,17 @@ func apiVersionToAPIRootMap(version string) map[string]interface{} {
"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 {
if len(path) > 0 && path[0] != '/' {
return u.responseURLBase + "/" + path
}
return u.responseURLBase + path
}