コード例 #1
0
 def unparse(self, pretty=False):
     if self.name is None or \
        self.capacity is None or \
        self.allocation is None or \
        self.target is None:
         raise StorageVolumeError("Missing mandatory fields value")
     try:
         xml_dict = {}
         xml_dict['name'] = self.name
         if self.key:
             xml_dict['key'] = self.key
         xml_dict['capacity'] = self.capacity
         xml_dict['allocation'] = self.allocation
         xml_dict['target'] = self.target
         if self.source:
             xml_dict['source'] = self.source
         if self.backingstore:
             xml_dict['backingStore'] = self.backingstore
         buffer = xmltodict.unparse({'volume': xml_dict})
         if pretty:
             try:
                 import xml.dom.minidom
                 nodes = xml.dom.minidom.parseString(buffer)
                 buffer = nodes.toprettyxml()
             except:
                 pass
         return buffer
     # error with xmltodict
     except Exception as e:
         raise StorageVolumeError("{0}".format(e))
コード例 #2
0
 def parse(xml):
     obj = StorageVolume()
     try:
         xml_dict = xmltodict.parse(xml)
         # ignore volume tag
         xml_dict = xml_dict['volume']
         # mandatory fields parsing
         obj.name = xml_dict['name']
         key = xml_dict.get('key', None)
         if key:
             obj.key = key
         obj.capacity = xml_dict['capacity']
         obj.allocation = xml_dict['allocation']
         obj.target = xml_dict['target']
         source = xml_dict.get('source', None)
         if source:
             obj.source = source
         backingstore = xml_dict.get('backingStore', None)
         if backingstore:
             obj.backingstore = backingstore
         return obj
     # error when fetching values
     except KeyError as e:
         raise StorageVolumeError("{0}".format(e))
     # error with xmltodict
     except Exception as e:
         raise StorageVolumeError("{0}".format(e))
コード例 #3
0
    def _set_target(self, value):
        # initialization
        path, type, permissions, encryption = None, None, None, None
        # parsing
        if isinstance(value, basestring):
            path = value
        elif isinstance(value, dict):
            path = value['path']
            format = value.get('format', None)
            if format:
                type = format.get('@type', None)
            permissions = value.get('permissions', None)
        else:
            reason = "'value' field '{0}' is not valid".format(value)
            raise StorageVolumeError(reason)

        # saving values into variables
        if not isinstance(path, basestring):
            reason = "'path' field '{0}' is not valid".format(path)
            raise StorageVolumeError(reason)
        if type is not None and type not in sum(
            [["NONE", "AUTO", "EXT2", "EXT3", "EXT4", "UFS", "ISO9660",
              "UDF", "GFS", "GFS2", "VFAT", "HFS", "XFS"],
             ["RAW", "DIR", "BOCHS", "CLOOP", "QCOW", "DMG", "ISO",
              "QCOW", "QCOW2", "VMDK", "VPC"]], []):
            reason = "'type' field '{0}' is not valid".format(type)
            raise StorageVolumeError(reason)
        if permissions:
            if isinstance(permissions, int):
                value = permissions
                permissions = {}
                permissions['mode'] = value
                permissions['owner'] = value
                permissions['group'] = value
            elif isinstance(permissions, dict):
                mode = permissions.pop('mode', None)
                owner = permissions.pop('owner', None)
                group = permissions.pop('group', None)
                label = permissions.pop('label', None)
                permissions = {}
                if mode is not None:
                    permissions['mode'] = mode
                if owner is not None:
                    permissions['owner'] = owner
                if group is not None:
                    permissions['group'] = group
                if label is not None:
                    permissions['label'] = label
            else:
                reason = ("'permissions' field '{0}' ".format(permissions) +
                          "is not valid")
                raise StorageVolumeError(reason)
        # commiting value to a new structure
        self._target = {}
        if path:
            self._target['path'] = path
        if type:
            self._target['format'] = {'@type': type}
        if permissions:
            self._target['permissions'] = permissions
コード例 #4
0
 def _set_allocation(self, value):
     # parsing and saving a variables
     size, unit = None, None
     if isinstance(value, (basestring, int)):
         size = int(value)
     elif isinstance(value, dict):
         size = int(value['#text'])
         unit = value.get('@unit', None)
     else:
         reason = "'value' field '{0}' is not valid".format(value)
         raise StorageVolumeError(reason)
     # checks on values
     if unit is not None and \
        unit not in ["B", "K", "KB", "KiB", "M", "MB", "MiB", "G",
                     "GB", "GiB", "T", "TB", "TiB", "P", "PB",
                     "PiB", "E", "EB", "EiB"]:
         reason = "'@unit' field '{0}' is not valid".format(value)
         raise StorageVolumeError(reason)
     # commiting variable to structure
     self._allocation = {}
     self._allocation["#text"] = size
     if unit:
         self._allocation['@unit'] = unit
コード例 #5
0
 def _set_source(self, value):
     if not value:
         self._source = None
     # parsing
     path = None
     if isinstance(value, basestring):
         path = value
     elif isinstance(value, dict):
         path = value['path']
     else:
         reason = "'value' field '{0}' is not valid".format(value)
         raise StorageVolumeError(reason)
     # commiting value to structure
     if path:
         self._source = {}
         self._source['path'] = path
コード例 #6
0
 def _set_name(self, value):
     if not isinstance(value, basestring):
         reason = "'value' field '{0}' is not valid".format(value)
         raise StorageVolumeError(reason)
     self._name = value
コード例 #7
0
 def _set_key(self, value):
     if value and not isinstance(value, str):
         reason = "'value' field '{0}' is not valid".format(value)
         raise StorageVolumeError(reason)
     self._key = value