pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
authorTero Marttila <terom@paivola.fi>
Sun, 01 Mar 2015 19:54:20 +0200
changeset 661 15946375b154
parent 660 5461fb492345
child 662 4750b8b85aa1
pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
pvl/hosts/config.py
pvl/hosts/tests.py
--- a/pvl/hosts/config.py	Sun Mar 01 19:41:38 2015 +0200
+++ b/pvl/hosts/config.py	Sun Mar 01 19:54:20 2015 +0200
@@ -101,7 +101,10 @@
             name        - (expanded) name of host
             domain      - domain for host
             config      - host config fields to parse
-        
+
+        Fields can either be scalar string values, or instance'd dicts. If a field has both a non-instance'd value and instance'd values,
+        the non-instance'd value will be use instance=None:
+
         Raises ValueError.
     """
 
@@ -115,16 +118,17 @@
             f = extensions.setdefault(extension, {})
         else:
             f = fields
-        
-        if not instance and field not in f:
-            f[field] = value
 
-        elif field not in f:
-            f[field] = {instance: value}
-        elif isinstance(f[field], dict):
-            f.setdefault(field, {})[instance] = value
+        if instance:
+            if field not in f:
+                f[field] = { }
+            elif not isinstance(f[field], dict):
+                # convert to dict
+                f[field] = {None: f[field]}
+            
+            f[field][instance] = value
         else:
-            raise ValueError("override instanced {field} value: {value}".format(field=field, value=value))
+            f[field] = value
     
     return Host.build(name, domain, **fields)
 
--- a/pvl/hosts/tests.py	Sun Mar 01 19:41:38 2015 +0200
+++ b/pvl/hosts/tests.py	Sun Mar 01 19:54:20 2015 +0200
@@ -57,12 +57,19 @@
                 }
         ))
    
-    def testApplyHostsConfigErrorDict(self):
-        with self.assertRaises(ValueError):
-            config.apply_host('test', 'foo', {
-                'ethernet': 'foo',
-                'ethernet.eth0': 'bar',
-            })
+    def testApplyHostsConfigErrorExtra(self):
+        host = config.apply_host('foo', 'test', {
+            'ethernet': '00:11:22:33:44:55',
+            'ethernet.eth1': '00:11:22:33:44:66',
+        })
+
+        self.assertHostEqual(host, 'foo@test', dict(
+                ethernet    = {
+                    None:   '00:11:22:33:44:55',
+                    'eth1': '00:11:22:33:44:66',
+                }
+        ))
+ 
 
     def testApplyHostConfigExtensions(self):
         host = config.apply_host('foo', 'test', {