Browse Source

Fetch transformations configuration from Nailgun settings

Change-Id: Ic03f729c738745ff18c849cb4f3a4b5b8d3fe90c
Closes-Bug: #1618177
Yuriy Taraday 2 years ago
parent
commit
f23d273ee8

+ 50
- 3
README.rst View File

@@ -6,6 +6,53 @@ cluster upgrading. This extension used by the fuel-octane project.
6 6
 
7 7
 Instalation
8 8
 -----------
9
-After installing `fuel-nailgun-extension-cluster-upgrade` package run:
10
-1) `nailgun_syncdb` - migrate database
11
-2) restart nailgun service
9
+After installing ``fuel-nailgun-extension-cluster-upgrade`` package run:
10
+ #. ``nailgun_syncdb`` - migrate database
11
+ #. restart nailgun service
12
+
13
+Transformer configuration
14
+-------------------------
15
+
16
+Every transformation manager has default config that hardcoded, but
17
+you can overwrite this config with your own transformations
18
+extensions. This could be done by extending ``nailgun/settings.yaml``
19
+file.
20
+
21
+**Example**
22
+
23
+::
24
+   CLUSTER_UPGRADE:
25
+     transformations:
26
+       cluster:
27
+         7.0: [transform_vips]
28
+         9.0: [first_transformation, second_transformation]
29
+
30
+   ...
31
+
32
+In extension you should define a entrypoint is such way:
33
+
34
+::
35
+   nailgun.cluster_upgrade.transformations.cluster.7.0 =
36
+      transform_vips = my_project.transformations:transform_cluster_vips
37
+
38
+on first line we have entripoint name where
39
+* ``nailgun.cluster_upgrade.transformations`` - namespace where all
40
+  transformations defined.
41
+* ``cluster`` - name of object which data transformed
42
+* ``7.0`` - cluster version where these transformations should happen
43
+
44
+on the second line
45
+* ``transform_vips`` - unique transformation name that you can use in
46
+  configuration file or in transformation manager
47
+* ``my_project.transformations`` - module name
48
+* ``transform_cluster_vips`` - transformer function name
49
+
50
+
51
+Transformation function must take only one argument - data to
52
+transform. When you call ``manager.apply(from_version, to_version,
53
+data)`` all transformer functions ordered by a version called one by
54
+one, and output of one transformer used as input to the other.
55
+
56
+In out example calling ``cluster_manager.apply('6.0', '9.1', data)``
57
+will call three functions ``transform_vips``,
58
+``first_transformation``, ``second_transformation``.

+ 30
- 6
cluster_upgrade/tests/test_transformations.py View File

@@ -21,13 +21,36 @@ from ..transformations import cluster
21 21
 
22 22
 
23 23
 class TestTransformations(nailgun_test_base.BaseUnitTest):
24
-    def test_get_config(self):
25
-        config = object()
26
-
24
+    def _test_get_config(self, def_config, settings, expected_result):
27 25
         class Manager(transformations.Manager):
28
-            default_config = config
26
+            default_config = def_config
27
+
28
+        with mock.patch("nailgun.settings.settings.config", new=settings):
29
+            result = Manager.get_config('testname')
30
+
31
+        self.assertEqual(expected_result, result)
32
+
33
+    @staticmethod
34
+    def _trans_settings(config):
35
+        return {'CLUSTER_UPGRADE': {'transformations': {'testname': config}}}
29 36
 
30
-        self.assertIs(config, Manager.get_config('testname'))
37
+    def test_get_config_default(self):
38
+        config = {'9.0': []}
39
+        self._test_get_config(config, {}, config)
40
+
41
+    def test_get_config_no_overwrite(self):
42
+        self._test_get_config(
43
+            {'9.0': ['a']},
44
+            self._trans_settings({'8.0': ['b']}),
45
+            {'8.0': ['b'], '9.0': ['a']},
46
+        )
47
+
48
+    def test_get_config_overwrite(self):
49
+        self._test_get_config(
50
+            {'9.0': ['a']},
51
+            self._trans_settings({'8.0': ['b'], '9.0': ['c']}),
52
+            {'8.0': ['b'], '9.0': ['c']},
53
+        )
31 54
 
32 55
     def setup_extension_manager(self, extensions):
33 56
         p = mock.patch("stevedore.ExtensionManager", spec=['__call__'])
@@ -153,8 +176,9 @@ class TestTransformations(nailgun_test_base.BaseUnitTest):
153 176
             ),
154 177
         ])
155 178
 
179
+    @mock.patch.object(transformations.Manager, 'get_config')
156 180
     @mock.patch.object(transformations.Manager, 'load_transformers')
157
-    def test_apply(self, mock_load):
181
+    def test_apply(self, mock_load, mock_config):
158 182
         mock_trans = mock.Mock()
159 183
         mock_load.return_value = [
160 184
             (version.StrictVersion('7.0'), [mock_trans.a, mock_trans.b]),

+ 11
- 3
cluster_upgrade/transformations/__init__.py View File

@@ -15,10 +15,12 @@ import distutils.version
15 15
 import logging
16 16
 import threading
17 17
 
18
-import six
19 18
 
19
+import six
20 20
 import stevedore
21 21
 
22
+from nailgun.settings import settings
23
+
22 24
 LOG = logging.getLogger(__name__)
23 25
 
24 26
 
@@ -37,8 +39,14 @@ class Manager(object):
37 39
 
38 40
     @classmethod
39 41
     def get_config(cls, name):
40
-        # TODO(yorik-sar): merge actual config with defaults
41
-        return cls.default_config
42
+        res = cls.default_config.copy()
43
+        try:
44
+            conf = settings.config['CLUSTER_UPGRADE']['transformations'][name]
45
+        except KeyError:
46
+            pass
47
+        else:
48
+            res.update(conf)
49
+        return res
42 50
 
43 51
     @staticmethod
44 52
     def load_transformers(name, config):

Loading…
Cancel
Save