def map_luns(self, *params):
     """ Lunmaps the given disk. Hosts and luns are integrated insite the disk and returns the luns if need be. """
     if not params or len(params)==0:
         raise TypeError("map_luns takes at lease 1 argument 0 given.")
     elif params and len(params)>2:
         raise TypeError("map_luns takes at lease 1 argument %u given." %(len(params)))
     disk=params[0]
     source=None
     if len(params)==2:
         source=params[1]
     luns=list()
     for lun in disk.getLuns():
         for host in disk.getHostNames(lun):
             try:
                 self.sssu.cmd("add lun %s vdisk=\"%s\" host=\"%s\"" %(lun, disk.getAttribute("name"), host))
                 if lun.count("\\")==0:
                     if host.count("\\")==0:
                         host="\Hosts\\%s" %(host)
                     self.sssu.cmd("ls lun \"%s\\%s\" xml" %(host,lun))
                 else:
                     self.sssu.cmd("ls lun \"%s\" xml" %(lun))
                 lun=None
                 if self.sssu.xml_output:
                     lun=HP_EVA_Object.fromXML(self.sssu.xml_output)
                     luns.append(lun)
             except CommandError, ce:
                 raise ErrorDuringExecution("Could not add lun to storage\nExecution errorcode %u: \ncmd: %s" %(self.sssu.last_error_code, self.sssu.last_cmd), ce)
 def unmap_luns(self, *params):
     """ Lunmaps the given disk. Hosts and luns are integrated insite the disk. """
     if not params or len(params) == 0:
         raise TypeError("unmap_luns takes at lease 1 argument 0 given.")
     elif params and len(params) > 2:
         raise TypeError("unmap_luns takes at lease 1 argument %u given." % (len(params)))
     disk = params[0]
     source = None
     if len(params) == 2:
         source = params[1]
     luns = list()
     for lun in disk.getLuns():
         for host in disk.getHostNames(lun):
             try:
                 if host.count("\\") > 0:
                     path = host + "\\"
                 else:
                     path = ""
                 if self.sssu.cmd('ls lun "%s%s" xml' % (path, lun)) == 0 and self.sssu.xml_output:
                     luns.append(HP_EVA_Object.fromXML(self.sssu.xml_output))
                 self.sssu.cmd('delete lun "%s%s"' % (path, lun))
             except CommandError, ce:
                 raise ErrorDuringExecution(
                     "Could not delete lun from storage\nExecution errorcode %u: \ncmd: %s"
                     % (self.sssu.last_error_code, self.sssu.last_cmd),
                     ce,
                 )
    def _add(self, type, name, properties):
        #
        # THOUGTS: We need to check disk for
        #  operationalstate .....................: attention
        # VS
        #  operationalstate .....................: good
        # to simulate something like WAIT_FOR_COMLETITION
        #
        try:
            mylogger.debug('add %s "%s" %s' % (type, name, properties))
            try:
                _result = self.sssu.cmd('add %s "%s"' % (type, name), properties)
            except TIMEOUT:
                _result = 0
            if _result == 0:
                if type == "vdisk":
                    lscmd = 'ls %s "%s\\ACTIVE" xml' % (type, name)
                elif type == "snapshot":
                    vdisk = properties["vdisk"].getAttribute("value")
                    if vdisk.find("\\ACTIVE") > 0:
                        vdisk = vdisk[: vdisk.rindex("\\ACTIVE")]
                    lscmd = 'ls %s "%s\\%s" xml' % (type, vdisk, name)
                else:
                    lscmd = 'ls %s "%s" xml' % (type, name)
                operational = False
                iterations = 0
                maxiterations = 10
                while not operational and iterations < maxiterations:
                    self.sssu.cmd(lscmd)
                    vdisk = None
                    if self.sssu.xml_output:
                        vdisk = HP_EVA_Object.fromXML(self.sssu.xml_output)
                        if hasattr(vdisk, "operationalstate"):
                            mylogger.debug("_add: operationalstate: %s" % (getattr(vdisk, "operationalstate")))
                            if getattr(vdisk, "operationalstate") == "good":
                                operational = True
                            else:
                                import time

                                mylogger.debug("_add: operationalstate is not good waiting(%u).." % (iterations))
                                time.sleep(5)
                                iterations += 1
                    else:
                        iterations = maxiterations
                return vdisk
            else:
                raise ErrorDuringExecution(
                    "Could not add %s %s to storage.\nExecution errorcode %u: \ncmd: %s, output: %s"
                    % (type, name, self.sssu.last_error_code, self.sssu.last_cmd, self.sssu.last_output)
                )
        except CommandError, ce:
            raise ErrorDuringExecution(
                "Could not add %s %s to storage\nExecution errorcode %u: \ncmd: %s\noutput: %s"
                % (type, name, ce.errorcode, ce.cmd, ce.output)
            )
    def _delete(self, _type, name, properties, checkfunc=None):
        try:
            vdisk = None
            default_params = {"vdisk": ["WAIT_FOR_COMPLETION"]}
            allowed_params = {"vdisk": ["WAIT_FOR_COMPLETION", "NOWAIT_FOR_COMPLETION"]}
            # this means we are called as undo in consequence as called like add so we have to make the name
            # and move other paramaters away
            if _type == "vdisk" and properties and properties.has_key("vdisk"):
                vdisk = properties["vdisk"].getAttribute("value")
                if vdisk.find("\\ACTIVE") > 0:
                    vdisk = vdisk[: vdisk.rindex("\\ACTIVE")]
                name = vdisk + "\\" + name
            # clear all other properties except those allowed for delete
            if properties:
                for key in properties.keys():
                    if allowed_params.has_key(_type) and key in allowed_params[_type]:
                        pass
                    else:
                        del properties[key]
            if default_params.has_key(_type):
                if not properties:
                    properties = dict()
                for def_parm in default_params[_type]:
                    if type(properties) == dict:
                        properties[def_parm] = True
                    else:
                        properties[def_parm] = ""
                        mylogger.debug(
                            "property[%s]=%s, type=%s"
                            % (def_parm, properties[def_parm].getValue(), type(properties[def_parm].getValue()))
                        )

            if self.sssu.cmd('ls %s "%s" xml' % (_type, name)) == 0 and self.sssu.xml_output:
                vdisk = HP_EVA_Object.fromXML(self.sssu.xml_output)
                mylogger.debug("deleting vdisk %s" % (vdisk.objectname))
                if isinstance(vdisk, HP_EVA_Object):
                    if not checkfunc or (checkfunc and checkfunc(vdisk)):
                        self.sssu.cmd('delete %s "%s"' % (_type, name), properties)
                return vdisk
            else:
                raise ErrorDuringExecution(
                    "Could not delete %s %s from storage. %s %s does not exist" % (type, name, type, name)
                )
        except CommandError, ce:
            raise ErrorDuringExecution(
                "Could not delete %s %s to storage\nExecution errorcode %u: \ncmd: %s"
                % (type, name, self.sssu.last_error_code, self.sssu.last_cmd),
                ce,
            )