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"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server contains info locating a kubernetes api server.
|
|
|
|
// Example usage:
|
|
|
|
// auth, err := LoadAuth(filename)
|
2014-06-22 21:18:01 +00:00
|
|
|
// c := New(url, auth)
|
|
|
|
// resp, err := c.Verb("GET").
|
2014-06-22 19:05:34 +00:00
|
|
|
// Path("pods").
|
|
|
|
// Selector("area=staging").
|
|
|
|
// Timeout(10*time.Second).
|
|
|
|
// Do()
|
|
|
|
// list, ok := resp.(api.PodList)
|
|
|
|
|
|
|
|
// Begin a request with a verb (GET, POST, PUT, DELETE)
|
2014-06-22 21:18:01 +00:00
|
|
|
func (c *Client) Verb(verb string) *Request {
|
2014-06-22 19:05:34 +00:00
|
|
|
return &Request{
|
|
|
|
verb: verb,
|
2014-06-22 21:18:01 +00:00
|
|
|
c: c,
|
2014-06-23 00:02:48 +00:00
|
|
|
path: "/api/v1beta1",
|
2014-06-22 19:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 00:02:48 +00:00
|
|
|
// Begin a POST request.
|
|
|
|
func (c *Client) Post() *Request {
|
|
|
|
return c.Verb("POST")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin a PUT request.
|
|
|
|
func (c *Client) Put() *Request {
|
|
|
|
return c.Verb("PUT")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin a GET request.
|
|
|
|
func (c *Client) Get() *Request {
|
|
|
|
return c.Verb("GET")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin a DELETE request.
|
|
|
|
func (c *Client) Delete() *Request {
|
|
|
|
return c.Verb("DELETE")
|
|
|
|
}
|
|
|
|
|
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-06-22 21:18:01 +00:00
|
|
|
c *Client
|
2014-06-22 19:05:34 +00:00
|
|
|
err error
|
|
|
|
verb string
|
|
|
|
path string
|
2014-06-23 00:02:48 +00:00
|
|
|
body io.Reader
|
2014-06-22 19:05:34 +00:00
|
|
|
selector labels.Selector
|
|
|
|
timeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append an item to the request path. You must call Path at least once.
|
|
|
|
func (r *Request) Path(item string) *Request {
|
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
r.path = path.Join(r.path, item)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-06-24 21:57:09 +00:00
|
|
|
// Overwrite an existing path with the path parameter.
|
|
|
|
func (r *Request) AbsPath(path string) *Request {
|
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
r.path = path
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-06-23 00:02:48 +00:00
|
|
|
// Parse the given string as a resource label selector. Optional.
|
|
|
|
func (r *Request) ParseSelector(item string) *Request {
|
2014-06-22 19:05:34 +00:00
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
r.selector, r.err = labels.ParseSelector(item)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-06-23 00:02:48 +00:00
|
|
|
// Use the given selector.
|
|
|
|
func (r *Request) Selector(s labels.Selector) *Request {
|
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
r.selector = s
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-06-22 19:05:34 +00:00
|
|
|
// Use the given duration as a timeout. Optional.
|
|
|
|
func (r *Request) Timeout(d time.Duration) *Request {
|
|
|
|
if r.err != nil {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
r.timeout = d
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use obj as the body of the request. Optional.
|
|
|
|
// If obj is a string, try to read a file of that name.
|
|
|
|
// If obj is a []byte, send it directly.
|
|
|
|
// Otherwise, assume obj is an api type and marshall it correctly.
|
|
|
|
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:
|
|
|
|
data, err := ioutil.ReadFile(t)
|
|
|
|
if err != nil {
|
|
|
|
r.err = err
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
r.body = bytes.NewBuffer(data)
|
|
|
|
case []byte:
|
|
|
|
r.body = bytes.NewBuffer(t)
|
2014-06-24 21:57:09 +00:00
|
|
|
case io.Reader:
|
|
|
|
r.body = obj.(io.Reader)
|
2014-06-23 00:02:48 +00:00
|
|
|
default:
|
|
|
|
data, err := api.Encode(obj)
|
|
|
|
if err != nil {
|
|
|
|
r.err = err
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
r.body = bytes.NewBuffer(data)
|
|
|
|
}
|
2014-06-22 19:05:34 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-06-24 05:18:14 +00:00
|
|
|
// Format and xecute the request. Returns the API object received, or an error.
|
2014-06-23 00:02:48 +00:00
|
|
|
func (r *Request) Do() Result {
|
2014-06-22 19:05:34 +00:00
|
|
|
if r.err != nil {
|
2014-06-23 00:02:48 +00:00
|
|
|
return Result{err: r.err}
|
2014-06-22 19:05:34 +00:00
|
|
|
}
|
2014-06-22 21:18:01 +00:00
|
|
|
finalUrl := r.c.host + r.path
|
2014-06-22 19:05:34 +00:00
|
|
|
query := url.Values{}
|
|
|
|
if r.selector != nil {
|
|
|
|
query.Add("labels", r.selector.String())
|
|
|
|
}
|
|
|
|
if r.timeout != 0 {
|
|
|
|
query.Add("timeout", r.timeout.String())
|
|
|
|
}
|
|
|
|
finalUrl += "?" + query.Encode()
|
2014-06-23 00:02:48 +00:00
|
|
|
req, err := http.NewRequest(r.verb, finalUrl, r.body)
|
2014-06-22 19:05:34 +00:00
|
|
|
if err != nil {
|
2014-06-23 00:02:48 +00:00
|
|
|
return Result{err: err}
|
2014-06-22 19:05:34 +00:00
|
|
|
}
|
2014-06-23 00:02:48 +00:00
|
|
|
respBody, err := r.c.doRequest(req)
|
2014-06-23 01:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
if statusErr, ok := err.(*StatusErr); ok {
|
|
|
|
// TODO: using the information in statusErr,
|
|
|
|
// loop querying the server to wait and retrieve
|
|
|
|
// the actual result.
|
|
|
|
_ = statusErr
|
|
|
|
}
|
|
|
|
}
|
2014-06-23 00:02:48 +00:00
|
|
|
return Result{respBody, err}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Result contains the result of calling Request.Do().
|
|
|
|
type Result struct {
|
|
|
|
body []byte
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Raw returns the raw result.
|
|
|
|
func (r Result) Raw() ([]byte, error) {
|
|
|
|
return r.body, r.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the result as an object.
|
|
|
|
func (r Result) Get() (interface{}, error) {
|
|
|
|
if r.err != nil {
|
|
|
|
return nil, r.err
|
|
|
|
}
|
|
|
|
return api.Decode(r.body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Into stores the result into obj, if possible..
|
|
|
|
func (r Result) Into(obj interface{}) error {
|
|
|
|
if r.err != nil {
|
|
|
|
return r.err
|
2014-06-22 19:05:34 +00:00
|
|
|
}
|
2014-06-23 00:02:48 +00:00
|
|
|
return api.DecodeInto(r.body, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the error executing the request, nil if no error occurred.
|
|
|
|
func (r Result) Error() error {
|
|
|
|
return r.err
|
2014-06-22 19:05:34 +00:00
|
|
|
}
|