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 --- a/_modules/maas.py
24 +++ b/_modules/maas.py
27 def process_single(name, config_data):
29 + if isinstance(config_data, dict) and 'name' in config_data:
30 + name = config_data['name']
32 data = self.fill_data(name, config_data, **extra)
35 def fill_data(self, name, subnet, fabrics):
38 - 'fabric': str(fabrics[subnet.get('fabric', '')]),
39 + 'fabric': str(fabrics[subnet.get('fabric',
40 + self._get_fabric_from_cidr(subnet.get('cidr')))]),
41 'cidr': subnet.get('cidr'),
42 'gateway_ip': subnet['gateway_ip'],
45 self._process_iprange(res_json['id'])
48 + def _get_fabric_from_cidr(self, cidr):
49 + subnets = json.loads(self._maas.get(u'api/2.0/subnets/').read())
50 + for subnet in subnets:
51 + if subnet['cidr'] == cidr:
52 + return subnet['vlan']['fabric']
55 def _process_iprange(self, subnet_id):
56 ipranges = json.loads(self._maas.get(u'api/2.0/ipranges/').read())
57 LOG.warn('all %s ipranges %s', subnet_id, ipranges)