1
0
mirror of https://github.com/rancher/norman.git synced 2025-06-24 14:31:42 +00:00
norman/api/builtin/schema.go

137 lines
3.5 KiB
Go
Raw Permalink Normal View History

2017-11-11 04:44:02 +00:00
package builtin
import (
"net/http"
"github.com/rancher/norman/store/schema"
"github.com/rancher/norman/types"
)
var (
Version = types.APIVersion{
2017-11-21 20:46:30 +00:00
Group: "meta.cattle.io",
Version: "v1",
2017-12-05 16:21:12 +00:00
Path: "/meta",
2017-11-11 04:44:02 +00:00
}
Schema = types.Schema{
ID: "schema",
2017-12-12 03:58:43 +00:00
PluralName: "schemas",
2017-11-11 04:44:02 +00:00
Version: Version,
CollectionMethods: []string{"GET"},
ResourceMethods: []string{"GET"},
ResourceFields: map[string]types.Field{
"collectionActions": {Type: "map[json]"},
"collectionFields": {Type: "map[json]"},
2017-11-21 20:46:30 +00:00
"collectionFilters": {Type: "map[json]"},
2017-11-11 04:44:02 +00:00
"collectionMethods": {Type: "array[string]"},
"pluralName": {Type: "string"},
"resourceActions": {Type: "map[json]"},
"resourceFields": {Type: "map[json]"},
"resourceMethods": {Type: "array[string]"},
"version": {Type: "map[json]"},
},
Formatter: SchemaFormatter,
Store: schema.NewSchemaStore(),
}
Error = types.Schema{
ID: "error",
Version: Version,
ResourceMethods: []string{},
CollectionMethods: []string{},
ResourceFields: map[string]types.Field{
2017-11-21 20:46:30 +00:00
"code": {Type: "string"},
"detail": {Type: "string", Nullable: true},
"message": {Type: "string", Nullable: true},
"fieldName": {Type: "string", Nullable: true},
"status": {Type: "int"},
2017-11-11 04:44:02 +00:00
},
}
Collection = types.Schema{
2017-11-28 21:28:25 +00:00
ID: "collection",
2017-11-11 04:44:02 +00:00
Version: Version,
ResourceMethods: []string{},
CollectionMethods: []string{},
ResourceFields: map[string]types.Field{
"data": {Type: "array[json]"},
"pagination": {Type: "map[json]"},
"sort": {Type: "map[json]"},
"filters": {Type: "map[json]"},
},
}
APIRoot = types.Schema{
ID: "apiRoot",
Version: Version,
2017-12-20 04:39:57 +00:00
CollectionMethods: []string{"GET"},
ResourceMethods: []string{"GET"},
2017-11-11 04:44:02 +00:00
ResourceFields: map[string]types.Field{
"apiVersion": {Type: "map[json]"},
"path": {Type: "string"},
},
Formatter: APIRootFormatter,
Store: NewAPIRootStore(nil),
}
Schemas = types.NewSchemas().
2017-12-12 03:58:43 +00:00
AddSchema(Schema).
AddSchema(Error).
AddSchema(Collection).
AddSchema(APIRoot)
2017-11-11 04:44:02 +00:00
)
2017-11-21 20:46:30 +00:00
func apiVersionFromMap(schemas *types.Schemas, apiVersion map[string]interface{}) types.APIVersion {
2017-11-11 04:44:02 +00:00
path, _ := apiVersion["path"].(string)
version, _ := apiVersion["version"].(string)
group, _ := apiVersion["group"].(string)
2017-11-21 20:46:30 +00:00
apiVersionObj := types.APIVersion{
2017-11-11 04:44:02 +00:00
Path: path,
Version: version,
Group: group,
}
2017-11-21 20:46:30 +00:00
for _, testVersion := range schemas.Versions() {
if testVersion.Equals(&apiVersionObj) {
return testVersion
}
}
return apiVersionObj
2017-11-11 04:44:02 +00:00
}
func SchemaFormatter(apiContext *types.APIContext, resource *types.RawResource) {
data, _ := resource.Values["version"].(map[string]interface{})
2017-11-21 20:46:30 +00:00
apiVersion := apiVersionFromMap(apiContext.Schemas, data)
2017-11-11 04:44:02 +00:00
schema := apiContext.Schemas.Schema(&apiVersion, resource.ID)
2017-11-21 20:46:30 +00:00
if schema == nil {
return
}
collectionLink := getSchemaCollectionLink(apiContext, schema, &apiVersion)
2017-11-11 04:44:02 +00:00
if collectionLink != "" {
resource.Links["collection"] = collectionLink
}
2017-11-21 20:46:30 +00:00
resource.Links["self"] = apiContext.URLBuilder.SchemaLink(schema)
2017-11-11 04:44:02 +00:00
}
2017-11-21 20:46:30 +00:00
func getSchemaCollectionLink(apiContext *types.APIContext, schema *types.Schema, apiVersion *types.APIVersion) string {
2017-11-11 04:44:02 +00:00
if schema != nil && contains(schema.CollectionMethods, http.MethodGet) {
2017-11-21 20:46:30 +00:00
return apiContext.URLBuilder.Collection(schema, apiVersion)
2017-11-11 04:44:02 +00:00
}
return ""
}
func contains(list []string, needle string) bool {
for _, v := range list {
if v == needle {
return true
}
}
return false
}