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)
Exemplo n.º 2
0
def main():
    """
    Main function
    """

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

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

    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']
    ssh_hostkeymust = True
    if 'ssh_hostkeymust' in input_params['credential']:
        ssh_hostkeymust = input_params['credential']['ssh_hostkeymust']
    throttle = input_params['throttle']
    vfid = input_params['vfid']
    gather_subset = input_params['gather_subset']
    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)

    facts = {}

    facts['ssh_hostkeymust'] = ssh_hostkeymust

    for area in valid_areas:
        if (gather_subset is None or area in gather_subset
                or "all" in gather_subset):
            if area == "brocade_interface_fibrechannel":
                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)

                interface = {}
                interface["fibrechannel"] = (
                    response["Response"]["fibrechannel"])

                for port in interface["fibrechannel"]:
                    to_human_fc(port)

                ret_code, response = fc_port_stats_get(fos_ip_addr, https,
                                                       auth, vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                interface["fibrechannel-statistics"] = (
                    response["Response"]["fibrechannel-statistics"])

                facts[area] = interface

            if area == "brocade_zoning":
                ret_code, response = defined_get(fos_ip_addr, https, auth,
                                                 vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                zoning = {}
                zoning["defined-configuration"] = (
                    response["Response"]["defined-configuration"])

                ret_code, response = effective_get(fos_ip_addr, https, auth,
                                                   vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                zoning["effective-configuration"] = (
                    response["Response"]["effective-configuration"])

                to_human_zoning(zoning["effective-configuration"])

                facts[area] = zoning

            if area == "brocade_chassis":
                ret_code, response = chassis_get(fos_user_name, fos_password,
                                                 fos_ip_addr, fos_version,
                                                 https, auth, vfid, result,
                                                 ssh_hostkeymust)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                to_human_chassis(response["Response"]["chassis"])

                facts[area] = response["Response"]["chassis"]

            if area == "brocade_fibrechannel_configuration_fabric":
                ret_code, response = fabric_get(fos_user_name, fos_password,
                                                fos_ip_addr, fos_version,
                                                https, auth, vfid, result,
                                                ssh_hostkeymust)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                to_human_fabric(response["Response"]["fabric"])

                facts[area] = response["Response"]["fabric"]

            if area == "brocade_fibrechannel_configuration_port_configuration":
                ret_code, response = port_configuration_get(
                    fos_user_name, fos_password, fos_ip_addr, fos_version,
                    https, auth, vfid, result, ssh_hostkeymust)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                to_human_port_configuration(
                    response["Response"]["port-configuration"])

                facts[area] = response["Response"]["port-configuration"]

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

                to_human_switch(response["Response"]["fibrechannel-switch"])

                facts[area] = response["Response"]["fibrechannel-switch"]

            if area == "brocade_time_clock_server":
                ret_code, response = clock_server_get(fos_ip_addr, https, auth,
                                                      vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                to_human_clock_server(response["Response"]["clock-server"])

                facts[area] = response["Response"]["clock-server"]

            if area == "brocade_time_time_zone":
                ret_code, response = time_zone_get(fos_ip_addr, https, auth,
                                                   vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                to_human_time_zone(response["Response"]["time-zone"])

                facts[area] = response["Response"]["time-zone"]

            if area == "brocade_logging_syslog_server":
                ret_code, response = syslog_server_get(fos_ip_addr, https,
                                                       auth, vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                if isinstance(response["Response"]["syslog-server"], list):
                    servers = response["Response"]["syslog-server"]
                else:
                    servers = [response["Response"]["syslog-server"]]

                for server in servers:
                    to_human_syslog_server(server)

                facts[area] = servers

            if area == "brocade_logging_audit":
                ret_code, response = audit_get(fos_ip_addr, https, auth, vfid,
                                               result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                to_human_audit(response["Response"]["audit"])

                facts[area] = response["Response"]["audit"]

            if area == "brocade_snmp_system":
                ret_code, response = system_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)

                to_human_system(response["Response"]["system"])

                facts[area] = response["Response"]["system"]

            if area == "brocade_security_ipfilter_rule":
                ret_code, response = ipfilter_rule_get(fos_ip_addr, https,
                                                       auth, vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                for rule in response["Response"]["ipfilter-rule"]:
                    to_human_ipfilter_rule(rule)

                facts[area] = response["Response"]["ipfilter-rule"]

            if area == "brocade_security_ipfilter_policy":
                ret_code, response = ipfilter_policy_get(
                    fos_ip_addr, https, auth, vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                for rule in response["Response"]["ipfilter-policy"]:
                    to_human_ipfilter_policy(rule)

                facts[area] = response["Response"]["ipfilter-policy"]

            if area == "brocade_security_user_config":
                ret_code, response = user_config_get(fos_ip_addr, https, auth,
                                                     vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                for rule in response["Response"]["user-config"]:
                    to_human_user_config(rule)

                facts[area] = response["Response"]["user-config"]

    result["ansible_facts"] = facts

    logout(fos_ip_addr, https, auth, result)
    module.exit_json(**result)
Exemplo n.º 3
0
def to_human_list(module_name, list_name, attributes_list, result):
    for attributes in attributes_list:
        yang_to_human(attributes)

        for k, v in attributes.items():
            if v == "true":
                attributes[k] = True
            elif v == "false":
                attributes[k] = False

        if module_name == "brocade_interface" and list_name == "fibrechannel":
            to_human_fc(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.b64decode(
                        pword)
            if "privacy_password" in attributes:
                pword = attributes["privacy_password"]
                if str(pword) != "None":
                    attributes["privacy_password"] = base64.b64decode(pword)

        if module_name == "brocade_security" and list_name == "user_config":
            if "virtual_fabric_role_id_list" in attributes and "role_id" in attributes[
                    "virtual_fabric_role_id_list"]:
                if not isinstance(
                        attributes["virtual_fabric_role_id_list"]["role_id"],
                        list):
                    new_list = []
                    new_list.append(
                        attributes["virtual_fabric_role_id_list"]["role_id"])
                    attributes["virtual_fabric_role_id_list"][
                        "role_id"] = new_list

        if module_name == "brocade_fibrechannel_switch" and list_name == "fibrechannel_switch":

            to_human_switch(attributes)

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

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

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

        if module_name == "brocade_access_gateway" and list_name == "port_group":
            if "port_group_n_ports" in attributes:
                if attributes[
                        "port_group_n_ports"] is not None and "n_port" in attributes[
                            "port_group_n_ports"]:
                    if not isinstance(
                            attributes["port_group_n_ports"]["n_port"], list):
                        new_list = []
                        new_list.append(
                            attributes["port_group_n_ports"]["n_port"])
                        attributes["port_group_n_ports"]["n_port"] = new_list

            if "port_group_f_ports" in attributes:
                if attributes[
                        "port_group_f_ports"] is not None and "f_port" in attributes[
                            "port_group_f_ports"]:
                    if not isinstance(
                            attributes["port_group_f_ports"]["f_port"], list):
                        new_list = []
                        new_list.append(
                            attributes["port_group_f_ports"]["f_port"])
                        attributes["port_group_f_ports"]["f_port"] = new_list

        if module_name == "brocade_access_gateway" and list_name == "n_port_map":
            if "configured_f_port_list" in attributes:
                if attributes[
                        "configured_f_port_list"] is not None and "f_port" in attributes[
                            "configured_f_port_list"]:
                    if not isinstance(
                            attributes["configured_f_port_list"]["f_port"],
                            list):
                        new_list = []
                        new_list.append(
                            attributes["configured_f_port_list"]["f_port"])
                        attributes["configured_f_port_list"][
                            "f_port"] = new_list