1 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
2 : Copyright (c) 2018 Mirantis Inc., Enea AB and others.
4 : All rights reserved. This program and the accompanying materials
5 : are made available under the terms of the Apache License, Version 2.0
6 : which accompanies this distribution, and is available at
7 : http://www.apache.org/licenses/LICENSE-2.0
8 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
9 From: Alexandru Avadanii <Alexandru.Avadanii@enea.com>
10 Date: Sat, 19 Aug 2017 02:03:01 +0200
11 Subject: [PATCH] maas: module: Obtain fabric ID from CIDR
13 MaaS subnet update requires specifying the correct fabric via reclass,
14 which we used to hardcode in our OPNFV reclass model to fabric-2.
15 However, fabric index numbers are not deterministic, so the old
18 Update MaaS custom py module to determine fabric name/ID on the
19 fly, based on CIDR matching (assuming we don't have CIDR conflicts).
21 This change maintains backwards compatibility:
22 - if fabric is specified via reclass model, it will be used as-is;
23 - if fabric is not specified via reclass model, we try to deduce it
24 based on CIDR; if no match is found, the old default ('') is used;
26 Signed-off-by: Alexandru Avadanii <Alexandru.Avadanii@enea.com>
27 Signed-off-by: Guillermo Herrero <Guillermo.Herrero@enea.com>
30 diff --git a/_modules/maas.py b/_modules/maas.py
31 --- a/_modules/maas.py
32 +++ b/_modules/maas.py
35 def process_single(name, config_data):
37 + if isinstance(config_data, dict) and 'name' in config_data:
38 + name = config_data['name']
40 data = self.fill_data(name, config_data, **extra)
43 def fill_data(self, name, subnet, fabrics):
46 - 'fabric': str(fabrics[subnet.get('fabric', '')]),
47 + 'fabric': str(fabrics[subnet.get('fabric',
48 + self._get_fabric_from_cidr(subnet.get('cidr')))]),
49 'cidr': subnet.get('cidr'),
50 'gateway_ip': subnet['gateway_ip'],
53 self._process_iprange(res_json['id'])
56 + def _get_fabric_from_cidr(self, cidr):
57 + subnets = json.loads(self._maas.get(u'api/2.0/subnets/').read())
58 + for subnet in subnets:
59 + if subnet['cidr'] == cidr:
60 + return subnet['vlan']['fabric']
63 def _process_iprange(self, subnet_id):
64 ipranges = json.loads(self._maas.get(u'api/2.0/ipranges/').read())
65 LOG.warn('all %s ipranges %s', subnet_id, ipranges)