a
    I_                     @   sp   d Z ddlZddlmZmZ G dd deZG dd deZG dd	 d	eZG d
d deZdd Z	dd Z
dS )zManipulating sites.    N)FLAG_MOD_ADDLdbErrorc                   @   s    e Zd ZdZdd Zdd ZdS )SiteExceptionzBase element for Sites errorsc                 C   s
   || _ d S )N)value)selfr    r   -/usr/lib/python3/dist-packages/samba/sites.py__init__   s    zSiteException.__init__c                 C   s   d| j j| jf S )Nz%s: %s)	__class____name__r   )r   r   r   r   __str__   s    zSiteException.__str__N)r   
__module____qualname____doc__r	   r   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )SiteNotFoundExceptionz>Raised when the site is not found and it's expected to exists.Nr   r   r   r   r   r   r   r   r   "   s   r   c                   @   s   e Zd ZdZdS )SiteAlreadyExistsExceptionzBRaised when the site is not found and it's expected not to exists.Nr   r   r   r   r   r   &   s   r   c                   @   s   e Zd ZdZdS )SiteServerNotEmptyExceptionz0Raised when the site still has servers attached.Nr   r   r   r   r   r   *   s   r   c                 C   s   | j |tjd| d}t|dkr.td| t }t| d|t|f |_t	dt
d|d< | | t }t| dt|j |_t	d	t
d|d< | | t }t| d
t|j |_t	dt
d|d< | | dS )a  
    Create a site

    :param samdb: A samdb connection
    :param configDn: The DN of the configuration partition
    :param siteName: Name of the site to create
    :return: True upon success
    :raise SiteAlreadyExists: if the site to be created already exists.
    z(&(objectclass=Site)(cn=%s))baseZscopeZ
expressionr   z&A site with the name %s already existszCn=%s,CN=Sites,%sZsiteZobjectclasszCn=NTDS Site Settings,%sZnTDSSiteSettingszCn=Servers,%sZserversContainerT)searchldbZSCOPE_SUBTREElenr   ZMessageDnstrZdnZMessageElementr   add)samdbconfigDnsiteNameretmZm2Zm3r   r   r   create_site.   s$    



r!   c           	   
   C   s  t | d}||dkr"td|ddkr8td|dd| t | d}|| z.| j|t jd	d
}t|dkrt	d| W nD t
y } z,|j\}}|t jkrt	d| W Y d}~n
d}~0 0 | j|t jdd
}t|dkrtd| | |dg dS )aV  
    Delete a site

    :param samdb: A samdb connection
    :param configDn: The DN of the configuration partition
    :param siteName: Name of the site to delete
    :return: True upon success
    :raise SiteNotFoundException: if the site to be deleted do not exists.
    :raise SiteServerNotEmpty: if the site has still servers in it.
    zCN=SitesFzdnsites.add_base() failedzCN=Xzdnsites.add_child() failedr   ZCNz
CN=ServerszobjectClass=siter      zSite %s does not existNz(objectclass=server)z9Site %s still has servers in it, move them before removalztree_delete:0T)r   r   Zadd_baser   Z	add_childZset_componentr   Z
SCOPE_BASEr   r   r   argsZERR_NO_SUCH_OBJECTZSCOPE_ONELEVELr   delete)	r   r   r   ZdnsiteZ	dnserversr   eenumZestrr   r   r   delete_siteS   s2    



"
r'   )r   r   r   r   	Exceptionr   r   r   r   r!   r'   r   r   r   r   <module>   s   
%