~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/environs/manual/addresses.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
}