Ejemplo n.º 1
0
def list_patch(login, password, fos_ip_addr, module_name, list_name,
               fos_version, is_https, auth, vfid, result, entries,
               ssh_hostkeymust, timeout):
    """
        update existing user config configurations

        :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 struct: dict
        :param result: dict to keep track of execution msgs
        :type result: dict
        :param diff_attributes: list of attributes for update
        :type ports: dict
        :return: code to indicate failure or success
        :rtype: int
        :return: list of dict of chassis configurations
        :rtype: list
    """
    if module_name == "brocade_fibrechannel_switch" and list_name == "fibrechannel_switch":
        return fc_switch_patch(login, password, fos_ip_addr, fos_version,
                               is_https, auth, vfid, result, entries[0],
                               ssh_hostkeymust, timeout)
    if module_name == "brocade_interface" and list_name == "fibrechannel":
        return fc_port_patch(fos_ip_addr, is_https, auth, vfid, result,
                             entries, timeout)
    if module_name == "brocade_security" and list_name == "user_config":
        return user_config_patch(login, password, fos_ip_addr, fos_version,
                                 is_https, auth, vfid, result, entries,
                                 ssh_hostkeymust, timeout)

    if module_name == "brocade_snmp" and list_name == "v1_trap":
        new_entries = v1_trap_patch(login, password, fos_ip_addr, fos_version,
                                    is_https, auth, vfid, result, entries,
                                    ssh_hostkeymust, timeout)

        if len(new_entries) == 0:
            return 0

        entries = new_entries

    if module_name == "brocade_snmp" and list_name == "v3_trap":
        new_entries = v3_trap_patch(login, password, fos_ip_addr, fos_version,
                                    is_https, auth, vfid, result, entries,
                                    ssh_hostkeymust, timeout)

        if len(new_entries) == 0:
            return 0

        entries = new_entries

    full_url, validate_certs = full_url_get(
        is_https, fos_ip_addr, REST_PREFIX + module_name + "/" + list_name)

    xml_str = list_xml_str(result, module_name, list_name, entries)

    result["patch_str"] = xml_str

    # AG always expect nport and fports to be removed from another
    # none default port group before being added. So, we go through
    # an port group that has nport or fport list being updated,
    # clean them out first, then do the normal patch to update to the
    # final list
    if module_name == "brocade_access_gateway" and list_name == "port_group":
        empty_port_groups = []
        for port_group in entries:
            if "port-group-n-ports" in port_group and port_group[
                    "port-group-n-ports"] is not None and "n-port" in port_group[
                        "port-group-n-ports"] and port_group[
                            "port-group-n-ports"]["n-port"] is not None:
                empty_port_groups.append({
                    "port-group-id":
                    port_group["port-group-id"],
                    "port-group-n-ports": {
                        "n-port": None
                    }
                })
            if "port-group-f-ports" in port_group and port_group[
                    "port-group-f-ports"] is not None and "f-port" in port_group[
                        "port-group-f-ports"] and port_group[
                            "port-group-f-ports"]["f-port"] is not None:
                empty_port_groups.append({
                    "port-group-id":
                    port_group["port-group-id"],
                    "port-group-f-ports": {
                        "f-port": None
                    }
                })
        if len(empty_port_groups) > 0:
            empty_xml_str = list_xml_str(result, module_name, list_name,
                                         empty_port_groups)

            result["patch_str_empty_ag"] = empty_xml_str
            empty_patch_result = url_patch(fos_ip_addr, is_https, auth, vfid,
                                           result, full_url, empty_xml_str,
                                           timeout)
            result["patch_str_empty_ag_result"] = empty_patch_result

    # AG always expect fports to be removed from another nport before
    # being added. to another nport So, we go through
    # an nport map that has fport list being updated,
    # clean them out first, then do the normal patch to update to the
    # final list
    if module_name == "brocade_access_gateway" and list_name == "n_port_map":
        empty_n_port_maps = []
        for n_port_map in entries:
            if "configured-f-port-list" in n_port_map and "f-port" in n_port_map[
                    "configured-f-port-list"] and n_port_map[
                        "configured-f-port-list"]["f-port"] is not None:
                empty_n_port_maps.append({
                    "n-port": n_port_map["n-port"],
                    "configured-f-port-list": {
                        "f-port": None
                    }
                })
        if len(empty_n_port_maps) > 0:
            empty_xml_str = list_xml_str(result, module_name, list_name,
                                         empty_n_port_maps)

            result["patch_str_empty_ag"] = empty_xml_str
            url_patch(fos_ip_addr, is_https, auth, vfid, result, full_url,
                      empty_xml_str, timeout)

    return (url_patch(fos_ip_addr, is_https, auth, vfid, result, full_url,
                      xml_str, timeout))
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)