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