Merge "virtual: odl-router: Stop overriding Ubuntu repos"
[fuel.git] / mcp / patches / 0005-maas-module-Obtain-fabric-ID-from-CIDR.patch
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
4
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
8 method is unreliable.
9
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).
12
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;
17
18 Signed-off-by: Alexandru Avadanii <Alexandru.Avadanii@enea.com>
19 Signed-off-by: Guillermo Herrero <Guillermo.Herrero@enea.com>
20 ---
21
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
26 @@ -198,7 +198,8 @@
27      def fill_data(self, name, subnet, fabrics):
28          data = {
29              'name': name,
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'],
35          }
36 @@ -215,6 +216,13 @@
37          self._process_iprange(res_json['id'])
38          return response
39
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']
45 +        return ''
46 +
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)