示例#1
0
def url_helper(url,
               body,
               method,
               auth,
               result,
               validate_certs,
               timeout=DEFAULT_TO,
               credential=None):
    myheaders = {}
    if credential == None:
        myheaders = {
            "Authorization": auth["auth"],
            'Content-Type': 'application/yang-data+xml'
        }
    else:
        myheaders = credential

    try:
        get_resp = ansible_urls.open_url(url,
                                         body,
                                         headers=myheaders,
                                         method=method,
                                         timeout=timeout,
                                         validate_certs=validate_certs)
    except urllib_error.HTTPError as e:
        e_data = e.read()
        if len(e_data) > 0:
            ret_code, root_dict = bsn_xmltodict(result, e_data)
            result[method + "_resp_data"] = root_dict
        else:
            result[method + "_resp_data"] = e_data

        if e.code == 404 and (root_dict["errors"]["error"]["error-message"]
                              == "No entries found"
                              or root_dict["errors"]["error"]["error-message"]
                              == "No syslog servers are configured"
                              or root_dict["errors"]["error"]["error-message"]
                              == "No entries in Name Server"):
            empty_list_resp = {}
            empty_list_resp["Response"] = {}
            empty_list_resp["Response"][os.path.basename(url)] = []
            return -1, 0, empty_list_resp, None

        result[method + "_url"] = url
        result[method + "_resp_code"] = e.code
        result[method + "_resp_reason"] = e.reason

        ret_val = -1
        if e.code == 405:
            ret_val = -2
        elif e.code == 503:
            ret_val = -3
            result["myretry"] = True
        else:
            result["failed"] = True
            result["msg"] = method + " failed"

        return -1, ret_val, None, None

    return 0, 0, None, get_resp,
示例#2
0
def url_post_resp(fos_ip_addr, is_https, auth, vfid, result, url, body,
                  timeout):
    """
        general function to post for a given url

        :param fos_ip_addr: fos switch ip address
        :type fos_ip_addr: str
        :param is_https: indicate to use HTTPS or HTTP
        :type fos_password: Bool
        :param auth: return authorization struct at the time of login
        :type auth: dict
        :param result: dict to store execution results in
        :type result: dict
        :param url: full url
        :type url: str
        :param body: body to post
        :type body: str
        :return: 0 for success or -1 for failure
        :rtype: int
    """
    not_used, validate_certs = full_url_get(is_https, "", "")

    if vfid is not None and vfid != -1:
        url = url + VF_ID + str(vfid)

    edict = {}
    retval, eret, edict, post_resp = url_helper(url, body, "POST", auth,
                                                result, validate_certs,
                                                timeout)
    if retval == ERROR_GENERIC:
        if eret == ERROR_SERVER_BUSY:
            time.sleep(auth["throttle"])
            retval, eret, edict, post_resp = url_helper(
                url, body, "POST", auth, result, validate_certs, timeout)
            if retval == ERROR_GENERIC:
                return eret, edict
        else:
            return eret, edict

    post_resp_data = post_resp.read()
    if len(post_resp_data) == 0:
        return 0, edict

    ret_code, root_dict = bsn_xmltodict(result, post_resp_data)
    if ret_code == -1:
        result["failed"] = True
        result["msg"] = "bsn_xmltodict failed"
        return -100, None

    return 0, root_dict
示例#3
0
def url_get_to_dict(fos_ip_addr, is_https, auth, vfid, result, url, timeout):
    """
        retrieve existing url content and return dict

        :param fos_ip_addr: ip address of FOS switch
        :type fos_ip_addr: str
        :param is_https: indicate to use HTTP or HTTPS
        :type is_https: bool
        :param auth: authorization struct from login
        :type auth: dict
        :param result: dict to keep track of execution msgs
        :type result: dict
        :param url: url for FOS REST API
        :type url: str
        :return: code to indicate failure or success
        :rtype: int
        :return: list of dict of port configurations
        :rtype: list
    """
    not_used, validate_certs = full_url_get(is_https, "", "")

    if vfid is not None and vfid != -1:
        url = url + VF_ID + str(vfid)

    retval = 0
    eret = 0
    edict = {}
    get_resp = {}
    retval, eret, edict, get_resp = url_helper(url, None, "GET", auth, result,
                                               validate_certs, timeout)
    if retval == ERROR_GENERIC:
        if eret == ERROR_SERVER_BUSY:
            time.sleep(auth["throttle"])
            retval, eret, edict, get_resp = url_helper(url, None, "GET", auth,
                                                       result, validate_certs,
                                                       timeout)
            if retval == ERROR_GENERIC:
                return eret, edict
        else:
            return eret, edict

    data = get_resp.read()
    ret_code, root_dict = bsn_xmltodict(result, data)
    if ret_code == -1:
        result["failed"] = True
        result["msg"] = "bsn_xmltodict failed"
        return -100, None

    return 0, root_dict
