Merge pull request #284 from icecrime/not_found_errors

Support NodeInspectWithRaw, improve not found errors
This commit is contained in:
Vincent Demeester 2016-06-17 01:44:23 +02:00 коммит произвёл GitHub
Родитель 19b4fb48a8 537a03e932
Коммит f3ed0875cb
4 изменённых файлов: 29 добавлений и 11 удалений

Просмотреть файл

@ -148,6 +148,11 @@ func (e serviceNotFoundError) Error() string {
return fmt.Sprintf("Error: No such service: %s", e.serviceID)
}
// NoFound indicates that this error type is of NotFound
func (e serviceNotFoundError) NotFound() bool {
return true
}
// IsErrServiceNotFound returns true if the error is caused
// when a service is not found.
func IsErrServiceNotFound(err error) bool {
@ -165,6 +170,11 @@ func (e taskNotFoundError) Error() string {
return fmt.Sprintf("Error: No such task: %s", e.taskID)
}
// NoFound indicates that this error type is of NotFound
func (e taskNotFoundError) NotFound() bool {
return true
}
// IsErrTaskNotFound returns true if the error is caused
// when a task is not found.
func IsErrTaskNotFound(err error) bool {

Просмотреть файл

@ -92,7 +92,7 @@ type NetworkAPIClient interface {
// NodeAPIClient defines API client methods for the nodes
type NodeAPIClient interface {
NodeInspect(ctx context.Context, nodeID string) (swarm.Node, error)
NodeInspectWithRaw(ctx context.Context, nodeID string) (swarm.Node, []byte, error)
NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error)
NodeRemove(ctx context.Context, nodeID string) error
NodeUpdate(ctx context.Context, nodeID string, version swarm.Version, node swarm.NodeSpec) error

Просмотреть файл

@ -1,25 +1,33 @@
package client
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/docker/engine-api/types/swarm"
"golang.org/x/net/context"
)
// NodeInspect returns the node information.
func (cli *Client) NodeInspect(ctx context.Context, nodeID string) (swarm.Node, error) {
// NodeInspectWithRaw returns the node information.
func (cli *Client) NodeInspectWithRaw(ctx context.Context, nodeID string) (swarm.Node, []byte, error) {
serverResp, err := cli.get(ctx, "/nodes/"+nodeID, nil, nil)
if err != nil {
if serverResp.statusCode == http.StatusNotFound {
return swarm.Node{}, nodeNotFoundError{nodeID}
return swarm.Node{}, nil, nodeNotFoundError{nodeID}
}
return swarm.Node{}, err
return swarm.Node{}, nil, err
}
defer ensureReaderClosed(serverResp)
body, err := ioutil.ReadAll(serverResp.body)
if err != nil {
return swarm.Node{}, nil, err
}
var response swarm.Node
err = json.NewDecoder(serverResp.body).Decode(&response)
ensureReaderClosed(serverResp)
return response, err
rdr := bytes.NewReader(body)
err = json.NewDecoder(rdr).Decode(&response)
return response, body, err
}

Просмотреть файл

@ -18,7 +18,7 @@ func TestNodeInspectError(t *testing.T) {
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.NodeInspect(context.Background(), "nothing")
_, _, err := client.NodeInspectWithRaw(context.Background(), "nothing")
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
@ -29,7 +29,7 @@ func TestNodeInspectNodeNotFound(t *testing.T) {
transport: newMockClient(nil, errorMock(http.StatusNotFound, "Server error")),
}
_, err := client.NodeInspect(context.Background(), "unknown")
_, _, err := client.NodeInspectWithRaw(context.Background(), "unknown")
if err == nil || !IsErrNodeNotFound(err) {
t.Fatalf("expected an nodeNotFoundError error, got %v", err)
}
@ -55,7 +55,7 @@ func TestNodeInspect(t *testing.T) {
}),
}
nodeInspect, err := client.NodeInspect(context.Background(), "node_id")
nodeInspect, _, err := client.NodeInspectWithRaw(context.Background(), "node_id")
if err != nil {
t.Fatal(err)
}