github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/environs/manual/addresses.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package manual
     5  
     6  import (
     7  	"net"
     8  
     9  	"github.com/juju/juju/network"
    10  )
    11  
    12  var netLookupHost = net.LookupHost
    13  
    14  // HostAddress returns an network.Address for the specified
    15  // hostname, depending on whether it is an IP or a resolvable
    16  // hostname. The address is given public scope.
    17  func HostAddress(hostname string) (network.Address, error) {
    18  	if ip := net.ParseIP(hostname); ip != nil {
    19  		addr := network.Address{
    20  			Value: ip.String(),
    21  			Type:  network.DeriveAddressType(ip.String()),
    22  			Scope: network.ScopePublic,
    23  		}
    24  		return addr, nil
    25  	}
    26  	// Only a resolvable hostname may be used as a public address.
    27  	if _, err := netLookupHost(hostname); err != nil {
    28  		return network.Address{}, err
    29  	}
    30  	addr := network.Address{
    31  		Value: hostname,
    32  		Type:  network.HostName,
    33  		Scope: network.ScopePublic,
    34  	}
    35  	return addr, nil
    36  }