1 From: Alexandru Avadanii <Alexandru.Avadanii@enea.com>
2 Date: Sat, 19 Aug 2017 02:03:01 +0200
3 Subject: [PATCH] maas: module: Obtain fabric ID from CIDR
5 MaaS subnet update requires specifying the correct fabric via reclass,
6 which we used to hardcode in our OPNFV reclass model to fabric-2.
7 However, fabric index numbers are not deterministic, so the old
10 Update MaaS custom py module to determine fabric name/ID on the
11 fly, based on CIDR matching (assuming we don't have CIDR conflicts).
13 This change maintains backwards compatibility:
14 - if fabric is specified via reclass model, it will be used as-is;
15 - if fabric is not specified via reclass model, we try to deduce it
16 based on CIDR; if no match is found, the old default ('') is used;
18 Signed-off-by: Alexandru Avadanii <Alexandru.Avadanii@enea.com>
19 Signed-off-by: Guillermo Herrero <Guillermo.Herrero@enea.com>
22 diff --git a/_modules/maas.py b/_modules/maas.py
23 index d3227ca..8a2243d 100644
24 --- a/_modules/maas.py
25 +++ b/_modules/maas.py
27 def fill_data(self, name, subnet, fabrics):
30 - 'fabric': str(fabrics[subnet.get('fabric', '')]),
31 + 'fabric': str(fabrics[subnet.get('fabric',
32 + self._get_fabric_from_cidr(subnet.get('cidr')))]),
33 'cidr': subnet.get('cidr'),
34 'gateway_ip': subnet['gateway_ip'],
37 self._process_iprange(res_json['id'])
40 + def _get_fabric_from_cidr(self, cidr):
41 + subnets = json.loads(self._maas.get(u'api/2.0/subnets/').read())
42 + for subnet in subnets:
43 + if subnet['cidr'] == cidr:
44 + return subnet['vlan']['fabric']
47 def _process_iprange(self, subnet_id):
48 ipranges = json.loads(self._maas.get(u'api/2.0/ipranges/').read())
49 LOG.warn('all %s ipranges %s', subnet_id, ipranges)