Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the optional IPAddress for a Link. #27

Merged
merged 1 commit into from
Apr 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
31 changes: 21 additions & 10 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
33 changes: 31 additions & 2 deletions link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -39,14 +42,40 @@ 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 = `
[
{
"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,
Expand Down