Pod Descriptor File compatible reclass structure
[fuel.git] / mcp / patches / 0006-maas-module-Add-VLAN-DHCP-enable-support.patch
1 From: Alexandru Avadanii <Alexandru.Avadanii@enea.com>
2 Date: Sat, 19 Aug 2017 02:03:01 +0200
3 Subject: [PATCH] maas: module: Add VLAN DHCP enable support
4
5 MaaS custom py module does not support VLAN configuration.
6 This should be implemented by adding a dedicated class for VLAN.
7 However, we are only interested in updating an existign VLAN to
8 enable DHCP on an existing IP range (set up via subnet configuration),
9 so extend existing subnet handling to include basic VLAN update.
10
11 NOTE: Design-wise, this is hacky, and its only purpose is to allow
12 setting 'dhcp_on=True' for an existing VLAN.
13
14 Example reclass model usage:
15 maas:
16   region:
17     subnets:
18       192.168.11.0/24:
19         # ...
20         vlans:
21           untagged:
22             vid: 0
23             dhcp_on: true
24             primary_rack: mas01
25
26 Signed-off-by: Alexandru Avadanii <Alexandru.Avadanii@enea.com>
27 ---
28
29 diff --git a/_modules/maas.py b/_modules/maas.py
30 index d3227ca..8a2243d 100644
31 --- a/_modules/maas.py
32 +++ b/_modules/maas.py
33 @@ -204,6 +204,7 @@
34              'gateway_ip': subnet['gateway_ip'],
35          }
36          self._iprange = subnet['iprange']
37 +        self._vlans = subnet['vlans']
38          return data
39
40      def update(self, new, old):
41 @@ -214,6 +215,7 @@
42          response = super(Subnet, self).send(data)
43          res_json = json.loads(response)
44          self._process_iprange(res_json['id'])
45 +        self._process_dhcp_vlans_update(data)
46          return response
47
48      def _get_fabric_from_cidr(self, cidr):
49 @@ -248,6 +250,32 @@
50          else:
51              self._maas.post(u'api/2.0/ipranges/', None, **data)
52
53 +    def _process_dhcp_vlans_update(self, subnet_data):
54 +        fabric_vlans = json.loads(self._maas.get(u'api/2.0/fabrics/{0}/vlans/'
55 +                                  .format(subnet_data['fabric'])).read())
56 +        LOG.warn('all fabric %s vlans %s', subnet_data['fabric'], fabric_vlans)
57 +        for vlan_name, vlan_data in self._vlans.iteritems():
58 +            update = False
59 +            old_data = None
60 +            for fabric_vlan in fabric_vlans:
61 +                if fabric_vlan['vid'] == vlan_data['vid']:
62 +                    update = True
63 +                    old_data = fabric_vlan
64 +                    break
65 +            data = {
66 +                'mtu': str(vlan_data.get('mtu', 1500)),
67 +                'dhcp_on': str(vlan_data.get('dhcp_on')),
68 +                'primary_rack': str(vlan_data.get('primary_rack')),
69 +                'secondary_rack': str(vlan_data.get('secondary_rack', ''))
70 +            }
71 +            if update:
72 +                LOG.warn('UPDATING %s %s', data, old_data)
73 +                self._maas.put(u'api/2.0/fabrics/{0}/vlans/{1}/'
74 +                               .format(subnet_data['fabric'], old_data['vid']),
75 +                               **data)
76 +            else:
77 +                LOG.warn('MISSING vlan %s, not doing anything', data)
78 +
79
80  class DHCPSnippet(MaasObject):
81      def __init__(self):