HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/states/__pycache__/netntp.cpython-310.pyc
o

�N�g�1�@s�dZddlZddlZzddlmZddlmZdZWne	y%dZYnwzddl
ZdZWne	y9dZYnwdZ
e�e�Zdd	�Zd
d�Zdd
�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd#dd �Zd$d!d"�ZdS)%aJ
Network NTP
===========

.. versionadded:: 2016.11.0

Manage the configuration of NTP peers and servers on the network devices through the NAPALM proxy.

:codeauthor: Mircea Ulinic <ping@mirceaulinic.net> & Jerome Fleury <jf@cloudflare.com>
:maturity:   new
:depends:    napalm
:platform:   unix

Dependencies
------------
- Requires netaddr_ to be installed: `pip install netaddr` to check if IP
  Addresses are correctly specified
- Requires dnspython_ to be installed: `pip install dnspython` to resolve the
  nameserver entities (in case the user does not configure the peers/servers
  using their IP addresses)
- :mod:`NAPALM proxy minion <salt.proxy.napalm>`
- :mod:`NTP operational and configuration management module <salt.modules.napalm_ntp>`

.. _netaddr: https://pythonhosted.org/netaddr/
.. _dnspython: http://www.dnspython.org/
�N)�	IPAddress)�AddrFormatErrorTFZnetntpcCstjj�ttt�S)z_
    NAPALM library must be installed for this module to work and run in a (proxy) minion.
    )�salt�utilsZnapalmZvirtual�__opts__�__virtualname__�__file__�r	r	�F/opt/saltstack/salt/lib/python3.10/site-packages/salt/states/netntp.py�__virtual__@srcCs|iddd�}|S)NF�)�name�changes�result�commentr	)r
�retr	r	r
�_default_retLsrcC�
td�S)zRetrieves configured NTP peersz	ntp.peers��__salt__r	r	r	r
�_retrieve_ntp_peersR�
rcCr)z Retrieves configured NTP serverszntp.serversrr	r	r	r
�_retrieve_ntp_serversXrrcCs�t|t�sdS|D]
}t|t�sdSq	tsdSg}|D]?}z|�tt|���Wqty[ts4Yqg}ztj	�
|�}Wntj	jyLYYdSw|D]	}|�t|��qOYqw|}dS)z_Checks whether the input is a valid list of peers and transforms domain names into IP AddressesFT)�
isinstance�list�str�HAS_NETADDR�appendrr�HAS_DNSRESOLVER�dnsZresolver�queryZNoAnswer)�peersZpeerZ
ip_only_peersZ	dns_replyZdns_ipr	r	r
�_check^s8

��
���r"cCsdd�|D�S)NcSsg|]}|r|�qSr	r	)�.0�elemr	r	r
�
<listcomp>�sz_clean.<locals>.<listcomp>r	)�lstr	r	r
�_clean�sr'cC�td|ddi�S)zCalls ntp.set_peers.z
ntp.set_peers�commitFr�r!r	r	r
�_set_ntp_peers��r+cCr()zCalls ntp.set_servers.zntp.set_serversr)Fr��serversr	r	r
�_set_ntp_servers�r,r/cCr()zCalls ntp.delete_peers.zntp.delete_peersr)Frr*r	r	r
�_delete_ntp_peers�r,r0cCr()zCalls ntp.delete_servers.zntp.delete_serversr)Frr-r	r	r
�_delete_ntp_servers�r,r1cGs&|tt����vrt��|�|�SdS)N)r�globals�keys�get)r
Zkargsr	r	r
�	_exec_fun�sr5r!cCs�t|�}ddg}||vr|Sd|��}t|�}|�dd�dur-dj||�d�d�|d<|St|�d	i��}t|�}||krK|�d
|�d�dd
��|St||�}	t||�}
t|	�}	t|
�}
i}|	rg|	|d<|
rm|
|d<|�d|i�tddur�|�ddd��|Sd}d}
d}|	r�d|��}t||	�}|�d�r�d}nd}
|dj||�d�d�7}|
r�d|��}t||
�}|�d�r�d}nd}
|dj||�d�d�7}|�|
||d��|S)Nr!r.Z_retrieve_ntp_rFz4Cannot retrieve NTP {what} from the device: {reason}r)Zwhat�reason�outzNTP z already configured as needed.T)rrZaddedZremovedr�testz,Testing mode: configuration was not changed!�rrrZ	_set_ntp_zCannot set NTP {what}: {reason}Z_delete_ntp_z"Cannot remove NTP {what}: {reason})�successfully_changed�expected_config_changer)	rr5r4�format�set�updaterr'r)Zfun_nameZ
peers_serversr
Z_retZ_optionsZ
_retrieve_funZntp_list_outputZconfigured_ntp_listZdesired_ntp_listZlist_to_setZlist_to_deleterr;r:rZ_set_funZ_setZ_delete_funZ_removedr	r	r
�_check_diff_and_configure�s�
�

�
���



�



���r?cCs�t|�}|�dd�}|�dd�}|�di�}t|t�s"t|t�s"|St|t�r1t|�s1d|d<|St|t�r@t|�s@d|d<|Sd}d}t|t�rzt||d	d
�}	|	�dd�}|	�dd�}|oc|	�dd�}|d
|	�dd�7}|	�di�}
|
rz|
|d	<t|t�r�t||dd
�}|p�|�dd�}|o�|�dd�}|o�|�dd�}|d
|�dd�7}|�di�}|r�||d<|�d|i�|s�|s�|�ddd��|Stddur�|�ddd��|S|r�td�\}
}|
o�|}||7}|�||d��|S)a�
    Manages the configuration of NTP peers and servers on the device, as specified in the state SLS file.
    NTP entities not specified in these lists will be removed whilst entities not configured on the device will be set.

    SLS Example:

    .. code-block:: yaml

        netntp_example:
            netntp.managed:
                 - peers:
                    - 192.168.0.1
                    - 172.17.17.1
                 - servers:
                    - 24.124.0.251
                    - 138.236.128.36

    Output example:

    .. code-block:: python

        {
            'edge01.nrt04': {
                'netntp_|-netntp_example_|-netntp_example_|-managed': {
                    'comment': 'NTP servers already configured as needed.',
                    'name': 'netntp_example',
                    'start_time': '12:45:24.056659',
                    'duration': 2938.857,
                    'changes': {
                        'peers': {
                            'removed': [
                                '192.168.0.2',
                                '192.168.0.3'
                            ],
                            'added': [
                                '192.168.0.1',
                                '172.17.17.1'
                            ]
                        }
                    },
                    'result': None
                }
            }
        }
    rFrrrz>NTP peers must be a list of valid IP Addresses or Domain Namesz@NTP servers must be a list of valid IP Addresses or Domain NamesTr!)r
r;r:�
r.zDevice configured properly.r9r8NzBThis is in testing mode, the device configuration was not changed!znet.config_control)	rr4rrr"r?r>rr)r
r!r.rrrrr:r;Z
_peers_retZ_changed_peersZ_servers_retZ_changed_serversZ
config_resultZconfig_commentr	r	r
�managedst/����

����rAr*)NN)�__doc__�loggingZsalt.utils.napalmrZnetaddrrZnetaddr.corerr�ImportErrorZdns.resolverrrr�	getLogger�__name__�logrrrrr"r'r+r/r0r1r5r?rAr	r	r	r
�<module>s>��
)
c