func (task *provisionerTask) prepareNetworkAndInterfaces(networkInfo []network.InterfaceInfo) ( networks []params.Network, ifaces []params.NetworkInterface, err error) { if len(networkInfo) == 0 { return nil, nil, nil } visitedNetworks := set.NewStrings() for _, info := range networkInfo { if !names.IsValidNetwork(info.NetworkName) { return nil, nil, errors.Errorf("invalid network name %q", info.NetworkName) } networkTag := names.NewNetworkTag(info.NetworkName).String() if !visitedNetworks.Contains(networkTag) { networks = append(networks, params.Network{ Tag: networkTag, ProviderId: string(info.ProviderId), CIDR: info.CIDR, VLANTag: info.VLANTag, }) visitedNetworks.Add(networkTag) } ifaces = append(ifaces, params.NetworkInterface{ InterfaceName: info.ActualInterfaceName(), MACAddress: info.MACAddress, NetworkTag: networkTag, IsVirtual: info.IsVirtual(), Disabled: info.Disabled, }) } return networks, ifaces, nil }
// networkNamesToTags returns the given network names converted to // tags, or an error. func networkNamesToTags(networks []string) ([]string, error) { var tags []string for _, network := range networks { if !names.IsValidNetwork(network) { return nil, fmt.Errorf("%q is not a valid network name", network) } tags = append(tags, names.NewNetworkTag(network).String()) } return tags, nil }
func (v *Value) validateNetworks(networks *[]string) error { if networks == nil { return nil } for _, name := range *networks { netName := strings.TrimPrefix(name, "^") if !names.IsValidNetwork(netName) { return errors.Errorf("%q is not a valid network name", netName) } } return nil }
func (s *networkSuite) TestNetworkNames(c *gc.C) { for i, test := range networkNameTests { c.Logf("test %d: %q", i, test.pattern) c.Check(names.IsValidNetwork(test.pattern), gc.Equals, test.valid) if test.valid { expectTag := fmt.Sprintf("%s-%s", names.NetworkTagKind, test.pattern) c.Check(names.NewNetworkTag(test.pattern).String(), gc.Equals, expectTag) } else { expectErr := fmt.Sprintf("%q is not a valid network name", test.pattern) testNetworkTag := func() { names.NewNetworkTag(test.pattern) } c.Check(testNetworkTag, gc.PanicMatches, regexp.QuoteMeta(expectErr)) } } }
// AddNetwork creates a new network with the given params. If a // network with the same name or provider id already exists in state, // an error satisfying errors.IsAlreadyExists is returned. func (st *State) AddNetwork(args NetworkInfo) (n *Network, err error) { defer errors.Contextf(&err, "cannot add network %q", args.Name) if args.CIDR != "" { _, _, err := net.ParseCIDR(args.CIDR) if err != nil { return nil, err } } if args.Name == "" { return nil, fmt.Errorf("name must be not empty") } if !names.IsValidNetwork(args.Name) { return nil, fmt.Errorf("invalid name") } if args.ProviderId == "" { return nil, fmt.Errorf("provider id must be not empty") } if args.VLANTag < 0 || args.VLANTag > 4094 { return nil, fmt.Errorf("invalid VLAN tag %d: must be between 0 and 4094", args.VLANTag) } doc := newNetworkDoc(args) ops := []txn.Op{{ C: st.networks.Name, Id: args.Name, Assert: txn.DocMissing, Insert: doc, }} err = st.runTransaction(ops) switch err { case txn.ErrAborted: if _, err = st.Network(args.Name); err == nil { return nil, errors.AlreadyExistsf("network %q", args.Name) } else if err != nil { return nil, err } case nil: // We have a unique key restriction on the ProviderId field, // which will cause the insert to fail if there is another // record with the same provider id in the table. The txn // logic does not report insertion errors, so we check that // the record has actually been inserted correctly before // reporting success. if _, err = st.Network(args.Name); err != nil { return nil, errors.AlreadyExistsf("network with provider id %q", args.ProviderId) } return newNetwork(st, doc), nil } return nil, err }
func (v *Value) validateNetworks(networks *[]string) error { if networks == nil { return nil } for _, netName := range *networks { if strings.HasPrefix(netName, "^") { netName = strings.TrimPrefix(netName, "^") } if !names.IsValidNetwork(netName) { return fmt.Errorf("%q is not a valid network name", netName) } } v.Networks = networks return nil }
func (task *provisionerTask) prepareNetworkAndInterfaces(networkInfo []network.InterfaceInfo) ( networks []params.Network, ifaces []params.NetworkInterface, err error) { if len(networkInfo) == 0 { return nil, nil, nil } visitedNetworks := set.NewStrings() for _, info := range networkInfo { // TODO(dimitern): The following few fields are required, but no longer // matter and will be dropped or changed soon as part of making spaces // and subnets usable across the board. if info.NetworkName == "" { info.NetworkName = network.DefaultPrivate } if info.ProviderId == "" { info.ProviderId = network.DefaultPrivate } if info.CIDR == "" { // TODO(dimitern): This is only when NOT using addressable // containers, as we don't fetch the subnet details, but since // networks in state are going away real soon, it's not important. info.CIDR = "0.0.0.0/32" } if !names.IsValidNetwork(info.NetworkName) { return nil, nil, errors.Errorf("invalid network name %q", info.NetworkName) } networkTag := names.NewNetworkTag(info.NetworkName).String() if !visitedNetworks.Contains(networkTag) { networks = append(networks, params.Network{ Tag: networkTag, ProviderId: string(info.ProviderId), CIDR: info.CIDR, VLANTag: info.VLANTag, }) visitedNetworks.Add(networkTag) } ifaces = append(ifaces, params.NetworkInterface{ InterfaceName: info.ActualInterfaceName(), MACAddress: info.MACAddress, NetworkTag: networkTag, IsVirtual: info.IsVirtual(), Disabled: info.Disabled, }) } return networks, ifaces, nil }