
    Vh!                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZ d dlmZmZmZmZmZmZ  G d d	e      Z G d
 de      Z G d de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: iosxr_banner
author:
- Trishna Guha (@trishnaguha)
- Kedar Kekan (@kedarX)
short_description: Module to configure multiline banners.
description:
- This module will configure both exec and motd banners on remote device running Cisco
  IOS XR. It allows playbooks to add or remove banner text from the running configuration.
version_added: 1.0.0
requirements:
- ncclient >= 0.5.3 when using netconf
- lxml >= 4.1.1 when using netconf
extends_documentation_fragment:
- cisco.iosxr.iosxr
notes:
- This module works with connection C(network_cli) and C(netconf). See L(the IOS-XR
  Platform Options,../network/user_guide/platform_iosxr.html).
options:
  banner:
    description:
    - Specifies the type of banner to configure on remote device.
    required: true
    type: str
    choices:
    - login
    - motd
  text:
    description:
    - Banner text to be configured. Accepts multi line string, without empty lines.
      When using a multi line string, the first and last characters must be the
      start and end delimiters for the banner
      Requires I(state=present).
    type: str
  state:
    description:
    - Existential state of the configuration on the device.
    default: present
    type: str
    choices:
    - present
    - absent
a  
- name: configure the login banner
  cisco.iosxr.iosxr_banner:
    banner: login
    text: |
      @this is my login banner
      that contains a multiline
      string@
    state: present
- name: remove the motd banner
  cisco.iosxr.iosxr_banner:
    banner: motd
    state: absent
- name: Configure banner from file
  cisco.iosxr.iosxr_banner:
    banner: motd
    text: '{{ lookup(''file'', ''./config_partial/raw_banner.cfg'') }}'
    state: present
a5  
commands:
  description: The list of configuration mode commands sent to device with transport C(cli)
  returned: always (empty list when no commands to send)
  type: list
  sample:
    - banner login
    - "@this is my login banner"
    - that contains a multiline
    - string@

xml:
  description: NetConf rpc xml sent to device with transport C(netconf)
  returned: always (empty list when no xml rpc to send)
  type: list
  sample:
    - '<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
            <banners xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-infra-infra-cfg">
                <banner xc:operation="merge">
                  <banner-name>motd</banner-name>
                  <banner-text>Ansible banner example</banner-text>
                </banner>
            </banners>
        </config>'
N)AnsibleModule)	build_xml
etree_find
get_config
is_cliconf
is_netconfload_configc                       e Zd Zd Zd Zy)
ConfigBasec                 B    || _         dg d| _        i | _        i | _        y )NF)changedwarnings)_module_result_want_have)selfmodules     l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/iosxr/plugins/modules/iosxr_banner.py__init__zConfigBase.__init__w   s#    #(b9

    c                 $   | j                   j                  d   }|r(dj                  t        |      j	                               }| j
                  j                  | j                   j                  d   || j                   j                  d   d       y )Ntextz{0}bannerstater   r   r   )r   paramsformatstrstripr   update)r   r   s     r   map_params_to_objzConfigBase.map_params_to_obj}   sp    ||""6*<<D	 12D

,,--h7,,W5	
r   N)__name__
__module____qualname__r   r%    r   r   r   r   v   s    

r   r   c                   0     e Zd Z fdZd Zd Zd Z xZS )CliConfigurationc                 ,    t         t        |   |       y N)superr+   r   r   r   	__class__s     r   r   zCliConfiguration.__init__   s    .v6r   c                    t               }| j                  j                  d   }|dk(  r| j                  j	                  d      dk7  rd| j                  j                         v r| j                  d   r|j                  dj                  | j                  j                  d                n|dk(  r| j                  d   r| j                  d   j                         j                  d      | j                  j	                  d      k7  rYdj                  | j                  j                  d         }|| j                  d   j                         z  }|j                  |       || j                  d	<   |rZ| j                  j                   }t        | j                  ||
      }|rt        |      | j                  d<   d| j                  d<   y y )Nr   absentr   zno banner {0!s}r   presentunicode_escapezbanner {0!s} commands)commitprepareddiffTr   )listr   r    r   getkeysappendr!   r   encodedecoder#   r   
