Merge pull request #283 from icecrime/service_inspect

Modify `ServiceInspect` to return raw data
This commit is contained in:
Victor Vieux 2016-06-16 09:52:10 -07:00 коммит произвёл GitHub
Родитель f50fbe5f9c a0da8ddf4b
Коммит d4b1f372e3
3 изменённых файлов: 19 добавлений и 11 удалений

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

@ -101,7 +101,7 @@ type NodeAPIClient interface {
// ServiceAPIClient defines API client methods for the services
type ServiceAPIClient interface {
ServiceCreate(ctx context.Context, service swarm.ServiceSpec) (types.ServiceCreateResponse, error)
ServiceInspect(ctx context.Context, serviceID string) (swarm.Service, error)
ServiceInspectWithRaw(ctx context.Context, serviceID string) (swarm.Service, []byte, error)
ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error)
ServiceRemove(ctx context.Context, serviceID string) error
ServiceUpdate(ctx context.Context, serviceID string, version swarm.Version, service swarm.ServiceSpec) 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"
)
// ServiceInspect returns the service information.
func (cli *Client) ServiceInspect(ctx context.Context, serviceID string) (swarm.Service, error) {
// ServiceInspectWithRaw returns the service information and the raw data.
func (cli *Client) ServiceInspectWithRaw(ctx context.Context, serviceID string) (swarm.Service, []byte, error) {
serverResp, err := cli.get(ctx, "/services/"+serviceID, nil, nil)
if err != nil {
if serverResp.statusCode == http.StatusNotFound {
return swarm.Service{}, serviceNotFoundError{serviceID}
return swarm.Service{}, nil, serviceNotFoundError{serviceID}
}
return swarm.Service{}, err
return swarm.Service{}, nil, err
}
defer ensureReaderClosed(serverResp)
body, err := ioutil.ReadAll(serverResp.body)
if err != nil {
return swarm.Service{}, nil, err
}
var response swarm.Service
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 TestServiceInspectError(t *testing.T) {
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ServiceInspect(context.Background(), "nothing")
_, _, err := client.ServiceInspectWithRaw(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 TestServiceInspectServiceNotFound(t *testing.T) {
transport: newMockClient(nil, errorMock(http.StatusNotFound, "Server error")),
}
_, err := client.ServiceInspect(context.Background(), "unknown")
_, _, err := client.ServiceInspectWithRaw(context.Background(), "unknown")
if err == nil || !IsErrServiceNotFound(err) {
t.Fatalf("expected an serviceNotFoundError error, got %v", err)
}
@ -55,7 +55,7 @@ func TestServiceInspect(t *testing.T) {
}),
}
serviceInspect, err := client.ServiceInspect(context.Background(), "service_id")
serviceInspect, _, err := client.ServiceInspectWithRaw(context.Background(), "service_id")
if err != nil {
t.Fatal(err)
}