FindNetwork returns a network based on: 1. Full ID 2. Full Name 3. Partial ID as long as there is no ambiguity
(term string)
| 63 | // 3. Partial ID |
| 64 | // as long as there is no ambiguity |
| 65 | func (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. |