2014-06-22 19:05:34 +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.
|
|
|
|
*/
|
|
|
|
|
2014-06-22 21:18:01 +00:00
|
|
|
package client
|
2014-06-22 19:05:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-07-17 23:09:29 +00:00
|
|
|
"fmt"
|
2014-06-22 19:05:34 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
2014-08-05 22:23:33 +00:00
|
|
|
"strconv"
|
2014-12-18 20:38:24 +00:00
|
|
|
"strings"
|
2014-06-22 19:05:34 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-10-29 02:48:13 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
2014-06-22 19:05:34 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-09-02 17:55:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-08-08 20:50:04 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-07-17 23:09:29 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
2014-09-17 04:33:48 +00:00
|
|
|
watchjson "github.com/GoogleCloudPlatform/kubernetes/pkg/watch/json"
|
2014-06-22 19:05:34 +00:00
|
|
|
)
|
|
|
|
|
2014-08-08 20:50:04 +00:00
|
|
|
// specialParams lists parameters that are handled specially and which users of Request
|
|
|
|
// are therefore not allowed to set manually.
|
|
|
|
var specialParams = util.NewStringSet("sync", "timeout")
|
|
|
|
|
2014-10-29 02:48:13 +00:00
|
|
|
// PollFunc is called when a server operation returns 202 accepted. The name of the
|
|
|
|
// operation is extracted from the response and passed to this function. Return a
|
|
|
|
// request to retrieve the result of the operation, or false for the second argument
|
|
|
|
// if polling should end.
|
|
|
|
type PollFunc func(name string) (*Request, bool)
|
|
|
|
|
|
|
|
// HTTPClient is an interface for testing a request object.
|
|
|
|
type HTTPClient interface {
|
|
|
|
Do(req *http.Request) (*http.Response, error)
|
|
|
|
}
|
|
|
|
|
2014-11-21 00:01:42 +00:00
|
|
|
// UnexpectedStatusError is returned as an error if a response's body and HTTP code don't
|
|
|
|
// make sense together.
|
|
|
|
type UnexpectedStatusError struct {
|
|
|
|
Request *http.Request
|
|
|
|
Response *http.Response
|
|
|
|
Body string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns a textual description of 'u'.
|
|
|
|
func (u *UnexpectedStatusError) Error() string {
|
2014-12-19 20:28:51 +00:00
|
|
|
return fmt.Sprintf("request [%+v] failed (%d) %s: %s", u.Request, u.Response.StatusCode, u.Response.Status, u.Body)
|
2014-11-21 00:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RequestConstructionError is returned when there's an error assembling a request.
|
|
|
|
type RequestConstructionError struct {
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns a textual description of 'r'.
|
|
|
|
func (r *RequestConstructionError) Error() string {
|
|
|
|
return fmt.Sprintf("request construction error: '%v'", r.Err)
|
|
|
|
}
|
|
|
|
|
2014-06-22 19:05:34 +00:00
|
|
|
// Request allows for building up a request to a server in a chained fashion.
|
2014-06-23 00:02:48 +00:00
|
|
|
// Any errors are stored until the end of your call, so you only have to
|
|
|
|
// check once.
|
2014-06-22 19:05:34 +00:00
|
|
|
type Request struct {
|
2014-10-29 02:48:13 +00:00
|
|
|
// required
|
|
|
|
client HTTPClient
|
|
|
|
verb string
|
|
|
|
baseURL *url.URL
|
|
|
|
codec runtime.Codec
|
|
|
|
|
|
|
|
// optional, will be invoked if the server returns a 202 to decide
|
|
|
|
// whether to poll.
|
|
|
|
poller PollFunc
|
|
|
|
|
2015-01-06 17:36:08 +00:00
|
|
|
// If true, add "?namespace=<namespace>" as a query parameter, if false put ns/<namespace> in path
|
|
|
|
// Query parameter is considered legacy behavior
|
|
|
|
namespaceInQuery bool
|
|
|
|
// If true, lowercase resource prior to inserting into a path, if false, leave it as is. Preserving
|
|
|
|
// case is considered legacy behavior.
|
|
|
|
preserveResourceCase bool
|
2014-12-19 21:32:42 +00:00
|
|
|
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
// generic components accessible via method setters
|
|
|
|
path string
|
|
|
|
subpath string
|
|
|
|
params map[string]string
|
|
|
|
|
|
|
|
// structural elements of the request that are part of the Kubernetes API conventions
|
|
|
|
namespace string
|
2014-12-12 21:33:18 +00:00
|
|
|
namespaceSet bool
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
resource string
|
|
|
|
resourceName string
|
|
|
|
selector labels.Selector
|
|
|
|
sync bool
|
|
|
|
timeout time.Duration
|
|
|
|
|
2014-10-29 02:48:13 +00:00
|
|
|
// output
|
|
|
|
err error
|
|
|
|
body io.Reader
|
|
|
|
}
|
|
|
|
|
2015-01-06 17:36:08 +00:00
|
|
|
// NewRequest creates a new request helper object for accessing runtime.Objects on a server.
|
|
|
|
func NewRequest(client HTTPClient, verb string, baseURL *url.URL,
|
|
|
|
codec runtime.Codec, namespaceInQuery bool, preserveResourceCase bool) *Request {
|
2014-10-29 02:48:13 +00:00
|
|
|
return &Request{
|
2015-01-06 17:36:08 +00:00
|
|
|
client: client,
|
|
|
|
verb: verb,
|
|
|
|
baseURL: baseURL,
|
|
|
|
path: baseURL.Path,
|
|
|
|
|
|
|
|
codec: codec,
|
|
|
|
namespaceInQuery: namespaceInQuery,
|
|
|
|
preserveResourceCase: preserveResourceCase,
|
2014-10-29 02:48:13 +00:00
|
|
|
}
|
2014-06-22 19:05:34 +00:00
|
|
|
}
|
|
|
|
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
// Prefix adds segments to the relative beginning to the request path. These
|
|
|
|
// items will be placed before the optional Namespace, Resource, or Name sections.
|
|
|
|
// Setting AbsPath will clear any previously set Prefix segments
|
|
|
|
func (r *Request) Prefix(segments ...string) *Request {
|
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
r.path = path.Join(r.path, path.Join(segments...))
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Suffix appends segments to the end of the path. These items will be placed after the prefix and optional
|
|
|
|
// Namespace, Resource, or Name sections.
|
|
|
|
func (r *Request) Suffix(segments ...string) *Request {
|
2014-06-22 19:05:34 +00:00
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
r.subpath = path.Join(r.subpath, path.Join(segments...))
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resource sets the resource to access (<resource>/[ns/<namespace>/]<name>)
|
|
|
|
func (r *Request) Resource(resource string) *Request {
|
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
if len(r.resource) != 0 {
|
|
|
|
r.err = fmt.Errorf("resource already set to %q, cannot change to %q", r.resource, resource)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
r.resource = resource
|
|
|
|
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 {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
if len(r.resourceName) != 0 {
|
|
|
|
r.err = fmt.Errorf("resource name already set to %q, cannot change to %q", r.resourceName, resourceName)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
r.resourceName = resourceName
|
2014-06-22 19:05:34 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// Sync sets sync/async call status by setting the "sync" parameter to "true"/"false".
|
2014-06-25 23:23:15 +00:00
|
|
|
func (r *Request) Sync(sync bool) *Request {
|
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
r.sync = sync
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
// Namespace applies the namespace scope to a request (<resource>/[ns/<namespace>/]<name>)
|
2014-10-03 15:44:06 +00:00
|
|
|
func (r *Request) Namespace(namespace string) *Request {
|
2014-10-29 02:48:59 +00:00
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
2014-12-12 21:33:18 +00:00
|
|
|
if r.namespaceSet {
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
r.err = fmt.Errorf("namespace already set to %q, cannot change to %q", r.namespace, namespace)
|
|
|
|
return r
|
2014-10-03 15:44:06 +00:00
|
|
|
}
|
2014-12-12 21:33:18 +00:00
|
|
|
r.namespaceSet = true
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
r.namespace = namespace
|
2014-10-03 15:44:06 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
// AbsPath overwrites an existing path with the segments provided. Trailing slashes are preserved
|
|
|
|
// when a single segment is passed.
|
|
|
|
func (r *Request) AbsPath(segments ...string) *Request {
|
2014-06-24 21:57:09 +00:00
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
if len(segments) == 1 {
|
|
|
|
// preserve any trailing slashes for legacy behavior
|
|
|
|
r.path = segments[0]
|
|
|
|
} else {
|
|
|
|
r.path = path.Join(segments...)
|
|
|
|
}
|
2014-06-24 21:57:09 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:23:33 +00:00
|
|
|
// ParseSelectorParam parses the given string as a resource label selector.
|
|
|
|
// This is a convenience function so you don't have to first check that it's a
|
|
|
|
// validly formatted selector.
|
|
|
|
func (r *Request) ParseSelectorParam(paramName, item string) *Request {
|
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
2014-08-08 20:50:04 +00:00
|
|
|
sel, err := labels.ParseSelector(item)
|
|
|
|
if err != nil {
|
2014-08-05 22:23:33 +00:00
|
|
|
r.err = err
|
2014-08-08 20:50:04 +00:00
|
|
|
return r
|
2014-08-05 22:23:33 +00:00
|
|
|
}
|
2014-08-08 20:50:04 +00:00
|
|
|
return r.setParam(paramName, sel.String())
|
2014-08-05 22:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SelectorParam adds the given selector as a query parameter with the name paramName.
|
|
|
|
func (r *Request) SelectorParam(paramName string, s labels.Selector) *Request {
|
2014-06-22 19:05:34 +00:00
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
2014-12-31 00:30:18 +00:00
|
|
|
if s.Empty() {
|
|
|
|
return r
|
|
|
|
}
|
2014-08-08 20:50:04 +00:00
|
|
|
return r.setParam(paramName, s.String())
|
2014-06-22 19:05:34 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:23:33 +00:00
|
|
|
// UintParam creates a query parameter with the given value.
|
|
|
|
func (r *Request) UintParam(paramName string, u uint64) *Request {
|
2014-06-23 00:02:48 +00:00
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
2014-08-08 20:50:04 +00:00
|
|
|
return r.setParam(paramName, strconv.FormatUint(u, 10))
|
|
|
|
}
|
|
|
|
|
2014-10-07 20:51:28 +00:00
|
|
|
// Param creates a query parameter with the given string value.
|
|
|
|
func (r *Request) Param(paramName, s string) *Request {
|
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
return r.setParam(paramName, s)
|
|
|
|
}
|
|
|
|
|
2014-08-08 20:50:04 +00:00
|
|
|
func (r *Request) setParam(paramName, value string) *Request {
|
|
|
|
if specialParams.Has(paramName) {
|
|
|
|
r.err = fmt.Errorf("must set %v through the corresponding function, not directly.", paramName)
|
|
|
|
return r
|
|
|
|
}
|
2014-10-29 02:48:13 +00:00
|
|
|
if r.params == nil {
|
|
|
|
r.params = make(map[string]string)
|
|
|
|
}
|
2014-08-08 20:50:04 +00:00
|
|
|
r.params[paramName] = value
|
2014-06-23 00:02:48 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:23:33 +00:00
|
|
|
// Timeout makes the request use the given duration as a timeout. Sets the "timeout"
|
|
|
|
// parameter. Ignored if sync=false.
|
2014-06-22 19:05:34 +00:00
|
|
|
func (r *Request) Timeout(d time.Duration) *Request {
|
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
r.timeout = d
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-07-08 07:15:41 +00:00
|
|
|
// Body makes the request use obj as the body. Optional.
|
2014-06-22 19:05:34 +00:00
|
|
|
// If obj is a string, try to read a file of that name.
|
|
|
|
// If obj is a []byte, send it directly.
|
2014-08-05 22:23:33 +00:00
|
|
|
// If obj is an io.Reader, use it directly.
|
2014-09-06 02:22:03 +00:00
|
|
|
// If obj is a runtime.Object, marshal it correctly.
|
|
|
|
// Otherwise, set an error.
|
2014-06-22 19:05:34 +00:00
|
|
|
func (r *Request) Body(obj interface{}) *Request {
|
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
2014-06-23 00:02:48 +00:00
|
|
|
switch t := obj.(type) {
|
|
|
|
case string:
|
2014-08-08 20:50:04 +00:00
|
|
|
data, err := ioutil.ReadFile(t)
|
|
|
|
if err != nil {
|
2014-06-23 00:02:48 +00:00
|
|
|
r.err = err
|
2014-08-08 20:50:04 +00:00
|
|
|
return r
|
2014-06-23 00:02:48 +00:00
|
|
|
}
|
2014-08-08 20:50:04 +00:00
|
|
|
r.body = bytes.NewBuffer(data)
|
2014-06-23 00:02:48 +00:00
|
|
|
case []byte:
|
|
|
|
r.body = bytes.NewBuffer(t)
|
2014-06-24 21:57:09 +00:00
|
|
|
case io.Reader:
|
2014-08-05 22:23:33 +00:00
|
|
|
r.body = t
|
2014-09-06 02:22:03 +00:00
|
|
|
case runtime.Object:
|
2014-10-29 02:48:13 +00:00
|
|
|
data, err := r.codec.Encode(t)
|
2014-08-08 20:50:04 +00:00
|
|
|
if err != nil {
|
2014-06-23 00:02:48 +00:00
|
|
|
r.err = err
|
2014-08-08 20:50:04 +00:00
|
|
|
return r
|
2014-06-23 00:02:48 +00:00
|
|
|
}
|
2014-08-08 20:50:04 +00:00
|
|
|
r.body = bytes.NewBuffer(data)
|
2014-09-06 02:22:03 +00:00
|
|
|
default:
|
2014-12-19 20:28:51 +00:00
|
|
|
r.err = fmt.Errorf("unknown type used for body: %+v", obj)
|
2014-06-23 00:02:48 +00:00
|
|
|
}
|
2014-06-22 19:05:34 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-10-29 02:48:13 +00:00
|
|
|
// NoPoll indicates a server "working" response should be returned as an error
|
|
|
|
func (r *Request) NoPoll() *Request {
|
|
|
|
return r.Poller(nil)
|
|
|
|
}
|
|
|
|
|
2014-10-29 02:48:59 +00:00
|
|
|
// Poller indicates this request should use the specified poll function to determine whether
|
|
|
|
// a server "working" response should be retried. The poller is responsible for waiting or
|
|
|
|
// outputting messages to the client.
|
2014-10-29 02:48:13 +00:00
|
|
|
func (r *Request) Poller(poller PollFunc) *Request {
|
2014-06-22 19:05:34 +00:00
|
|
|
if r.err != nil {
|
2014-06-26 23:10:38 +00:00
|
|
|
return r
|
2014-06-22 19:05:34 +00:00
|
|
|
}
|
2014-10-29 02:48:13 +00:00
|
|
|
r.poller = poller
|
2014-06-26 23:10:38 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-07-17 23:09:29 +00:00
|
|
|
func (r *Request) finalURL() string {
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
p := r.path
|
2014-12-12 21:33:18 +00:00
|
|
|
if r.namespaceSet && !r.namespaceInQuery && len(r.namespace) > 0 {
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
p = path.Join(p, "ns", r.namespace)
|
|
|
|
}
|
|
|
|
if len(r.resource) != 0 {
|
2015-01-06 17:36:08 +00:00
|
|
|
resource := r.resource
|
|
|
|
if !r.preserveResourceCase {
|
|
|
|
resource = strings.ToLower(resource)
|
|
|
|
}
|
|
|
|
p = path.Join(p, resource)
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
}
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2014-10-29 02:48:13 +00:00
|
|
|
finalURL := *r.baseURL
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
finalURL.Path = p
|
|
|
|
|
2014-07-17 23:09:29 +00:00
|
|
|
query := url.Values{}
|
2014-08-05 22:23:33 +00:00
|
|
|
for key, value := range r.params {
|
|
|
|
query.Add(key, value)
|
2014-07-17 23:09:29 +00:00
|
|
|
}
|
2014-10-03 15:44:06 +00:00
|
|
|
|
2014-12-12 21:33:18 +00:00
|
|
|
if r.namespaceSet && r.namespaceInQuery && len(r.namespace) > 0 {
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
query.Add("namespace", r.namespace)
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:23:33 +00:00
|
|
|
// sync and timeout are handled specially here, to allow setting them
|
|
|
|
// in any order.
|
2014-07-17 23:09:29 +00:00
|
|
|
if r.sync {
|
|
|
|
query.Add("sync", "true")
|
|
|
|
if r.timeout != 0 {
|
|
|
|
query.Add("timeout", r.timeout.String())
|
|
|
|
}
|
|
|
|
}
|
2014-09-30 00:15:00 +00:00
|
|
|
finalURL.RawQuery = query.Encode()
|
|
|
|
return finalURL.String()
|
2014-07-17 23:09:29 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// Watch attempts to begin watching the requested location.
|
|
|
|
// Returns a watch.Interface, or an error.
|
2014-07-17 23:09:29 +00:00
|
|
|
func (r *Request) Watch() (watch.Interface, error) {
|
|
|
|
if r.err != nil {
|
|
|
|
return nil, r.err
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest(r.verb, r.finalURL(), r.body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-29 02:48:13 +00:00
|
|
|
client := r.client
|
2014-09-30 00:15:00 +00:00
|
|
|
if client == nil {
|
|
|
|
client = http.DefaultClient
|
2014-07-17 23:09:29 +00:00
|
|
|
}
|
2014-10-29 02:48:13 +00:00
|
|
|
resp, err := client.Do(req)
|
2014-07-17 23:09:29 +00:00
|
|
|
if err != nil {
|
2014-12-18 20:38:24 +00:00
|
|
|
if isProbableEOF(err) {
|
|
|
|
return watch.NewEmptyWatch(), nil
|
|
|
|
}
|
2014-07-17 23:09:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-29 02:48:13 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2014-11-12 21:31:24 +00:00
|
|
|
var body []byte
|
|
|
|
if resp.Body != nil {
|
|
|
|
body, _ = ioutil.ReadAll(resp.Body)
|
|
|
|
}
|
2014-12-19 20:28:51 +00:00
|
|
|
return nil, fmt.Errorf("for request '%+v', got status: %v\nbody: %v", req.URL, resp.StatusCode, string(body))
|
2014-07-17 23:09:29 +00:00
|
|
|
}
|
2014-10-29 02:48:13 +00:00
|
|
|
return watch.NewStreamWatcher(watchjson.NewDecoder(resp.Body, r.codec)), nil
|
2014-07-17 23:09:29 +00:00
|
|
|
}
|
|
|
|
|
2014-12-18 20:38:24 +00:00
|
|
|
// isProbableEOF returns true if the given error resembles a connection termination
|
|
|
|
// scenario that would justify assuming that the watch is empty. The watch stream
|
|
|
|
// mechanism handles many common partial data errors, so closed connections can be
|
|
|
|
// retried in many cases.
|
|
|
|
func isProbableEOF(err error) bool {
|
|
|
|
if uerr, ok := err.(*url.Error); ok {
|
|
|
|
err = uerr.Err
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case err == io.EOF:
|
|
|
|
return true
|
|
|
|
case err.Error() == "http: can't write HTTP request on broken connection":
|
|
|
|
return true
|
|
|
|
case strings.Contains(err.Error(), "connection reset by peer"):
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-10-02 00:19:00 +00:00
|
|
|
// Stream formats and executes the request, and offers streaming of the response.
|
|
|
|
// Returns io.ReadCloser which could be used for streaming of the response, or an error
|
|
|
|
func (r *Request) Stream() (io.ReadCloser, error) {
|
|
|
|
if r.err != nil {
|
|
|
|
return nil, r.err
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest(r.verb, r.finalURL(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-29 02:48:13 +00:00
|
|
|
client := r.client
|
2014-10-02 00:19:00 +00:00
|
|
|
if client == nil {
|
|
|
|
client = http.DefaultClient
|
|
|
|
}
|
2014-10-29 02:48:13 +00:00
|
|
|
resp, err := client.Do(req)
|
2014-10-02 00:19:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-29 02:48:13 +00:00
|
|
|
return resp.Body, nil
|
2014-10-02 00:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 02:48:13 +00:00
|
|
|
// Do formats and executes the request. Returns a Result object for easy response
|
|
|
|
// processing. Handles polling the server in the event a continuation was sent.
|
2014-11-21 00:01:42 +00:00
|
|
|
//
|
|
|
|
// Error type:
|
|
|
|
// * If the request can't be constructed, or an error happened earlier while building its
|
|
|
|
// arguments: *RequestConstructionError
|
|
|
|
// * If the server responds with a status: *errors.StatusError or *errors.UnexpectedObjectError
|
|
|
|
// * If the status code and body don't make sense together: *UnexpectedStatusError
|
|
|
|
// * http.Client.Do errors are returned directly.
|
2014-06-26 23:10:38 +00:00
|
|
|
func (r *Request) Do() Result {
|
2014-10-29 02:48:13 +00:00
|
|
|
client := r.client
|
|
|
|
if client == nil {
|
|
|
|
client = http.DefaultClient
|
|
|
|
}
|
|
|
|
|
2014-06-26 23:10:38 +00:00
|
|
|
for {
|
|
|
|
if r.err != nil {
|
2014-11-21 00:01:42 +00:00
|
|
|
return Result{err: &RequestConstructionError{r.err}}
|
2014-06-25 23:23:15 +00:00
|
|
|
}
|
2014-10-29 02:48:13 +00:00
|
|
|
|
2014-07-17 23:09:29 +00:00
|
|
|
req, err := http.NewRequest(r.verb, r.finalURL(), r.body)
|
2014-06-26 23:10:38 +00:00
|
|
|
if err != nil {
|
2014-11-21 00:01:42 +00:00
|
|
|
return Result{err: &RequestConstructionError{err}}
|
2014-06-26 23:10:38 +00:00
|
|
|
}
|
2014-10-29 02:48:13 +00:00
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
2014-06-26 23:10:38 +00:00
|
|
|
if err != nil {
|
2014-10-29 02:48:13 +00:00
|
|
|
return Result{err: err}
|
2014-06-23 01:14:32 +00:00
|
|
|
}
|
2014-10-29 02:48:13 +00:00
|
|
|
|
|
|
|
respBody, created, err := r.transformResponse(resp, req)
|
|
|
|
if poll, ok := r.shouldPoll(err); ok {
|
|
|
|
r = poll
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result{respBody, created, err, r.codec}
|
2014-06-23 01:14:32 +00:00
|
|
|
}
|
2014-06-23 00:02:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 02:48:13 +00:00
|
|
|
// shouldPoll checks the server error for an incomplete operation
|
|
|
|
// and if found returns a request that would check the response.
|
|
|
|
// If no polling is necessary or possible, it will return false.
|
|
|
|
func (r *Request) shouldPoll(err error) (*Request, bool) {
|
|
|
|
if err == nil || r.poller == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
apistatus, ok := err.(APIStatus)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
status := apistatus.Status()
|
|
|
|
if status.Status != api.StatusWorking {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if status.Details == nil || len(status.Details.ID) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return r.poller(status.Details.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// transformResponse converts an API response into a structured API object.
|
|
|
|
func (r *Request) transformResponse(resp *http.Response, req *http.Request) ([]byte, bool, error) {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Did the server give us a status response?
|
|
|
|
isStatusResponse := false
|
|
|
|
var status api.Status
|
|
|
|
if err := r.codec.DecodeInto(body, &status); err == nil && status.Status != "" {
|
|
|
|
isStatusResponse = true
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case resp.StatusCode < http.StatusOK || resp.StatusCode > http.StatusPartialContent:
|
|
|
|
if !isStatusResponse {
|
2014-12-15 17:32:46 +00:00
|
|
|
var err error = &UnexpectedStatusError{
|
2014-11-21 00:01:42 +00:00
|
|
|
Request: req,
|
|
|
|
Response: resp,
|
|
|
|
Body: string(body),
|
|
|
|
}
|
2014-12-15 17:32:46 +00:00
|
|
|
// TODO: handle other error classes we know about
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusConflict:
|
|
|
|
if req.Method == "POST" {
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
err = errors.NewAlreadyExists(r.resource, r.resourceName)
|
2014-12-15 17:32:46 +00:00
|
|
|
} else {
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
err = errors.NewConflict(r.resource, r.resourceName, err)
|
2014-12-15 17:32:46 +00:00
|
|
|
}
|
|
|
|
case http.StatusNotFound:
|
Introduce Resource/ResourceName/Prefix/Suffix options to RESTClient
RESTClient is an abstraction for simplifying access to resources that
follow the Kubernetes API pattern. Currently, both Namespace and Path
are coupled, which means changes across versions is complex. In general,
most access to resources should be to a resource collection (e.g.
"services") with a name (e.g. "foo"). Other constructs, like prefix sections
("watch") or proposed suffix sections ("/pods/foo/spec") only modify this
core pattern.
This commit removes the Path() helper from Request and introduces:
* Prefix(segments ...string) - segments that should go to the beginning of the path.
* Suffix(segments ...string) - segments that should go to the end of the path.
* Resource(string) - collection name, should be after prefix
* Namespace(string) - if specified, should be set after resource but before name
* Name(string) - if specified, should be after namespace
Now, only Prefix and Suffix are order dependent (and with variadics, should be
simpler). Resource, Namespace, and Name may be specified in any order.
Path() has been removed to prevent downstream consumers of RESTClient from experiencing
behavior change.
2014-12-23 21:14:32 +00:00
|
|
|
err = errors.NewNotFound(r.resource, r.resourceName)
|
2014-12-15 17:32:46 +00:00
|
|
|
case http.StatusBadRequest:
|
|
|
|
err = errors.NewBadRequest(err.Error())
|
|
|
|
}
|
|
|
|
return nil, false, err
|
2014-10-29 02:48:13 +00:00
|
|
|
}
|
|
|
|
return nil, false, errors.FromObject(&status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the server gave us a status back, look at what it was.
|
|
|
|
if isStatusResponse && status.Status != api.StatusSuccess {
|
|
|
|
// "Working" requests need to be handled specially.
|
|
|
|
// "Failed" requests are clearly just an error and it makes sense to return them as such.
|
|
|
|
return nil, false, errors.FromObject(&status)
|
|
|
|
}
|
|
|
|
|
|
|
|
created := resp.StatusCode == http.StatusCreated
|
|
|
|
return body, created, err
|
|
|
|
}
|
|
|
|
|
2014-06-23 00:02:48 +00:00
|
|
|
// Result contains the result of calling Request.Do().
|
|
|
|
type Result struct {
|
2014-10-24 17:16:02 +00:00
|
|
|
body []byte
|
|
|
|
created bool
|
|
|
|
err error
|
|
|
|
|
2014-08-29 17:53:14 +00:00
|
|
|
codec runtime.Codec
|
2014-06-23 00:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Raw returns the raw result.
|
|
|
|
func (r Result) Raw() ([]byte, error) {
|
|
|
|
return r.body, r.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the result as an object.
|
2014-09-06 02:22:03 +00:00
|
|
|
func (r Result) Get() (runtime.Object, error) {
|
2014-06-23 00:02:48 +00:00
|
|
|
if r.err != nil {
|
|
|
|
return nil, r.err
|
|
|
|
}
|
2014-08-29 17:53:14 +00:00
|
|
|
return r.codec.Decode(r.body)
|
2014-06-23 00:02:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// Into stores the result into obj, if possible.
|
2014-09-06 02:22:03 +00:00
|
|
|
func (r Result) Into(obj runtime.Object) error {
|
2014-06-23 00:02:48 +00:00
|
|
|
if r.err != nil {
|
|
|
|
return r.err
|
2014-06-22 19:05:34 +00:00
|
|
|
}
|
2014-08-29 17:53:14 +00:00
|
|
|
return r.codec.DecodeInto(r.body, obj)
|
2014-06-23 00:02:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 17:16:02 +00:00
|
|
|
// WasCreated updates the provided bool pointer to whether the server returned
|
|
|
|
// 201 created or a different response.
|
|
|
|
func (r Result) WasCreated(wasCreated *bool) Result {
|
|
|
|
*wasCreated = r.created
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// Error returns the error executing the request, nil if no error occurred.
|
2014-11-21 00:01:42 +00:00
|
|
|
// See the Request.Do() comment for what errors you might get.
|
2014-06-23 00:02:48 +00:00
|
|
|
func (r Result) Error() error {
|
|
|
|
return r.err
|
2014-06-22 19:05:34 +00:00
|
|
|
}
|