示例#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'),
                         user_configs=dict(required=False, type='list'),
                         delete_user_configs=dict(required=False, 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']
    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']
    user_configs = input_params['user_configs']
    delete_user_configs = input_params['delete_user_configs']
    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 = user_config_get(fos_ip_addr, https, auth, vfid,
                                         result)
    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module)

    resp_uc = response["Response"]["user-config"]

    if isinstance(resp_uc, list):
        c_user_configs = resp_uc
    else:
        c_user_configs = [resp_uc]

    for c_user_config in c_user_configs:
        if "virtual-fabric-role-id-list" in c_user_config and not isinstance(
                c_user_config["virtual-fabric-role-id-list"], list):
            c_user_config["virtual-fabric-role-id-list"] = [
                c_user_config["virtual-fabric-role-id-list"]
            ]

    # convert REST to human readable format first
    for c_user_config in c_user_configs:
        to_human_user_config(c_user_config)

    # if delete user config is not None, then we make sure
    # the user config is not present.
    # user config creation or update does not happen at the same
    # time
    if delete_user_configs != None:
        to_delete = []
        for delete_user_config in delete_user_configs:
            found = False
            for c_user_config in c_user_configs:
                if c_user_config["name"] == delete_user_config["name"]:
                    found = True
                    break
            if found:
                to_delete.append(delete_user_config)

        if len(to_delete) > 0:
            if not module.check_mode:
                ret_code = user_config_delete(fos_ip_addr, https, auth, vfid,
                                              result, to_delete)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

            result["changed"] = True

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

    diff_user_configs = []
    for new_uc in user_configs:
        for c_user_config in c_user_configs:
            if new_uc["name"] == c_user_config["name"]:
                diff_attributes = generate_diff(result, c_user_config, new_uc)
                # cannot change password using patch
                # so skip for diff identification
                if "password" in diff_attributes:
                    diff_attributes.pop("password")

                if len(diff_attributes) > 0:
                    result["c_user_config"] = c_user_config
                    diff_attributes["name"] = new_uc["name"]
                    ret_code = to_fos_user_config(diff_attributes, result)
                    if ret_code != 0:
                        exit_after_login(fos_ip_addr, https, auth, result,
                                         module)

                    diff_user_configs.append(diff_attributes)

    add_user_configs = []
    for new_uc in user_configs:
        found = False
        for c_user_config in c_user_configs:
            if new_uc["name"] == c_user_config["name"]:
                found = True
        if not found:
            new_user_config = {}
            for k, v in new_uc.items():
                new_user_config[k] = v
            ret_code = to_fos_user_config(new_user_config, result)
            result["retcode"] = ret_code
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

            add_user_configs.append(new_user_config)

    result["resp_uc"] = resp_uc
    result["user_configs"] = user_configs
    result["diff_user_configs"] = diff_user_configs
    result["add_user_configs"] = add_user_configs

    if len(diff_user_configs) > 0:
        if not module.check_mode:
            ret_code = user_config_patch(fos_user_name, fos_password,
                                         fos_ip_addr, fos_version, https, auth,
                                         vfid, result, diff_user_configs,
                                         ssh_hostkeymust)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        result["changed"] = True

    if len(add_user_configs) > 0:
        if not module.check_mode:
            ret_code = user_config_post(fos_ip_addr, https, auth, vfid, result,
                                        add_user_configs)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        result["changed"] = True

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