U
    (f                     @   s(  d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
mZ d dlmZ d dlmZmZ d dlmZ d dlmZ d dlmZ d d	lmZmZmZmZmZ d d
lmZmZm Z  d dl!m"Z" G dd deeZ#e#dddZ$ee#dddZ%ee#dddZ&edde%ddZ'ddde#ej(dfgddd Z)dS )!    )List)daemonentitlements
exceptionslockmessagestimerutil)ProgressWrapper)APIEndpoint)AdditionalInfoErrorWarningObject)_reboot_required)_is_attached)UAConfig)BoolDataValue
DataObjectFieldStringDataValue	data_list)machine_tokennoticesstate_files)update_motd_messagesc                   @   s@   e Zd ZedeeddededdgZee	 e
dddZd	S )
DetachResultdisabledz1The services disabled during the detach operation)docreboot_requiredz?True if the system requires a reboot after the detach operationr   r   c                 C   s   || _ || _d S Nr   )selfr   r    r!   >/usr/lib/python3/dist-packages/uaclient/api/u/pro/detach/v1.py__init__0   s    zDetachResult.__init__N)__name__
__module____qualname__r   r   r   r   Zfieldsr   strboolr#   r!   r!   r!   r"   r      s   r   )returnc                   C   s
   t t S r   )_detachr   r!   r!   r!   r"   detach5   s    r+   )cfgr)   c              
   C   sf   t  stjz$tjdd t| }W 5 Q R X W n. tk
r` } zt  |W 5 d}~X Y nX |S )zZ
    This endpoint allows the user to detach the machine from a Pro
    subscription.
    zpro.api.u.pro.detach.v1)Zlock_holderN)	r	   Zwe_are_currently_rootr   NonRootUserErrorr   Z	RetryLock_detach_in_lock	ExceptionZclear_lock_file_if_present)r,   reter!   r!   r"   r*   9   s    r*   c              	   C   s8  t | jstg ddS g }g }t| D ]}ztj| |d}W n tjk
rX   Y q(Y nX |jdd\}}|r(|	t
 \}}|s|r|jr|jj}	|jj}
ntjj|d}	d}
|t|	|
d|id	 q(|| q(ttjj t  t   t|  t  t   t!| }tt"||j#d
kd}||_$|S )NFr   )r,   nameT)Zignore_dependent_services)title service)r3   codemetaZyes)%r   Zis_attachedr   r   Zentitlements_disable_orderZentitlement_factoryr   ZEntitlementNotFoundErrorcan_disabledisabler
   messagemsgr2   r   ZDISABLE_FAILED_TMPLformatappendr   r   removeZNoticeZLIMITED_TO_RELEASEr   Zdelete_state_filesr   Zget_machine_token_filedeleter   r   startr   stopr   sortedr   warnings)r,   r   rC   Zent_nameZentr8   _r0   reasonr;   r6   Zreboot_required_resultresultr!   r!   r"   r.   L   sT    



r.   Zv1ZDetachN)versionr2   fnZoptions_clsZ32TzD
from uaclient.api.u.pro.detach.v1 import detach

result = detach()
z1Raised if a non-root user executes this endpoint.zpro api u.pro.detach.v1zL
{
    "disabled": ["service1", "service2"],
    "reboot_required": false
}
)Zintroduced_inZrequires_networkZexample_pythonZresult_classr   Zexample_cliZexample_json)*typingr   Zuaclientr   r   r   r   r   r   r	   Zuaclient.apir
   Zuaclient.api.apir   Zuaclient.api.data_typesr   r   Z5uaclient.api.u.pro.security.status.reboot_required.v1r   Z(uaclient.api.u.pro.status.is_attached.v1r   Zuaclient.configr   Zuaclient.data_typesr   r   r   r   r   Zuaclient.filesr   r   r   Zuaclient.timer.update_messagingr   r   r+   r*   r.   Zendpointr-   Z_docr!   r!   r!   r"   <module>   s>   $	9