o
    K&br                  	   @   s   d dl Z d dlZ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 e dZe	 Zdejdeded	dfd
dZdejdejd	dfddZddddejdededefddZddddejdee defddZdS )    N)Optional)cloudsconfigcontractentitlementsevent_logger
exceptionsmessages)identityz
ua.actionscfgtokenallow_enablereturnc              
   C   s   ddl m} z
tj| ||d W n6 tjy' } z
|   ||  |d}~w tjyF } ztj	|j
tjd |   ||  |d}~ww t }|rS| d| ||  dS )a:  
    Common functionality to take a token and attach via contract backend
    :raise UrlError: On unexpected connectivity issues to contract
        server or inability to access identity doc from metadata service.
    :raise ContractAPIError: On unexpected errors when talking to the contract
        server.
    r   )update_apt_and_motd_messages)r   N)Z	file_typezinstance-id)Zuaclient.jobs.update_messagingr   r   Zrequest_updated_contractr   ZUrlErrorstatusZUserFacingErroreventinfomsgsysstderrr
   Zget_instance_idZwrite_cache)r   r   r   r   excZcurrent_iid r   2/usr/lib/python3/dist-packages/uaclient/actions.pyattach_with_token   s*   

r   cloudc              
   C   s   t | }z|j|d}W n& tjy3 } z|jr-d|j  kr$dk r-n |ttj|d}~ww |d }t	| |dd dS )aS  
    :raise UrlError: On unexpected connectivity issues to contract
        server or inability to access identity doc from metadata service.
    :raise ContractAPIError: On unexpected errors when talking to the contract
        server.
    :raise NonAutoAttachImageError: If this cloud type does not have
        auto-attach support.
    )instancei  i  NZcontractTokenT)r   r   )
r   ZUAContractClientZ"request_auto_attach_contract_tokenr   ZContractAPIErrorcodeZNonAutoAttachImageErrorr	   ZUNSUPPORTED_AUTO_ATTACHr   )r   r   Zcontract_clientZtokenResponseer   r   r   r   auto_attach5   s    

r   F)
assume_yes
allow_betanamer   r    c                C   s"   t |}|| |||d}| S )z
    Constructs an entitlement based on the name provided. Passes kwargs onto
    the entitlement constructor.
    :raise EntitlementNotFoundError: If no entitlement with the given name is
        found, then raises this error.
    )r   r    Zcalled_name)r   Zentitlement_factoryZenable)r   r!   r   r    Zent_clsZentitlementr   r   r   enable_entitlement_by_nameQ   s
   
r"   )simulate_with_token	show_betar#   r$   c                C   s6   |r| j ||d\}}||fS | j|d}d}||fS )z5
    Construct the current UA status dictionary.
    )r   r$   )r$   r   )Zsimulate_statusr   )r   r#   r$   r   retr   r   r   r   e   s   	
r   )Zloggingr   typingr   Zuaclientr   r   r   r   r   r   r	   Zuaclient.cloudsr
   Z	getLoggerZLOGZget_event_loggerr   ZUAConfigstrboolr   ZAutoAttachCloudInstancer   r"   r   r   r   r   r   <module>   sV    $	

!
 
