Update references from Path() to the appropriate segment use

This commit is contained in:
Clayton Coleman
2014-12-26 15:06:25 -05:00
parent afedbba3fc
commit e355f54eda
20 changed files with 258 additions and 111 deletions

View File

@@ -53,14 +53,14 @@ func newEndpoints(c *Client, namespace string) *endpoints {
// Create creates a new endpoint.
func (c *endpoints) Create(endpoints *api.Endpoints) (*api.Endpoints, error) {
result := &api.Endpoints{}
err := c.r.Post().Namespace(c.ns).Path("endpoints").Body(endpoints).Do().Into(result)
err := c.r.Post().Namespace(c.ns).Resource("endpoints").Body(endpoints).Do().Into(result)
return result, err
}
// List takes a selector, and returns the list of endpoints that match that selector
func (c *endpoints) List(selector labels.Selector) (result *api.EndpointsList, err error) {
result = &api.EndpointsList{}
err = c.r.Get().Namespace(c.ns).Path("endpoints").SelectorParam("labels", selector).Do().Into(result)
err = c.r.Get().Namespace(c.ns).Resource("endpoints").SelectorParam("labels", selector).Do().Into(result)
return
}
@@ -71,16 +71,16 @@ func (c *endpoints) Get(name string) (result *api.Endpoints, err error) {
}
result = &api.Endpoints{}
err = c.r.Get().Namespace(c.ns).Path("endpoints").Path(name).Do().Into(result)
err = c.r.Get().Namespace(c.ns).Resource("endpoints").Name(name).Do().Into(result)
return
}
// Watch returns a watch.Interface that watches the requested endpoints for a service.
func (c *endpoints) Watch(label, field labels.Selector, resourceVersion string) (watch.Interface, error) {
return c.r.Get().
Path("watch").
Prefix("watch").
Namespace(c.ns).
Path("endpoints").
Resource("endpoints").
Param("resourceVersion", resourceVersion).
SelectorParam("labels", label).
SelectorParam("fields", field).
@@ -94,8 +94,8 @@ func (c *endpoints) Update(endpoints *api.Endpoints) (*api.Endpoints, error) {
}
err := c.r.Put().
Namespace(c.ns).
Path("endpoints").
Path(endpoints.Name).
Resource("endpoints").
Name(endpoints.Name).
Body(endpoints).
Do().
Into(result)

View File

@@ -66,7 +66,7 @@ func (e *events) Create(event *api.Event) (*api.Event, error) {
result := &api.Event{}
err := e.client.Post().
Namespace(event.Namespace).
Path("events").
Resource("events").
Body(event).
Do().
Into(result)
@@ -78,7 +78,7 @@ func (e *events) List(label, field labels.Selector) (*api.EventList, error) {
result := &api.EventList{}
err := e.client.Get().
Namespace(e.namespace).
Path("events").
Resource("events").
SelectorParam("labels", label).
SelectorParam("fields", field).
Do().
@@ -95,8 +95,8 @@ func (e *events) Get(name string) (*api.Event, error) {
result := &api.Event{}
err := e.client.Get().
Namespace(e.namespace).
Path("events").
Path(name).
Resource("events").
Name(name).
Do().
Into(result)
return result, err
@@ -105,9 +105,9 @@ func (e *events) Get(name string) (*api.Event, error) {
// Watch starts watching for events matching the given selectors.
func (e *events) Watch(label, field labels.Selector, resourceVersion string) (watch.Interface, error) {
return e.client.Get().
Path("watch").
Prefix("watch").
Namespace(e.namespace).
Path("events").
Resource("events").
Param("resourceVersion", resourceVersion).
SelectorParam("labels", label).
SelectorParam("fields", field).

View File

@@ -55,14 +55,14 @@ func (c *nodes) resourceName() string {
// Create creates a new minion.
func (c *nodes) Create(minion *api.Node) (*api.Node, error) {
result := &api.Node{}
err := c.r.Post().Path(c.resourceName()).Body(minion).Do().Into(result)
err := c.r.Post().Resource(c.resourceName()).Body(minion).Do().Into(result)
return result, err
}
// List lists all the nodes in the cluster.
func (c *nodes) List() (*api.NodeList, error) {
result := &api.NodeList{}
err := c.r.Get().Path(c.resourceName()).Do().Into(result)
err := c.r.Get().Resource(c.resourceName()).Do().Into(result)
return result, err
}
@@ -73,11 +73,11 @@ func (c *nodes) Get(name string) (*api.Node, error) {
}
result := &api.Node{}
err := c.r.Get().Path(c.resourceName()).Path(name).Do().Into(result)
err := c.r.Get().Resource(c.resourceName()).Name(name).Do().Into(result)
return result, err
}
// Delete deletes an existing minion.
func (c *nodes) Delete(name string) error {
return c.r.Delete().Path(c.resourceName()).Path(name).Do().Error()
return c.r.Delete().Resource(c.resourceName()).Name(name).Do().Error()
}

View File

@@ -55,7 +55,7 @@ func newPods(c *Client, namespace string) *pods {
// ListPods takes a selector, and returns the list of pods that match that selector.
func (c *pods) List(selector labels.Selector) (result *api.PodList, err error) {
result = &api.PodList{}
err = c.r.Get().Namespace(c.ns).Path("pods").SelectorParam("labels", selector).Do().Into(result)
err = c.r.Get().Namespace(c.ns).Resource("pods").SelectorParam("labels", selector).Do().Into(result)
return
}
@@ -66,19 +66,19 @@ func (c *pods) Get(name string) (result *api.Pod, err error) {
}
result = &api.Pod{}
err = c.r.Get().Namespace(c.ns).Path("pods").Path(name).Do().Into(result)
err = c.r.Get().Namespace(c.ns).Resource("pods").Name(name).Do().Into(result)
return
}
// DeletePod takes the name of the pod, and returns an error if one occurs
func (c *pods) Delete(name string) error {
return c.r.Delete().Namespace(c.ns).Path("pods").Path(name).Do().Error()
return c.r.Delete().Namespace(c.ns).Resource("pods").Name(name).Do().Error()
}
// CreatePod takes the representation of a pod. Returns the server's representation of the pod, and an error, if it occurs.
func (c *pods) Create(pod *api.Pod) (result *api.Pod, err error) {
result = &api.Pod{}
err = c.r.Post().Namespace(c.ns).Path("pods").Body(pod).Do().Into(result)
err = c.r.Post().Namespace(c.ns).Resource("pods").Body(pod).Do().Into(result)
return
}
@@ -89,6 +89,6 @@ func (c *pods) Update(pod *api.Pod) (result *api.Pod, err error) {
err = fmt.Errorf("invalid update object, missing resource version: %v", pod)
return
}
err = c.r.Put().Namespace(c.ns).Path("pods").Path(pod.Name).Body(pod).Do().Into(result)
err = c.r.Put().Namespace(c.ns).Resource("pods").Name(pod.Name).Body(pod).Do().Into(result)
return
}

View File

@@ -54,7 +54,7 @@ func newReplicationControllers(c *Client, namespace string) *replicationControll
// List takes a selector, and returns the list of replication controllers that match that selector.
func (c *replicationControllers) List(selector labels.Selector) (result *api.ReplicationControllerList, err error) {
result = &api.ReplicationControllerList{}
err = c.r.Get().Namespace(c.ns).Path("replicationControllers").SelectorParam("labels", selector).Do().Into(result)
err = c.r.Get().Namespace(c.ns).Resource("replicationControllers").SelectorParam("labels", selector).Do().Into(result)
return
}
@@ -65,14 +65,14 @@ func (c *replicationControllers) Get(name string) (result *api.ReplicationContro
}
result = &api.ReplicationController{}
err = c.r.Get().Namespace(c.ns).Path("replicationControllers").Path(name).Do().Into(result)
err = c.r.Get().Namespace(c.ns).Resource("replicationControllers").Name(name).Do().Into(result)
return
}
// Create creates a new replication controller.
func (c *replicationControllers) Create(controller *api.ReplicationController) (result *api.ReplicationController, err error) {
result = &api.ReplicationController{}
err = c.r.Post().Namespace(c.ns).Path("replicationControllers").Body(controller).Do().Into(result)
err = c.r.Post().Namespace(c.ns).Resource("replicationControllers").Body(controller).Do().Into(result)
return
}
@@ -83,21 +83,21 @@ func (c *replicationControllers) Update(controller *api.ReplicationController) (
err = fmt.Errorf("invalid update object, missing resource version: %v", controller)
return
}
err = c.r.Put().Namespace(c.ns).Path("replicationControllers").Path(controller.Name).Body(controller).Do().Into(result)
err = c.r.Put().Namespace(c.ns).Resource("replicationControllers").Name(controller.Name).Body(controller).Do().Into(result)
return
}
// Delete deletes an existing replication controller.
func (c *replicationControllers) Delete(name string) error {
return c.r.Delete().Namespace(c.ns).Path("replicationControllers").Path(name).Do().Error()
return c.r.Delete().Namespace(c.ns).Resource("replicationControllers").Name(name).Do().Error()
}
// Watch returns a watch.Interface that watches the requested controllers.
func (c *replicationControllers) Watch(label, field labels.Selector, resourceVersion string) (watch.Interface, error) {
return c.r.Get().
Path("watch").
Prefix("watch").
Namespace(c.ns).
Path("replicationControllers").
Resource("replicationControllers").
Param("resourceVersion", resourceVersion).
SelectorParam("labels", label).
SelectorParam("fields", field).

View File

@@ -32,6 +32,7 @@ import (
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
apierrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
@@ -54,9 +55,12 @@ func TestRequestWithErrorWontChange(t *testing.T) {
SelectorParam("labels", labels.Set{"a": "b"}.AsSelector()).
UintParam("uint", 1).
AbsPath("/abs").
Path("test").
Prefix("test").
Suffix("testing").
ParseSelectorParam("foo", "a=b").
Namespace("new").
Resource("foos").
Name("bars").
NoPoll().
Body("foo").
Poller(skipPolling).
@@ -70,6 +74,74 @@ func TestRequestWithErrorWontChange(t *testing.T) {
}
}
func TestRequestPreservesBaseTrailingSlash(t *testing.T) {
r := &Request{baseURL: &url.URL{}, path: "/path/"}
if s := r.finalURL(); s != "/path/" {
t.Errorf("trailing slash should be preserved: %s", s)
}
}
func TestRequestAbsPathPreservesTrailingSlash(t *testing.T) {
r := (&Request{baseURL: &url.URL{}}).AbsPath("/foo/")
if s := r.finalURL(); s != "/foo/" {
t.Errorf("trailing slash should be preserved: %s", s)
}
}
func TestRequestAbsPathJoins(t *testing.T) {
r := (&Request{baseURL: &url.URL{}}).AbsPath("foo/bar", "baz")
if s := r.finalURL(); s != "foo/bar/baz" {
t.Errorf("trailing slash should be preserved: %s", s)
}
}
func TestRequestSetsNamespace(t *testing.T) {
r := (&Request{
baseURL: &url.URL{
Path: "/",
},
}).Namespace("foo")
if r.namespace == "" {
t.Errorf("namespace should be set: %#v", r)
}
if s := r.finalURL(); s != "?namespace=foo" {
t.Errorf("namespace should be in params: %s", s)
}
r = (&Request{
baseURL: &url.URL{
Path: "/",
},
namespaceInPath: true,
}).Namespace("foo")
if s := r.finalURL(); s != "ns/foo" {
t.Errorf("namespace should be in path: %s", s)
}
}
func TestRequestOrdersNamespaceInPath(t *testing.T) {
r := (&Request{
baseURL: &url.URL{},
path: "/test/",
namespaceInPath: true,
}).Name("bar").Resource("baz").Namespace("foo")
if s := r.finalURL(); s != "/test/ns/foo/baz/bar" {
t.Errorf("namespace should be in order in path: %s", s)
}
}
func TestRequestSetTwiceError(t *testing.T) {
if (&Request{}).Name("bar").Name("baz").err == nil {
t.Errorf("setting name twice should result in error")
}
if (&Request{}).Namespace("bar").Namespace("baz").err == nil {
t.Errorf("setting namespace twice should result in error")
}
if (&Request{}).Resource("bar").Resource("baz").err == nil {
t.Errorf("setting resource twice should result in error")
}
}
func TestRequestParseSelectorParam(t *testing.T) {
r := (&Request{}).ParseSelectorParam("foo", "a")
if r.err == nil || r.params != nil {
@@ -133,6 +205,9 @@ func TestTransformResponse(t *testing.T) {
{Response: &http.Response{StatusCode: 201}, Data: []byte{}, Created: true},
{Response: &http.Response{StatusCode: 199}, Error: true},
{Response: &http.Response{StatusCode: 500}, Error: true},
{Response: &http.Response{StatusCode: 422}, Error: true},
{Response: &http.Response{StatusCode: 409}, Error: true},
{Response: &http.Response{StatusCode: 404}, Error: true},
{Response: &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewReader(invalid))}, Data: invalid},
{Response: &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewReader(invalid))}, Data: invalid},
}
@@ -155,6 +230,80 @@ func TestTransformResponse(t *testing.T) {
}
}
func TestTransformUnstructuredError(t *testing.T) {
testCases := []struct {
Req *http.Request
Res *http.Response
Resource string
Name string
ErrFn func(error) bool
}{
{
Resource: "foo",
Name: "bar",
Req: &http.Request{
Method: "POST",
},
Res: &http.Response{
StatusCode: http.StatusConflict,
Body: ioutil.NopCloser(bytes.NewReader(nil)),
},
ErrFn: apierrors.IsAlreadyExists,
},
{
Resource: "foo",
Name: "bar",
Req: &http.Request{
Method: "PUT",
},
Res: &http.Response{
StatusCode: http.StatusConflict,
Body: ioutil.NopCloser(bytes.NewReader(nil)),
},
ErrFn: apierrors.IsConflict,
},
{
Resource: "foo",
Name: "bar",
Req: &http.Request{},
Res: &http.Response{
StatusCode: http.StatusNotFound,
Body: ioutil.NopCloser(bytes.NewReader(nil)),
},
ErrFn: apierrors.IsNotFound,
},
{
Req: &http.Request{},
Res: &http.Response{
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(bytes.NewReader(nil)),
},
ErrFn: apierrors.IsBadRequest,
},
}
for _, testCase := range testCases {
r := &Request{
codec: latest.Codec,
resourceName: testCase.Name,
resource: testCase.Resource,
}
_, _, err := r.transformResponse(testCase.Res, testCase.Req)
if !testCase.ErrFn(err) {
t.Errorf("unexpected error: %v", err)
continue
}
if len(testCase.Name) != 0 && !strings.Contains(err.Error(), testCase.Name) {
t.Errorf("unexpected error string: %s", err)
}
if len(testCase.Resource) != 0 && !strings.Contains(err.Error(), testCase.Resource) {
t.Errorf("unexpected error string: %s", err)
}
}
}
type clientFunc func(req *http.Request) (*http.Response, error)
func (f clientFunc) Do(req *http.Request) (*http.Response, error) {
@@ -333,8 +482,8 @@ func TestDoRequestNewWay(t *testing.T) {
defer testServer.Close()
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta2", Username: "user", Password: "pass"})
obj, err := c.Verb("POST").
Path("foo/bar").
Path("baz").
Prefix("foo", "bar").
Suffix("baz").
ParseSelectorParam("labels", "name=foo").
Timeout(time.Second).
Body([]byte(reqBody)).
@@ -367,8 +516,9 @@ func TestDoRequestNewWayReader(t *testing.T) {
testServer := httptest.NewServer(&fakeHandler)
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta1", Username: "user", Password: "pass"})
obj, err := c.Verb("POST").
Path("foo/bar").
Path("baz").
Resource("bar").
Name("baz").
Prefix("foo").
SelectorParam("labels", labels.Set{"name": "foo"}.AsSelector()).
Sync(true).
Timeout(time.Second).
@@ -403,8 +553,9 @@ func TestDoRequestNewWayObj(t *testing.T) {
testServer := httptest.NewServer(&fakeHandler)
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta2", Username: "user", Password: "pass"})
obj, err := c.Verb("POST").
Path("foo/bar").
Path("baz").
Suffix("baz").
Name("bar").
Resource("foo").
SelectorParam("labels", labels.Set{"name": "foo"}.AsSelector()).
Timeout(time.Second).
Body(reqObj).
@@ -453,8 +604,7 @@ func TestDoRequestNewWayFile(t *testing.T) {
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta1", Username: "user", Password: "pass"})
wasCreated := true
obj, err := c.Verb("POST").
Path("foo/bar").
Path("baz").
Prefix("foo/bar", "baz").
ParseSelectorParam("labels", "name=foo").
Timeout(time.Second).
Body(file.Name()).
@@ -496,8 +646,7 @@ func TestWasCreated(t *testing.T) {
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta1", Username: "user", Password: "pass"})
wasCreated := false
obj, err := c.Verb("PUT").
Path("foo/bar").
Path("baz").
Prefix("foo/bar", "baz").
ParseSelectorParam("labels", "name=foo").
Timeout(time.Second).
Body(reqBodyExpected).
@@ -541,7 +690,7 @@ func TestVerbs(t *testing.T) {
func TestAbsPath(t *testing.T) {
expectedPath := "/bar/foo"
c := NewOrDie(&Config{})
r := c.Post().Path("/foo").AbsPath(expectedPath)
r := c.Post().Prefix("/foo").AbsPath(expectedPath)
if r.path != expectedPath {
t.Errorf("unexpected path: %s, expected %s", r.path, expectedPath)
}
@@ -792,7 +941,7 @@ func TestWatch(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
watching, err := s.Get().Path("path/to/watch/thing").Watch()
watching, err := s.Get().Prefix("path/to/watch/thing").Watch()
if err != nil {
t.Fatalf("Unexpected error")
}
@@ -841,7 +990,7 @@ func TestStream(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
readCloser, err := s.Get().Path("path/to/stream/thing").Stream()
readCloser, err := s.Get().Prefix("path/to/stream/thing").Stream()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

View File

@@ -59,7 +59,9 @@ type RESTClient struct {
// NewRESTClient creates a new RESTClient. This client performs generic REST functions
// such as Get, Put, Post, and Delete on specified paths. Codec controls encoding and
// decoding of responses from the server.
// decoding of responses from the server. If the namespace should be specified as part
// of the path (after the resource), set namespaceInPath to true, otherwise it will be
// passed as "namespace" in the query string.
func NewRESTClient(baseURL *url.URL, c runtime.Codec, namespaceInPath bool) *RESTClient {
base := *baseURL
if !strings.HasSuffix(base.Path, "/") {
@@ -129,7 +131,7 @@ func (c *RESTClient) Delete() *Request {
// PollFor makes a request to do a single poll of the completion of the given operation.
func (c *RESTClient) Operation(name string) *Request {
return c.Get().Path("operations").Path(name).Sync(false).NoPoll()
return c.Get().Resource("operations").Name(name).Sync(false).NoPoll()
}
func (c *RESTClient) DefaultPoll(name string) (*Request, bool) {

View File

@@ -137,7 +137,7 @@ func TestDoRequestAccepted(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
body, err := c.Get().Path("test").Do().Raw()
body, err := c.Get().Prefix("test").Do().Raw()
if err == nil {
t.Fatalf("Unexpected non-error")
}
@@ -171,7 +171,7 @@ func TestDoRequestAcceptedSuccess(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
body, err := c.Get().Path("test").Do().Raw()
body, err := c.Get().Prefix("test").Do().Raw()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -231,7 +231,7 @@ func TestDoRequestCreated(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
created := false
body, err := c.Get().Path("test").Do().WasCreated(&created).Raw()
body, err := c.Get().Prefix("test").Do().WasCreated(&created).Raw()
if err != nil {
t.Errorf("Unexpected error %#v", err)
}

View File

@@ -54,7 +54,7 @@ func newServices(c *Client, namespace string) *services {
// List takes a selector, and returns the list of services that match that selector
func (c *services) List(selector labels.Selector) (result *api.ServiceList, err error) {
result = &api.ServiceList{}
err = c.r.Get().Namespace(c.ns).Path("services").SelectorParam("labels", selector).Do().Into(result)
err = c.r.Get().Namespace(c.ns).Resource("services").SelectorParam("labels", selector).Do().Into(result)
return
}
@@ -65,14 +65,14 @@ func (c *services) Get(name string) (result *api.Service, err error) {
}
result = &api.Service{}
err = c.r.Get().Namespace(c.ns).Path("services").Path(name).Do().Into(result)
err = c.r.Get().Namespace(c.ns).Resource("services").Name(name).Do().Into(result)
return
}
// Create creates a new service.
func (c *services) Create(svc *api.Service) (result *api.Service, err error) {
result = &api.Service{}
err = c.r.Post().Namespace(c.ns).Path("services").Body(svc).Do().Into(result)
err = c.r.Post().Namespace(c.ns).Resource("services").Body(svc).Do().Into(result)
return
}
@@ -83,21 +83,21 @@ func (c *services) Update(svc *api.Service) (result *api.Service, err error) {
err = fmt.Errorf("invalid update object, missing resource version: %v", svc)
return
}
err = c.r.Put().Namespace(c.ns).Path("services").Path(svc.Name).Body(svc).Do().Into(result)
err = c.r.Put().Namespace(c.ns).Resource("services").Name(svc.Name).Body(svc).Do().Into(result)
return
}
// Delete deletes an existing service.
func (c *services) Delete(name string) error {
return c.r.Delete().Namespace(c.ns).Path("services").Path(name).Do().Error()
return c.r.Delete().Namespace(c.ns).Resource("services").Name(name).Do().Error()
}
// Watch returns a watch.Interface that watches the requested services.
func (c *services) Watch(label, field labels.Selector, resourceVersion string) (watch.Interface, error) {
return c.r.Get().
Path("watch").
Prefix("watch").
Namespace(c.ns).
Path("services").
Resource("services").
Param("resourceVersion", resourceVersion).
SelectorParam("labels", label).
SelectorParam("fields", field).