o
    ÙKÕi  ã                   @   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#de#fdd„Z$dede#fdd„Z%dede#f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                   @   sB   e Zd Zedeeƒdd�ededd�gZdee	 de
f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 operationc                 C   s   || _ || _d S ©N©r   r   )Ú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   Ú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,   Úcfgc              
   C   sr   t  ¡ stj‚ztjdd�� t| ƒ}W d  ƒ W |S 1 sw   Y  W |S  ty8 } zt ¡  |‚d}~ww )zZ
    This endpoint allows the user to detach the machine from a Pro
    subscription.
    zpro.api.u.pro.detach.v1)Úlock_holderN)	r	   Úwe_are_currently_rootr   ÚNonRootUserErrorr   Ú	RetryLockÚ_detach_in_lockÚ	ExceptionÚclear_lock_file_if_present)r-   ÚretÚer!   r!   r"   r+   9   s    ÿ
ýùý€þr+   c              	   C   s2  t | ƒjstg dd�S g }g }t | ¡D ]S}z	tj| |d�}W n
 tjy)   Y qw |jdd�\}}|rg| 	t
ƒ ¡\}}|sb|rL|jrL|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)Úignore_dependent_services)ÚtitleÚ Úservice)r9   ÚcodeÚmetaÚyes)%r   Úis_attachedr   r   Úentitlements_disable_orderÚentitlement_factoryr   ÚEntitlementNotFoundErrorÚcan_disableÚdisabler
   ÚmessageÚmsgr7   r   ÚDISABLE_FAILED_TMPLÚformatÚappendr   r   ÚremoveÚNoticeÚLIMITED_TO_RELEASEr   Údelete_state_filesr   Úget_machine_token_fileÚdeleter   r   Ústartr   Ústopr   Úsortedr   Úwarnings)r-   r   rS   Úent_nameÚentrC   Ú_r5   ÚreasonrF   r<   Úreboot_required_resultÚresultr!   r!   r"   r2   L   sX   
þÿ

ýÿ
€þr2   Úv1ÚDetachN)Úversionr7   ÚfnÚoptions_clsÚ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
}
)Úintroduced_inÚrequires_networkÚexample_pythonÚresult_classr   Úexample_cliÚexample_json)*Útypingr   Úuaclientr   r   r   r   r   r   r	   Úuaclient.apir
   Úuaclient.api.apir   Úuaclient.api.data_typesr   r   Ú5uaclient.api.u.pro.security.status.reboot_required.v1r   Ú(uaclient.api.u.pro.status.is_attached.v1r   Úuaclient.configr   Úuaclient.data_typesr   r   r   r   r   Úuaclient.filesr   r   r   Úuaclient.timer.update_messagingr   r   r,   r+   r2   Úendpointr0   Ú_docr!   r!   r!   r"   Ú<module>   s@    $	9üþÿ
ð