Split RESTStorage into separate interfaces

Omit unimplemented interfaces from Swagger
This commit is contained in:
Clayton Coleman
2015-01-12 00:33:25 -05:00
parent a52b216324
commit 22c99c98e2
15 changed files with 239 additions and 92 deletions

View File

@@ -23,7 +23,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/admission"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/httplog"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
@@ -48,14 +48,13 @@ func (h *RESTHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
notFound(w, req)
return
}
storage := h.storage[kind]
if storage == nil {
httplog.LogOf(req, w).Addf("'%v' has no storage object", kind)
storage, ok := h.storage[kind]
if !ok {
notFound(w, req)
return
}
h.handleRESTStorage(parts, req, w, storage, namespace)
h.handleRESTStorage(parts, req, w, storage, namespace, kind)
}
// Sets the SelfLink field of the object.
@@ -148,7 +147,7 @@ func curry(f func(runtime.Object, *http.Request) error, req *http.Request) func(
// sync=[false|true] Synchronous request (only applies to create, update, delete operations)
// timeout=<duration> Timeout for synchronous requests, only applies if sync=true
// labels=<label-selector> Used for filtering list operations
func (h *RESTHandler) handleRESTStorage(parts []string, req *http.Request, w http.ResponseWriter, storage RESTStorage, namespace string) {
func (h *RESTHandler) handleRESTStorage(parts []string, req *http.Request, w http.ResponseWriter, storage RESTStorage, namespace, kind string) {
ctx := api.WithNamespace(api.NewContext(), namespace)
sync := req.URL.Query().Get("sync") == "true"
timeout := parseTimeout(req.URL.Query().Get("timeout"))
@@ -166,7 +165,12 @@ func (h *RESTHandler) handleRESTStorage(parts []string, req *http.Request, w htt
errorJSON(err, h.codec, w)
return
}
list, err := storage.List(ctx, label, field)
lister, ok := storage.(RESTLister)
if !ok {
errorJSON(errors.NewMethodNotSupported(kind, "list"), h.codec, w)
return
}
list, err := lister.List(ctx, label, field)
if err != nil {
errorJSON(err, h.codec, w)
return
@@ -177,7 +181,12 @@ func (h *RESTHandler) handleRESTStorage(parts []string, req *http.Request, w htt
}
writeJSON(http.StatusOK, h.codec, list, w)
case 2:
item, err := storage.Get(ctx, parts[1])
getter, ok := storage.(RESTGetter)
if !ok {
errorJSON(errors.NewMethodNotSupported(kind, "get"), h.codec, w)
return
}
item, err := getter.Get(ctx, parts[1])
if err != nil {
errorJSON(err, h.codec, w)
return
@@ -196,6 +205,12 @@ func (h *RESTHandler) handleRESTStorage(parts []string, req *http.Request, w htt
notFound(w, req)
return
}
creater, ok := storage.(RESTCreater)
if !ok {
errorJSON(errors.NewMethodNotSupported(kind, "create"), h.codec, w)
return
}
body, err := readBody(req)
if err != nil {
errorJSON(err, h.codec, w)
@@ -215,7 +230,7 @@ func (h *RESTHandler) handleRESTStorage(parts []string, req *http.Request, w htt
return
}
out, err := storage.Create(ctx, obj)
out, err := creater.Create(ctx, obj)
if err != nil {
errorJSON(err, h.codec, w)
return
@@ -228,6 +243,11 @@ func (h *RESTHandler) handleRESTStorage(parts []string, req *http.Request, w htt
notFound(w, req)
return
}
deleter, ok := storage.(RESTDeleter)
if !ok {
errorJSON(errors.NewMethodNotSupported(kind, "delete"), h.codec, w)
return
}
// invoke admission control
err := h.admissionControl.Admit(admission.NewAttributesRecord(nil, namespace, parts[0], "DELETE"))
@@ -236,7 +256,7 @@ func (h *RESTHandler) handleRESTStorage(parts []string, req *http.Request, w htt
return
}
out, err := storage.Delete(ctx, parts[1])
out, err := deleter.Delete(ctx, parts[1])
if err != nil {
errorJSON(err, h.codec, w)
return
@@ -249,6 +269,12 @@ func (h *RESTHandler) handleRESTStorage(parts []string, req *http.Request, w htt
notFound(w, req)
return
}
updater, ok := storage.(RESTUpdater)
if !ok {
errorJSON(errors.NewMethodNotSupported(kind, "create"), h.codec, w)
return
}
body, err := readBody(req)
if err != nil {
errorJSON(err, h.codec, w)
@@ -268,7 +294,7 @@ func (h *RESTHandler) handleRESTStorage(parts []string, req *http.Request, w htt
return
}
out, err := storage.Update(ctx, obj)
out, err := updater.Update(ctx, obj)
if err != nil {
errorJSON(err, h.codec, w)
return