Support SubResource call on Request

Allows better semantic use of Request when dealing with sub resources,
and allows clients to ignore ordering.  Supports multiple segments because
sub-resources are less tightly structured than regular resources.
This commit is contained in:
Clayton Coleman
2015-03-08 23:52:53 -04:00
parent 4b16a87096
commit 227a1d306d
2 changed files with 31 additions and 2 deletions

View File

@@ -99,6 +99,7 @@ type Request struct {
namespaceSet bool
resource string
resourceName string
subresource string
selector labels.Selector
timeout time.Duration
@@ -160,6 +161,21 @@ func (r *Request) Resource(resource string) *Request {
return r
}
// SubResource sets a sub-resource path which can be multiple segments segment after the resource
// name but before the suffix.
func (r *Request) SubResource(subresources ...string) *Request {
if r.err != nil {
return r
}
subresource := path.Join(subresources...)
if len(r.subresource) != 0 {
r.err = fmt.Errorf("subresource already set to %q, cannot change to %q", r.resource, subresource)
return r
}
r.subresource = subresource
return r
}
// Name sets the name of a resource to access (<resource>/[ns/<namespace>/]<name>)
func (r *Request) Name(resourceName string) *Request {
if r.err != nil {
@@ -354,8 +370,8 @@ func (r *Request) finalURL() string {
p = path.Join(p, resource)
}
// Join trims trailing slashes, so preserve r.path's trailing slash for backwards compat if nothing was changed
if len(r.resourceName) != 0 || len(r.subpath) != 0 {
p = path.Join(p, r.resourceName, r.subpath)
if len(r.resourceName) != 0 || len(r.subpath) != 0 || len(r.subresource) != 0 {
p = path.Join(p, r.resourceName, r.subresource, r.subpath)
}
finalURL := *r.baseURL