-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ba704a
commit 38c9c5c
Showing
78 changed files
with
1,692 additions
and
994 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package builtin | ||
|
||
import ( | ||
"github.com/rancher/norman/store/empty" | ||
"github.com/rancher/norman/types" | ||
"github.com/rancher/norman/types/convert" | ||
) | ||
|
||
func APIRootFormatter(apiContext *types.APIContext, resource *types.RawResource) { | ||
path, _ := resource.Values["path"].(string) | ||
if path == "" { | ||
return | ||
} | ||
|
||
delete(resource.Values, "path") | ||
|
||
resource.Links["root"] = apiContext.URLBuilder.RelativeToRoot(path) | ||
|
||
data, _ := resource.Values["apiVersion"].(map[string]interface{}) | ||
apiVersion := apiVersionFromMap(apiContext.Schemas, data) | ||
|
||
resource.Links["self"] = apiContext.URLBuilder.Version(apiVersion) | ||
|
||
if len(apiVersion.SubContexts) > 0 { | ||
subContextToSchema := apiContext.Schemas.SubContextSchemas() | ||
if len(subContextToSchema) > 0 { | ||
for _, schema := range subContextToSchema { | ||
addCollectionLink(apiContext, schema, resource.Links) | ||
} | ||
|
||
for _, schema := range getNonReferencedSchemas(apiContext.Schemas.SchemasForVersion(apiVersion), | ||
subContextToSchema) { | ||
addCollectionLink(apiContext, schema, resource.Links) | ||
} | ||
|
||
return | ||
} | ||
} | ||
|
||
for _, schema := range apiContext.Schemas.SchemasForVersion(apiVersion) { | ||
addCollectionLink(apiContext, schema, resource.Links) | ||
} | ||
|
||
return | ||
} | ||
|
||
func getNonReferencedSchemas(schemas map[string]*types.Schema, subContexts map[string]*types.Schema) []*types.Schema { | ||
var result []*types.Schema | ||
typeNames := map[string]bool{} | ||
|
||
for _, subContext := range subContexts { | ||
ref := convert.ToReference(subContext.ID) | ||
fullRef := convert.ToFullReference(subContext.Version.Path, subContext.ID) | ||
typeNames[ref] = true | ||
typeNames[fullRef] = true | ||
} | ||
|
||
outer: | ||
for _, schema := range schemas { | ||
for _, field := range schema.ResourceFields { | ||
if typeNames[field.Type] { | ||
continue outer | ||
} | ||
} | ||
|
||
result = append(result, schema) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func addCollectionLink(apiContext *types.APIContext, schema *types.Schema, links map[string]string) { | ||
collectionLink := getSchemaCollectionLink(apiContext, schema, nil) | ||
if collectionLink != "" { | ||
links[schema.PluralName] = collectionLink | ||
} | ||
} | ||
|
||
type APIRootStore struct { | ||
empty.Store | ||
roots []string | ||
} | ||
|
||
func NewAPIRootStore(roots []string) types.Store { | ||
return &APIRootStore{roots: roots} | ||
} | ||
|
||
func (a *APIRootStore) ByID(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) { | ||
for _, version := range apiContext.Schemas.Versions() { | ||
if version.Path == id { | ||
return apiVersionToAPIRootMap(version), nil | ||
} | ||
} | ||
return nil, nil | ||
} | ||
|
||
func (a *APIRootStore) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) { | ||
var roots []map[string]interface{} | ||
|
||
for _, version := range apiContext.Schemas.Versions() { | ||
roots = append(roots, apiVersionToAPIRootMap(version)) | ||
} | ||
|
||
for _, root := range a.roots { | ||
roots = append(roots, map[string]interface{}{ | ||
"path": root, | ||
}) | ||
} | ||
|
||
return roots, nil | ||
} | ||
|
||
func apiVersionToAPIRootMap(version types.APIVersion) map[string]interface{} { | ||
return map[string]interface{}{ | ||
"type": "/v1-meta/schemas/apiRoot", | ||
"apiVersion": map[string]interface{}{ | ||
"version": version.Version, | ||
"group": version.Group, | ||
"path": version.Path, | ||
}, | ||
"path": version.Path, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package formatter | ||
|
||
import ( | ||
"github.com/rancher/norman/types" | ||
"github.com/rancher/norman/types/convert" | ||
) | ||
|
||
func SubContextFormatter(apiContext *types.APIContext, resource *types.RawResource) { | ||
if resource.Schema.SubContext == "" { | ||
return | ||
} | ||
|
||
ref := convert.ToReference(resource.Schema.ID) | ||
fullRef := convert.ToFullReference(resource.Schema.Version.Path, resource.Schema.ID) | ||
|
||
outer: | ||
for _, schema := range apiContext.Schemas.Schemas() { | ||
for _, field := range schema.ResourceFields { | ||
if (field.Type == ref || field.Type == fullRef) && schema.Version.SubContexts[resource.Schema.SubContext] { | ||
resource.Links[schema.PluralName] = apiContext.URLBuilder.SubContextCollection(resource.Schema, resource.ID, schema) | ||
continue outer | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package handler | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/rancher/norman/types" | ||
) | ||
|
||
func CreateHandler(apiContext *types.APIContext) error { | ||
var err error | ||
|
||
data, err := ParseAndValidateBody(apiContext) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
store := apiContext.Schema.Store | ||
if store != nil { | ||
data, err = store.Create(apiContext, apiContext.Schema, data) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
apiContext.WriteResponse(http.StatusCreated, data) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package handlers | ||
package handler | ||
|
||
import ( | ||
"net/http" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.