Пример #1
0
def to_fos_list(module_name, list_name, attributes_list, result):
    for attributes in attributes_list:
        human_to_yang(attributes)

        if module_name == "brocade_snmp" and list_name == "v3_account":
            if "authentication-password" in attributes:
                pword = attributes["authentication-password"]
                if str(pword) != "None":
                    attributes["authentication-password"] = base64.b64encode(
                        pword.encode('ascii')).decode('utf-8')
            if "privacy-password" in attributes:
                pword = attributes["privacy-password"]
                if str(pword) != "None":
                    attributes["privacy-password"] = base64.b64encode(
                        pword.encode('ascii')).decode('utf-8')

        if module_name == "brocade_interface" and list_name == "fibrechannel":
            to_fos_fc(attributes, result)

        if module_name == "brocade_fibrechannel_switch" and list_name == "fibrechannel_switch":
            to_fos_switch(attributes, result)

        if module_name == "brocade_security" and list_name == "user_config":
            if "password" in attributes:
                pword = attributes["password"]
                if str(pword) != "None":
                    attributes["password"] = base64.b64encode(
                        pword.encode('ascii')).decode('utf-8')

        for k, v in attributes.items():
            if isinstance(v, bool):
                if v == True:
                    attributes[k] = "true"
                else:
                    attributes[k] = "false"

    return 0
def main():
    """
    Main function
    """

    argument_spec = dict(credential=dict(required=True, type='dict'),
                         vfid=dict(required=False, type='int'),
                         throttle=dict(required=False, type='float'),
                         switch=dict(required=True, type='dict'))

    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    input_params = module.params

    # Set up state variables
    fos_ip_addr = input_params['credential']['fos_ip_addr']
    fos_user_name = input_params['credential']['fos_user_name']
    fos_password = input_params['credential']['fos_password']
    https = input_params['credential']['https']
    throttle = input_params['throttle']
    vfid = input_params['vfid']
    switch = input_params['switch']
    result = {"changed": False}

    if vfid is None:
        vfid = 128

    ret_code, auth, fos_version = login(fos_ip_addr, fos_user_name,
                                        fos_password, https, throttle, result)
    if ret_code != 0:
        module.exit_json(**result)

    ret_code, response = fc_switch_get(fos_user_name, fos_password,
                                       fos_ip_addr, fos_version, https, auth,
                                       vfid, result)
    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module)

    resp_switch = response["Response"]["fibrechannel-switch"]

    to_human_switch(resp_switch)

    if "dns_servers" in resp_switch:
        if resp_switch[
                "dns_servers"] is not None and "dns_server" in resp_switch[
                    "dns_servers"]:
            if not isinstance(resp_switch["dns_servers"]["dns_server"], list):
                new_list = []
                new_list.append(resp_switch["dns_servers"]["dns_server"])
                resp_switch["dns_servers"]["dns_server"] = new_list

    if "ip_address" in resp_switch:
        if resp_switch[
                "ip_address"] is not None and "ip_address" in resp_switch[
                    "ip_address"]:
            if not isinstance(resp_switch["ip_address"]["ip_address"], list):
                new_list = []
                new_list.append(resp_switch["ip_address"]["ip_address"])
                resp_switch["ip_address"]["ip_address"] = new_list

    if "ip_static_gateway_list" in resp_switch:
        if resp_switch[
                "ip_static_gateway_list"] is not None and "ip_static_gateway" in resp_switch[
                    "ip_static_gateway_list"]:
            if not isinstance(
                    resp_switch["ip_static_gateway_list"]["ip_static_gateway"],
                    list):
                new_list = []
                new_list.append(
                    resp_switch["ip_static_gateway_list"]["ip_static_gateway"])
                resp_switch["ip_static_gateway_list"][
                    "ip_static_gateway"] = new_list

    diff_attributes = generate_diff(result, resp_switch, switch)

    result["diff_attributes"] = diff_attributes
    result["resp_switch"] = resp_switch
    result["switch"] = switch

    if len(diff_attributes) > 0:
        # let's add name key to it
        diff_attributes["name"] = resp_switch["name"]
        ret_code = to_fos_switch(diff_attributes, result)
        if ret_code != 0:
            exit_after_login(fos_ip_addr, https, auth, result, module)

        if not module.check_mode:
            ret_code = fc_switch_patch(fos_user_name, fos_password,
                                       fos_ip_addr, fos_version, https, auth,
                                       vfid, result, diff_attributes)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        result["changed"] = True
    else:
        logout(fos_ip_addr, https, auth, result)
        module.exit_json(**result)

    logout(fos_ip_addr, https, auth, result)
    module.exit_json(**result)