2014-08-09 21:12:55 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package apiserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2015-03-02 23:00:09 +00:00
|
|
|
"net/url"
|
2015-02-12 19:21:47 +00:00
|
|
|
gpath "path"
|
2014-08-09 21:12:55 +00:00
|
|
|
"time"
|
|
|
|
|
2015-01-06 16:44:43 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/admission"
|
2014-08-09 21:12:55 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2015-01-12 05:33:25 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
2014-08-09 21:12:55 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-09-06 02:22:03 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-09-26 00:20:28 +00:00
|
|
|
|
2015-02-09 14:47:13 +00:00
|
|
|
"github.com/emicklei/go-restful"
|
2015-02-12 19:21:47 +00:00
|
|
|
"github.com/golang/glog"
|
2014-08-09 21:12:55 +00:00
|
|
|
)
|
|
|
|
|
2015-02-11 22:09:25 +00:00
|
|
|
// ContextFunc returns a Context given a request - a context must be returned
|
|
|
|
type ContextFunc func(req *restful.Request) api.Context
|
|
|
|
|
2015-02-12 19:21:47 +00:00
|
|
|
// ScopeNamer handles accessing names from requests and objects
|
|
|
|
type ScopeNamer interface {
|
|
|
|
// Namespace returns the appropriate namespace value from the request (may be empty) or an
|
|
|
|
// error.
|
|
|
|
Namespace(req *restful.Request) (namespace string, err error)
|
|
|
|
// Name returns the name from the request, and an optional namespace value if this is a namespace
|
|
|
|
// scoped call. An error is returned if the name is not available.
|
|
|
|
Name(req *restful.Request) (namespace, name string, err error)
|
|
|
|
// ObjectName returns the namespace and name from an object if they exist, or an error if the object
|
|
|
|
// does not support names.
|
|
|
|
ObjectName(obj runtime.Object) (namespace, name string, err error)
|
|
|
|
// SetSelfLink sets the provided URL onto the object. The method should return nil if the object
|
|
|
|
// does not support selfLinks.
|
|
|
|
SetSelfLink(obj runtime.Object, url string) error
|
|
|
|
// GenerateLink creates a path and query for a given runtime object that represents the canonical path.
|
|
|
|
GenerateLink(req *restful.Request, obj runtime.Object) (path, query string, err error)
|
|
|
|
// GenerateLink creates a path and query for a list that represents the canonical path.
|
|
|
|
GenerateListLink(req *restful.Request) (path, query string, err error)
|
|
|
|
}
|
2014-08-09 21:12:55 +00:00
|
|
|
|
2015-02-09 14:47:13 +00:00
|
|
|
// GetResource returns a function that handles retrieving a single resource from a RESTStorage object.
|
2015-02-12 19:21:47 +00:00
|
|
|
func GetResource(r RESTGetter, ctxFn ContextFunc, namer ScopeNamer, codec runtime.Codec) restful.RouteFunction {
|
2015-02-09 14:47:13 +00:00
|
|
|
return func(req *restful.Request, res *restful.Response) {
|
|
|
|
w := res.ResponseWriter
|
2015-02-12 19:21:47 +00:00
|
|
|
namespace, name, err := namer.Name(req)
|
2015-02-09 14:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
notFound(w, req.Request)
|
|
|
|
return
|
|
|
|
}
|
2015-02-11 22:09:25 +00:00
|
|
|
ctx := ctxFn(req)
|
2015-02-12 19:21:47 +00:00
|
|
|
ctx = api.WithNamespace(ctx, namespace)
|
|
|
|
|
|
|
|
result, err := r.Get(ctx, name)
|
2015-02-09 14:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
|
|
|
}
|
2015-02-12 19:21:47 +00:00
|
|
|
if err := setSelfLink(result, req, namer); err != nil {
|
2015-02-09 14:47:13 +00:00
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2014-12-09 19:23:21 +00:00
|
|
|
}
|
2015-02-12 19:21:47 +00:00
|
|
|
writeJSON(http.StatusOK, codec, result, w)
|
2014-11-24 18:35:24 +00:00
|
|
|
}
|
2015-02-09 14:47:13 +00:00
|
|
|
}
|
2014-12-09 19:23:21 +00:00
|
|
|
|
2015-03-02 23:00:09 +00:00
|
|
|
func parseSelectorQueryParams(query url.Values, version, apiResource string) (label, field labels.Selector, err error) {
|
2015-02-25 16:19:10 +00:00
|
|
|
label, err = labels.Parse(query.Get("labels"))
|
2015-03-02 23:00:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
convertToInternalVersionFunc := func(label, value string) (newLabel, newValue string, err error) {
|
|
|
|
return api.Scheme.ConvertFieldLabel(version, apiResource, label, value)
|
|
|
|
}
|
|
|
|
field, err = labels.ParseAndTransformSelector(query.Get("fields"), convertToInternalVersionFunc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return label, field, nil
|
|
|
|
}
|
|
|
|
|
2015-02-09 14:47:13 +00:00
|
|
|
// ListResource returns a function that handles retrieving a list of resources from a RESTStorage object.
|
2015-03-02 23:00:09 +00:00
|
|
|
func ListResource(r RESTLister, ctxFn ContextFunc, namer ScopeNamer, codec runtime.Codec, requestInfoResolver *APIRequestInfoResolver) restful.RouteFunction {
|
2015-02-09 14:47:13 +00:00
|
|
|
return func(req *restful.Request, res *restful.Response) {
|
|
|
|
w := res.ResponseWriter
|
2014-10-30 22:04:11 +00:00
|
|
|
|
2015-02-12 19:21:47 +00:00
|
|
|
namespace, err := namer.Namespace(req)
|
2015-02-09 14:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
notFound(w, req.Request)
|
|
|
|
return
|
|
|
|
}
|
2015-02-11 22:09:25 +00:00
|
|
|
ctx := ctxFn(req)
|
2015-02-12 19:21:47 +00:00
|
|
|
ctx = api.WithNamespace(ctx, namespace)
|
|
|
|
|
2015-03-02 23:00:09 +00:00
|
|
|
requestInfo, err := requestInfoResolver.GetAPIRequestInfo(req.Request)
|
2015-02-09 14:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
|
|
|
}
|
2015-03-02 23:00:09 +00:00
|
|
|
|
|
|
|
label, field, err := parseSelectorQueryParams(req.Request.URL.Query(), requestInfo.APIVersion, requestInfo.Resource)
|
2015-02-09 14:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2014-10-30 22:04:11 +00:00
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
|
2015-02-12 19:21:47 +00:00
|
|
|
result, err := r.List(ctx, label, field)
|
2015-02-09 14:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2014-12-09 19:23:21 +00:00
|
|
|
}
|
2015-02-12 19:21:47 +00:00
|
|
|
if err := setListSelfLink(result, req, namer); err != nil {
|
2015-02-09 14:47:13 +00:00
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
|
|
|
}
|
2015-02-12 19:21:47 +00:00
|
|
|
writeJSON(http.StatusOK, codec, result, w)
|
2014-11-24 18:35:24 +00:00
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 14:47:13 +00:00
|
|
|
// CreateResource returns a function that will handle a resource creation.
|
2015-02-12 19:21:47 +00:00
|
|
|
func CreateResource(r RESTCreater, ctxFn ContextFunc, namer ScopeNamer, codec runtime.Codec, resource string, admit admission.Interface) restful.RouteFunction {
|
2015-02-09 14:47:13 +00:00
|
|
|
return func(req *restful.Request, res *restful.Response) {
|
|
|
|
w := res.ResponseWriter
|
|
|
|
|
|
|
|
// TODO: we either want to remove timeout or document it (if we document, move timeout out of this function and declare it in api_installer)
|
|
|
|
timeout := parseTimeout(req.Request.URL.Query().Get("timeout"))
|
|
|
|
|
2015-02-12 19:21:47 +00:00
|
|
|
namespace, err := namer.Namespace(req)
|
2015-02-09 14:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
notFound(w, req.Request)
|
|
|
|
return
|
|
|
|
}
|
2015-02-11 22:09:25 +00:00
|
|
|
ctx := ctxFn(req)
|
2015-02-12 19:21:47 +00:00
|
|
|
ctx = api.WithNamespace(ctx, namespace)
|
2014-09-26 00:20:28 +00:00
|
|
|
|
2015-02-09 14:47:13 +00:00
|
|
|
body, err := readBody(req.Request)
|
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 14:47:13 +00:00
|
|
|
obj := r.New()
|
|
|
|
if err := codec.DecodeInto(body, obj); err != nil {
|
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-02-09 14:47:13 +00:00
|
|
|
|
|
|
|
err = admit.Admit(admission.NewAttributesRecord(obj, namespace, resource, "CREATE"))
|
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2015-01-12 05:33:25 +00:00
|
|
|
}
|
|
|
|
|
2015-02-10 14:26:26 +00:00
|
|
|
result, err := finishRequest(timeout, func() (runtime.Object, error) {
|
2015-02-11 23:33:14 +00:00
|
|
|
out, err := r.Create(ctx, obj)
|
|
|
|
if status, ok := out.(*api.Status); ok && err == nil && status.Code == 0 {
|
|
|
|
status.Code = http.StatusCreated
|
|
|
|
}
|
|
|
|
return out, err
|
2015-02-10 14:26:26 +00:00
|
|
|
})
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
2015-02-09 14:47:13 +00:00
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-02-09 14:47:13 +00:00
|
|
|
|
2015-02-12 19:21:47 +00:00
|
|
|
if err := setSelfLink(result, req, namer); err != nil {
|
2015-02-09 14:47:13 +00:00
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-01-06 16:44:43 +00:00
|
|
|
|
2015-02-10 14:26:26 +00:00
|
|
|
writeJSON(http.StatusCreated, codec, result, w)
|
2015-02-09 14:47:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateResource returns a function that will handle a resource update
|
2015-02-12 19:21:47 +00:00
|
|
|
func UpdateResource(r RESTUpdater, ctxFn ContextFunc, namer ScopeNamer, codec runtime.Codec, resource string, admit admission.Interface) restful.RouteFunction {
|
2015-02-09 14:47:13 +00:00
|
|
|
return func(req *restful.Request, res *restful.Response) {
|
|
|
|
w := res.ResponseWriter
|
|
|
|
|
|
|
|
// TODO: we either want to remove timeout or document it (if we document, move timeout out of this function and declare it in api_installer)
|
|
|
|
timeout := parseTimeout(req.Request.URL.Query().Get("timeout"))
|
|
|
|
|
2015-02-12 19:21:47 +00:00
|
|
|
namespace, name, err := namer.Name(req)
|
2015-01-06 16:44:43 +00:00
|
|
|
if err != nil {
|
2015-02-09 14:47:13 +00:00
|
|
|
notFound(w, req.Request)
|
|
|
|
return
|
|
|
|
}
|
2015-02-11 22:09:25 +00:00
|
|
|
ctx := ctxFn(req)
|
2015-02-12 19:21:47 +00:00
|
|
|
ctx = api.WithNamespace(ctx, namespace)
|
2015-01-06 16:44:43 +00:00
|
|
|
|
2015-02-09 14:47:13 +00:00
|
|
|
body, err := readBody(req.Request)
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
2015-02-09 14:47:13 +00:00
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 14:47:13 +00:00
|
|
|
obj := r.New()
|
|
|
|
if err := codec.DecodeInto(body, obj); err != nil {
|
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-02-09 14:47:13 +00:00
|
|
|
|
2015-02-12 19:21:47 +00:00
|
|
|
// check the provided name against the request
|
|
|
|
if objNamespace, objName, err := namer.ObjectName(obj); err == nil {
|
|
|
|
if objName != name {
|
|
|
|
errorJSON(errors.NewBadRequest("the name of the object does not match the name on the URL"), codec, w)
|
2015-02-09 14:47:13 +00:00
|
|
|
return
|
|
|
|
}
|
2015-02-12 19:21:47 +00:00
|
|
|
if len(namespace) > 0 {
|
|
|
|
if len(objNamespace) > 0 && objNamespace != namespace {
|
|
|
|
errorJSON(errors.NewBadRequest("the namespace of the object does not match the namespace on the request"), codec, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-01-12 05:33:25 +00:00
|
|
|
}
|
2015-01-06 16:44:43 +00:00
|
|
|
|
2015-02-09 14:47:13 +00:00
|
|
|
err = admit.Admit(admission.NewAttributesRecord(obj, namespace, resource, "UPDATE"))
|
2015-01-06 16:44:43 +00:00
|
|
|
if err != nil {
|
2015-02-09 14:47:13 +00:00
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2015-01-06 16:44:43 +00:00
|
|
|
}
|
|
|
|
|
2015-02-10 14:26:26 +00:00
|
|
|
wasCreated := false
|
|
|
|
result, err := finishRequest(timeout, func() (runtime.Object, error) {
|
|
|
|
obj, created, err := r.Update(ctx, obj)
|
|
|
|
wasCreated = created
|
|
|
|
return obj, err
|
|
|
|
})
|
2015-02-09 14:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-02-09 14:47:13 +00:00
|
|
|
|
2015-02-12 19:21:47 +00:00
|
|
|
if err := setSelfLink(result, req, namer); err != nil {
|
2015-02-09 14:47:13 +00:00
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2015-01-12 05:33:25 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 14:47:13 +00:00
|
|
|
status := http.StatusOK
|
2015-02-10 14:26:26 +00:00
|
|
|
if wasCreated {
|
2015-02-09 14:47:13 +00:00
|
|
|
status = http.StatusCreated
|
|
|
|
}
|
2015-02-10 14:26:26 +00:00
|
|
|
writeJSON(status, codec, result, w)
|
2015-02-09 14:47:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteResource returns a function that will handle a resource deletion
|
2015-02-12 19:21:47 +00:00
|
|
|
func DeleteResource(r RESTDeleter, ctxFn ContextFunc, namer ScopeNamer, codec runtime.Codec, resource, kind string, admit admission.Interface) restful.RouteFunction {
|
2015-02-09 14:47:13 +00:00
|
|
|
return func(req *restful.Request, res *restful.Response) {
|
|
|
|
w := res.ResponseWriter
|
|
|
|
|
|
|
|
// TODO: we either want to remove timeout or document it (if we document, move timeout out of this function and declare it in api_installer)
|
|
|
|
timeout := parseTimeout(req.Request.URL.Query().Get("timeout"))
|
|
|
|
|
2015-02-12 19:21:47 +00:00
|
|
|
namespace, name, err := namer.Name(req)
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
2015-02-09 14:47:13 +00:00
|
|
|
notFound(w, req.Request)
|
|
|
|
return
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-02-11 22:09:25 +00:00
|
|
|
ctx := ctxFn(req)
|
2015-02-09 14:47:13 +00:00
|
|
|
if len(namespace) > 0 {
|
|
|
|
ctx = api.WithNamespace(ctx, namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = admit.Admit(admission.NewAttributesRecord(nil, namespace, resource, "DELETE"))
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
2015-02-09 14:47:13 +00:00
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-01-06 16:44:43 +00:00
|
|
|
|
2015-02-10 14:26:26 +00:00
|
|
|
result, err := finishRequest(timeout, func() (runtime.Object, error) {
|
|
|
|
return r.Delete(ctx, name)
|
|
|
|
})
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
2015-02-09 14:47:13 +00:00
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 14:47:13 +00:00
|
|
|
// if the RESTDeleter returns a nil object, fill out a status. Callers may return a valid
|
|
|
|
// object with the response.
|
2015-02-10 14:26:26 +00:00
|
|
|
if result == nil {
|
|
|
|
result = &api.Status{
|
2015-02-09 14:47:13 +00:00
|
|
|
Status: api.StatusSuccess,
|
|
|
|
Code: http.StatusOK,
|
|
|
|
Details: &api.StatusDetails{
|
|
|
|
ID: name,
|
|
|
|
Kind: kind,
|
|
|
|
},
|
|
|
|
}
|
2015-02-10 14:26:26 +00:00
|
|
|
} else {
|
|
|
|
// when a non-status response is returned, set the self link
|
|
|
|
if _, ok := result.(*api.Status); !ok {
|
2015-02-12 19:21:47 +00:00
|
|
|
if err := setSelfLink(result, req, namer); err != nil {
|
2015-02-10 14:26:26 +00:00
|
|
|
errorJSON(err, codec, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-02-09 14:47:13 +00:00
|
|
|
}
|
2015-02-10 14:26:26 +00:00
|
|
|
writeJSON(http.StatusOK, codec, result, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 14:26:26 +00:00
|
|
|
// resultFunc is a function that returns a rest result and can be run in a goroutine
|
|
|
|
type resultFunc func() (runtime.Object, error)
|
|
|
|
|
|
|
|
// finishRequest makes a given resultFunc asynchronous and handles errors returned by the response.
|
2015-02-09 14:47:13 +00:00
|
|
|
// Any api.Status object returned is considered an "error", which interrupts the normal response flow.
|
2015-02-10 14:26:26 +00:00
|
|
|
func finishRequest(timeout time.Duration, fn resultFunc) (result runtime.Object, err error) {
|
|
|
|
ch := make(chan runtime.Object)
|
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
if result, err := fn(); err != nil {
|
|
|
|
errCh <- err
|
|
|
|
} else {
|
|
|
|
ch <- result
|
2015-02-09 14:47:13 +00:00
|
|
|
}
|
2015-02-10 14:26:26 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case result = <-ch:
|
|
|
|
if status, ok := result.(*api.Status); ok {
|
2015-02-09 14:47:13 +00:00
|
|
|
return nil, errors.FromObject(status)
|
|
|
|
}
|
2015-02-10 14:26:26 +00:00
|
|
|
return result, nil
|
|
|
|
case err = <-errCh:
|
|
|
|
return nil, err
|
2015-02-09 14:47:13 +00:00
|
|
|
case <-time.After(timeout):
|
|
|
|
return nil, errors.NewTimeoutError("request did not complete within allowed duration")
|
|
|
|
}
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 14:47:13 +00:00
|
|
|
// setSelfLink sets the self link of an object (or the child items in a list) to the base URL of the request
|
|
|
|
// plus the path and query generated by the provided linkFunc
|
2015-02-12 19:21:47 +00:00
|
|
|
func setSelfLink(obj runtime.Object, req *restful.Request, namer ScopeNamer) error {
|
|
|
|
// TODO: SelfLink generation should return a full URL?
|
|
|
|
path, query, err := namer.GenerateLink(req, obj)
|
|
|
|
if err == errEmptyName {
|
|
|
|
return nil
|
2015-02-09 14:47:13 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-12 19:21:47 +00:00
|
|
|
newURL := *req.Request.URL
|
|
|
|
// use only canonical paths
|
|
|
|
newURL.Path = gpath.Clean(path)
|
2015-02-09 14:47:13 +00:00
|
|
|
newURL.RawQuery = query
|
|
|
|
newURL.Fragment = ""
|
|
|
|
|
2015-02-12 19:21:47 +00:00
|
|
|
return namer.SetSelfLink(obj, newURL.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// setListSelfLink sets the self link of a list to the base URL, then sets the self links
|
|
|
|
// on all child objects returned.
|
|
|
|
func setListSelfLink(obj runtime.Object, req *restful.Request, namer ScopeNamer) error {
|
2015-02-09 14:47:13 +00:00
|
|
|
if !runtime.IsListType(obj) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 19:21:47 +00:00
|
|
|
// TODO: List SelfLink generation should return a full URL?
|
|
|
|
path, query, err := namer.GenerateListLink(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newURL := *req.Request.URL
|
|
|
|
newURL.Path = path
|
|
|
|
newURL.RawQuery = query
|
|
|
|
// use the path that got us here
|
|
|
|
newURL.Fragment = ""
|
|
|
|
if err := namer.SetSelfLink(obj, newURL.String()); err != nil {
|
|
|
|
glog.V(4).Infof("Unable to set self link on object: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-02-09 14:47:13 +00:00
|
|
|
// Set self-link of objects in the list.
|
|
|
|
items, err := runtime.ExtractList(obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range items {
|
2015-02-12 19:21:47 +00:00
|
|
|
if err := setSelfLink(items[i], req, namer); err != nil {
|
2015-02-09 14:47:13 +00:00
|
|
|
return err
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-09 14:47:13 +00:00
|
|
|
return runtime.SetList(obj, items)
|
2015-02-12 19:21:47 +00:00
|
|
|
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|