address.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright (c) 2016 Arista Networks, Inc.
  2. // Use of this source code is governed by the Apache License 2.0
  3. // that can be found in the COPYING file.
  4. package netns
  5. import (
  6. "fmt"
  7. "strings"
  8. )
  9. // ParseAddress takes in an address string, parsing out the address
  10. // and an optional VRF name. It returns the namespace corresponding to the VRF.
  11. // The expected form is [<vrf-name>/]address:port. However, ParseAddress
  12. // will not actually check to see if the VRF name or address are valid.
  13. // Presumably, when those values are used later, they will fail if they
  14. // are malformed
  15. func ParseAddress(address string) (nsName string, addr string, err error) {
  16. split := strings.Split(address, "/")
  17. if l := len(split); l == 1 {
  18. addr = split[0]
  19. } else if l == 2 {
  20. nsName = VRFToNetNS(split[0])
  21. addr = split[1]
  22. } else {
  23. err = fmt.Errorf("Could not parse out a <vrf-name>/address for %s", address)
  24. }
  25. return
  26. }
  27. // VRFToNetNS converts a VRF name to network namespace's name corresponding to that VRF.
  28. func VRFToNetNS(vrf string) string {
  29. if vrf != "" && vrf != "default" && !strings.HasPrefix(vrf, "ns-") {
  30. vrf = "ns-" + vrf
  31. }
  32. return vrf
  33. }