Merge lp:~thumper/juju-core/find-ipv4-address into lp:~go-bot/juju-core/trunk

Proposed by Tim Penhey
Status: Merged
Approved by: John A Meinel
Approved revision: no longer in the source branch.
Merged at revision: 1392
Proposed branch: lp:~thumper/juju-core/find-ipv4-address
Merge into: lp:~go-bot/juju-core/trunk
Prerequisite: lp:~thumper/juju-core/move-cert-gen-to-config
Diff against target: 109 lines (+100/-0)
2 files modified
utils/network.go (+26/-0)
utils/network_test.go (+74/-0)
To merge this branch: bzr merge lp:~thumper/juju-core/find-ipv4-address
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+173119@code.launchpad.net

Commit message

Utility function to find IPv4 address.

There was no simple way to get an IPv4 address for
a particular net.Interface. So I added this.

https://codereview.appspot.com/10951043/

Description of the change

Utility function to find IPv4 address.

There was no simple way to get an IPv4 address for
a particular net.Interface. So I added this.

https://codereview.appspot.com/10951043/

To post a comment you must log in.
Revision history for this message
Tim Penhey (thumper) wrote :

Reviewers: mp+173119_code.launchpad.net,

Message:
Please take a look.

Description:
Utility function to find IPv4 address.

There was no simple way to get an IPv4 address for
a particular net.Interface. So I added this.

https://code.launchpad.net/~thumper/juju-core/find-ipv4-address/+merge/173119

Requires:
https://code.launchpad.net/~thumper/juju-core/move-cert-gen-to-config/+merge/173117

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/10951043/

Affected files:
   A [revision details]
   A utils/network.go
   A utils/network_test.go

Revision history for this message
John A Meinel (jameinel) wrote :

Is this an address or a range? /24 is a range over 256 addresses, isnt it?
I may be wrong on the name, but I would have thought this would be
something more like GetIPv4AddressRange. I realize you are dropping the /24
but that is only valid if it is /32 (fully specify one ip) isnt it?

Also, is it possible to avoid forcing v4? It would be really nice if we
didnt have to do a lot of work to be compatible with v6.

Anyway, if we need to do this the helper is nice to have and it looks well
tested.

John
=:->
On Jul 5, 2013 5:07 AM, "Tim Penhey" <email address hidden> wrote:

> The proposal to merge lp:~thumper/juju-core/find-ipv4-address into
> lp:juju-core has been updated.
>
> Description changed to:
>
> Utility function to find IPv4 address.
>
> There was no simple way to get an IPv4 address for
> a particular net.Interface. So I added this.
>
> https://codereview.appspot.com/10951043/
>
>
> For more details, see:
>
> https://code.launchpad.net/~thumper/juju-core/find-ipv4-address/+merge/173119
> --
>
> https://code.launchpad.net/~thumper/juju-core/find-ipv4-address/+merge/173119
> You are subscribed to branch lp:juju-core.
>

Revision history for this message
Ian Booth (wallyworld) wrote :

LGTM, with the ParseCIDR() function used to break up the IP address

https://codereview.appspot.com/10951043/

Revision history for this message
Tim Penhey (thumper) wrote :

Please take a look.

https://codereview.appspot.com/10951043/diff/1/utils/network.go
File utils/network.go (right):

https://codereview.appspot.com/10951043/diff/1/utils/network.go#newcode15
utils/network.go:15: for _, addr := range addresses {
On 2013/07/05 03:06:51, dfc wrote:
> net.ParseCIDR ?

Done.

https://codereview.appspot.com/10951043/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'utils/network.go'
2--- utils/network.go 1970-01-01 00:00:00 +0000
3+++ utils/network.go 2013-07-08 02:51:26 +0000
4@@ -0,0 +1,26 @@
5+// Copyright 2013 Canonical Ltd.
6+// Licensed under the AGPLv3, see LICENCE file for details.
7+
8+package utils
9+
10+import (
11+ "fmt"
12+ "net"
13+)
14+
15+// GetIPv4Address iterates through the addresses expecting the format from
16+// func (ifi *net.Interface) Addrs() ([]net.Addr, error)
17+func GetIPv4Address(addresses []net.Addr) (string, error) {
18+ for _, addr := range addresses {
19+ ip, _, err := net.ParseCIDR(addr.String())
20+ if err != nil {
21+ return "", err
22+ }
23+ ipv4 := ip.To4()
24+ if ipv4 == nil {
25+ continue
26+ }
27+ return ipv4.String(), nil
28+ }
29+ return "", fmt.Errorf("no addresses match")
30+}
31
32=== added file 'utils/network_test.go'
33--- utils/network_test.go 1970-01-01 00:00:00 +0000
34+++ utils/network_test.go 2013-07-08 02:51:26 +0000
35@@ -0,0 +1,74 @@
36+// Copyright 2013 Canonical Ltd.
37+// Licensed under the AGPLv3, see LICENCE file for details.
38+
39+package utils_test
40+
41+import (
42+ "net"
43+
44+ gc "launchpad.net/gocheck"
45+ "launchpad.net/juju-core/utils"
46+)
47+
48+type networkSuite struct {
49+}
50+
51+var _ = gc.Suite(&networkSuite{})
52+
53+type fakeAddress struct {
54+ address string
55+}
56+
57+func (fake fakeAddress) Network() string {
58+ return "ignored"
59+}
60+
61+func (fake fakeAddress) String() string {
62+ return fake.address
63+}
64+
65+func makeAddresses(values ...string) (result []net.Addr) {
66+ for _, v := range values {
67+ result = append(result, &fakeAddress{v})
68+ }
69+ return
70+}
71+
72+func (*networkSuite) TestGetIPv4Address(c *gc.C) {
73+ for _, test := range []struct {
74+ addresses []net.Addr
75+ expected string
76+ errorString string
77+ }{{
78+ addresses: makeAddresses(
79+ "complete",
80+ "nonsense"),
81+ errorString: "invalid CIDR address: complete",
82+ }, {
83+ addresses: makeAddresses(
84+ "fe80::90cf:9dff:fe6e:ece/64",
85+ ),
86+ errorString: "no addresses match",
87+ }, {
88+ addresses: makeAddresses(
89+ "fe80::90cf:9dff:fe6e:ece/64",
90+ "10.0.3.1/24",
91+ ),
92+ expected: "10.0.3.1",
93+ }, {
94+ addresses: makeAddresses(
95+ "10.0.3.1/24",
96+ "fe80::90cf:9dff:fe6e:ece/64",
97+ ),
98+ expected: "10.0.3.1",
99+ }} {
100+ ip, err := utils.GetIPv4Address(test.addresses)
101+ if test.errorString == "" {
102+ c.Assert(err, gc.IsNil)
103+ c.Assert(ip, gc.Equals, test.expected)
104+ } else {
105+ c.Assert(err, gc.ErrorMatches, test.errorString)
106+ c.Assert(ip, gc.Equals, "")
107+ }
108+ }
109+}

Subscribers

People subscribed via source and target branches

to status/vote changes: