def fromElement(self, element):
     """
     initializes Storage connection from element.
     First properties are read and then attributes in the element. Results to attributes precedence.
     """
     from comoonics.ComProperties import Properties
     props=element.getElementsByTagName(Properties.TAGNAME)
     #log.debug("fromElement: %s, %u" %(element, len(props)))
     if len(props)>=1:
         self.properties=Properties(props[0])
         for propertyname in self.properties.keys():
             self.log.debug("fromElement: Setting attribute %s, %s" %(propertyname, self.properties[propertyname].getAttribute("value")))
             setattr(self, propertyname, self.properties[propertyname].getAttribute("value"))
     for attribute in element.attributes:
         self.__dict__[attribute.name]=attribute.value
Example #2
0
 def _parseSection(self, section, sectionelement, validsubsections, parent=None, rootElement=False):
     _name=section
     if sectionelement.hasAttribute("name"):
         _name=sectionelement.getAttribute('name')
     _cursect={'__name__': _name,
               '__element__': sectionelement}
     if parent:
         if not rootElement:
             _cursect['__parent__']=parent
             _cursect['parent']=parent['__name__']
         else:
             rootElement=False
     _add_tags=None
     if self.ADD_TAGS.has_key(sectionelement.nodeName):
         _add_tags=self.ADD_TAGS[sectionelement.nodeName]
     _section=None
     if validsubsections:
         _args=None
         for subsection in validsubsections:
             for _child in sectionelement.childNodes:
                 if _child.nodeType == Node.ELEMENT_NODE and _child.nodeName == subsection:
                     mylogger.debug("_parseSection(%s, %s): subsection, %s, %s" %(section, validsubsections, subsection, _child))
                     _section=self._parseSection(subsection, _child, validsubsections, _cursect, rootElement)
                     self._subsection_prefixes[section]=subsection
                     self._sections[self.formatSubsectionPair(section, _section['__name__'])]=_section
                 if _add_tags and _child.nodeName in _add_tags:
                     if hasattr(self, "_parse_"+_child.nodeName):
                         func=getattr(self, "_parse_"+_child.nodeName)
                         func(_cursect, _child)
     for _child in sectionelement.childNodes:
         if _child.nodeType == Node.ELEMENT_NODE and _child.nodeName == self.ARGS_TAGNAME:
             _cursect[self.ARGS_TAGNAME]=self.formatArgs(self._parseArgs(_child))
         elif _child.nodeType == Node.ELEMENT_NODE and _child.nodeName == Properties.TAGNAME:
             _properties=Properties(_child)
             mylogger.debug("_parseSection(%s): properties: %s" %(section, _properties))
             for (_name, _property) in _properties.items():
                 _cursect[_name]=_property.getValue()
     for i in range(sectionelement.attributes.length):
         _child=sectionelement.attributes.item(i)
         _cursect[_child.name]=_child.value
         mylogger.debug("_parseSection(%s, %s): %s->attribute: %s: %s" %(section, validsubsections, _cursect['__name__'], _child.name, _child.value))
     return _cursect
 def __init__(self, testMethod="runTest"):
    from comoonics import XmlTools
    super(test_Properties, self).__init__(testMethod)
    document=XmlTools.parseXMLString(xml_str)
    self.properties=Properties(document.documentElement, document)
class test_Properties(unittest.TestCase):
   def __init__(self, testMethod="runTest"):
      from comoonics import XmlTools
      super(test_Properties, self).__init__(testMethod)
      document=XmlTools.parseXMLString(xml_str)
      self.properties=Properties(document.documentElement, document)

   def test_str(self):
      self.assertEquals(str(self.properties), "Properties:{Property:{ testflag: }, Property:{ testname2: testvalue2}, Property:{ testname1: testvalue1}}")
   
   def test_str_property(self):
      self.assertEquals(str(self.properties["testname1"]), "Property:{ testname1: testvalue1}")
   
   def test_getProperty1(self):
      property_name="testname1"
      result1="testvalue1"
      result2=self.properties[property_name].getValue()
      self.assertEquals(result1, result2, "Property[%s]=%s != %s" %(property_name, result1, result2)) 

   def test_getProperty2(self):
      property_name="testname2"
      result1="testvalue2"
      result2=self.properties[property_name].getValue()
      self.assertEquals(result1, result2, "Property[%s]=%s != %s" %(property_name, result1, result2)) 

   def test_getProperty3(self):
      property_name="testflag"
      result1=""
      result2=self.properties[property_name].getValue()
      self.assertEquals(result1, result2, "Property[%s]=%s != %s" %(property_name, result1, result2)) 

   def test_setProperty4(self):
      property_name="testname3"
      result1="testvalue3"
      self.properties[property_name]=result1
      result2=self.properties[property_name].getValue()
      self.assertEquals(result1, result2, "Property[%s]=%s != %s" %(property_name, result1, result2)) 

   def test_setProperty5(self):
      property_name="testflag2"
      result1=True
      self.properties[property_name]=result1
      result2=self.properties[property_name].getValue()
      self.assertEquals("", result2, "Property[%s]=%s != %s" %(property_name, result1, result2)) 

   def test_setProperty6(self):
      property_name="testname4"
      result1=True
      self.properties[property_name]=result1
      result2=self.properties[property_name].getValue()
      self.assertEquals("", result2, "Property[%s]=%s != %s" %(property_name, result1, result2)) 

   def test_getNonProperty(self):
      property_name="testname5"
      self.assertRaises(KeyError, self.properties.getAttribute, property_name)

   def test_keys(self):
      keys1=["testname1", "testname2", "testflag"]
      keys1.sort()
      keys2=self.properties.keys()
      keys2.sort()
      self.assertEquals(keys1, keys2, "self.properties.keys(): %s, expectedresult: %s" %(keys2, keys1))

   def test_list(self):
      expected_result=["testname1=testvalue1", "testname2=testvalue2", "testflag="]
      expected_result.sort()
      result1=self.properties.list().split("\n")
      result1.sort()
      self.assertEquals(result1, expected_result, "self.properties.list(): %s\nexpectedresult: %s" %(result1, expected_result))
class Storage(object):
    """
    Baseclass for storagesystem implementations. All supported methods should be implemented. If not an exception is
    raised.
    """
    __logStrLevel__="Storage"
    log=ComLog.getLogger(__logStrLevel__)

    def getStorageObject(implementation, the_element):
        """ returns the storage object by the implementation attribute and the given element."""
        module=__import__(implementation)
        for i in implementation.split(".")[1:]:
            module = getattr(module, i)
        if module:
            cls=None
            for key in module.__dict__.keys():
                import inspect
                if inspect.isclass(getattr(module, key)) and inspect.getclasstree([getattr(module, key)], True)[0][0] == Storage:
                    cls=getattr(module, key)
                    break
            if cls:
                try:
                    inst=object.__new__(cls)
                    Storage.log.debug("class is %s" %(cls))
                    inst.__init__(element=the_element)
                    connname=inst.getConnectionName()
                    if not StorageConnections.has_key(connname):
                        Storage.log.debug("Creating new storage connection %s %s" %(connname, StorageConnections.keys()))
                        StorageConnections[connname]=inst
                        return inst
                    else:
                        Storage.log.debug("Returning already established storage connection %s" %(connname))
                        return StorageConnections[connname]
                except:
                    import traceback
                    traceback.print_exc()
                    raise IncompatibleObjectException(cls, Storage)
            else:
                raise IncompatibleObjectException(getattr(module, key), Storage)
        else:
            raise ModuleNotFoundException(implementation)
    getStorageObject=staticmethod(getStorageObject)

    def __init__(self, **kwds):
        """ Default constructor does nothing here. Except saving the parameters in attributes."""
        self.system=self.username=self.password=""
        if kwds.has_key("system"):
            self.system=kwds["system"]
        if kwds.has_key("username"):
            self.username=kwds["username"]
        if kwds.has_key("password"):
            self.password=kwds["password"]
        if kwds.has_key("element"):
            self.fromElement(kwds["element"])

    def fromElement(self, element):
        """
        initializes Storage connection from element.
        First properties are read and then attributes in the element. Results to attributes precedence.
        """
        from comoonics.ComProperties import Properties
        props=element.getElementsByTagName(Properties.TAGNAME)
        #log.debug("fromElement: %s, %u" %(element, len(props)))
        if len(props)>=1:
            self.properties=Properties(props[0])
            for propertyname in self.properties.keys():
                self.log.debug("fromElement: Setting attribute %s, %s" %(propertyname, self.properties[propertyname].getAttribute("value")))
                setattr(self, propertyname, self.properties[propertyname].getAttribute("value"))
        for attribute in element.attributes:
            self.__dict__[attribute.name]=attribute.value
