Merge pull request #22 from alexandruavadanii/fabric-from-cidr

Module: Obtain fabric ID from CIDR
diff --git a/_modules/maas.py b/_modules/maas.py
index 9ba76e5..426aff5 100644
--- a/_modules/maas.py
+++ b/_modules/maas.py
@@ -218,7 +218,8 @@
     def fill_data(self, name, subnet, fabrics):
         data = {
             'name': name,
-            'fabric': str(fabrics[subnet.get('fabric', '')]),
+            'fabric': str(fabrics[subnet.get('fabric',
+                self._get_fabric_from_cidr(subnet.get('cidr')))]),
             'cidr': subnet.get('cidr'),
             'gateway_ip': subnet['gateway_ip'],
         }
@@ -235,6 +236,13 @@
         self._process_iprange(res_json['id'])
         return response
 
+    def _get_fabric_from_cidr(self, cidr):
+        subnets = json.loads(self._maas.get(u'api/2.0/subnets/').read())
+        for subnet in subnets:
+            if subnet['cidr'] == cidr:
+                return subnet['vlan']['fabric']
+        return ''
+
     def _process_iprange(self, subnet_id):
         ipranges = json.loads(self._maas.get(u'api/2.0/ipranges/').read())
         LOG.warn('all %s ipranges %s', subnet_id, ipranges)