示例#4
0
def url_post(fos_ip_addr, is_https, auth, vfid, result, url, body):
    """
        general function to post for a given url

        :param fos_ip_addr: fos switch ip address
        :type fos_ip_addr: str
        :param is_https: indicate to use HTTPS or HTTP
        :type fos_password: Bool
        :param auth: return authorization struct at the time of login
        :type auth: dict
        :param result: dict to store execution results in
        :type result: dict
        :param url: full url
        :type url: str
        :param body: body to post
        :type body: str
        :return: 0 for success or -1 for failure
        :rtype: int
    """
    not_used, validate_certs = full_url_get(is_https, "", "")

    if vfid is not None and vfid != -1:
        url = url + VF_ID + str(vfid)

    try:
        resp = ansible_urls.open_url(url,
                                     body,
                                     headers={
                                         "Authorization":
                                         auth["auth"],
                                         'Content-Type':
                                         'application/yang-data+xml'
                                     },
                                     method="POST",
                                     validate_certs=validate_certs)
    except urllib_error.HTTPError as e:
        result["post_url"] = url
        result["post_resp_code"] = e.code
        result["post_resp_reason"] = e.reason
        ret_code, root_dict = bsn_xmltodict(result, e.read())
        result["post_resp_data"] = root_dict
        result["failed"] = True
        result["msg"] = "url_post failed"
        return -1

    time.sleep(auth["throttle"])

    return 0
示例#5
0
def url_get_to_dict(fos_ip_addr, is_https, auth, vfid, result, url):
    """
        retrieve existing url content and return dict

        :param fos_ip_addr: ip address of FOS switch
        :type fos_ip_addr: str
        :param is_https: indicate to use HTTP or HTTPS
        :type is_https: bool
        :param auth: authorization struct from login
        :type auth: dict
        :param result: dict to keep track of execution msgs
        :type result: dict
        :param url: url for FOS REST API
        :type url: str
        :return: code to indicate failure or success
        :rtype: int
        :return: list of dict of port configurations
        :rtype: list
    """
    not_used, validate_certs = full_url_get(is_https, "", "")

    if vfid is not None and vfid != -1:
        url = url + VF_ID + str(vfid)

    try:
        get_resp = ansible_urls.open_url(url,
                                         headers={
                                             "Authorization":
                                             auth["auth"],
                                             'Content-Type':
                                             'application/yang-data+xml'
                                         },
                                         method="GET",
                                         validate_certs=validate_certs)
    except urllib_error.HTTPError as e:
        e_data = e.read()
        ret_code, root_dict = bsn_xmltodict(result, e_data)
        if e.code == 404 and (root_dict["errors"]["error"]["error-message"]
                              == "No entries found"
                              or root_dict["errors"]["error"]["error-message"]
                              == "No syslog servers are configured"):
            empty_list_resp = {}
            empty_list_resp["Response"] = {}
            empty_list_resp["Response"][os.path.basename(url)] = []
            return 0, empty_list_resp

        ret_val = -1
        result["get_url"] = url
        result["get_resp_code"] = e.code
        result["get_resp_reason"] = e.reason
        result["get_resp_data"] = root_dict
        if e.code == 405:
            ret_val = -2
        else:
            result["failed"] = True
            result["msg"] = "url_get_to_dict failed"
        return ret_val, None

    data = get_resp.read()
    ret_code, root_dict = bsn_xmltodict(result, data)
    if ret_code == -1:
        result["failed"] = True
        result["msg"] = "bsn_xmltodict failed"
        return -100, None

    time.sleep(auth["throttle"])

    return 0, root_dict
