
    Uٵg[              	       N   d dl 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 d dlmZmZmZmZ dZ G d	 d
e      Z G d dee      ZdedefdZdede	defdZ eddee      Zdddeej.                  dfej0                  dfej2                  dfej4                  dfgdddZy)    N)
exceptionssecret_manager)APIEndpoint)AdditionalInfo)UAConfig)UAContractClient)
DataObjectFieldIntDataValueStringDataValueF   c                   0    e Zd Z eded      gZdefdZy)MagicAttachWaitOptionsmagic_tokenz,The Token provided by the initiate endpoint.docc                     || _         y Nr   )selfr   s     I/usr/lib/python3/dist-packages/uaclient/api/u/pro/attach/magic/wait/v1.py__init__zMagicAttachWaitOptions.__init__   s
    &    N)__name__
__module____qualname__r
   r   fieldsstrr    r   r   r   r      s&    >	
F'C 'r   r   c                       e Zd Z eded       eded       eded       eded	       ed
ed       eded      gZdedededed
edefdZ	y)MagicAttachWaitResult	user_codezACode the user will see in the UI when confirming the Magic Attachr   tokenz1The same Magic Token that was sent as an argumentexpiresz0Timestamp of the Magic Attach process expiration
expires_inz/Seconds before the Magic Attach process expirescontract_idz2ID of the contract the machine will be attached tocontract_tokenz(The contract Token to attach the machinec                 X    || _         || _        || _        || _        || _        || _        y r   r"   r#   r$   r%   r&   r'   )r   r"   r#   r$   r%   r&   r'   s          r   r   zMagicAttachWaitResult.__init__D   s0     #
$&,r   N)
r   r   r   r
   r   r   r   r   intr   r   r   r   r!   r!      s    		
 	C	

 	B	

 	A	

 	D	

 	:	
;"FH-- - 	-
 - - -r   r!   optionsreturnc                 *    t        | t                     S r   )_waitr   )r+   s    r   waitr/   U   s     (*%%r   cfgc           	         t        |      }d}d}d}t        j                  j                  | j                         |t
        k  rd}	 |j                  | j                        }d}|r=|j                  d      ,t        |d	   |d
   |d   t        |d         |d   |d         S |rd}t        j                  |       |dz  }|t
        k  rt        j                         # t        j                  $ r Y t        j                         t        j                  $ r d}Y t        j                  $ r}|dk  r|dz  }n|Y d}~d}~ww xY w)zi
    This endpoint polls the contracts service waiting for the user to confirm
    the Magic Attach.
    r   
   Nr            contractTokenuserCoder#   r$   	expiresIn
contractIDr)   )r   r   secrets
add_secretr   MAXIMUM_ATTEMPTSget_magic_attach_token_infor   MagicAttachTokenErrorMagicAttachUnavailableConnectivityErrorgetr!   r*   timesleep)r+   r0   contractnum_attemptsnum_connection_errors	wait_time	wait_respes           r   r.   r.   [   st     $HLI%%g&9&9:
)
)		 <<#// = I %&!& 7C(#J/(!),y56%l3(9  I

9O )
)R 
*
*
,,C // 	@ 
*
*
,,5 00 	I ++ 	$q(%*% &	s$   C   EEE0EEv1MagicAttachWait)versionnamefnoptions_clsz27.11Tz
from uaclient.api.u.pro.attach.magic.wait.v1 import MagicAttachWaitOptions, wait

options = MagicAttachWaitOptions(magic_token="<magic_token>")
result = wait(options)
zARaised if it is not possible to connect to the contracts service.zLRaised if there is an unexpected error in the contracts service interaction.z-Raised when an invalid/expired Token is sent.zHRaised if the Magic Attach service is busy or unavailable at the moment.zCpro api u.pro.attach.magic.wait.v1 --args magic_token=<magic_token>z
{
    "user_code":"<UI_code>",
    "token":"<magic_token>",
    "expires": "<yyyy-MM-dd>T<HH:mm:ss>.<TZ>",
    "expires_in": 500,
    "contract_id": "<Contract-ID>",
    "contract_token": "<attach_token>",
}
)introduced_inrequires_networkexample_pythonresult_classr   example_cliexample_json)rB   uaclientr   r   uaclient.api.apir   uaclient.api.data_typesr   uaclient.configr   uaclient.contractr   uaclient.data_typesr	   r
   r   r   r<   r   r!   r/   r.   endpointr@   ContractAPIErrorr>   r?   _docr   r   r   <module>r_      s    / ( 2 $ .   
'Z 
'3-J 3-l&#&&7-#7-*27-7-t 	&	  * ((	
 ''(	
 ,,;	

 --	
'6 Y	M0r   