Merge pull request #10060 from nikhiljindal/scopeParamPath

Updating the path param name to "namespace" instead of "namespaces"
This commit is contained in:
Maxwell Forbes 2015-06-25 09:56:15 -07:00
commit fc349fdd47
5 changed files with 445 additions and 459 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -115,10 +115,8 @@ type RESTScope interface {
// ParamName is the optional name of the parameter that should be inserted in the resource url // ParamName is the optional name of the parameter that should be inserted in the resource url
// If empty, no param will be inserted // If empty, no param will be inserted
ParamName() string ParamName() string
// ParamPath is a boolean that controls how the parameter is manifested in resource paths // ArgumentName is the optional name that should be used for the variable holding the value.
// If true, this parameter is encoded in path (i.e. /{paramName}/{paramValue}) ArgumentName() string
// If false, this parameter is encoded in query (i.e. ?{paramName}={paramValue})
ParamPath() bool
// ParamDescription is the optional description to use to document the parameter in api documentation // ParamDescription is the optional description to use to document the parameter in api documentation
ParamDescription() string ParamDescription() string
} }

View File

@ -26,7 +26,7 @@ import (
type restScope struct { type restScope struct {
name RESTScopeName name RESTScopeName
paramName string paramName string
paramPath bool argumentName string
paramDescription string paramDescription string
} }
@ -36,24 +36,17 @@ func (r *restScope) Name() RESTScopeName {
func (r *restScope) ParamName() string { func (r *restScope) ParamName() string {
return r.paramName return r.paramName
} }
func (r *restScope) ParamPath() bool { func (r *restScope) ArgumentName() string {
return r.paramPath return r.argumentName
} }
func (r *restScope) ParamDescription() string { func (r *restScope) ParamDescription() string {
return r.paramDescription return r.paramDescription
} }
var RESTScopeNamespaceLegacy = &restScope{
name: RESTScopeNameNamespace,
paramName: "namespace",
paramPath: false,
paramDescription: "object name and auth scope, such as for teams and projects",
}
var RESTScopeNamespace = &restScope{ var RESTScopeNamespace = &restScope{
name: RESTScopeNameNamespace, name: RESTScopeNameNamespace,
paramName: "namespaces", paramName: "namespaces",
paramPath: true, argumentName: "namespace",
paramDescription: "object name and auth scope, such as for teams and projects", paramDescription: "object name and auth scope, such as for teams and projects",
} }

View File

@ -250,7 +250,8 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage rest.Storag
actions := []action{} actions := []action{}
// Get the list of actions for the given scope. // Get the list of actions for the given scope.
if scope.Name() != meta.RESTScopeNameNamespace { switch scope.Name() {
case meta.RESTScopeNameRoot:
// Handle non-namespace scoped resources like nodes. // Handle non-namespace scoped resources like nodes.
resourcePath := resource resourcePath := resource
resourceParams := params resourceParams := params
@ -283,14 +284,11 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage rest.Storag
actions = appendIf(actions, action{"PROXY", "proxy/" + itemPath, nameParams, namer}, isRedirector) actions = appendIf(actions, action{"PROXY", "proxy/" + itemPath, nameParams, namer}, isRedirector)
actions = appendIf(actions, action{"CONNECT", itemPath, nameParams, namer}, isConnecter) actions = appendIf(actions, action{"CONNECT", itemPath, nameParams, namer}, isConnecter)
actions = appendIf(actions, action{"CONNECT", itemPath + "/{path:*}", proxyParams, namer}, isConnecter && connectSubpath) actions = appendIf(actions, action{"CONNECT", itemPath + "/{path:*}", proxyParams, namer}, isConnecter && connectSubpath)
break
} else { case meta.RESTScopeNameNamespace:
// Handle namespace scoped resources like pods.
if scope.ParamPath() {
// Handle the case when namespace is part of the path.
// Handler for standard REST verbs (GET, PUT, POST and DELETE). // Handler for standard REST verbs (GET, PUT, POST and DELETE).
namespaceParam := ws.PathParameter(scope.ParamName(), scope.ParamDescription()).DataType("string") namespaceParam := ws.PathParameter(scope.ArgumentName(), scope.ParamDescription()).DataType("string")
namespacedPath := scope.ParamName() + "/{" + scope.ParamName() + "}/" + resource namespacedPath := scope.ParamName() + "/{" + scope.ArgumentName() + "}/" + resource
namespaceParams := []*restful.Parameter{namespaceParam} namespaceParams := []*restful.Parameter{namespaceParam}
resourcePath := namespacedPath resourcePath := namespacedPath
@ -305,13 +303,11 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage rest.Storag
} }
namer := scopeNaming{scope, a.group.Linker, gpath.Join(a.prefix, itemPath), false} namer := scopeNaming{scope, a.group.Linker, gpath.Join(a.prefix, itemPath), false}
// Add actions at the resource path: /api/apiVersion/namespaces/{namespaces}/resource
actions = appendIf(actions, action{"LIST", resourcePath, resourceParams, namer}, isLister) actions = appendIf(actions, action{"LIST", resourcePath, resourceParams, namer}, isLister)
actions = appendIf(actions, action{"POST", resourcePath, resourceParams, namer}, isCreater) actions = appendIf(actions, action{"POST", resourcePath, resourceParams, namer}, isCreater)
// DEPRECATED // DEPRECATED
actions = appendIf(actions, action{"WATCHLIST", "watch/" + resourcePath, resourceParams, namer}, allowWatchList) actions = appendIf(actions, action{"WATCHLIST", "watch/" + resourcePath, resourceParams, namer}, allowWatchList)
// Add actions at the item path: /api/apiVersion/namespaces/{namespaces}/resource/{name}
actions = appendIf(actions, action{"GET", itemPath, nameParams, namer}, isGetter) actions = appendIf(actions, action{"GET", itemPath, nameParams, namer}, isGetter)
if getSubpath { if getSubpath {
actions = appendIf(actions, action{"GET", itemPath + "/{path:*}", proxyParams, namer}, isGetter) actions = appendIf(actions, action{"GET", itemPath + "/{path:*}", proxyParams, namer}, isGetter)
@ -334,10 +330,9 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage rest.Storag
actions = appendIf(actions, action{"POST", resource, params, namer}, isCreater) actions = appendIf(actions, action{"POST", resource, params, namer}, isCreater)
actions = appendIf(actions, action{"WATCHLIST", "watch/" + resource, params, namer}, allowWatchList) actions = appendIf(actions, action{"WATCHLIST", "watch/" + resource, params, namer}, allowWatchList)
} }
} else { break
// Legacy behavior: Namespace as param is no longer supported default:
return fmt.Errorf("namespace as a parameter is no longer supported") return fmt.Errorf("unsupported restscope: %s", scope.Name())
}
} }
// Create Routes for the actions. // Create Routes for the actions.
@ -658,7 +653,7 @@ func (n scopeNaming) Namespace(req *restful.Request) (namespace string, err erro
if n.allNamespaces { if n.allNamespaces {
return "", nil return "", nil
} }
namespace = req.PathParameter(n.scope.ParamName()) namespace = req.PathParameter(n.scope.ArgumentName())
if len(namespace) == 0 { if len(namespace) == 0 {
// a URL was constructed without the namespace, or this method was invoked // a URL was constructed without the namespace, or this method was invoked
// on an object without a namespace path parameter. // on an object without a namespace path parameter.
@ -694,7 +689,7 @@ func (n scopeNaming) GenerateLink(req *restful.Request, obj runtime.Object) (pat
return "", "", errEmptyName return "", "", errEmptyName
} }
path = strings.Replace(n.itemPath, "{name}", name, 1) path = strings.Replace(n.itemPath, "{name}", name, 1)
path = strings.Replace(path, "{"+n.scope.ParamName()+"}", namespace, 1) path = strings.Replace(path, "{"+n.scope.ArgumentName()+"}", namespace, 1)
return path, "", nil return path, "", nil
} }