1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-01 15:18:20 +00:00

More initial dev

This commit is contained in:
Darren Shepherd
2017-11-10 21:44:02 -07:00
parent c06696a1e4
commit c8cab3f4f8
72 changed files with 5674 additions and 1211 deletions

45
parse/validate.go Normal file
View File

@@ -0,0 +1,45 @@
package parse
import (
"fmt"
"net/http"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
)
var (
supportedMethods = map[string]bool{
http.MethodPost: true,
http.MethodGet: true,
http.MethodPut: true,
http.MethodDelete: true,
}
)
func ValidateMethod(request *types.APIContext) error {
if request.Action != "" && request.Method == http.MethodPost {
return nil
}
if !supportedMethods[request.Method] {
return httperror.NewAPIError(httperror.METHOD_NOT_ALLOWED, fmt.Sprintf("Method %s not supported", request.Method))
}
if request.Type == "" || request.Schema == nil {
return nil
}
allowed := request.Schema.ResourceMethods
if request.ID == "" {
allowed = request.Schema.CollectionMethods
}
for _, method := range allowed {
if method == request.Method {
return nil
}
}
return httperror.NewAPIError(httperror.METHOD_NOT_ALLOWED, fmt.Sprintf("Method %s not supported", request.Method))
}