fix bugs when adding and removing sliver attributes
authorsmbaker <smbaker@fc8clean.lan>
Mon, 5 Dec 2011 06:45:23 +0000 (22:45 -0800)
committersmbaker <smbaker@fc8clean.lan>
Mon, 5 Dec 2011 06:45:23 +0000 (22:45 -0800)
sfa/rspecs/elements/versions/sfav1Sliver.py
sfa/rspecs/versions/sfav1.py

index 8a113f3..f037533 100644 (file)
@@ -34,7 +34,7 @@ class SFAv1Sliver:
         for elem in xml.iterchildren():
             if elem.tag not in Sliver.fields:
                 xml_element = XmlElement(elem, xml.namespaces)
-                instance = Element(xml_element)
+                instance = Element(fields=xml_element, element=elem)
                 instance['name'] = elem.tag
                 instance['value'] = elem.text
                 attribs.append(instance)
index affab84..8f172bd 100644 (file)
@@ -104,17 +104,20 @@ class SFAv1(BaseVersion):
         return attributes
 
 
-    def add_sliver_attribute(self, hostname, name, value, network=None):
-        nodes = self.get_nodes({'component_id': '*%s*' % hostname})
-        if not nodes:
+    def add_sliver_attribute(self, component_id, name, value, network=None):
+        nodes = self.get_nodes({'component_id': '*%s*' % component_id})
+        if nodes is not None and isinstance(nodes, list) and len(nodes) > 0:
             node = nodes[0]
             slivers = SFAv1Sliver.get_slivers(node)
             if slivers:
                 sliver = slivers[0]
-                SFAv1Sliver.add_attribute(sliver, name, value)
+                SFAv1Sliver.add_sliver_attribute(sliver, name, value)
+        else:
+            # should this be an assert / raise an exception?
+            print "WARNING: failed to find component_id", component_id
 
-    def get_sliver_attributes(self, hostname, network=None):
-        nodes = self.get_nodes({'component_id': '*%s*' %hostname})
+    def get_sliver_attributes(self, component_id, network=None):
+        nodes = self.get_nodes({'component_id': '*%s*' % component_id})
         attribs = []
         if nodes is not None and isinstance(nodes, list) and len(nodes) > 0:
             node = nodes[0]
@@ -124,11 +127,13 @@ class SFAv1(BaseVersion):
                 attribs = SFAv1Sliver.get_sliver_attributes(sliver.element)
         return attribs
 
-    def remove_sliver_attribute(self, hostname, name, value, network=None):
-        attribs = self.get_sliver_attributes(hostname)
+    def remove_sliver_attribute(self, component_id, name, value, network=None):
+        attribs = self.get_sliver_attributes(component_id)
         for attrib in attribs:
             if attrib['name'] == name and attrib['value'] == value:
-                attrib.element.delete()
+                #attrib.element.delete()
+                parent = attrib.element.getparent()
+                parent.remove(attrib.element)
 
     def add_default_sliver_attribute(self, name, value, network=None):
         if network:
@@ -153,14 +158,12 @@ class SFAv1(BaseVersion):
         return SFAv1Sliver.get_sliver_attributes(defaults[0])
     
     def remove_default_sliver_attribute(self, name, value, network=None):
-        if network:
-            defaults = self.xml.xpath("//network[@name='%s']/sliver_defaults" % network)
-        else:
-            defaults = self.xml.xpath("//sliver_defaults" % network)
-        attribs = SFAv1Sliver.get_sliver_attributes(defaults)
+        attribs = self.get_default_sliver_attributes(network)
         for attrib in attribs:
             if attrib['name'] == name and attrib['value'] == value:
-                attrib.element.delete()    
+                #attrib.element.delete()
+                parent = attrib.element.getparent()
+                parent.remove(attrib.element)
 
     # Links