Skip to content

Commit

Permalink
feature: inspect network by ID
Browse files Browse the repository at this point in the history
Signed-off-by: 程飞 <fay.cheng.cn@gmail.com>
  • Loading branch information
faycheng committed Apr 3, 2018
1 parent 46c67ec commit 45eddc0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 13 deletions.
85 changes: 73 additions & 12 deletions daemon/mgr/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"path"
"strings"

apitypes "github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/daemon/config"
Expand Down Expand Up @@ -149,26 +150,86 @@ func (nm *NetworkManager) List(ctx context.Context, labels map[string]string) ([
return net, nil
}

// Get returns the information of network that specified name/id.
func (nm *NetworkManager) Get(ctx context.Context, name string) (*types.Network, error) {
// GetNetworkByName returns the information of network that specified name.
func (nm *NetworkManager) GetNetworkByName(name string) (*types.Network, error) {
n, err := nm.controller.NetworkByName(name)
if err != nil {
if err == libnetwork.ErrNoSuchNetwork(name) {
return nil, errors.Wrap(errtypes.ErrNotfound, err.Error())
}
return nil, err
}

if n != nil {
if err == nil {
return &types.Network{
Name: name,
Name: n.Name(),
ID: n.ID(),
Type: n.Type(),
Network: n,
}, nil
}
return nil, err
}

return nil, nil
// GetNetworksByID returns a list of networks that specified id.
func (nm *NetworkManager) GetNetworksByID(id string) []*types.Network {
var matchedNetworks []*types.Network

walker := func(nw libnetwork.Network) bool {
if strings.HasPrefix(nw.ID(), id) {
matchedNetwork := &types.Network{
Name: nw.Name(),
ID: nw.ID(),
Type: nw.Type(),
Network: nw,
}
matchedNetworks = append(matchedNetworks, matchedNetwork)
}
return false
}
nm.controller.WalkNetworks(walker)
return matchedNetworks
}

// GetNetworkByID returns the information of network that specified id.
func (nm *NetworkManager) GetNetworkByID(id string) (*types.Network, error) {
network, err := nm.controller.NetworkByID(id)
if err == nil {
return &types.Network{
Name: network.Name(),
ID: network.ID(),
Type: network.Type(),
Network: network,
}, nil
}
if !isNoSuchNetworkError(err) {
return nil, err
}
matchedNetworks := nm.GetNetworksByID(id)
if len(matchedNetworks) == 0 {
return nil, errors.Wrap(errtypes.ErrNotfound, libnetwork.ErrNoSuchNetwork(id).Error())
}
if len(matchedNetworks) > 1 {
return nil, errors.Wrap(errtypes.ErrTooMany, "network: "+id)
}
return matchedNetworks[0], nil
}

// isNoSuchNetworkError looks up the error type and returns a bool if it is ErrNoSuchNetwork or not.
func isNoSuchNetworkError(err error) bool {
_, ok := err.(libnetwork.ErrNoSuchNetwork)
return ok
}

// Get returns the information of network that specified name/id.
func (nm *NetworkManager) Get(ctx context.Context, idName string) (*types.Network, error) {
n, err := nm.GetNetworkByName(idName)
if err != nil && !isNoSuchNetworkError(err) {
return nil, err
}

if n != nil {
return n, nil
}

n, err = nm.GetNetworkByID(idName)
if err != nil {
return nil, err
}
return n, err
}

// EndpointCreate is used to create network endpoint.
Expand Down
13 changes: 12 additions & 1 deletion test/cli_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,25 @@ func (suite *PouchNetworkSuite) SetUpSuite(c *check.C) {
// TestNetworkInspectFormat tests the inspect format of network works.
func (suite *PouchNetworkSuite) TestNetworkInspectFormat(c *check.C) {
output := command.PouchRun("network", "inspect", "bridge").Stdout()
result := &types.ContainerJSON{}
result := &types.NetworkInspectResp{}
if err := json.Unmarshal([]byte(output), result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}

// inspect network name
output = command.PouchRun("network", "inspect", "-f", "{{.Name}}", "bridge").Stdout()
c.Assert(output, check.Equals, "bridge\n")

output = command.PouchRun("network", "inspect", "bridge").Stdout()
network := &types.NetworkInspectResp{}
if err := json.Unmarshal([]byte(output), network); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}
networkID := network.ID

// inspect network name by ID
output = command.PouchRun("network", "inspect", "-f", "{{.Name}}", networkID).Stdout()
c.Assert(output, check.Equals, "bridge\n")
}

// TestNetworkDefault tests the creation of default bridge/none/host network.
Expand Down

0 comments on commit 45eddc0

Please sign in to comment.