#            log.debug("fromElement: attribute(%s)=%s" %(attribute.name, getattr(self, attribute.name)))

    def getConnectionName(self):
        """ for singleton implementation if you want to have only one connection per storage system you can use
        this string as unique reference.
        Returns the self.system
        """
        return self.system

    def isConnected(self):
        """ Returns True if this instance is already connected to the storagesystem or otherwise False.
        Default is False."""
        return False

    def connect(self):
        """ Connects to the storage system """
        raise NotImplementedYet()

    def map_luns(self, dest, source=None):
        """ Lunmaps the given disk. Hosts and luns are integrated insite the disk. """
        raise NotImplementedYet()

    def unmap_luns(self, dest, source=None):
        """ Lunmaps the given disk (dest). Hosts and luns are integrated insite the disk. """
        raise NotImplementedYet()

    def add(self, dest, source=None):
        """ Adds the given disk (dest). Parameters are packed as properties insite the disk. """
        raise NotImplementedYet()
    def add_snapshot(self, dest, source=None):
        """ Snapshots the given sourcedisk to destdisk. Options are packed as properties insite of destdisk."""
        raise NotImplementedYet()

    def add_clone(self, dest, source=None):
        """ Clones the given sourcedisk to destdisk. Options are packed as properties insite of destdisk. """
        raise NotImplementedYet()

    def delete(self, dest, source=None):
        """ Deletes the given disk (dest). If you only want to support the deleting of snapshots use delete_snaphot. """
        raise NotImplementedYet()

    def delete_snapshot(self, dest, source=None):
        """ Deletes the given disk (dest) only if its a snapshot. """
        raise NotImplementedYet()

    def delete_clone(self, dest, source=None):
        """ Deletes the given disk(dest) only if its a clone. """
        raise NotImplementedYet()

    def close(self):
        """ Cleans up and closes all open connections to the storagesystem """
Example #6
0
 def __init__(self, testMethod="runTest"):
     from comoonics import XmlTools
     super(test_Properties, self).__init__(testMethod)
     document = XmlTools.parseXMLString(xml_str)
     self.properties = Properties(document.documentElement, document)
Example #7
0
class test_Properties(unittest.TestCase):
    def __init__(self, testMethod="runTest"):
        from comoonics import XmlTools
        super(test_Properties, self).__init__(testMethod)
        document = XmlTools.parseXMLString(xml_str)
        self.properties = Properties(document.documentElement, document)

    def test_getProperty1(self):
        property_name = "testname1"
        result1 = "testvalue1"
        result2 = self.properties[property_name].getValue()
        self.assertEquals(
            result1, result2,
            "Property[%s]=%s != %s" % (property_name, result1, result2))

    def test_getProperty2(self):
        property_name = "testname2"
        result1 = "testvalue2"
        result2 = self.properties[property_name].getValue()
        self.assertEquals(
            result1, result2,
            "Property[%s]=%s != %s" % (property_name, result1, result2))

    def test_getProperty3(self):
        property_name = "testflag"
        result1 = ""
        result2 = self.properties[property_name].getValue()
        self.assertEquals(
            result1, result2,
            "Property[%s]=%s != %s" % (property_name, result1, result2))

    def test_setProperty4(self):
        property_name = "testname3"
        result1 = "testvalue3"
        self.properties[property_name] = result1
        result2 = self.properties[property_name].getValue()
        self.assertEquals(
            result1, result2,
            "Property[%s]=%s != %s" % (property_name, result1, result2))

    def test_setProperty5(self):
        property_name = "testflag2"
        result1 = True
        self.properties[property_name] = result1
        result2 = self.properties[property_name].getValue()
        self.assertEquals(
            "", result2,
            "Property[%s]=%s != %s" % (property_name, result1, result2))

    def test_setProperty6(self):
        property_name = "testname4"
        result1 = True
        self.properties[property_name] = result1
        result2 = self.properties[property_name].getValue()
        self.assertEquals(
            "", result2,
            "Property[%s]=%s != %s" % (property_name, result1, result2))

    def test_getNonProperty(self):
        property_name = "testname5"
        self.assertRaises(KeyError, self.properties.getAttribute,
                          property_name)

    def test_keys(self):
        keys1 = ["testname1", "testname2", "testflag"]
        keys1.sort()
        keys2 = self.properties.keys()
        keys2.sort()
        self.assertEquals(
            keys1, keys2,
            "self.properties.keys(): %s, expectedresult: %s" % (keys2, keys1))

    def test_list(self):
        expected_result = [
            "testname1=testvalue1", "testname2=testvalue2", "testflag="
        ]
        expected_result.sort()
        result1 = self.properties.list().split("\n")
        result1.sort()
        self.assertEquals(
            result1, expected_result,
            "self.properties.list(): %s\nexpectedresult: %s" %
            (result1, expected_result))