U
    JhD                     @   s  d dl Z d dlZd dlmZmZmZmZmZ d dlm	Z	m
Z
mZmZmZmZmZmZmZmZ d dlmZmZ d dlmZmZmZ d dlmZmZ d dlmZ d dlm Z  d d	l!m"Z"m#Z#m$Z$ d d
l%m&Z& e'e(e)Z*edde+fde+fdeee,ef  fgZ-d5e
j.e+ee	j/ dddZ0e
j.e,ee ee e,e,e,dddZ1e+ee,ef ee, ee, eee,ef  eee,ef  e+dddZ2e
j.e,e,e+e+e+eee,  ee ee e-d
ddZ3e j4e j5e 6e j7e8dddZ9e$dej:ej;e9e&j<de#e"d ej=j>d!?ej@e
. d"d#d$d%d&e"d'ejAj>dd(d)d*e"d+ejBd)d*e"d,ejCd)d*e"d-ejDj>d.d/d$d.d0gd.d1e"d2ejEd$d*gd3gd4ZFdS )6    N)AnyDictList
NamedTupleOptional)
apiconfigcontractentitlementsevent_logger
exceptionslockmessagesstatusutil)ServiceWithDependencies_dependencies)EnableOptionsEnableResult_enable)EnabledService_enabled_services)_is_attached)cli_util)ProArgumentProArgumentGroup
ProCommand)HelpCategory_EnableOneServiceResultsuccessneeds_rebooterror)cfgaccess_onlyprogress_objectc           
   
   C   s   t |}tj| d||d}d}d}z,tjdd |j|d\}}W 5 Q R X W n. tk
r~ } zt  |W 5 d}~X Y nX |s|dk	r|j	dk	r|j	}	nt
j}	tjd|	dtdgg dg d	S )
a  
    Landscape gets special treatment because it currently not supported by our
    enable API. This function is a temporary workaround until we have a proper
    API for enabling landscape, which will happen after Landscape is fully
    integrated with the contracts backend.
    	landscape)called_namer#   
extra_argsFNzcli.enable._enable_landscape)Zlock_holder)progress)servicereason)enabledZdisabledreboot_requiredr   )r   ZProgressWrapperr
   ZLandscapeEntitlementr   Z	RetryLockenable	ExceptionZclear_lock_file_if_presentmessager   ZGENERIC_UNKNOWN_ISSUEr   EntitlementNotEnabledErrorr   )
r"   r#   r'   r$   r(   r%   r   Zfail_reasoner*    r2   5/usr/lib/python3/dist-packages/uaclient/cli/enable.py_enable_landscape/   s>    
    r4   )r"   r)   all_dependenciesenabled_servicesr&   variantservice_titlec                    s  g }g }dd |D  t fdd|D d }	|	d k	r^ fdd|	jD } fdd|	jD }|D ]N}
tj| jdd}t| |
}tj	j
||d	}|stj|d
sbtj||d	qb|D ]:}t| |}tjj
||d}tj|d
stj||dqt fdd|D d }|d k	rd k	rt| }t| |j}tj| jdd}tj	j
||d	}|srtj|d
stj||d	d S )Nc                 S   s   g | ]
}|j qS r2   name.0sr2   r2   r3   
<listcomp>f   s     z2prompt_for_dependency_handling.<locals>.<listcomp>c                 3   s   | ]}|j  kr|V  qd S Nr9   r;   )r)   r2   r3   	<genexpr>i   s     
 z1prompt_for_dependency_handling.<locals>.<genexpr>c                    s   g | ]}|j  kr|j qS r2   r9   r;   enabled_service_namesr2   r3   r>   l   s   
c                    s   g | ]}|j  kr|j qS r2   r9   r;   rA   r2   r3   r>   q   s   
z features.block_disable_on_enable)r   Zpath_to_value)service_being_enabledincompatible_service)msg)rC   required_servicec                 3   s,   | ]$}|j  kr|jr|jkr|V  qd S r?   r:   variant_enabledvariant_namer;   )r)   r7   r2   r3   r@      s
   

)nextZincompatible_withZ
depends_onr   Zis_config_value_truer"   r
   Z	get_titler   ZINCOMPATIBLE_SERVICEformatZprompt_for_confirmationr   ZIncompatibleServiceStopsEnableZREQUIRED_SERVICEZRequiredServiceStopsEnablerI   )r"   r)   r5   r6   r&   r7   r8   Zincompatible_servicesZrequired_servicesZdependenciesrD   Zcfg_block_disable_on_enableZincompatible_service_titleZuser_msgrF   Zrequired_service_titlerH   Zto_be_enabled_titleZenabled_variant_titler2   )rB   r)   r7   r3   prompt_for_dependency_handling[   s    	 

 
  
rL   json_outputjson_responseprocessed_servicesfailed_serviceserrorswarningsr   c                 C   s^   | rZ|   |   |rdnd|d< ||d< ||d< ||d< ||d< ttj|tjdd	 d S )
Nr   ZfailureresultrP   rQ   rR   rS   T)clsZ	sort_keys)sortprintjsondumpsr   ZDatetimeAwareJSONEncoderrM   r2   r2   r3   _print_json_output   s    	rZ   )
r"   ent_namer7   r#   
assume_yesrN   r'   r6   r5   returnc	                    s  t |}	tj| |||d}
|
j |
j}t fdd|D d }|d k	rtjj	|d}|	|j
 |	tjj	|d tddd||j
