Browse Source

[trac926] error on duplicate add

Jelte Jansen 13 years ago
parent
commit
2cd7eb5d2c

+ 2 - 0
src/bin/bindctl/bindcmd.py

@@ -398,6 +398,8 @@ class BindCmdInterpreter(Cmd):
                 print("Error: " + str(dte))
                 print("Error: " + str(dte))
             except isc.cc.data.DataNotFoundError as dnfe:
             except isc.cc.data.DataNotFoundError as dnfe:
                 print("Error: " + str(dnfe))
                 print("Error: " + str(dnfe))
+            except isc.cc.data.DataAlreadyPresentError as dnfe:
+                print("Error: " + str(dnfe))
             # [XX] TODO: add back
             # [XX] TODO: add back
             #except KeyError as ke:
             #except KeyError as ke:
             #    print("Error: missing " + str(ke))
             #    print("Error: missing " + str(ke))

+ 16 - 2
src/lib/python/isc/cc/data.py

@@ -22,8 +22,22 @@
 
 
 import json
 import json
 
 
-class DataNotFoundError(Exception): pass
-class DataTypeError(Exception): pass
+class DataNotFoundError(Exception):
+    """Raised if an identifier does not exist according to a spec file,
+       or if an item is addressed that is not in the current (or default)
+       config (such as a nonexistent list or map element)"""
+    pass
+
+class DataAlreadyPresentError(Exception):
+    """Raised if there is an attemt to add an element to a list or a
+       map that is already present in that list or map (i.e. if 'add'
+       is used when it should be 'set')"""
+    pass
+
+class DataTypeError(Exception):
+    """Raised if there is an attempt to set an element that is of a
+       different type than the type specified in the specification."""
+    pass
 
 
 def remove_identical(a, b):
 def remove_identical(a, b):
     """Removes the values from dict a that are the same as in dict b.
     """Removes the values from dict a that are the same as in dict b.

+ 11 - 3
src/lib/python/isc/config/ccsession.py

@@ -441,6 +441,8 @@ class UIModuleCCSession(MultiConfigData):
         if value not in cur_list:
         if value not in cur_list:
             cur_list.append(value)
             cur_list.append(value)
             self.set_value(identifier, cur_list)
             self.set_value(identifier, cur_list)
+        else:
+            raise isc.cc.data.DataAlreadyPresentError(value + " already in " + identifier)
 
 
     def _add_value_to_named_map(self, identifier, value):
     def _add_value_to_named_map(self, identifier, value):
         if value is None:
         if value is None:
@@ -451,15 +453,21 @@ class UIModuleCCSession(MultiConfigData):
             cur_map, status = self.get_value(identifier)
             cur_map, status = self.get_value(identifier)
             if not cur_map:
             if not cur_map:
                 cur_map = {}
                 cur_map = {}
-            cur_map[value] = {}
-            self.set_value(identifier, cur_map)
+            if value not in cur_map:
+                cur_map[value] = {}
+                self.set_value(identifier, cur_map)
+            else:
+                raise isc.cc.data.DataAlreadyPresentError(value + " already in " + identifier)
 
 
     def add_value(self, identifier, value_str = None):
     def add_value(self, identifier, value_str = None):
         """Add a value to a configuration list. Raises a DataTypeError
         """Add a value to a configuration list. Raises a DataTypeError
            if the value does not conform to the list_item_spec field
            if the value does not conform to the list_item_spec field
            of the module config data specification. If value_str is
            of the module config data specification. If value_str is
            not given, we add the default as specified by the .spec
            not given, we add the default as specified by the .spec
-           file."""
+           file. Raises a DataNotFoundError if the given identifier
+           is not specified in the specification as a map or list.
+           Raises a DataAlreadyPresentError if the specified element
+           already exists."""
         module_spec = self.find_spec_part(identifier)
         module_spec = self.find_spec_part(identifier)
         if module_spec is None:
         if module_spec is None:
             raise isc.cc.data.DataNotFoundError("Unknown item " + str(identifier))
             raise isc.cc.data.DataNotFoundError("Unknown item " + str(identifier))