Make MAAS2 constraint matches lists #51

Merged
merged 3 commits into from Apr 28, 2016
Jump to file or symbol
Failed to load files and symbols.
+24 −14
Split
Viewing a subset of changes. View all
Prev

Extract interface{} -> map[string][]int conversion

  • Loading branch information...
commit 61b3b97003699be0c103f21cb26e7fb924d19b4b @babbageclunk babbageclunk committed Apr 28, 2016
View
@@ -830,37 +830,47 @@ func parseAllocateConstraintsResponse(source interface{}, machine *machine) (Con
}
if interfaceMatches, found := constraintsMap["interfaces"]; found {
- matchMap := interfaceMatches.(map[string]interface{})
- for label, values := range matchMap {
- matches := values.([]interface{})
- interfaces := make([]Interface, 0, len(matches))
- for _, value := range matches {
- id := value.(int)
+ matches := convertConstraintMatches(interfaceMatches)
+ for label, ids := range matches {
+ interfaces := make([]Interface, len(ids))
+ for index, id := range ids {
iface := machine.Interface(id)
if iface == nil {
return empty, NewDeserializationError("constraint match interface %q: %d does not match an interface for the machine", label, id)
}
- interfaces = append(interfaces, iface)
+ interfaces[index] = iface
}
result.Interfaces[label] = interfaces
}
}
if storageMatches, found := constraintsMap["storage"]; found {
- matchMap := storageMatches.(map[string]interface{})
- for label, values := range matchMap {
- matches := values.([]interface{})
- blockDevices := make([]BlockDevice, 0, len(matches))
- for _, value := range matches {
- id := value.(int)
+ matches := convertConstraintMatches(storageMatches)
+ for label, ids := range matches {
+ blockDevices := make([]BlockDevice, len(ids))
+ for index, id := range ids {
blockDevice := machine.PhysicalBlockDevice(id)
if blockDevice == nil {
return empty, NewDeserializationError("constraint match storage %q: %d does not match a physical block device for the machine", label, id)
}
- blockDevices = append(blockDevices, blockDevice)
+ blockDevices[index] = blockDevice
}
result.Storage[label] = blockDevices
}
}
return result, nil
}
+
+func convertConstraintMatches(source interface{}) map[string][]int {
+ // These casts are all safe because of the schema check.
+ result := make(map[string][]int)
+ matchMap := source.(map[string]interface{})
+ for label, values := range matchMap {
+ items := values.([]interface{})
+ result[label] = make([]int, len(items))
+ for index, value := range items {
+ result[label][index] = value.(int)
+ }
+ }
+ return result
+}