Identify jump host bridges based on IDF / PDF nets
[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 --- a/_modules/maas.py
24 +++ b/_modules/maas.py
25 @@ -126,6 +126,8 @@
26
27              def process_single(name, config_data):
28                  self._update = False
29 +                if isinstance(config_data, dict) and 'name' in config_data:
30 +                    name = config_data['name']
31                  try:
32                      data = self.fill_data(name, config_data, **extra)
33                      if data is None:
34 @@ -198,7 +198,8 @@
35      def fill_data(self, name, subnet, fabrics):
36          data = {
37              'name': name,
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'],
43          }
44 @@ -215,6 +216,13 @@
45          self._process_iprange(res_json['id'])
46          return response
47
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']
53 +        return ''
54 +
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)