github.com/psiphon-inc/goarista@v0.0.0-20160825065156-d002785f4c67/netns/address.go (about)

     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  
     5  package netns
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  )
    11  
    12  // ParseAddress takes in an address string, parsing out the address
    13  // and an optional VRF name. It returns the namespace corresponding to the VRF.
    14  // The expected form is [<vrf-name>/]address:port. However, ParseAddress
    15  // will not actually check to see if the VRF name or address are valid.
    16  // Presumably, when those values are used later, they will fail if they
    17  // are malformed
    18  func ParseAddress(address string) (nsName string, addr string, err error) {
    19  	split := strings.Split(address, "/")
    20  	if l := len(split); l == 1 {
    21  		addr = split[0]
    22  	} else if l == 2 {
    23  		nsName = VRFToNetNS(split[0])
    24  		addr = split[1]
    25  	} else {
    26  		err = fmt.Errorf("Could not parse out a <vrf-name>/address for %s", address)
    27  	}
    28  	return
    29  }
    30  
    31  // VRFToNetNS converts a VRF name to network namespace's name corresponding to that VRF.
    32  func VRFToNetNS(vrf string) string {
    33  	if vrf != "" && vrf != "default" && !strings.HasPrefix(vrf, "ns-") {
    34  		vrf = "ns-" + vrf
    35  	}
    36  	return vrf
    37  }