MCPcopy
hub / github.com/moby/moby / FindNetwork

Method FindNetwork

daemon/network.go:65–94  ·  view source on GitHub ↗

FindNetwork returns a network based on: 1. Full ID 2. Full Name 3. Partial ID as long as there is no ambiguity

(term string)

Source from the content-addressed store, hash-verified

63// 3. Partial ID
64// as long as there is no ambiguity
65func (daemon *Daemon) FindNetwork(term string) (*libnetwork.Network, error) {
66 var listByFullName, listByPartialID []*libnetwork.Network
67 for _, nw := range daemon.getAllNetworks() {
68 nwID := nw.ID()
69 if nwID == term {
70 return nw, nil
71 }
72 if strings.HasPrefix(nw.ID(), term) {
73 listByPartialID = append(listByPartialID, nw)
74 }
75 if nw.Name() == term {
76 listByFullName = append(listByFullName, nw)
77 }
78 }
79 switch {
80 case len(listByFullName) == 1:
81 return listByFullName[0], nil
82 case len(listByFullName) > 1:
83 return nil, errdefs.InvalidParameter(fmt.Errorf("network %s is ambiguous (%d matches found on name)", term, len(listByFullName)))
84 case len(listByPartialID) == 1:
85 return listByPartialID[0], nil
86 case len(listByPartialID) > 1:
87 return nil, errdefs.InvalidParameter(fmt.Errorf("network %s is ambiguous (%d matches found based on ID prefix)", term, len(listByPartialID)))
88 }
89
90 // Be very careful to change the error type here, the
91 // libnetwork.ErrNoSuchNetwork error is used by the controller
92 // to retry the creation of the network as managed through the swarm manager
93 return nil, errdefs.NotFound(libnetwork.ErrNoSuchNetwork(term))
94}
95
96// GetNetworkByID function returns a network whose ID matches the given ID.
97// It fails with an error if no matching network is found.

Callers 14

ContainerRenameMethod · 0.95
createSpecMethod · 0.95
TestFindNetworkErrorTypeFunction · 0.95
updateNetworkSettingsMethod · 0.95
updateNetworkMethod · 0.95
findAndAttachNetworkMethod · 0.95
ForceEndpointDeleteMethod · 0.95
normalizeNetModeMethod · 0.95
releaseNetworkMethod · 0.95

Calls 7

getAllNetworksMethod · 0.95
InvalidParameterFunction · 0.92
NotFoundFunction · 0.92
ErrNoSuchNetworkTypeAlias · 0.92
ErrorfMethod · 0.80
IDMethod · 0.65
NameMethod · 0.65

Tested by 1

TestFindNetworkErrorTypeFunction · 0.76