예제 #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'),
                         ports=dict(required=True, type='list'))

    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']
    ports = input_params['ports']
    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_port_get(fos_ip_addr, https, auth, vfid, result)
    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module)

    resp_ports = response["Response"]["fibrechannel"]
    if isinstance(resp_ports, list):
        current_ports = resp_ports
    else:
        current_ports = [resp_ports]

    diff_ports = []
    for port in ports:
        for current_port in current_ports:
            if port["name"] == current_port["name"]:
                to_human_fc(current_port)
                diff_attributes = generate_diff(result, current_port, port)
                if len(diff_attributes) > 0:
                    result["current_port"] = current_port
                    diff_attributes["name"] = port["name"]
                    ret_code = to_fos_fc(diff_attributes, result)
                    if ret_code != 0:
                        exit_after_login(fos_ip_addr, https, auth, result,
                                         module)
                    diff_ports.append(diff_attributes)

    result["diff_ports"] = diff_ports

    if len(diff_ports) > 0:
        if not module.check_mode:
            ret_code = fc_port_patch(fos_ip_addr, https, auth, vfid, result,
                                     diff_ports)
            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)