Allow multiple PXE mac addresses

MAAS allow specifying multiple pxe mac addresses for a machine during
machine creation. This commit makes it possible to specify the mac
addresses that the server may use during pxe boot as a list in the
pillar data.
To be able to pass multiple mac addresses via the API, the way of
creating the multipart message in the REST POST call had to be changed
too. The old logic took key-value pairs assuming the value is scalar.
This commit changes this logic to accept list values and in that case
create multiple message parts with the same name.
Due to alowing multiple mac addresses, the logic in the machine update
had to be changed too. The old logic checked if the new single mac
address is among the mac addresses that the host has. The new logic
checks if there is intersection between the new mac addresses and the
ones the machine has. If intersection found, that is used as new mac
addresses, and forces recreation of the machine only if no intersection
is found.

Change-Id: I9f0bb5d1654f638ad5c09ce355699c2eb37f3404
diff --git a/_modules/maas.py b/_modules/maas.py
index 426aff5..aa57498 100644
--- a/_modules/maas.py
+++ b/_modules/maas.py
@@ -445,13 +445,22 @@
         return data
 
     def update(self, new, old):
+        LOG.debug('Updating machine')
         old_macs = set(v['mac_address'].lower() for v in old['interface_set'])
-        if new['mac_addresses'].lower() not in old_macs:
+        LOG.debug('old_macs: %s' % old_macs)
+        if isinstance(new['mac_addresses'], list):
+            new_macs = set(v.lower() for v in new['mac_addresses'])
+        else:
+            new_macs = set([new['mac_addresses'].lower()])
+        LOG.debug('new_macs: %s' % new_macs)
+        intersect = list(new_macs.intersection(old_macs))
+        if not intersect:
             self._update = False
             LOG.info('Mac changed deleting old machine %s', old['system_id'])
             self._maas.delete(u'api/2.0/machines/{0}/'
                               .format(old['system_id']))
         else:
+            new['mac_addresses'] = intersect
             new[self._update_key] = str(old[self._update_key])
         return new
 
diff --git a/_modules/multipart.py b/_modules/multipart.py
index 7ed17a6..45aebf3 100644
--- a/_modules/multipart.py
+++ b/_modules/multipart.py
@@ -88,9 +88,12 @@
 
 def build_multipart_message(data):
     message = MIMEMultipart("form-data")
-    for name, content in data:
-        for payload in make_payloads(name, content):
-            message.attach(payload)
+    for name, contents in data:
+        if not isinstance(contents, list):
+            contents = [contents]
+        for content in contents:
+            for payload in make_payloads(name, content):
+                message.attach(payload)
     return message