Skip to content

Commit

Permalink
Merge pull request #53 from howbazaar/more-nils
Browse files Browse the repository at this point in the history
Update for more potential nils.

Instead of the hit and miss approach we have done so far, I went through the maas source code as well as I could and looked for any of the DB fields that could be null.

I have added defaults for the possible nulls in the off chance that at some stage in the future the maas api starts omitting them.
  • Loading branch information
jujubot committed May 2, 2016
2 parents 4e9c3d7 + 5f662dc commit 8d0fc40
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 18 deletions.
17 changes: 11 additions & 6 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,18 @@ func device_2_0(source map[string]interface{}) (*device, error) {
"system_id": schema.String(),
"hostname": schema.String(),
"fqdn": schema.String(),
"parent": schema.String(),
"owner": schema.String(),
"parent": schema.OneOf(schema.Nil(""), schema.String()),
"owner": schema.OneOf(schema.Nil(""), schema.String()),

"ip_addresses": schema.List(schema.String()),
"interface_set": schema.List(schema.StringMap(schema.Any())),
"zone": schema.StringMap(schema.Any()),
}
checker := schema.FieldMap(fields, nil) // no defaults
defaults := schema.Defaults{
"owner": "",
"parent": "",
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "device 2.0 schema check failed")
Expand All @@ -270,15 +274,16 @@ func device_2_0(source map[string]interface{}) (*device, error) {
if err != nil {
return nil, errors.Trace(err)
}

owner, _ := valid["owner"].(string)
parent, _ := valid["parent"].(string)
result := &device{
resourceURI: valid["resource_uri"].(string),

systemID: valid["system_id"].(string),
hostname: valid["hostname"].(string),
fqdn: valid["fqdn"].(string),
parent: valid["parent"].(string),
owner: valid["owner"].(string),
parent: parent,
owner: owner,

ipAddresses: convertToStringSlice(valid["ip_addresses"]),
interfaceSet: interfaceSet,
Expand Down
14 changes: 14 additions & 0 deletions device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ func (*deviceSuite) TestReadDevices(c *gc.C) {
c.Check(zone.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
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, "")
}

func (*deviceSuite) TestLowVersion(c *gc.C) {
_, err := readDevices(version.MustParse("1.9.0"), parseJSON(c, devicesResponse))
c.Assert(err, jc.Satisfies, IsUnsupportedVersionError)
Expand Down
11 changes: 7 additions & 4 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,16 @@ func interface_2_0(source map[string]interface{}) (*interface_, error) {
"vlan": schema.StringMap(schema.Any()),
"links": schema.List(schema.StringMap(schema.Any())),

"mac_address": schema.String(),
"mac_address": schema.OneOf(schema.Nil(""), schema.String()),
"effective_mtu": schema.ForceInt(),

"parents": schema.List(schema.String()),
"children": schema.List(schema.String()),
}
checker := schema.FieldMap(fields, nil) // no defaults
defaults := schema.Defaults{
"mac_address": "",
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "interface 2.0 schema check failed")
Expand All @@ -409,7 +412,7 @@ func interface_2_0(source map[string]interface{}) (*interface_, error) {
if err != nil {
return nil, errors.Trace(err)
}

macAddress, _ := valid["mac_address"].(string)
result := &interface_{
resourceURI: valid["resource_uri"].(string),

Expand All @@ -422,7 +425,7 @@ func interface_2_0(source map[string]interface{}) (*interface_, error) {
vlan: vlan,
links: links,

macAddress: valid["mac_address"].(string),
macAddress: macAddress,
effectiveMTU: valid["effective_mtu"].(int),

parents: convertToStringSlice(valid["parents"]),
Expand Down
8 changes: 8 additions & 0 deletions interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ func (s *interfaceSuite) TestReadInterface(c *gc.C) {
s.checkInterface(c, result)
}

func (s *interfaceSuite) TestReadInterfaceNilMAC(c *gc.C) {
json := parseJSON(c, interfaceResponse)
json.(map[string]interface{})["mac_address"] = nil
result, err := readInterface(twoDotOh, json)
c.Assert(err, jc.ErrorIsNil)
c.Assert(result.MACAddress(), gc.Equals, "")
}

func (*interfaceSuite) TestLowVersion(c *gc.C) {
_, err := readInterfaces(version.MustParse("1.9.0"), parseJSON(c, interfacesResponse))
c.Assert(err, jc.Satisfies, IsUnsupportedVersionError)
Expand Down
11 changes: 7 additions & 4 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func machine_2_0(source map[string]interface{}) (*machine, error) {

"osystem": schema.String(),
"distro_series": schema.String(),
"architecture": schema.String(),
"architecture": schema.OneOf(schema.Nil(""), schema.String()),
"memory": schema.ForceInt(),
"cpu_count": schema.ForceInt(),

Expand All @@ -418,7 +418,10 @@ func machine_2_0(source map[string]interface{}) (*machine, error) {
"physicalblockdevice_set": schema.List(schema.StringMap(schema.Any())),
"blockdevice_set": schema.List(schema.StringMap(schema.Any())),
}
checker := schema.FieldMap(fields, nil) // no defaults
defaults := schema.Defaults{
"architecture": "",
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "machine 2.0 schema check failed")
Expand Down Expand Up @@ -447,7 +450,7 @@ func machine_2_0(source map[string]interface{}) (*machine, error) {
if err != nil {
return nil, errors.Trace(err)
}

architecture, _ := valid["architecture"].(string)
result := &machine{
resourceURI: valid["resource_uri"].(string),

Expand All @@ -458,7 +461,7 @@ func machine_2_0(source map[string]interface{}) (*machine, error) {

operatingSystem: valid["osystem"].(string),
distroSeries: valid["distro_series"].(string),
architecture: valid["architecture"].(string),
architecture: architecture,
memory: valid["memory"].(int),
cpuCount: valid["cpu_count"].(int),

Expand Down
10 changes: 10 additions & 0 deletions machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ func (*machineSuite) TestReadMachines(c *gc.C) {
c.Assert(machine.PhysicalBlockDevice(id+5), gc.IsNil)
}

func (*machineSuite) TestReadMachinesNilArch(c *gc.C) {
json := parseJSON(c, machinesResponse)
json.([]interface{})[0].(map[string]interface{})["architecture"] = nil
machines, err := readMachines(twoDotOh, json)
c.Assert(err, jc.ErrorIsNil)
c.Assert(machines, gc.HasLen, 3)
machine := machines[0]
c.Check(machine.Architecture(), gc.Equals, "")
}

func (*machineSuite) TestLowVersion(c *gc.C) {
_, err := readMachines(version.MustParse("1.9.0"), parseJSON(c, machinesResponse))
c.Assert(err, jc.Satisfies, IsUnsupportedVersionError)
Expand Down
11 changes: 7 additions & 4 deletions partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,17 @@ func partition_2_0(source map[string]interface{}) (*partition, error) {

"id": schema.ForceInt(),
"path": schema.String(),
"uuid": schema.String(),
"uuid": schema.OneOf(schema.Nil(""), schema.String()),

"used_for": schema.String(),
"size": schema.ForceUint(),

"filesystem": schema.OneOf(schema.Nil(""), schema.StringMap(schema.Any())),
}
checker := schema.FieldMap(fields, nil)
defaults := schema.Defaults{
"uuid": "",
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "partition 2.0 schema check failed")
Expand All @@ -128,12 +131,12 @@ func partition_2_0(source map[string]interface{}) (*partition, error) {
return nil, errors.Trace(err)
}
}

uuid, _ := valid["uuid"].(string)
result := &partition{
resourceURI: valid["resource_uri"].(string),
id: valid["id"].(int),
path: valid["path"].(string),
uuid: valid["uuid"].(string),
uuid: uuid,
usedFor: valid["used_for"].(string),
size: valid["size"].(uint64),
filesystem: filesystem,
Expand Down
10 changes: 10 additions & 0 deletions partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ func (*partitionSuite) TestReadPartitions(c *gc.C) {
c.Assert(fs.MountPoint(), gc.Equals, "/")
}

func (*partitionSuite) TestReadPartitionsNilUUID(c *gc.C) {
json := parseJSON(c, partitionsResponse)
json.([]interface{})[0].(map[string]interface{})["uuid"] = nil
partitions, err := readPartitions(twoDotOh, json)
c.Assert(err, jc.ErrorIsNil)
c.Assert(partitions, gc.HasLen, 1)
partition := partitions[0]
c.Check(partition.UUID(), gc.Equals, "")
}

func (*partitionSuite) TestLowVersion(c *gc.C) {
_, err := readPartitions(version.MustParse("1.9.0"), parseJSON(c, partitionsResponse))
c.Assert(err, jc.Satisfies, IsUnsupportedVersionError)
Expand Down

0 comments on commit 8d0fc40

Please sign in to comment.