From 995930966c29ac36a746cefb176e3ccdb656c67e Mon Sep 17 00:00:00 2001 From: Tim Penhey Date: Fri, 8 Apr 2016 10:10:53 +1200 Subject: [PATCH] Return the optional IPAddress for a Link. --- interfaces.go | 3 +++ link.go | 31 +++++++++++++++++++++---------- link_test.go | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 12 deletions(-) diff --git a/interfaces.go b/interfaces.go index ef32ac1..845ef83 100644 --- a/interfaces.go +++ b/interfaces.go @@ -250,4 +250,7 @@ type Link interface { ID() int Mode() string Subnet() Subnet + // IPAddress returns the address if one has been assigned. + // If unavailble, the address will be empty. + IPAddress() string } diff --git a/link.go b/link.go index d4441a0..f6c4d19 100644 --- a/link.go +++ b/link.go @@ -10,9 +10,10 @@ import ( ) type link struct { - id int - mode string - subnet *subnet + id int + mode string + subnet *subnet + ipAddress string } // NOTE: not using lowercase L as the receiver as it is a horrible idea. @@ -33,6 +34,11 @@ func (k *link) Subnet() Subnet { return k.subnet } +// IPAddress implements Link. +func (k *link) IPAddress() string { + return k.ipAddress +} + func readLinks(controllerVersion version.Number, source interface{}) ([]*link, error) { checker := schema.List(schema.StringMap(schema.Any())) coerced, err := checker.Coerce(source, nil) @@ -79,11 +85,15 @@ var linkDeserializationFuncs = map[version.Number]linkDeserializationFunc{ func link_2_0(source map[string]interface{}) (*link, error) { fields := schema.Fields{ - "id": schema.ForceInt(), - "mode": schema.String(), - "subnet": schema.StringMap(schema.Any()), + "id": schema.ForceInt(), + "mode": schema.String(), + "subnet": schema.StringMap(schema.Any()), + "ip_address": schema.String(), + } + defaults := schema.Defaults{ + "ip_address": "", } - checker := schema.FieldMap(fields, nil) // no defaults + checker := schema.FieldMap(fields, defaults) coerced, err := checker.Coerce(source, nil) if err != nil { return nil, WrapWithDeserializationError(err, "link 2.0 schema check failed") @@ -98,9 +108,10 @@ func link_2_0(source map[string]interface{}) (*link, error) { } result := &link{ - id: valid["id"].(int), - mode: valid["mode"].(string), - subnet: subnet, + id: valid["id"].(int), + mode: valid["mode"].(string), + subnet: subnet, + ipAddress: valid["ip_address"].(string), } return result, nil } diff --git a/link_test.go b/link_test.go index 8365eb5..4a98292 100644 --- a/link_test.go +++ b/link_test.go @@ -22,13 +22,16 @@ func (*linkSuite) TestReadLinksBadSchema(c *gc.C) { func (*linkSuite) TestReadLinks(c *gc.C) { links, err := readLinks(twoDotOh, parseJSON(c, linksResponse)) c.Assert(err, jc.ErrorIsNil) - c.Assert(links, gc.HasLen, 1) + c.Assert(links, gc.HasLen, 2) link := links[0] c.Assert(link.ID(), gc.Equals, 69) c.Assert(link.Mode(), gc.Equals, "auto") + c.Assert(link.IPAddress(), gc.Equals, "192.168.100.5") subnet := link.Subnet() c.Assert(subnet, gc.NotNil) c.Assert(subnet.Name(), gc.Equals, "192.168.100.0/24") + // Second link has missing ip_address + c.Assert(links[1].IPAddress(), gc.Equals, "") } func (*linkSuite) TestLowVersion(c *gc.C) { @@ -39,7 +42,7 @@ func (*linkSuite) TestLowVersion(c *gc.C) { func (*linkSuite) TestHighVersion(c *gc.C) { links, err := readLinks(version.MustParse("2.1.9"), parseJSON(c, linksResponse)) c.Assert(err, jc.ErrorIsNil) - c.Assert(links, gc.HasLen, 1) + c.Assert(links, gc.HasLen, 2) } var linksResponse = ` @@ -47,6 +50,32 @@ var linksResponse = ` { "id": 69, "mode": "auto", + "ip_address": "192.168.100.5", + "subnet": { + "resource_uri": "/MAAS/api/2.0/subnets/1/", + "id": 1, + "rdns_mode": 2, + "vlan": { + "resource_uri": "/MAAS/api/2.0/vlans/1/", + "id": 1, + "secondary_rack": null, + "mtu": 1500, + "primary_rack": "4y3h7n", + "name": "untagged", + "fabric": "fabric-0", + "dhcp_on": true, + "vid": 0 + }, + "dns_servers": [], + "space": "space-0", + "name": "192.168.100.0/24", + "gateway_ip": "192.168.100.1", + "cidr": "192.168.100.0/24" + } + }, + { + "id": 70, + "mode": "auto", "subnet": { "resource_uri": "/MAAS/api/2.0/subnets/1/", "id": 1,