check_moder   dict)r   r5   r   
banner_cmdr6   r9   s         r   map_obj_to_commandsz$CliConfiguration.map_obj_to_commands   sq   6##G,Hzz~~g&(2$**//++

60B 1 8 89L9LX9V WXizz&!djj&8&?&?&A&H&H '''( -33DLL4G4G4QR
djj06688

+#+Z 000Ft||XfED'+T':V$&*DLL# r   c                 ~   dj                  | j                  j                  d         }t        | j                  |      }t	        j
                  d|t        j                        }|r|j                  d      }nd }| j                  j                  d   dd}|r
||d<   d	|d
<   | j                  j                  |       y )Nzbanner {0!s}r   )config_filterzbanner (\S+) (.*)   r2   )r   r   r   r3   r   )
r!   r   r    r	   researchDOTALLgroupr   r$   )r   
cli_filteroutputmatchr   objs         r   map_config_to_objz"CliConfiguration.map_config_to_obj   s    #**4<<+>+>x+HI
DLL
C		.		B;;q>DD,,X6JCK$CL

#r   c                 z    | j                          | j                          | j                          | j                  S r-   )r%   rO   rC   r   r   s    r   runzCliConfiguration.run   s0        "||r   )r&   r'   r(   r   rC   rO   rR   __classcell__r0   s   @r   r+   r+      s    7+.r   r+   c                   *     e Zd Z fdZd Zd Z xZS )NCConfigurationc                     t         t        |   |       t        j                         | _        | j
                  j                  dddddfdddifd	d
ddfg       y )Nr   zbanners/bannerT	operation)xpathtagattribza:bannerrY   zbanner/banner-nameza:textzbanner/banner-textedit)rY   rX   )r.   rV   r   collectionsOrderedDict_banners_metar$   r/   s     r   r   zNCConfiguration.__init__   sh    ot-f5(446!!%5dkZ[g';<=%9OP	
r   c                 .   | j                   j                  d   }t        d| j                  | j                   j                  d      }t	        | j                   d|      }d }d }t        |d      ,t        |d      j                  }t        |d      j                  }d }|d	k(  r*|| j                   j                  d
   k(  rt        |      rd}n|dk(  rd}g | j                  d<   |rt        d| j                  | j                   j                  |      }|| j                   j                   }t        | j                   ||||      }	|	rN|| j                  d<   | j                   j                  rt        |	      | j                  d<   d| j                  d<   y y y y )Nr   bannersfilter)xmapr    opcoderunning)sourcerE   zbanner-textzbanner-namer2   r   deleter3   mergexml)r6   re   nc_get_filterr7   r9   Tr   )r   r    r   r_   r	   r   r   lenr   r@   r   _diffrA   )
r   r   _get_filterre   banner_namebanner_textrd   _edit_filterr6   r9   s
             r   map_obj_to_xml_rpcz"NCConfiguration.map_obj_to_xml_rpc   s   ##G,##<<&&	
 T\\);Wg}-9$Wm<AAK$Wm<AAKH0C0CH0M!MRUVaRbFiF U$''||**	L '!\\444"LL !#"- *6DLL'||))/3T/BV,.2DLL+  ( r   c                 Z    | j                          | j                          | j                  S r-   )r%   rq   r   rQ   s    r   rR   zNCConfiguration.run   s$     !||r   )r&   r'   r(   r   rq   rR   rS   rT   s   @r   rV   rV      s    	
/3br   rV   c            	      0   t        t        dddg      t               t        dddg            } d	g}t        | |d
      }d}t        |      rt        |      }nt	        |      rt        |      }d}||j                         } |j                  di | y)z%main entry point for module executionTloginmotd)requiredchoicesr3   r2   )defaultrw   r   )r   r3   )r   )argument_specrequired_ifsupports_check_modeNr)   )rA   r   r
   r+   r   rV   rR   	exit_json)ry   rz   r   config_objectresults        r   mainr      s    TGV+<=V9y(.CDM 33K# F M&(0	F	'/F ""$Fvr   __main__)
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESRETURNr]   rG   ansible.module_utils.basicr   Hansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.iosxrr   r   r	   r
   r   r   objectr   r+   rV   r   r&   r)   r   r   <module>r      s    A @ *X(
4  	 4 
 
(.z .bAj AH: zF r   