|jddS |szt|  ||||d	 W n^ tjk
r } z<t| |	|j
 |	tjj	|d tddd d W Y S d }~X Y nX z|r d }nt j|d
} dkrHt| |||d}ntt||d| |d}tj| d |r|	tjj	|d n|	tjj	|d |j}|r|	tjj	dd |jD ]}|	| qtd|d dW S  tjk
rZ } z^|jd }|d }|d }|d }|	| |	tjj	|d tddd||||dd W Y S d }~X Y nf tjk
r } zD|	|j
 |	tjj	|d tddd||j
|j|jdd W Y S d }~X Y nX d S )N)r7   r#   r'   c                 3   s0   | ](}|j  krr$|jr|jkr|V  qd S r?   rG   r;   Z	real_namer7   r2   r3   r@      s   
 
z&_enable_one_service.<locals>.<genexpr>)titleFr)   )typer)   r/   message_code)r   r    r!   )r&   r7   r8   )r\   r%   )r'   r$   )r)   r7   r#   )r$   r"   install)Z	operationTr*   coder_   additional_infor`   r)   r/   ra   re   )r   &create_interactive_only_print_functionr
   Zentitlement_factoryr:   r_   rJ   r   ZALREADY_ENABLEDrK   rE   ZENABLE_FAILEDr   rL   r   UbuntuProErrorLOGZ	exceptionZCLIEnableDisableProgressr4   r   r   r   ZACCESS_ENABLED_TMPLZENABLED_TMPLr,   ZENABLE_REBOOT_REQUIRED_TMPLr0   re   msg_code)r"   r[   r7   r#   r\   rN   r'   r6   r5   interactive_only_printZentZ	ent_titleZalready_enabledrE   r1   r(   Zenable_resultr    r/   r*   Zerr_codeerr_msgZerr_infor2   r^   r3   _enable_one_service   s    	

	






rm   )r]   c                K   s6  g }g }g }g }t jdd}| jdk}t|}	t| dd}
| j}| j}|
r^|r^tj	ddd|	t
j zt| W nD tjtjfk
r   tjd	d
d |dt
jjt
jjd Y nX t|js
t }|	|j |d|j|jd t||||||dd dS t| dg }t||\}}t|j}t|j }d
}t!||D ]n}t"|||
||||#d||	}|j$r|| |j%rd
|d< n&d}|| |j&dk	rJ||j& qJ|r d}||7 }tj'||d}|	|j |dd|j|j|j(d t)|}|*  t|||||||d |r2dS dS )z^Perform the enable action on a named entitlement.

    @return: 0 on success, 1 otherwise
    F)Z_schema_versionr    rX   r7    --access-only	--variant)Zoption1Zoption2zFailed to refresh contractT)exc_infosystem)r`   r/   ra   )r      r)   r'   r    Nrb   rf   r   )+r   ZJSON_SCHEMA_VERSIONrK   r   rg   getattrr#   r\   r   ZInvalidOptionCombinationr   ZREFRESH_CONTRACT_ENABLEr	   ZrefreshZConnectivityErrorrh   ri   ZwarningappendZE_REFRESH_CONTRACT_FAILURErE   r:   r   Zis_attached_and_contract_validZContractExpiredErrorrj   rZ   r
   Zget_valid_entitlement_namesr   r6   r   servicesZorder_entitlements_for_enablingrm   getr   r    r!   Z*create_enable_entitlements_not_found_errorre   ZUAContractClientZupdate_activity_token)argsr"   kwargsrP   rQ   rR   rS   rO   rN   rk   r7   r#   r\   Zexpired_errnamesZentitlements_foundZentitlements_not_foundr6   r5   Zretr[   rT   errZcontract_clientr2   r2   r3   action_enableu  s    
 


	


 


 



r|   r-      r)   z, rb   )ZoptionsZstore+)helpactionnargsz--assume-yes)Zcommand
store_true)r   r   ro   z--betaz--formatZcli)defaultrX   )r   r   choicesr   rp   )Z	arguments)r   Zdescriptionr   Zhelp_categoryZhelp_positionZargument_groups)N)GrX   Zloggingtypingr   r   r   r   r   Zuaclientr   r   r	   r
   r   r   r   r   r   r   Z+uaclient.api.u.pro.services.dependencies.v1r   r   Z%uaclient.api.u.pro.services.enable.v1r   r   r   Z-uaclient.api.u.pro.status.enabled_services.v1r   r   Z(uaclient.api.u.pro.status.is_attached.v1r   Zuaclient.clir   Zuaclient.cli.commandsr   r   r   Zuaclient.cli.parserr   Z	getLoggerZreplace_top_level_logger_name__name__ri   boolstrr   ZUAConfigZAbstractProgressr4   rL   rZ   rm   Zverify_json_format_argsZassert_rootZassert_attachedZ&_raise_enable_disable_unattached_errorintr|   ZCLI_ROOT_ENABLEZCLI_ENABLE_DESCZ
QUICKSTARTZCLI_ENABLE_SERVICErK   joinZvalid_servicesZCLI_ASSUME_YESZCLI_ENABLE_ACCESS_ONLYZCLI_ENABLE_BETAZCLI_FORMAT_DESCZCLI_ENABLE_VARIANTZenable_commandr2   r2   r2   r3   <module>   s   0 -]

 #
 
