Skip to content

Commit

Permalink
Merge pull request #81 from wallyworld/allow-devices-without-pools
Browse files Browse the repository at this point in the history
#81

Based on #80 by @stgraber

Allow device responses which do not specify a pool.
  • Loading branch information
jujubot authored Aug 12, 2019
2 parents 65f2e26 + 2ac91a3 commit 7f9373c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
12 changes: 7 additions & 5 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func device_2_0(source map[string]interface{}) (*device, error) {
"ip_addresses": schema.List(schema.String()),
"interface_set": schema.List(schema.StringMap(schema.Any())),
"zone": schema.StringMap(schema.Any()),
"pool": schema.StringMap(schema.Any()),
"pool": schema.OneOf(schema.Nil(""), schema.StringMap(schema.Any())),
}
defaults := schema.Defaults{
"owner": "",
Expand All @@ -286,11 +286,13 @@ func device_2_0(source map[string]interface{}) (*device, error) {
return nil, errors.Trace(err)
}

pool, err := pool_2_0(valid["pool"].(map[string]interface{}))

if err != nil {
return nil, errors.Trace(err)
var pool *pool
if valid["pool"] != nil {
if pool, err = pool_2_0(valid["pool"].(map[string]interface{})); err != nil {
return nil, errors.Trace(err)
}
}

owner, _ := valid["owner"].(string)
parent, _ := valid["parent"].(string)
result := &device{
Expand Down
7 changes: 6 additions & 1 deletion device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,25 @@ func (*deviceSuite) TestReadDevices(c *gc.C) {
zone := device.Zone()
c.Check(zone, gc.NotNil)
c.Check(zone.Name(), gc.Equals, "default")
pool := device.Pool()
c.Check(pool, gc.NotNil)
c.Check(pool.Name(), gc.Equals, "default")
}

func (*deviceSuite) TestReadDevicesNils(c *gc.C) {
json := parseJSON(c, devicesResponse)
deviceMap := json.([]interface{})[0].(map[string]interface{})
deviceMap["owner"] = nil
deviceMap["parent"] = nil
deviceMap["pool"] = nil
devices, err := readDevices(twoDotOh, json)
c.Assert(err, jc.ErrorIsNil)
c.Assert(devices, gc.HasLen, 1)

device := devices[0]
c.Check(device.Owner(), gc.Equals, "")
c.Check(device.Parent(), gc.Equals, "")
c.Check(device.Pool(), gc.IsNil)
}

func (*deviceSuite) TestLowVersion(c *gc.C) {
Expand Down Expand Up @@ -234,7 +239,7 @@ const (
"resource_uri": "/MAAS/api/2.0/zones/default/",
"name": "default"
},
"pool": {
"pool": {
"description": "",
"resource_uri": "/MAAS/api/2.0/pools/default/",
"name": "default"
Expand Down
1 change: 0 additions & 1 deletion testservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2039,7 +2039,6 @@ func (suite *TestMAASObjectSuite) TestAcquireFilterTag(c *C) {
c.Assert(err, IsNil)
acquiredNode, err := jsonResponse.GetMAASObject()
c.Assert(err, IsNil)
fmt.Printf("%v\n", acquiredNode)
tag, _ := acquiredNode.GetField("tag_names")
c.Assert(tag, Equals, "GPU")
}
Expand Down

0 comments on commit 7f9373c

Please sign in to comment.