Exemple #1
0
def import_imc_session(file_path, key):
    """
    This operation will do a login to each IMC which is present in credential file.

    - file_path specifies the path of the credential file.
    - key specifies string used for secure encryption while export_imc_session operation.
    """
    from ImcUtilityCore import _ImcLoginXml

    if file_path is None:
        raise ImcValidationException('[Error]: Please provide file_path')

    if key is None:
        raise ImcValidationException('[Error]: Please provide key')

    if not os.path.isfile(file_path) or not os.path.exists(file_path):
        raise ImcValidationException('[Error]: File <%s> does not exist ' %(file_path))

    doc = xml.dom.minidom.parse(file_path)
    top_node = doc.documentElement
    #print top_node.localName

    if top_node is None or top_node.localName != _ImcLoginXml.IMC_HANDLES:
        return None

    if top_node.hasChildNodes():
        child_list = top_node.childNodes
        child_count = len(child_list)
        for count in range(child_count):
            child_node = child_list.item(count)
            if child_node.nodeType != Node.ELEMENT_NODE:
                continue

            if child_node.localName != _ImcLoginXml.IMC:
                continue

            lname = None
            lusername = None
            lpassword = None
            lnossl = False
            lport = None

            if child_node.hasAttribute(_ImcLoginXml.NAME):
                lname = child_node.getAttribute(_ImcLoginXml.NAME)
            if child_node.hasAttribute(_ImcLoginXml.USER_NAME):
                lusername = child_node.getAttribute(_ImcLoginXml.USER_NAME)
            if child_node.hasAttribute(_ImcLoginXml.PASSWORD):
                #lpassword = p3_decrypt(child_node.getAttribute(_ImcLoginXml.PASSWORD), key)
                lpassword = ImcUtils.decrypt_password(child_node.getAttribute(_ImcLoginXml.PASSWORD), key)
            if child_node.hasAttribute(_ImcLoginXml.NO_SSL):
                lnossl = child_node.getAttribute(_ImcLoginXml.NO_SSL)
            if child_node.hasAttribute(_ImcLoginXml.PORT):
                lport = child_node.getAttribute(_ImcLoginXml.PORT)

            # Process login
            if lname is None or lusername == None or lpassword == None:
                ImcUtils.write_imc_warning("[Warning] Insufficient information for login ...")
                continue
            try:

                handle = ImcHandle()
                handle.login(name=lname, username=lusername, password=lpassword, nossl=lnossl, port=lport)

            except Exception, err:
                ImcUtils.write_imc_warning("[Connection Error<%s>] %s" %(lname, str(err)))