示例#6
0
def url_helper(url,
               body,
               method,
               auth,
               result,
               validate_certs,
               timeout,
               credential=None):
    myheaders = {}
    if credential == None:
        myheaders = {
            "Authorization": auth["auth"],
            'Content-Type': 'application/yang-data+xml'
        }
    else:
        myheaders = credential

    if timeout == None:
        timeout = DEFAULT_TO

    try:
        get_resp = ansible_urls.open_url(url,
                                         body,
                                         headers=myheaders,
                                         method=method,
                                         timeout=timeout,
                                         validate_certs=validate_certs,
                                         follow_redirects=False)
    except urllib_error.HTTPError as e:
        e_data = e.read()
        if len(e_data) > 0:
            ret_code, root_dict = bsn_xmltodict(result, e_data)
            result[method + "_resp_data"] = root_dict
        else:
            result[method + "_resp_data"] = e_data

        if e.code == 404 and root_dict["errors"]["error"][
                "error-message"] in messages_404:
            empty_list_resp = {}
            empty_list_resp["Response"] = {}
            empty_list_resp["Response"][os.path.basename(url)] = []
            return ERROR_GENERIC, 0, empty_list_resp, None

        result[method + "_url"] = url
        result[method + "_resp_code"] = e.code
        result[method + "_resp_reason"] = e.reason

        ret_val = ERROR_GENERIC
        if e.code == 405:
            ret_val = ERROR_LIST_EMPTY
        elif e.code == 503:
            is_chassis_not_ready, err_msg = chassis_not_ready_message(
                root_dict["errors"]["error"])
            if is_chassis_not_ready:
                result["failed"] = True
                result["msg"] = method + " failed"
            else:
                ret_val = ERROR_SERVER_BUSY
                result["myretry"] = True
        elif e.code == 400:
            is_known, err_msg = known_empty_message(
                root_dict["errors"]["error"])
            if is_known:
                result["msg"] = err_msg
                ret_val = ERROR_LIST_EMPTY
            else:
                result["failed"] = True
                result["msg"] = method + " failed"
        else:
            result["failed"] = True
            result["msg"] = method + " failed"

        return ERROR_GENERIC, ret_val, None, None

    return 0, 0, None, get_resp,
示例#7
0
def login(fos_ip_addr, fos_user_name, fos_password, is_https, throttle,
          result):
    """
        login to the fos switch at ip address specified

        :param fos_ip_addr: fos switch ip address
        :type fos_ip_addr: str
        :param fos_user_name: login name
        :type fos_user_name: str
        :param fos_password: password
        :type fos_password: password
        :param is_https: indicate to use HTTPS or HTTP
        :type fos_password: Bool
        :param throttle: throttle delay
        :type fos_password: float
        :return: returned header, None if not successfully logged in
        :rtype: dict
    """
    full_login_url = (HTTPS if is_https else HTTP) + fos_ip_addr + REST_LOGIN
    logininfo = fos_user_name + ":" + fos_password
    login_encoded = base64.b64encode(logininfo.encode())

    credential = {
        "Authorization": "Basic " + login_encoded.decode(),
        "User-Agent": "Rest-Conf"
    }
    try:
        login_resp = ansible_urls.open_url(full_login_url,
                                           headers=credential,
                                           method="POST")
    except urllib_error.HTTPError as e:
        result["login_resp_code"] = e.code
        result["login_resp_reason"] = e.reason
        ret_code, root_dict = bsn_xmltodict(result, e.read())
        result["login_resp_data"] = root_dict
        result["failed"] = True
        result["msg"] = "failed to login"
        return -1, None, None

    full_switch_url = (HTTPS if is_https else HTTP) + fos_ip_addr + REST_SWITCH

    auth = {}
    auth["auth"] = login_resp.info()["Authorization"]
    if throttle == None:
        auth["throttle"] = DEFAULT_THROTTLE
    else:
        auth["throttle"] = throttle

    # get fos version from the default switch
    rtype, rdict = url_get_to_dict(fos_ip_addr, is_https, auth, -1, result,
                                   full_switch_url)
    if rtype != 0:
        result["failed"] = True
        result["msg"] = "API failed to return switch firmware version"
        logout(fos_ip_addr, is_https, auth, result)
        return -1, None, None

    time.sleep(auth["throttle"] * 2)

    return 0, auth, rdict["Response"]["fibrechannel-switch"][
        "firmware-version"]