Merge pull request #31710 from sanimej/drillerrr

Add verbose flag to network inspect to show all services & tasks in swarm mode
This commit is contained in:
Madhu Venugopal 2017-03-13 21:12:32 -07:00 committed by GitHub
commit d99bce7b1c
3 changed files with 49 additions and 13 deletions

View File

@ -91,8 +91,8 @@ type NetworkAPIClient interface {
NetworkConnect(ctx context.Context, networkID, container string, config *network.EndpointSettings) error NetworkConnect(ctx context.Context, networkID, container string, config *network.EndpointSettings) error
NetworkCreate(ctx context.Context, name string, options types.NetworkCreate) (types.NetworkCreateResponse, error) NetworkCreate(ctx context.Context, name string, options types.NetworkCreate) (types.NetworkCreateResponse, error)
NetworkDisconnect(ctx context.Context, networkID, container string, force bool) error NetworkDisconnect(ctx context.Context, networkID, container string, force bool) error
NetworkInspect(ctx context.Context, networkID string) (types.NetworkResource, error) NetworkInspect(ctx context.Context, networkID string, verbose bool) (types.NetworkResource, error)
NetworkInspectWithRaw(ctx context.Context, networkID string) (types.NetworkResource, []byte, error) NetworkInspectWithRaw(ctx context.Context, networkID string, verbose bool) (types.NetworkResource, []byte, error)
NetworkList(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) NetworkList(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error)
NetworkRemove(ctx context.Context, networkID string) error NetworkRemove(ctx context.Context, networkID string) error
NetworksPrune(ctx context.Context, pruneFilter filters.Args) (types.NetworksPruneReport, error) NetworksPrune(ctx context.Context, pruneFilter filters.Args) (types.NetworksPruneReport, error)

View File

@ -5,21 +5,30 @@ import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
// NetworkInspect returns the information for a specific network configured in the docker host. // NetworkInspect returns the information for a specific network configured in the docker host.
func (cli *Client) NetworkInspect(ctx context.Context, networkID string) (types.NetworkResource, error) { func (cli *Client) NetworkInspect(ctx context.Context, networkID string, verbose bool) (types.NetworkResource, error) {
networkResource, _, err := cli.NetworkInspectWithRaw(ctx, networkID) networkResource, _, err := cli.NetworkInspectWithRaw(ctx, networkID, verbose)
return networkResource, err return networkResource, err
} }
// NetworkInspectWithRaw returns the information for a specific network configured in the docker host and its raw representation. // NetworkInspectWithRaw returns the information for a specific network configured in the docker host and its raw representation.
func (cli *Client) NetworkInspectWithRaw(ctx context.Context, networkID string) (types.NetworkResource, []byte, error) { func (cli *Client) NetworkInspectWithRaw(ctx context.Context, networkID string, verbose bool) (types.NetworkResource, []byte, error) {
var networkResource types.NetworkResource var (
resp, err := cli.get(ctx, "/networks/"+networkID, nil, nil) networkResource types.NetworkResource
resp serverResponse
err error
)
query := url.Values{}
if verbose {
query.Set("verbose", "true")
}
resp, err = cli.get(ctx, "/networks/"+networkID, query, nil)
if err != nil { if err != nil {
if resp.statusCode == http.StatusNotFound { if resp.statusCode == http.StatusNotFound {
return networkResource, nil, networkNotFoundError{networkID} return networkResource, nil, networkNotFoundError{networkID}

View File

@ -10,6 +10,7 @@ import (
"testing" "testing"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
@ -18,7 +19,7 @@ func TestNetworkInspectError(t *testing.T) {
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.NetworkInspect(context.Background(), "nothing") _, err := client.NetworkInspect(context.Background(), "nothing", false)
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err) t.Fatalf("expected a Server Error, got %v", err)
} }
@ -29,7 +30,7 @@ func TestNetworkInspectContainerNotFound(t *testing.T) {
client: newMockClient(errorMock(http.StatusNotFound, "Server error")), client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
} }
_, err := client.NetworkInspect(context.Background(), "unknown") _, err := client.NetworkInspect(context.Background(), "unknown", false)
if err == nil || !IsErrNetworkNotFound(err) { if err == nil || !IsErrNetworkNotFound(err) {
t.Fatalf("expected a networkNotFound error, got %v", err) t.Fatalf("expected a networkNotFound error, got %v", err)
} }
@ -46,9 +47,23 @@ func TestNetworkInspect(t *testing.T) {
return nil, fmt.Errorf("expected GET method, got %s", req.Method) return nil, fmt.Errorf("expected GET method, got %s", req.Method)
} }
content, err := json.Marshal(types.NetworkResource{ var (
content []byte
err error
)
if strings.HasPrefix(req.URL.RawQuery, "verbose=true") {
s := map[string]network.ServiceInfo{
"web": {},
}
content, err = json.Marshal(types.NetworkResource{
Name: "mynetwork",
Services: s,
})
} else {
content, err = json.Marshal(types.NetworkResource{
Name: "mynetwork", Name: "mynetwork",
}) })
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -59,11 +74,23 @@ func TestNetworkInspect(t *testing.T) {
}), }),
} }
r, err := client.NetworkInspect(context.Background(), "network_id") r, err := client.NetworkInspect(context.Background(), "network_id", false)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if r.Name != "mynetwork" { if r.Name != "mynetwork" {
t.Fatalf("expected `mynetwork`, got %s", r.Name) t.Fatalf("expected `mynetwork`, got %s", r.Name)
} }
r, err = client.NetworkInspect(context.Background(), "network_id", true)
if err != nil {
t.Fatal(err)
}
if r.Name != "mynetwork" {
t.Fatalf("expected `mynetwork`, got %s", r.Name)
}
_, ok := r.Services["web"]
if !ok {
t.Fatalf("expected service `web` missing in the verbose output")
}
} }